use of com.orientechnologies.orient.core.storage.OStorage in project orientdb by orientechnologies.
the class OSchemaShared method dropClass.
/*
* (non-Javadoc)
*
* @see com.orientechnologies.orient.core.metadata.schema.OSchema#dropClass(java.lang.String)
*/
public void dropClass(final String className) {
final ODatabaseDocumentInternal db = getDatabase();
final OStorage storage = db.getStorage();
final StringBuilder cmd;
acquireSchemaWriteLock();
try {
if (getDatabase().getTransaction().isActive())
throw new IllegalStateException("Cannot drop a class inside a transaction");
if (className == null)
throw new IllegalArgumentException("Class name is null");
getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_DELETE);
final String key = className.toLowerCase();
OClass cls = classes.get(key);
if (cls == null)
throw new OSchemaException("Class '" + className + "' was not found in current database");
if (!cls.getSubclasses().isEmpty())
throw new OSchemaException("Class '" + className + "' cannot be dropped because it has sub classes " + cls.getSubclasses() + ". Remove the dependencies before trying to drop it again");
cmd = new StringBuilder("drop class ");
cmd.append(className);
cmd.append(" unsafe");
if (executeThroughDistributedStorage()) {
final OAutoshardedStorage autoshardedStorage = (OAutoshardedStorage) storage;
OCommandSQL commandSQL = new OCommandSQL(cmd.toString());
commandSQL.addExcludedNode(autoshardedStorage.getNodeId());
db.command(commandSQL).execute();
dropClassInternal(className);
} else if (storage instanceof OStorageProxy) {
final OCommandSQL commandSQL = new OCommandSQL(cmd.toString());
db.command(commandSQL).execute();
final OClass classToDrop = getClass(className);
reload();
if (// really dropped, for example there may be no rights to drop a class
getClass(className) == null)
dropClassIndexes(classToDrop);
} else
dropClassInternal(className);
// FREE THE RECORD CACHE
getDatabase().getLocalCache().freeCluster(cls.getDefaultClusterId());
} finally {
releaseSchemaWriteLock();
}
}
use of com.orientechnologies.orient.core.storage.OStorage in project orientdb by orientechnologies.
the class OSchemaShared method doCreateClass.
private OClass doCreateClass(final String className, final int clusters, final int retry, OClass... superClasses) {
OClass result;
final ODatabaseDocumentInternal db = getDatabase();
final OStorage storage = db.getStorage();
StringBuilder cmd = null;
getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_CREATE);
if (superClasses != null)
OClassImpl.checkParametersConflict(Arrays.asList(superClasses));
acquireSchemaWriteLock();
try {
final String key = className.toLowerCase();
if (classes.containsKey(key) && retry == 0)
throw new OSchemaException("Class '" + className + "' already exists in current database");
cmd = new StringBuilder("create class ");
// if (getDatabase().getStorage().getConfiguration().isStrictSql())
// cmd.append('`');
cmd.append(className);
// if (getDatabase().getStorage().getConfiguration().isStrictSql())
// cmd.append('`');
List<OClass> superClassesList = new ArrayList<OClass>();
if (superClasses != null && superClasses.length > 0) {
boolean first = true;
for (OClass superClass : superClasses) {
// Filtering for null
if (superClass != null) {
if (first)
cmd.append(" extends ");
else
cmd.append(", ");
cmd.append(superClass.getName());
first = false;
superClassesList.add(superClass);
}
}
}
if (clusters == 0)
cmd.append(" abstract");
else {
cmd.append(" clusters ");
cmd.append(clusters);
}
if (executeThroughDistributedStorage()) {
final int[] clusterIds = createClusters(className, clusters);
createClassInternal(className, clusterIds, superClassesList);
final OAutoshardedStorage autoshardedStorage = (OAutoshardedStorage) storage;
OCommandSQL commandSQL = new OCommandSQL(cmd.toString());
commandSQL.addExcludedNode(autoshardedStorage.getNodeId());
final Object res = db.command(commandSQL).execute();
} else if (storage instanceof OStorageProxy) {
db.command(new OCommandSQL(cmd.toString())).execute();
reload();
} else {
final int[] clusterIds = createClusters(className, clusters);
createClassInternal(className, clusterIds, superClassesList);
}
result = classes.get(className.toLowerCase());
// WAKE UP DB LIFECYCLE LISTENER
for (Iterator<ODatabaseLifecycleListener> it = Orient.instance().getDbLifecycleListeners(); it.hasNext(); ) it.next().onCreateClass(getDatabase(), result);
} catch (ClusterIdsAreEmptyException e) {
throw OException.wrapException(new OSchemaException("Cannot create class '" + className + "'"), e);
} finally {
releaseSchemaWriteLock();
}
return result;
}
use of com.orientechnologies.orient.core.storage.OStorage in project orientdb by orientechnologies.
the class OSchemaShared method removeClusterForClass.
void removeClusterForClass(int clusterId, OClass cls) {
acquireSchemaWriteLock();
try {
if (!clustersCanNotBeSharedAmongClasses)
return;
if (clusterId < 0)
return;
final OStorage storage = getDatabase().getStorage();
checkEmbedded(storage);
clustersToClasses.remove(clusterId);
} finally {
releaseSchemaWriteLock();
}
}
use of com.orientechnologies.orient.core.storage.OStorage in project orientdb by orientechnologies.
the class ODatabaseCompare method compareRecords.
@SuppressFBWarnings("NP_NULL_ON_SOME_PATH")
private boolean compareRecords(ODocumentHelper.RIDMapper ridMapper) {
listener.onMessage("\nStarting deep comparison record by record. This may take a few minutes. Wait please...");
Collection<String> clusterNames1 = makeDbCall(databaseOne, new ODbRelatedCall<Collection<String>>() {
@Override
public Collection<String> call(ODatabaseDocumentInternal database) {
return database.getClusterNames();
}
});
for (final String clusterName : clusterNames1) {
// CHECK IF THE CLUSTER IS INCLUDED
if (includeClusters != null) {
if (!includeClusters.contains(clusterName))
continue;
} else if (excludeClusters != null) {
if (excludeClusters.contains(clusterName))
continue;
}
final int clusterId1 = makeDbCall(databaseOne, new ODbRelatedCall<Integer>() {
@Override
public Integer call(ODatabaseDocumentInternal database) {
return database.getClusterIdByName(clusterName);
}
});
final long[] db1Range = makeDbCall(databaseOne, new ODbRelatedCall<long[]>() {
@Override
public long[] call(ODatabaseDocumentInternal database) {
return database.getStorage().getClusterDataRange(clusterId1);
}
});
final long[] db2Range = makeDbCall(databaseTwo, new ODbRelatedCall<long[]>() {
@Override
public long[] call(ODatabaseDocumentInternal database) {
return database.getStorage().getClusterDataRange(clusterId1);
}
});
final long db1Max = db1Range[1];
final long db2Max = db2Range[1];
databaseOne.activateOnCurrentThread();
final ODocument doc1 = new ODocument();
databaseTwo.activateOnCurrentThread();
final ODocument doc2 = new ODocument();
final ORecordId rid = new ORecordId(clusterId1);
// TODO why this maximums can be different?
final long clusterMax = Math.max(db1Max, db2Max);
final OStorage storage;
ODatabaseDocumentInternal selectedDatabase;
if (clusterMax == db1Max)
selectedDatabase = databaseOne;
else
selectedDatabase = databaseTwo;
OPhysicalPosition[] physicalPositions = makeDbCall(selectedDatabase, new ODbRelatedCall<OPhysicalPosition[]>() {
@Override
public OPhysicalPosition[] call(ODatabaseDocumentInternal database) {
return database.getStorage().ceilingPhysicalPositions(clusterId1, new OPhysicalPosition(0));
}
});
OStorageConfiguration configuration1 = makeDbCall(databaseOne, new ODbRelatedCall<OStorageConfiguration>() {
@Override
public OStorageConfiguration call(ODatabaseDocumentInternal database) {
return database.getStorage().getConfiguration();
}
});
OStorageConfiguration configuration2 = makeDbCall(databaseTwo, new ODbRelatedCall<OStorageConfiguration>() {
@Override
public OStorageConfiguration call(ODatabaseDocumentInternal database) {
return database.getStorage().getConfiguration();
}
});
String storageType1 = makeDbCall(databaseOne, new ODbRelatedCall<String>() {
@Override
public String call(ODatabaseDocumentInternal database) {
return database.getStorage().getType();
}
});
String storageType2 = makeDbCall(databaseTwo, new ODbRelatedCall<String>() {
@Override
public String call(ODatabaseDocumentInternal database) {
return database.getStorage().getType();
}
});
long recordsCounter = 0;
while (physicalPositions.length > 0) {
for (OPhysicalPosition physicalPosition : physicalPositions) {
try {
recordsCounter++;
final long position = physicalPosition.clusterPosition;
rid.setClusterPosition(position);
if (rid.equals(new ORecordId(configuration1.indexMgrRecordId)) && rid.equals(new ORecordId(configuration2.indexMgrRecordId)))
continue;
if (rid.equals(new ORecordId(configuration1.schemaRecordId)) && rid.equals(new ORecordId(configuration2.schemaRecordId)))
continue;
if (rid.getClusterId() == 0 && rid.getClusterPosition() == 0) {
// Skip the compare of raw structure if the storage type are different, due the fact that are different by definition.
if (!storageType1.equals(storageType2))
continue;
}
final ORecordId rid2;
if (ridMapper == null)
rid2 = rid;
else {
final ORID newRid = ridMapper.map(rid);
if (newRid == null)
rid2 = rid;
else
rid2 = new ORecordId(newRid);
}
final ORawBuffer buffer1 = makeDbCall(databaseOne, new ODbRelatedCall<ORawBuffer>() {
@Override
public ORawBuffer call(ODatabaseDocumentInternal database) {
return database.getStorage().readRecord(rid, null, true, false, null).getResult();
}
});
final ORawBuffer buffer2 = makeDbCall(databaseTwo, new ODbRelatedCall<ORawBuffer>() {
@Override
public ORawBuffer call(ODatabaseDocumentInternal database) {
return database.getStorage().readRecord(rid2, null, true, false, null).getResult();
}
});
if (buffer1 == null && buffer2 == null)
// BOTH RECORD NULL, OK
continue;
else if (buffer1 == null && buffer2 != null) {
// REC1 NULL
listener.onMessage("\n- ERR: RID=" + clusterId1 + ":" + position + " is null in DB1");
++differences;
} else if (buffer1 != null && buffer2 == null) {
// REC2 NULL
listener.onMessage("\n- ERR: RID=" + clusterId1 + ":" + position + " is null in DB2");
++differences;
} else {
if (buffer1.recordType != buffer2.recordType) {
listener.onMessage("\n- ERR: RID=" + clusterId1 + ":" + position + " recordType is different: " + (char) buffer1.recordType + " <-> " + (char) buffer2.recordType);
++differences;
}
if (buffer1.buffer == null && buffer2.buffer == null) {
} else if (buffer1.buffer == null && buffer2.buffer != null) {
listener.onMessage("\n- ERR: RID=" + clusterId1 + ":" + position + " content is different: null <-> " + buffer2.buffer.length);
++differences;
} else if (buffer1.buffer != null && buffer2.buffer == null) {
listener.onMessage("\n- ERR: RID=" + clusterId1 + ":" + position + " content is different: " + buffer1.buffer.length + " <-> null");
++differences;
} else {
if (buffer1.recordType == ODocument.RECORD_TYPE) {
// DOCUMENT: TRY TO INSTANTIATE AND COMPARE
makeDbCall(databaseOne, new ODocumentHelper.ODbRelatedCall<Object>() {
public Object call(ODatabaseDocumentInternal database) {
doc1.reset();
doc1.fromStream(buffer1.buffer);
return null;
}
});
makeDbCall(databaseTwo, new ODocumentHelper.ODbRelatedCall<Object>() {
public Object call(ODatabaseDocumentInternal database) {
doc2.reset();
doc2.fromStream(buffer2.buffer);
return null;
}
});
if (rid.toString().equals(configuration1.schemaRecordId) && rid.toString().equals(configuration2.schemaRecordId)) {
makeDbCall(databaseOne, new ODocumentHelper.ODbRelatedCall<java.lang.Object>() {
public Object call(ODatabaseDocumentInternal database) {
convertSchemaDoc(doc1);
return null;
}
});
makeDbCall(databaseTwo, new ODocumentHelper.ODbRelatedCall<java.lang.Object>() {
public Object call(ODatabaseDocumentInternal database) {
convertSchemaDoc(doc2);
return null;
}
});
}
if (!ODocumentHelper.hasSameContentOf(doc1, databaseOne, doc2, databaseTwo, ridMapper)) {
listener.onMessage("\n- ERR: RID=" + clusterId1 + ":" + position + " document content is different");
listener.onMessage("\n--- REC1: " + new String(buffer1.buffer));
listener.onMessage("\n--- REC2: " + new String(buffer2.buffer));
listener.onMessage("\n");
++differences;
}
} else {
if (buffer1.buffer.length != buffer2.buffer.length) {
// CHECK IF THE TRIMMED SIZE IS THE SAME
final String rec1 = new String(buffer1.buffer).trim();
final String rec2 = new String(buffer2.buffer).trim();
if (rec1.length() != rec2.length()) {
listener.onMessage("\n- ERR: RID=" + clusterId1 + ":" + position + " content length is different: " + buffer1.buffer.length + " <-> " + buffer2.buffer.length);
if (buffer1.recordType == ODocument.RECORD_TYPE)
listener.onMessage("\n--- REC1: " + rec1);
if (buffer2.recordType == ODocument.RECORD_TYPE)
listener.onMessage("\n--- REC2: " + rec2);
listener.onMessage("\n");
++differences;
}
} else {
// CHECK BYTE PER BYTE
for (int b = 0; b < buffer1.buffer.length; ++b) {
if (buffer1.buffer[b] != buffer2.buffer[b]) {
listener.onMessage("\n- ERR: RID=" + clusterId1 + ":" + position + " content is different at byte #" + b + ": " + buffer1.buffer[b] + " <-> " + buffer2.buffer[b]);
listener.onMessage("\n--- REC1: " + new String(buffer1.buffer));
listener.onMessage("\n--- REC2: " + new String(buffer2.buffer));
listener.onMessage("\n");
++differences;
break;
}
}
}
}
}
}
} catch (RuntimeException e) {
OLogManager.instance().error(this, "Error during data comparison of records with rid " + rid);
throw e;
}
}
final OPhysicalPosition[] curPosition = physicalPositions;
physicalPositions = makeDbCall(selectedDatabase, new ODbRelatedCall<OPhysicalPosition[]>() {
@Override
public OPhysicalPosition[] call(ODatabaseDocumentInternal database) {
return database.getStorage().higherPhysicalPositions(clusterId1, curPosition[curPosition.length - 1]);
}
});
if (recordsCounter % 10000 == 0)
listener.onMessage("\n" + recordsCounter + " records were processed for cluster " + clusterName + " ...");
}
listener.onMessage("\nCluster comparison was finished, " + recordsCounter + " records were processed for cluster " + clusterName + " ...");
}
return true;
}
use of com.orientechnologies.orient.core.storage.OStorage in project orientdb by orientechnologies.
the class Orient method shutdownAllStorages.
private void shutdownAllStorages() {
engineLock.writeLock().lock();
try {
// CLOSE ALL THE STORAGES
final List<OStorage> storagesCopy = new ArrayList<OStorage>(storages.values());
for (OStorage stg : storagesCopy) {
try {
OLogManager.instance().info(this, "- shutdown storage: " + stg.getName() + "...");
stg.shutdown();
} catch (Throwable e) {
OLogManager.instance().warn(this, "-- error on shutdown storage", e);
}
}
storages.clear();
} finally {
engineLock.writeLock().unlock();
}
}
Aggregations