use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.
the class OSchemaShared method dropClassInternal.
private void dropClassInternal(final String className) {
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();
final 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");
checkEmbedded(getDatabase().getStorage());
for (OClass superClass : cls.getSuperClasses()) {
// REMOVE DEPENDENCY FROM SUPERCLASS
((OClassImpl) superClass).removeBaseClassInternal(cls);
}
for (int id : cls.getClusterIds()) {
if (id != -1)
deleteCluster(getDatabase(), id);
}
dropClassIndexes(cls);
classes.remove(key);
if (cls.getShortName() != null)
// REMOVE THE ALIAS TOO
classes.remove(cls.getShortName().toLowerCase());
removeClusterClassMap(cls);
// WAKE UP DB LIFECYCLE LISTENER
for (Iterator<ODatabaseLifecycleListener> it = Orient.instance().getDbLifecycleListeners(); it.hasNext(); ) it.next().onDropClass(getDatabase(), cls);
} finally {
releaseSchemaWriteLock();
}
}
use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.
the class OSchemaShared method saveInternal.
private void saveInternal() {
final ODatabaseDocument db = getDatabase();
if (db.getTransaction().isActive()) {
reload(null, true);
throw new OSchemaException("Cannot change the schema while a transaction is active. Schema changes are not transactional");
}
setDirty();
OScenarioThreadLocal.executeAsDistributed(new Callable<Object>() {
@Override
public Object call() {
try {
toStream();
document.save(OMetadataDefault.CLUSTER_INTERNAL_NAME);
} catch (OConcurrentModificationException e) {
reload(null, true);
throw e;
}
return null;
}
});
snapshot = new OImmutableSchema(this);
}
use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.
the class OClassImpl method setNameInternal.
private void setNameInternal(final String name) {
getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
acquireSchemaWriteLock();
try {
checkEmbedded();
final String oldName = this.name;
owner.changeClassName(this.name, name, this);
this.name = name;
ODatabaseDocumentInternal database = getDatabase();
final OStorage storage = database.getStorage();
if (!database.getStorageVersions().classesAreDetectedByClusterId()) {
for (int clusterId : clusterIds) {
long[] range = storage.getClusterDataRange(clusterId);
OPhysicalPosition[] positions = storage.ceilingPhysicalPositions(clusterId, new OPhysicalPosition(range[0]));
do {
for (OPhysicalPosition position : positions) {
final ORecordId identity = new ORecordId(clusterId, position.clusterPosition);
final ORawBuffer record = storage.readRecord(identity, null, true, false, null).getResult();
if (record.recordType == ODocument.RECORD_TYPE) {
final ORecordSerializerSchemaAware2CSV serializer = (ORecordSerializerSchemaAware2CSV) ORecordSerializerFactory.instance().getFormat(ORecordSerializerSchemaAware2CSV.NAME);
String persName = new String(record.buffer, "UTF-8");
if (serializer.getClassName(persName).equalsIgnoreCase(name)) {
final ODocument document = new ODocument();
document.setLazyLoad(false);
document.fromStream(record.buffer);
ORecordInternal.setVersion(document, record.version);
ORecordInternal.setIdentity(document, identity);
document.setClassName(name);
document.setDirty();
document.save();
}
}
if (positions.length > 0)
positions = storage.higherPhysicalPositions(clusterId, positions[positions.length - 1]);
}
} while (positions.length > 0);
}
}
renameCluster(oldName, this.name);
} catch (UnsupportedEncodingException e) {
throw OException.wrapException(new OSchemaException("Error reading schema"), e);
} finally {
releaseSchemaWriteLock();
}
}
use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.
the class OClassImpl method addSuperClassInternal.
void addSuperClassInternal(final OClass superClass) {
acquireSchemaWriteLock();
try {
final OClassImpl cls;
if (superClass instanceof OClassAbstractDelegate)
cls = (OClassImpl) ((OClassAbstractDelegate) superClass).delegate;
else
cls = (OClassImpl) superClass;
if (cls != null) {
// CHECK THE USER HAS UPDATE PRIVILEGE AGAINST EXTENDING CLASS
final OSecurityUser user = getDatabase().getUser();
if (user != null)
user.allow(ORule.ResourceGeneric.CLASS, cls.getName(), ORole.PERMISSION_UPDATE);
if (superClasses.contains(superClass)) {
throw new OSchemaException("Class: '" + this.getName() + "' already has the class '" + superClass.getName() + "' as superclass");
}
cls.addBaseClass(this);
superClasses.add(cls);
}
} finally {
releaseSchemaWriteLock();
}
}
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;
}
Aggregations