Skip to content

Commit

Permalink
refactor: use direct locking instead of syncrhonize for legacy databa…
Browse files Browse the repository at this point in the history
…se implementation
  • Loading branch information
tglman committed Feb 8, 2024
1 parent 2396719 commit 5eaf08e
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
import java.util.Map.Entry;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/** Created by tglman on 20/07/16. @Deprecated use {@link OrientDB} instead. */
@Deprecated
Expand All @@ -72,6 +74,9 @@ public class ODatabaseDocumentTx implements ODatabaseDocumentInternal {
protected static ConcurrentMap<String, OrientDBInternal> embedded = new ConcurrentHashMap<>();
protected static ConcurrentMap<String, OrientDBInternal> remote = new ConcurrentHashMap<>();

protected static final Lock embeddedLock = new ReentrantLock();
protected static final Lock remoteLock = new ReentrantLock();

protected ODatabaseDocumentInternal internal;
private final String url;
private OrientDBInternal factory;
Expand Down Expand Up @@ -110,37 +115,51 @@ public int getPriority() {
}

public static void closeAll() {
synchronized (embedded) {
embeddedLock.lock();
try {
for (OrientDBInternal factory : embedded.values()) {
factory.close();
}
embedded.clear();
} finally {
embeddedLock.unlock();
}
synchronized (remote) {

remoteLock.lock();
try {
for (OrientDBInternal factory : remote.values()) {
factory.close();
}
remote.clear();
} finally {
remoteLock.unlock();
}
}

protected static OrientDBInternal getOrCreateRemoteFactory(String baseUrl) {
OrientDBInternal factory;
synchronized (remote) {

remoteLock.lock();
try {
factory = remote.get(baseUrl);
if (factory == null || !factory.isOpen()) {
factory = OrientDBInternal.fromUrl("remote:" + baseUrl, null);
remote.put(baseUrl, factory);
}
} finally {
remoteLock.unlock();
}
return factory;
}

protected static OrientDBInternal getOrCreateEmbeddedFactory(
String baseUrl, OrientDBConfig config) {
if (!baseUrl.endsWith("/")) baseUrl += "/";
if (!baseUrl.endsWith("/")) {
baseUrl += "/";
}
OrientDBInternal factory;
synchronized (embedded) {
embeddedLock.lock();
try {
factory = embedded.get(baseUrl);
if (factory == null || !factory.isOpen()) {
try {
Expand All @@ -150,11 +169,16 @@ protected static OrientDBInternal getOrCreateEmbeddedFactory(
}
embedded.put(baseUrl, factory);
}
} finally {
embeddedLock.unlock();
}

return factory;
}

/** @Deprecated use {{@link OrientDB}} instead. */
/**
* @Deprecated use {{@link OrientDB}} instead.
*/
@Deprecated
public ODatabaseDocumentTx(String url) {
this(url, true);
Expand Down Expand Up @@ -189,13 +213,17 @@ public static void setDefaultSerializer(ORecordSerializer defaultSerializer) {

@Override
public OCurrentStorageComponentsFactory getStorageVersions() {
if (internal == null) return null;
if (internal == null) {
return null;
}
return internal.getStorageVersions();
}

@Override
public OSBTreeCollectionManager getSbTreeCollectionManager() {
if (internal == null) return null;
if (internal == null) {
return null;
}
return internal.getSbTreeCollectionManager();
}

Expand All @@ -208,7 +236,9 @@ public OBinarySerializerFactory getSerializerFactory() {
@Override
public ORecordSerializer getSerializer() {
if (internal == null) {
if (serializer != null) return serializer;
if (serializer != null) {
return serializer;
}
return ORecordSerializerFactory.instance().getDefaultRecordSerializer();
}
return internal.getSerializer();
Expand Down Expand Up @@ -378,7 +408,9 @@ public void checkIfActive() {
}

protected void checkOpenness() {
if (internal == null) throw new ODatabaseException("Database '" + getURL() + "' is closed");
if (internal == null) {
throw new ODatabaseException("Database '" + getURL() + "' is closed");
}
}

@Override
Expand All @@ -396,7 +428,9 @@ public void callOnCloseListeners() {
@Override
@Deprecated
public OStorage getStorage() {
if (internal == null) return delegateStorage;
if (internal == null) {
return delegateStorage;
}
return internal.getStorage();
}

Expand All @@ -412,23 +446,30 @@ public void replaceStorage(OStorage iNewStorage) {

@Override
public void resetInitialization() {
if (internal != null) internal.resetInitialization();
if (internal != null) {
internal.resetInitialization();
}
}

@Override
public ODatabaseInternal<?> getDatabaseOwner() {
ODatabaseInternal<?> current = databaseOwner;

while (current != null && current != this && current.getDatabaseOwner() != current)
while (current != null && current != this && current.getDatabaseOwner() != current) {
current = current.getDatabaseOwner();
if (current == null) return this;
}
if (current == null) {
return this;
}
return current;
}

@Override
public ODatabaseInternal<?> setDatabaseOwner(ODatabaseInternal<?> iOwner) {
databaseOwner = iOwner;
if (internal != null) internal.setDatabaseOwner(iOwner);
if (internal != null) {
internal.setDatabaseOwner(iOwner);
}
return this;
}

Expand Down Expand Up @@ -557,7 +598,9 @@ public ODictionary<ORecord> getDictionary() {

@Override
public OSecurityUser getUser() {
if (internal != null) return internal.getUser();
if (internal != null) {
return internal.getUser();
}
return null;
}

Expand Down Expand Up @@ -898,11 +941,18 @@ public <DB extends ODatabase> DB open(String iUserName, String iUserPassword) {
OrientDBConfig config = buildConfig(null);
internal = factory.open(dbName, iUserName, iUserPassword, config);
}
if (databaseOwner != null) internal.setDatabaseOwner(databaseOwner);
if (conflictStrategy != null) internal.setConflictStrategy(conflictStrategy);
if (serializer != null) internal.setSerializer(serializer);
for (Entry<String, Object> pro : preopenProperties.entrySet())
if (databaseOwner != null) {
internal.setDatabaseOwner(databaseOwner);
}
if (conflictStrategy != null) {
internal.setConflictStrategy(conflictStrategy);
}
if (serializer != null) {
internal.setSerializer(serializer);
}
for (Entry<String, Object> pro : preopenProperties.entrySet()) {
internal.setProperty(pro.getKey(), pro.getValue());
}
} catch (RuntimeException e) {
clearOwner();
throw e;
Expand All @@ -911,7 +961,9 @@ public <DB extends ODatabase> DB open(String iUserName, String iUserPassword) {
}

protected void setupThreadOwner() {
if (!ownerProtection) return;
if (!ownerProtection) {
return;
}

final Thread current = Thread.currentThread();
final Thread o = owner.get();
Expand All @@ -923,7 +975,9 @@ protected void setupThreadOwner() {
}

protected void clearOwner() {
if (!ownerProtection) return;
if (!ownerProtection) {
return;
}
owner.set(null);
}

Expand Down Expand Up @@ -973,11 +1027,18 @@ public <DB extends ODatabase> DB create(Map<OGlobalConfiguration, Object> iIniti
internal.registerListener(oDatabaseListener);
}
}
if (databaseOwner != null) internal.setDatabaseOwner(databaseOwner);
if (conflictStrategy != null) internal.setConflictStrategy(conflictStrategy);
if (serializer != null) internal.setSerializer(serializer);
for (Entry<String, Object> pro : preopenProperties.entrySet())
if (databaseOwner != null) {
internal.setDatabaseOwner(databaseOwner);
}
if (conflictStrategy != null) {
internal.setConflictStrategy(conflictStrategy);
}
if (serializer != null) {
internal.setSerializer(serializer);
}
for (Entry<String, Object> pro : preopenProperties.entrySet()) {
internal.setProperty(pro.getKey(), pro.getValue());
}
} catch (RuntimeException e) {
clearOwner();
throw e;
Expand All @@ -987,13 +1048,17 @@ public <DB extends ODatabase> DB create(Map<OGlobalConfiguration, Object> iIniti

@Override
public ODatabase activateOnCurrentThread() {
if (internal != null) internal.activateOnCurrentThread();
if (internal != null) {
internal.activateOnCurrentThread();
}
return this;
}

@Override
public boolean isActiveOnCurrentThread() {
if (internal != null) return internal.isActiveOnCurrentThread();
if (internal != null) {
return internal.isActiveOnCurrentThread();
}
return false;
}

Expand Down Expand Up @@ -1021,22 +1086,27 @@ public OContextConfiguration getConfiguration() {

@Override
public boolean declareIntent(OIntent iIntent) {
if (internal != null) return internal.declareIntent(iIntent);
else {
if (internal != null) {
return internal.declareIntent(iIntent);
} else {
intent = iIntent;
return true;
}
}

@Override
public OIntent getActiveIntent() {
if (internal == null) return intent;
if (internal == null) {
return intent;
}
return internal.getActiveIntent();
}

@Override
public boolean exists() {
if (internal != null) return true;
if (internal != null) {
return true;
}
if ("remote".equals(type)) {
throw new UnsupportedOperationException();
} else {
Expand Down Expand Up @@ -1216,14 +1286,20 @@ public boolean dropCluster(int iClusterId) {

@Override
public Object setProperty(String iName, Object iValue) {
if (internal != null) return internal.setProperty(iName, iValue);
else return preopenProperties.put(iName, iValue);
if (internal != null) {
return internal.setProperty(iName, iValue);
} else {
return preopenProperties.put(iName, iValue);
}
}

@Override
public Object getProperty(String iName) {
if (internal != null) return internal.getProperty(iName);
else return preopenProperties.get(iName);
if (internal != null) {
return internal.getProperty(iName);
} else {
return preopenProperties.get(iName);
}
}

@Override
Expand All @@ -1243,8 +1319,11 @@ public Object get(ATTRIBUTES iAttribute) {

@Override
public <DB extends ODatabase> DB set(ATTRIBUTES iAttribute, Object iValue) {
if (internal != null) internal.set(iAttribute, iValue);
else preopenAttributes.put(iAttribute, iValue);
if (internal != null) {
internal.set(iAttribute, iValue);
} else {
preopenAttributes.put(iAttribute, iValue);
}
return (DB) this;
}

Expand Down Expand Up @@ -1370,32 +1449,39 @@ private OrientDBConfig buildConfig(final Map<OGlobalConfiguration, Object> iProp
pars != null
? (String) pars.get(OGlobalConfiguration.CLIENT_CONNECTION_STRATEGY.getKey())
: null;
if (connectionStrategy != null)
if (connectionStrategy != null) {
builder.addConfig(OGlobalConfiguration.CLIENT_CONNECTION_STRATEGY, connectionStrategy);
}

final String compressionMethod =
pars != null
? (String) pars.get(OGlobalConfiguration.STORAGE_COMPRESSION_METHOD.getKey())
: null;
if (compressionMethod != null)
// SAVE COMPRESSION METHOD IN CONFIGURATION
// SAVE COMPRESSION METHOD IN CONFIGURATION
{
builder.addConfig(OGlobalConfiguration.STORAGE_COMPRESSION_METHOD, compressionMethod);
}

final String encryptionMethod =
pars != null
? (String) pars.get(OGlobalConfiguration.STORAGE_ENCRYPTION_METHOD.getKey())
: null;
if (encryptionMethod != null)
// SAVE ENCRYPTION METHOD IN CONFIGURATION
// SAVE ENCRYPTION METHOD IN CONFIGURATION
{
builder.addConfig(OGlobalConfiguration.STORAGE_ENCRYPTION_METHOD, encryptionMethod);
}

final String encryptionKey =
pars != null
? (String) pars.get(OGlobalConfiguration.STORAGE_ENCRYPTION_KEY.getKey())
: null;
if (encryptionKey != null)
// SAVE ENCRYPTION KEY IN CONFIGURATION
// SAVE ENCRYPTION KEY IN CONFIGURATION
{
builder.addConfig(OGlobalConfiguration.STORAGE_ENCRYPTION_KEY, encryptionKey);
}

for (Map.Entry<ATTRIBUTES, Object> attr : preopenAttributes.entrySet()) {
builder.addAttribute(attr.getKey(), attr.getValue());
Expand Down
Loading

0 comments on commit 5eaf08e

Please sign in to comment.