Search in sources :

Example 21 with OSchemaException

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();
    }
}
Also used : ODatabaseLifecycleListener(com.orientechnologies.orient.core.db.ODatabaseLifecycleListener) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException)

Example 22 with OSchemaException

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);
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException)

Example 23 with OSchemaException

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();
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ORecordSerializerSchemaAware2CSV(com.orientechnologies.orient.core.serialization.serializer.record.string.ORecordSerializerSchemaAware2CSV) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 24 with OSchemaException

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();
    }
}
Also used : OSecurityUser(com.orientechnologies.orient.core.metadata.security.OSecurityUser) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException)

Example 25 with OSchemaException

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;
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException)

Aggregations

OSchemaException (com.orientechnologies.orient.core.exception.OSchemaException)26 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)10 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)10 OException (com.orientechnologies.common.exception.OException)7 OConfigurationException (com.orientechnologies.orient.core.exception.OConfigurationException)5 OStorage (com.orientechnologies.orient.core.storage.OStorage)5 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)4 ODatabaseLifecycleListener (com.orientechnologies.orient.core.db.ODatabaseLifecycleListener)3 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)3 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)3 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)3 OAutoshardedStorage (com.orientechnologies.orient.core.storage.OAutoshardedStorage)3 OStorageProxy (com.orientechnologies.orient.core.storage.OStorageProxy)3 OObjectNotDetachedException (com.orientechnologies.orient.object.db.OObjectNotDetachedException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Field (java.lang.reflect.Field)3 Method (java.lang.reflect.Method)3 OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)2 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)2 OSecurityAccessException (com.orientechnologies.orient.core.exception.OSecurityAccessException)2