Search in sources :

Example 61 with ODatabaseDocumentInternal

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

the class OClassImpl method setOverSize.

public OClass setOverSize(final float overSize) {
    getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
    acquireSchemaWriteLock();
    try {
        final ODatabaseDocumentInternal database = getDatabase();
        final OStorage storage = database.getStorage();
        if (storage instanceof OStorageProxy) {
            // FORMAT FLOAT LOCALE AGNOSTIC
            final String cmd = String.format("alter class `%s` oversize %s", name, new Float(overSize).toString());
            database.command(new OCommandSQL(cmd)).execute();
        } else if (isDistributedCommand()) {
            // FORMAT FLOAT LOCALE AGNOSTIC
            final String cmd = String.format("alter class `%s` oversize %s", name, new Float(overSize).toString());
            final OCommandSQL commandSQL = new OCommandSQL(cmd);
            commandSQL.addExcludedNode(((OAutoshardedStorage) storage).getNodeId());
            database.command(commandSQL).execute();
            setOverSizeInternal(overSize);
        } else
            setOverSizeInternal(overSize);
    } finally {
        releaseSchemaWriteLock();
    }
    return this;
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Example 62 with ODatabaseDocumentInternal

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

Example 63 with ODatabaseDocumentInternal

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

Example 64 with ODatabaseDocumentInternal

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

the class OClassImpl method removeSuperClass.

@Override
public OClass removeSuperClass(OClass superClass) {
    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 class `%s` superclass -`%s`", name, superClass != null ? superClass.getName() : null);
            database.command(new OCommandSQL(cmd)).execute();
        } else if (isDistributedCommand()) {
            final String cmd = String.format("alter class `%s` superclass -`%s`", name, superClass != null ? superClass.getName() : null);
            final OCommandSQL commandSQL = new OCommandSQL(cmd);
            commandSQL.addExcludedNode(((OAutoshardedStorage) storage).getNodeId());
            database.command(commandSQL).execute();
            removeSuperClassInternal(superClass);
        } else
            removeSuperClassInternal(superClass);
    } finally {
        releaseSchemaWriteLock();
    }
    return this;
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Example 65 with ODatabaseDocumentInternal

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

the class OAbstractPaginatedStorage method commit.

public List<ORecordOperation> commit(final OTransaction clientTx, Runnable callback) {
    checkOpeness();
    checkLowDiskSpaceFullCheckpointRequestsAndBackgroundDataFlushExceptions();
    txBegun.incrementAndGet();
    final ODatabaseDocumentInternal databaseRecord = (ODatabaseDocumentInternal) clientTx.getDatabase();
    final OIndexManagerProxy indexManager = databaseRecord.getMetadata().getIndexManager();
    final TreeMap<String, OTransactionIndexChanges> indexesToCommit = getSortedIndexEntries(clientTx);
    final Map<ORecordOperation, Integer> clusterOverrides = new IdentityHashMap<ORecordOperation, Integer>();
    databaseRecord.getMetadata().makeThreadLocalSchemaSnapshot();
    if (OLogManager.instance().isDebugEnabled())
        OLogManager.instance().debug(this, "%d Committing transaction %d on database '%s' (items=%d)...", Thread.currentThread().getId(), clientTx.getId(), databaseRecord.getName(), clientTx.getEntryCount());
    final Iterable<ORecordOperation> entries = (Iterable<ORecordOperation>) clientTx.getAllRecordEntries();
    final TreeMap<Integer, OCluster> clustersToLock = new TreeMap<Integer, OCluster>();
    final Set<ORecordOperation> newRecords = new TreeSet<ORecordOperation>(new Comparator<ORecordOperation>() {

        @Override
        public int compare(final ORecordOperation o1, final ORecordOperation o2) {
            return o1.getRecord().getIdentity().compareTo(o2.getRecord().getIdentity());
        }
    });
    for (ORecordOperation txEntry : entries) {
        if (txEntry.type == ORecordOperation.CREATED || txEntry.type == ORecordOperation.UPDATED) {
            final ORecord record = txEntry.getRecord();
            if (record instanceof ODocument)
                ((ODocument) record).validate();
        }
        if (txEntry.type == ORecordOperation.UPDATED || txEntry.type == ORecordOperation.DELETED) {
            final int clusterId = txEntry.getRecord().getIdentity().getClusterId();
            clustersToLock.put(clusterId, getClusterById(clusterId));
        } else if (txEntry.type == ORecordOperation.CREATED) {
            newRecords.add(txEntry);
            final ORecord record = txEntry.getRecord();
            final ORID rid = record.getIdentity();
            int clusterId = rid.getClusterId();
            if (record.isDirty() && clusterId == ORID.CLUSTER_ID_INVALID && record instanceof ODocument) {
                // TRY TO FIX CLUSTER ID TO THE DEFAULT CLUSTER ID DEFINED IN SCHEMA CLASS
                final OImmutableClass class_ = ODocumentInternal.getImmutableSchemaClass(((ODocument) record));
                if (class_ != null) {
                    clusterId = class_.getClusterForNewInstance((ODocument) record);
                    clusterOverrides.put(txEntry, clusterId);
                }
            }
            clustersToLock.put(clusterId, getClusterById(clusterId));
        }
    }
    final List<ORecordOperation> result = new ArrayList<ORecordOperation>();
    final List<Lock[]> indexKeyLockList = new ArrayList<Lock[]>(indexesToCommit.size());
    stateLock.acquireReadLock();
    try {
        try {
            try {
                checkOpeness();
                lockIndexKeys(indexManager, indexesToCommit, indexKeyLockList);
                makeStorageDirty();
                startStorageTx(clientTx);
                lockClusters(clustersToLock);
                lockRidBags(clustersToLock, indexesToCommit);
                lockIndexes(indexesToCommit);
                Map<ORecordOperation, OPhysicalPosition> positions = new IdentityHashMap<ORecordOperation, OPhysicalPosition>();
                for (ORecordOperation txEntry : newRecords) {
                    ORecord rec = txEntry.getRecord();
                    if (rec.isDirty()) {
                        ORecordId rid = (ORecordId) rec.getIdentity().copy();
                        ORecordId oldRID = rid.copy();
                        final Integer clusterOverride = clusterOverrides.get(txEntry);
                        final int clusterId = clusterOverride == null ? rid.getClusterId() : clusterOverride;
                        final OCluster cluster = getClusterById(clusterId);
                        OPhysicalPosition ppos = cluster.allocatePosition(ORecordInternal.getRecordType(rec));
                        rid.setClusterId(cluster.getId());
                        if (rid.getClusterPosition() > -1) {
                            // RECORD HAVING A HIGHER CLUSTER POSITION
                            while (rid.getClusterPosition() > ppos.clusterPosition) {
                                ppos = cluster.allocatePosition(ORecordInternal.getRecordType(rec));
                            }
                            if (rid.getClusterPosition() != ppos.clusterPosition)
                                throw new OConcurrentCreateException(rid, new ORecordId(rid.getClusterId(), ppos.clusterPosition));
                        }
                        positions.put(txEntry, ppos);
                        rid.setClusterPosition(ppos.clusterPosition);
                        clientTx.updateIdentityAfterCommit(oldRID, rid);
                    }
                }
                for (ORecordOperation txEntry : entries) {
                    commitEntry(txEntry, positions.get(txEntry));
                    result.add(txEntry);
                }
                commitIndexes(indexesToCommit);
                endStorageTx();
                OTransactionAbstract.updateCacheFromEntries(clientTx, entries, true);
                txCommit.incrementAndGet();
            } catch (IOException ioe) {
                makeRollback(clientTx, ioe);
            } catch (RuntimeException e) {
                makeRollback(clientTx, e);
            } finally {
                unlockIndexKeys(indexesToCommit, indexKeyLockList);
                transaction.set(null);
            }
        } finally {
            databaseRecord.getMetadata().clearThreadLocalSchemaSnapshot();
        }
    } finally {
        stateLock.releaseReadLock();
    }
    if (OLogManager.instance().isDebugEnabled())
        OLogManager.instance().debug(this, "%d Committed transaction %d on database '%s' (result=%s)", Thread.currentThread().getId(), clientTx.getId(), databaseRecord.getName(), result);
    return result;
}
Also used : ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OImmutableClass(com.orientechnologies.orient.core.metadata.schema.OImmutableClass) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Lock(java.util.concurrent.locks.Lock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ORecord(com.orientechnologies.orient.core.record.ORecord)

Aggregations

ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)139 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 OAutoshardedStorage (com.orientechnologies.orient.core.storage.OAutoshardedStorage)18 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)17 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)16 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)14 ORID (com.orientechnologies.orient.core.id.ORID)13 IOException (java.io.IOException)13 OException (com.orientechnologies.common.exception.OException)12 ORecordId (com.orientechnologies.orient.core.id.ORecordId)11 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)10 OSchemaException (com.orientechnologies.orient.core.exception.OSchemaException)10 ORecord (com.orientechnologies.orient.core.record.ORecord)7 OMetadataInternal (com.orientechnologies.orient.core.metadata.OMetadataInternal)6 OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)5 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)5 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)5