Search in sources :

Example 1 with OAutoshardedStorage

use of com.orientechnologies.orient.core.storage.OAutoshardedStorage 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 2 with OAutoshardedStorage

use of com.orientechnologies.orient.core.storage.OAutoshardedStorage 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 3 with OAutoshardedStorage

use of com.orientechnologies.orient.core.storage.OAutoshardedStorage in project orientdb by orientechnologies.

the class OPropertyImpl method setLinkedType.

public OProperty setLinkedType(final OType linkedType) {
    getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
    checkLinkTypeSupport(getType());
    acquireSchemaWriteLock();
    try {
        final ODatabaseDocumentInternal database = getDatabase();
        final OStorage storage = database.getStorage();
        if (storage instanceof OStorageProxy) {
            final String cmd = String.format("alter property %s linkedtype %s", getFullNameQuoted(), linkedType);
            database.command(new OCommandSQL(cmd)).execute();
        } else if (isDistributedCommand()) {
            final String cmd = String.format("alter property %s linkedtype %s", getFullNameQuoted(), linkedType);
            final OCommandSQL commandSQL = new OCommandSQL(cmd);
            commandSQL.addExcludedNode(((OAutoshardedStorage) storage).getNodeId());
            database.command(commandSQL).execute();
            setLinkedTypeInternal(linkedType);
        } else
            setLinkedTypeInternal(linkedType);
    } finally {
        releaseSchemaWriteLock();
    }
    return this;
}
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) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Example 4 with OAutoshardedStorage

use of com.orientechnologies.orient.core.storage.OAutoshardedStorage in project orientdb by orientechnologies.

the class OPropertyImpl method setName.

public OProperty setName(final String name) {
    getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
    acquireSchemaWriteLock();
    try {
        final ODatabaseDocumentInternal database = getDatabase();
        final OStorage storage = database.getStorage();
        if (storage instanceof OStorageProxy) {
            final String cmd = String.format("alter property %s name %s", getFullNameQuoted(), quoteString(name));
            database.command(new OCommandSQL(cmd)).execute();
        } else if (isDistributedCommand()) {
            final String cmd = String.format("alter property %s name %s", getFullNameQuoted(), quoteString(name));
            final OCommandSQL commandSQL = new OCommandSQL(cmd);
            commandSQL.addExcludedNode(((OAutoshardedStorage) storage).getNodeId());
            database.command(commandSQL).execute();
            setNameInternal(name);
        } else
            setNameInternal(name);
    } finally {
        releaseSchemaWriteLock();
    }
    return this;
}
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) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Example 5 with OAutoshardedStorage

use of com.orientechnologies.orient.core.storage.OAutoshardedStorage in project orientdb by orientechnologies.

the class OPropertyImpl method setCustom.

public OPropertyImpl setCustom(final String name, final String value) {
    getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
    acquireSchemaWriteLock();
    try {
        final String cmd = String.format("alter property %s custom %s=%s", getFullNameQuoted(), name, quoteString(value));
        final ODatabaseDocumentInternal database = getDatabase();
        final OStorage storage = database.getStorage();
        if (storage instanceof OStorageProxy) {
            database.command(new OCommandSQL(cmd)).execute();
        } else if (isDistributedCommand()) {
            final OCommandSQL commandSQL = new OCommandSQL(cmd);
            commandSQL.addExcludedNode(((OAutoshardedStorage) storage).getNodeId());
            database.command(commandSQL).execute();
            setCustomInternal(name, value);
        } else
            setCustomInternal(name, value);
    } finally {
        releaseSchemaWriteLock();
    }
    return this;
}
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) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Aggregations

ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)18 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)18 OAutoshardedStorage (com.orientechnologies.orient.core.storage.OAutoshardedStorage)18 OStorage (com.orientechnologies.orient.core.storage.OStorage)18 OStorageProxy (com.orientechnologies.orient.core.storage.OStorageProxy)18 OSchemaException (com.orientechnologies.orient.core.exception.OSchemaException)3 ODatabaseLifecycleListener (com.orientechnologies.orient.core.db.ODatabaseLifecycleListener)2