use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.
the class OClassImpl method addCluster.
@Override
public OClass addCluster(final String clusterNameOrId) {
getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
if (isAbstract()) {
throw new OSchemaException("Impossible to associate a cluster to an abstract class class");
}
acquireSchemaWriteLock();
try {
final ODatabaseDocumentInternal database = getDatabase();
final OStorage storage = database.getStorage();
if (storage instanceof OStorageProxy) {
final String cmd = String.format("alter class `%s` addcluster `%s`", name, clusterNameOrId);
database.command(new OCommandSQL(cmd)).execute();
} else if (isDistributedCommand()) {
final int clusterId = owner.createClusterIfNeeded(clusterNameOrId);
addClusterIdInternal(clusterId);
final String cmd = String.format("alter class `%s` addcluster `%s`", name, clusterNameOrId);
final OCommandSQL commandSQL = new OCommandSQL(cmd);
commandSQL.addExcludedNode(((OAutoshardedStorage) storage).getNodeId());
database.command(commandSQL).execute();
} else {
final int clusterId = owner.createClusterIfNeeded(clusterNameOrId);
addClusterIdInternal(clusterId);
}
} finally {
releaseSchemaWriteLock();
}
return this;
}
use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.
the class OClassImpl method addClusterId.
public OClass addClusterId(final int clusterId) {
getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
if (isAbstract()) {
throw new OSchemaException("Impossible to associate a cluster to an abstract class class");
}
acquireSchemaWriteLock();
try {
final ODatabaseDocumentInternal database = getDatabase();
final OStorage storage = database.getStorage();
if (storage instanceof OStorageProxy) {
final String cmd = String.format("alter class `%s` addcluster %d", name, clusterId);
database.command(new OCommandSQL(cmd)).execute();
} else if (isDistributedCommand()) {
final String cmd = String.format("alter class `%s` addcluster %d", name, clusterId);
final OCommandSQL commandSQL = new OCommandSQL(cmd);
commandSQL.addExcludedNode(((OAutoshardedStorage) storage).getNodeId());
database.command(commandSQL).execute();
addClusterIdInternal(clusterId);
} else
addClusterIdInternal(clusterId);
} finally {
releaseSchemaWriteLock();
}
return this;
}
use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.
the class OrientdbEdgeTest method testEdges.
@Test
public void testEdges() throws Exception {
OrientGraphFactory factory = getGraphFactory();
OrientBaseGraph g = factory.getNoTx();
try {
try {
g.createEdgeType("some-label");
} catch (OSchemaException ex) {
if (!ex.getMessage().contains("exists"))
throw (ex);
g.command(new OCommandSQL("delete edge some-label")).execute();
}
try {
g.createVertexType("some-v-label");
} catch (OSchemaException ex) {
if (!ex.getMessage().contains("exists"))
throw (ex);
g.command(new OCommandSQL("delete vertex some-v-label")).execute();
}
} finally {
g.shutdown();
}
OrientGraph t = factory.getTx();
try {
Vertex v1 = t.addVertex("class:some-v-label");
Vertex v2 = t.addVertex("class:some-v-label");
v1.setProperty("_id", "v1");
v2.setProperty("_id", "v2");
OrientEdge edge = t.addEdge(null, v1, v2, "some-label");
edge.setProperty("some", "thing");
t.commit();
t.shutdown();
t = factory.getTx();
assertEquals(2, t.countVertices("some-v-label"));
assertEquals(1, t.countEdges());
assertNotNull(t.getVertices("_id", "v1").iterator().next());
assertNotNull(t.getVertices("_id", "v2").iterator().next());
t.commit();
t.shutdown();
t = factory.getTx();
// works
assertEquals(1, t.getVertices("_id", "v1").iterator().next().query().labels("some-label").count());
// NoSuchElementException
assertNotNull(t.getVertices("_id", "v1").iterator().next().query().labels("some-label").edges().iterator().next());
t.commit();
} finally {
t.shutdown();
}
}
use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.
the class OrientElement method checkForClassInSchema.
/**
* Check if a class already exists, otherwise create it at the fly. If a transaction is running commit changes, create the class
* and begin a new transaction.
*
* @param className
* Class's name
*/
protected String checkForClassInSchema(final String className) {
if (className == null)
return null;
OrientBaseGraph graph = getGraph();
if (graph == null)
return className;
final OSchema schema = graph.getRawGraph().getMetadata().getSchema();
if (!schema.existsClass(className)) {
// CREATE A NEW CLASS AT THE FLY
try {
graph.executeOutsideTx(new OCallable<OClass, OrientBaseGraph>() {
@Override
public OClass call(final OrientBaseGraph g) {
return schema.createClass(className, schema.getClass(getBaseClassName()));
}
}, "Committing the active transaction to create the new type '", className, "' as subclass of '", getBaseClassName(), "'. The transaction will be reopen right after that. To avoid this behavior create the classes outside the transaction");
} catch (OSchemaException e) {
if (!schema.existsClass(className))
throw e;
}
} else {
// CHECK THE CLASS INHERITANCE
final OClass cls = schema.getClass(className);
if (!cls.isSubClassOf(getBaseClassName()))
throw new IllegalArgumentException("Class '" + className + "' is not an instance of " + getBaseClassName());
}
return className;
}
use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.
the class ODatabaseImport method importRecord.
private ORID importRecord() throws Exception {
String value = jsonReader.readString(OJSONReader.END_OBJECT, true);
// JUMP EMPTY RECORDS
while (!value.isEmpty() && value.charAt(0) != '{') {
value = value.substring(1);
}
record = null;
try {
try {
record = ORecordSerializerJSON.INSTANCE.fromString(value, record, null);
} catch (OSerializationException e) {
if (e.getCause() instanceof OSchemaException) {
// EXTRACT CLASS NAME If ANY
final int pos = value.indexOf("\"@class\":\"");
if (pos > -1) {
final int end = value.indexOf("\"", pos + "\"@class\":\"".length() + 1);
final String value1 = value.substring(0, pos + "\"@class\":\"".length());
final String clsName = value.substring(pos + "\"@class\":\"".length(), end);
final String value2 = value.substring(end);
final String newClassName = convertedClassNames.get(clsName);
value = value1 + newClassName + value2;
// OVERWRITE CLASS NAME WITH NEW NAME
record = ORecordSerializerJSON.INSTANCE.fromString(value, record, null);
}
} else
throw OException.wrapException(new ODatabaseImportException("Error on importing record"), e);
}
// Incorrect record format , skip this record
if (record == null || record.getIdentity() == null) {
OLogManager.instance().warn(this, "Broken record was detected and will be skipped");
return null;
}
if (schemaImported && record.getIdentity().equals(schemaRecordId)) {
// JUMP THE SCHEMA
return null;
}
// CHECK IF THE CLUSTER IS INCLUDED
if (includeClusters != null) {
if (!includeClusters.contains(database.getClusterNameById(record.getIdentity().getClusterId()))) {
jsonReader.readNext(OJSONReader.NEXT_IN_ARRAY);
return null;
}
} else if (excludeClusters != null) {
if (excludeClusters.contains(database.getClusterNameById(record.getIdentity().getClusterId())))
return null;
}
if (record.getIdentity().getClusterId() == 0 && record.getIdentity().getClusterPosition() == 1)
// JUMP INTERNAL RECORDS
return null;
if (exporterVersion >= 3) {
int oridsId = database.getClusterIdByName("ORIDs");
int indexId = database.getClusterIdByName(OMetadataDefault.CLUSTER_INDEX_NAME);
if (record.getIdentity().getClusterId() == indexId || record.getIdentity().getClusterId() == oridsId)
// JUMP INDEX RECORDS
return null;
}
final int manualIndexCluster = database.getClusterIdByName(OMetadataDefault.CLUSTER_MANUAL_INDEX_NAME);
final int internalCluster = database.getClusterIdByName(OMetadataDefault.CLUSTER_INTERNAL_NAME);
final int indexCluster = database.getClusterIdByName(OMetadataDefault.CLUSTER_INDEX_NAME);
if (exporterVersion >= 4) {
if (record.getIdentity().getClusterId() == manualIndexCluster)
// JUMP INDEX RECORDS
return null;
}
if (record.getIdentity().equals(indexMgrRecordId))
return null;
final ORID rid = record.getIdentity();
final int clusterId = rid.getClusterId();
if ((clusterId != manualIndexCluster && clusterId != internalCluster && clusterId != indexCluster)) {
ORecordInternal.setVersion(record, 0);
record.setDirty();
ORecordInternal.setIdentity(record, new ORecordId());
if (!preserveRids && record instanceof ODocument && ODocumentInternal.getImmutableSchemaClass(((ODocument) record)) != null)
record.save();
else
record.save(database.getClusterNameById(clusterId));
if (!rid.equals(record.getIdentity()))
// SAVE IT ONLY IF DIFFERENT
exportImportHashTable.put(rid, record.getIdentity());
}
} catch (Exception t) {
if (record != null)
OLogManager.instance().error(this, "Error importing record " + record.getIdentity() + ". Source line " + jsonReader.getLineNumber() + ", column " + jsonReader.getColumnNumber());
else
OLogManager.instance().error(this, "Error importing record. Source line " + jsonReader.getLineNumber() + ", column " + jsonReader.getColumnNumber());
throw t;
} finally {
jsonReader.readNext(OJSONReader.NEXT_IN_ARRAY);
}
return record.getIdentity();
}
Aggregations