Search in sources :

Example 31 with ODatabaseDocumentInternal

use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal 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(Locale.ENGLISH);
        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();
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OAutoshardedStorage(com.orientechnologies.orient.core.storage.OAutoshardedStorage) OStorageProxy(com.orientechnologies.orient.core.storage.OStorageProxy) OStorage(com.orientechnologies.orient.core.storage.OStorage) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Example 32 with ODatabaseDocumentInternal

use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal in project orientdb by orientechnologies.

the class OSchemaShared method createClassInternal.

private OClass createClassInternal(final String className, final int[] clusterIdsToAdd, final List<OClass> superClasses) throws ClusterIdsAreEmptyException {
    acquireSchemaWriteLock();
    try {
        if (className == null || className.length() == 0)
            throw new OSchemaException("Found class name null or empty");
        if (Character.isDigit(className.charAt(0)))
            throw new OSchemaException("Found invalid class name. Cannot start with numbers");
        final Character wrongCharacter = checkClassNameIfValid(className);
        if (wrongCharacter != null)
            throw new OSchemaException("Found invalid class name. Character '" + wrongCharacter + "' cannot be used in class name.");
        final ODatabaseDocumentInternal database = getDatabase();
        final OStorage storage = database.getStorage();
        checkEmbedded(storage);
        checkClustersAreAbsent(clusterIdsToAdd);
        final int[] clusterIds;
        if (clusterIdsToAdd == null || clusterIdsToAdd.length == 0) {
            throw new ClusterIdsAreEmptyException();
        } else
            clusterIds = clusterIdsToAdd;
        database.checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_CREATE);
        final String key = className.toLowerCase(Locale.ENGLISH);
        if (classes.containsKey(key))
            throw new OSchemaException("Class '" + className + "' already exists in current database");
        OClassImpl cls = new OClassImpl(this, className, clusterIds);
        classes.put(key, cls);
        if (superClasses != null && superClasses.size() > 0) {
            cls.setSuperClassesInternal(superClasses);
            for (OClass superClass : superClasses) {
                // UPDATE INDEXES
                final int[] clustersToIndex = superClass.getPolymorphicClusterIds();
                final String[] clusterNames = new String[clustersToIndex.length];
                for (int i = 0; i < clustersToIndex.length; i++) clusterNames[i] = database.getClusterNameById(clustersToIndex[i]);
                for (OIndex<?> index : superClass.getIndexes()) for (String clusterName : clusterNames) if (clusterName != null)
                    database.getMetadata().getIndexManager().addClusterToIndex(clusterName, index.getName());
            }
        }
        addClusterClassMap(cls);
        return cls;
    } finally {
        releaseSchemaWriteLock();
    }
}
Also used : OStorage(com.orientechnologies.orient.core.storage.OStorage) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Example 33 with ODatabaseDocumentInternal

use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal 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(Locale.ENGLISH);
        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(Locale.ENGLISH));
        // 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;
}
Also used : OStorageProxy(com.orientechnologies.orient.core.storage.OStorageProxy) OStorage(com.orientechnologies.orient.core.storage.OStorage) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OAutoshardedStorage(com.orientechnologies.orient.core.storage.OAutoshardedStorage) ODatabaseLifecycleListener(com.orientechnologies.orient.core.db.ODatabaseLifecycleListener)

Example 34 with ODatabaseDocumentInternal

use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal in project orientdb by orientechnologies.

the class OSchemaShared method create.

public void create() {
    rwSpinLock.acquireWriteLock();
    try {
        final ODatabaseDocumentInternal db = getDatabase();
        super.save(OMetadataDefault.CLUSTER_INTERNAL_NAME);
        db.getStorage().getConfiguration().schemaRecordId = document.getIdentity().toString();
        db.getStorage().getConfiguration().update();
        snapshot = new OImmutableSchema(this);
    } finally {
        rwSpinLock.releaseWriteLock();
    }
}
Also used : ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Example 35 with ODatabaseDocumentInternal

use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal in project orientdb by orientechnologies.

the class OClassImpl method dropProperty.

public void dropProperty(final String propertyName) {
    if (getDatabase().getTransaction().isActive())
        throw new IllegalStateException("Cannot drop a property inside a transaction");
    getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_DELETE);
    final String lowerName = propertyName.toLowerCase(Locale.ENGLISH);
    acquireSchemaWriteLock();
    try {
        if (!properties.containsKey(lowerName))
            throw new OSchemaException("Property '" + propertyName + "' not found in class " + name + "'");
        final ODatabaseDocumentInternal database = getDatabase();
        final OStorage storage = database.getStorage();
        if (storage instanceof OStorageProxy) {
            if (getDatabase().getStorage().getConfiguration().isStrictSql()) {
                database.command(new OCommandSQL("drop property " + name + ".`" + propertyName + "`")).execute();
            } else {
                database.command(new OCommandSQL("drop property " + name + '.' + propertyName)).execute();
            }
        } else if (isDistributedCommand()) {
            OScenarioThreadLocal.executeAsDistributed(new Callable<OProperty>() {

                @Override
                public OProperty call() throws Exception {
                    dropPropertyInternal(propertyName);
                    return null;
                }
            });
            String stm;
            if (getDatabase().getStorage().getConfiguration().isStrictSql()) {
                stm = "drop property " + name + ".`" + propertyName + "`";
            } else {
                stm = "drop property " + name + "." + propertyName;
            }
            final OCommandSQL commandSQL = new OCommandSQL(stm);
            commandSQL.addExcludedNode(((OAutoshardedStorage) storage).getNodeId());
            database.command(commandSQL).execute();
        } else
            OScenarioThreadLocal.executeAsDistributed(new Callable<OProperty>() {

                @Override
                public OProperty call() throws Exception {
                    dropPropertyInternal(propertyName);
                    return null;
                }
            });
    } finally {
        releaseSchemaWriteLock();
    }
}
Also used : OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) Callable(java.util.concurrent.Callable) OException(com.orientechnologies.common.exception.OException) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) IOException(java.io.IOException) OSecurityAccessException(com.orientechnologies.orient.core.exception.OSecurityAccessException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL)

Aggregations

ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)149 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)42 OStorage (com.orientechnologies.orient.core.storage.OStorage)31 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)26 OStorageProxy (com.orientechnologies.orient.core.storage.OStorageProxy)20 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)18 OAutoshardedStorage (com.orientechnologies.orient.core.storage.OAutoshardedStorage)18 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)16 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)16 ORID (com.orientechnologies.orient.core.id.ORID)14 OException (com.orientechnologies.common.exception.OException)13 IOException (java.io.IOException)13 ORecordId (com.orientechnologies.orient.core.id.ORecordId)12 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)10 OSchemaException (com.orientechnologies.orient.core.exception.OSchemaException)10 ORecord (com.orientechnologies.orient.core.record.ORecord)8 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)7 OMetadataInternal (com.orientechnologies.orient.core.metadata.OMetadataInternal)6 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)5 OSecurityException (com.orientechnologies.orient.core.exception.OSecurityException)5