Search in sources :

Example 6 with OPaginatedClusterException

use of com.orientechnologies.orient.core.exception.OPaginatedClusterException in project orientdb by orientechnologies.

the class OPaginatedCluster method readDebug.

public OPaginatedClusterDebug readDebug(long clusterPosition) throws IOException {
    startOperation();
    try {
        OPaginatedClusterDebug debug = new OPaginatedClusterDebug();
        debug.clusterPosition = clusterPosition;
        debug.fileId = fileId;
        OAtomicOperation atomicOperation = storageLocal.getAtomicOperationsManager().getCurrentOperation();
        OClusterPositionMapBucket.PositionEntry positionEntry = clusterPositionMap.get(clusterPosition, 1);
        if (positionEntry == null) {
            debug.empty = true;
            return debug;
        }
        long pageIndex = positionEntry.getPageIndex();
        int recordPosition = positionEntry.getRecordPosition();
        if (getFilledUpTo(atomicOperation, fileId) <= pageIndex) {
            debug.empty = true;
            return debug;
        }
        debug.pages = new ArrayList<OClusterPageDebug>();
        int contentSize = 0;
        long nextPagePointer;
        boolean firstEntry = true;
        do {
            OClusterPageDebug debugPage = new OClusterPageDebug();
            debugPage.pageIndex = pageIndex;
            OCacheEntry cacheEntry = loadPage(atomicOperation, fileId, pageIndex, false);
            cacheEntry.acquireSharedLock();
            try {
                final OClusterPage localPage = new OClusterPage(cacheEntry, false, getChanges(atomicOperation, cacheEntry));
                if (localPage.isDeleted(recordPosition)) {
                    if (debug.pages.isEmpty()) {
                        debug.empty = true;
                        return debug;
                    } else
                        throw new OPaginatedClusterException("Content of record " + new ORecordId(id, clusterPosition) + " was broken", this);
                }
                debugPage.inPagePosition = recordPosition;
                debugPage.inPageSize = localPage.getRecordSize(recordPosition);
                byte[] content = localPage.getRecordBinaryValue(recordPosition, 0, debugPage.inPageSize);
                debugPage.content = content;
                if (firstEntry && content[content.length - OLongSerializer.LONG_SIZE - OByteSerializer.BYTE_SIZE] == 0) {
                    debug.empty = true;
                    return debug;
                }
                debug.pages.add(debugPage);
                nextPagePointer = OLongSerializer.INSTANCE.deserializeNative(content, content.length - OLongSerializer.LONG_SIZE);
                contentSize += content.length - OLongSerializer.LONG_SIZE - OByteSerializer.BYTE_SIZE;
                firstEntry = false;
            } finally {
                cacheEntry.releaseSharedLock();
                releasePage(atomicOperation, cacheEntry);
            }
            pageIndex = getPageIndex(nextPagePointer);
            recordPosition = getRecordPosition(nextPagePointer);
        } while (nextPagePointer >= 0);
        debug.contentSize = contentSize;
        return debug;
    } finally {
        completeOperation();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) OPaginatedClusterException(com.orientechnologies.orient.core.exception.OPaginatedClusterException) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry)

Example 7 with OPaginatedClusterException

use of com.orientechnologies.orient.core.exception.OPaginatedClusterException in project orientdb by orientechnologies.

the class OResurrectRecordTask method executeRecordTask.

@Override
public Object executeRecordTask(ODistributedRequestId requestId, final OServer iServer, ODistributedServerManager iManager, final ODatabaseDocumentInternal database) throws Exception {
    ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "Resurrecting deleted record %s/%s v.%d reqId=%s", database.getName(), rid.toString(), version, requestId);
    try {
        database.getStorage().recyclePosition(rid, new byte[] {}, version, recordType);
        // CREATE A RECORD TO CALL ALL THE HOOKS (LIKE INDEXES FOR UNIQUE CONSTRAINTS)
        final ORecord loadedRecordInstance = Orient.instance().getRecordFactoryManager().newInstance(recordType);
        ORecordInternal.fill(loadedRecordInstance, rid, version, content, true);
        loadedRecordInstance.save();
        ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "+-> resurrected deleted record");
        return Boolean.TRUE;
    } catch (OPaginatedClusterException e) {
        ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "+-> no resurrection, because the record was not deleted");
        return Boolean.TRUE;
    } catch (Exception e) {
        ODistributedServerLog.error(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "+-> error on resurrecting deleted record: the record is already deleted");
    }
    return Boolean.FALSE;
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) OPaginatedClusterException(com.orientechnologies.orient.core.exception.OPaginatedClusterException) OPaginatedClusterException(com.orientechnologies.orient.core.exception.OPaginatedClusterException)

Example 8 with OPaginatedClusterException

use of com.orientechnologies.orient.core.exception.OPaginatedClusterException in project orientdb by orientechnologies.

the class OPaginatedCluster method recycleRecord.

/**
   * Recycles a deleted record.
   */
public void recycleRecord(final long clusterPosition, byte[] content, final int recordVersion, final byte recordType) throws IOException {
    startOperation();
    OSessionStoragePerformanceStatistic statistic = performanceStatisticManager.getSessionPerformanceStatistic();
    if (statistic != null)
        statistic.startRecordUpdateTimer();
    try {
        OAtomicOperation atomicOperation = startAtomicOperation(true);
        acquireExclusiveLock();
        try {
            final OClusterPositionMapBucket.PositionEntry positionEntry = clusterPositionMap.get(clusterPosition, 1);
            if (positionEntry != null) {
                // NOT DELETED
                throw new OPaginatedClusterException("Record with rid " + new ORecordId(id, clusterPosition) + " was not deleted", this);
            }
            content = compression.compress(content);
            content = encryption.encrypt(content);
            int entryContentLength = getEntryContentLength(content.length);
            if (entryContentLength < OClusterPage.MAX_RECORD_SIZE) {
                try {
                    byte[] entryContent = new byte[entryContentLength];
                    int entryPosition = 0;
                    entryContent[entryPosition] = recordType;
                    entryPosition++;
                    OIntegerSerializer.INSTANCE.serializeNative(content.length, entryContent, entryPosition);
                    entryPosition += OIntegerSerializer.INT_SIZE;
                    System.arraycopy(content, 0, entryContent, entryPosition, content.length);
                    entryPosition += content.length;
                    entryContent[entryPosition] = 1;
                    entryPosition++;
                    OLongSerializer.INSTANCE.serializeNative(-1L, entryContent, entryPosition);
                    final AddEntryResult addEntryResult = addEntry(fileId, pinnedStateEntryIndex, recordVersion, entryContent, atomicOperation);
                    updateClusterState(fileId, pinnedStateEntryIndex, 1, addEntryResult.recordsSizeDiff, atomicOperation);
                    clusterPositionMap.resurrect(clusterPosition, new OClusterPositionMapBucket.PositionEntry(addEntryResult.pageIndex, addEntryResult.pagePosition));
                    addAtomicOperationMetadata(new ORecordId(id, clusterPosition), atomicOperation);
                    endAtomicOperation(false, null);
                } catch (Exception e) {
                    endAtomicOperation(true, e);
                    throw OException.wrapException(new OPaginatedClusterException("Error during record recycling", this), e);
                }
            } else {
                try {
                    int entrySize = content.length + OIntegerSerializer.INT_SIZE + OByteSerializer.BYTE_SIZE;
                    int fullEntryPosition = 0;
                    byte[] fullEntry = new byte[entrySize];
                    fullEntry[fullEntryPosition] = recordType;
                    fullEntryPosition++;
                    OIntegerSerializer.INSTANCE.serializeNative(content.length, fullEntry, fullEntryPosition);
                    fullEntryPosition += OIntegerSerializer.INT_SIZE;
                    System.arraycopy(content, 0, fullEntry, fullEntryPosition, content.length);
                    long prevPageRecordPointer = -1;
                    long firstPageIndex = -1;
                    int firstPagePosition = -1;
                    int from = 0;
                    int to = from + (OClusterPage.MAX_RECORD_SIZE - OByteSerializer.BYTE_SIZE - OLongSerializer.LONG_SIZE);
                    int recordsSizeDiff = 0;
                    do {
                        byte[] entryContent = new byte[to - from + OByteSerializer.BYTE_SIZE + OLongSerializer.LONG_SIZE];
                        System.arraycopy(fullEntry, from, entryContent, 0, to - from);
                        if (from > 0)
                            entryContent[entryContent.length - OLongSerializer.LONG_SIZE - OByteSerializer.BYTE_SIZE] = 0;
                        else
                            entryContent[entryContent.length - OLongSerializer.LONG_SIZE - OByteSerializer.BYTE_SIZE] = 1;
                        OLongSerializer.INSTANCE.serializeNative(-1L, entryContent, entryContent.length - OLongSerializer.LONG_SIZE);
                        final AddEntryResult addEntryResult = addEntry(fileId, pinnedStateEntryIndex, recordVersion, entryContent, atomicOperation);
                        recordsSizeDiff += addEntryResult.recordsSizeDiff;
                        if (firstPageIndex == -1) {
                            firstPageIndex = addEntryResult.pageIndex;
                            firstPagePosition = addEntryResult.pagePosition;
                        }
                        long addedPagePointer = createPagePointer(addEntryResult.pageIndex, addEntryResult.pagePosition);
                        if (prevPageRecordPointer >= 0) {
                            long prevPageIndex = getPageIndex(prevPageRecordPointer);
                            int prevPageRecordPosition = getRecordPosition(prevPageRecordPointer);
                            final OCacheEntry prevPageCacheEntry = loadPage(atomicOperation, fileId, prevPageIndex, false);
                            prevPageCacheEntry.acquireExclusiveLock();
                            try {
                                final OClusterPage prevPage = new OClusterPage(prevPageCacheEntry, false, getChanges(atomicOperation, prevPageCacheEntry));
                                prevPage.setRecordLongValue(prevPageRecordPosition, -OLongSerializer.LONG_SIZE, addedPagePointer);
                            } finally {
                                prevPageCacheEntry.releaseExclusiveLock();
                                releasePage(atomicOperation, prevPageCacheEntry);
                            }
                        }
                        prevPageRecordPointer = addedPagePointer;
                        from = to;
                        to = to + (OClusterPage.MAX_RECORD_SIZE - OLongSerializer.LONG_SIZE - OByteSerializer.BYTE_SIZE);
                        if (to > fullEntry.length)
                            to = fullEntry.length;
                    } while (from < to);
                    updateClusterState(fileId, pinnedStateEntryIndex, 1, recordsSizeDiff, atomicOperation);
                    clusterPositionMap.update(clusterPosition, new OClusterPositionMapBucket.PositionEntry(firstPageIndex, firstPagePosition));
                    addAtomicOperationMetadata(new ORecordId(id, clusterPosition), atomicOperation);
                    endAtomicOperation(false, null);
                } catch (RuntimeException e) {
                    endAtomicOperation(true, e);
                    if (e instanceof OPaginatedClusterException)
                        throw e;
                    else
                        throw OException.wrapException(new OPaginatedClusterException("Error during record recycling", this), e);
                }
            }
        } catch (RuntimeException e) {
            endAtomicOperation(true, e);
            if (e instanceof OPaginatedClusterException)
                throw e;
            else
                throw OException.wrapException(new OPaginatedClusterException("Error during record recycling", this), e);
        } finally {
            releaseExclusiveLock();
        }
    } finally {
        if (statistic != null)
            statistic.stopRecordUpdateTimer();
        completeOperation();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) OPaginatedClusterException(com.orientechnologies.orient.core.exception.OPaginatedClusterException) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OException(com.orientechnologies.common.exception.OException) OPaginatedClusterException(com.orientechnologies.orient.core.exception.OPaginatedClusterException) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) IOException(java.io.IOException) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry) OSessionStoragePerformanceStatistic(com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic)

Example 9 with OPaginatedClusterException

use of com.orientechnologies.orient.core.exception.OPaginatedClusterException in project orientdb by orientechnologies.

the class OPaginatedCluster method create.

@Override
public void create(final int startSize) throws IOException {
    startOperation();
    try {
        final OAtomicOperation atomicOperation = startAtomicOperation(false);
        acquireExclusiveLock();
        try {
            fileId = addFile(atomicOperation, getFullName());
            pinnedStateEntryIndex = initCusterState(fileId, atomicOperation);
            if (config.root.clusters.size() <= config.id)
                config.root.clusters.add(config);
            else
                config.root.clusters.set(config.id, config);
            clusterPositionMap.create();
            endAtomicOperation(false, null);
        } catch (Exception e) {
            endAtomicOperation(true, e);
            throw OException.wrapException(new OPaginatedClusterException("Error during creation of cluster with name " + getName(), this), e);
        } finally {
            releaseExclusiveLock();
        }
    } finally {
        completeOperation();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) OException(com.orientechnologies.common.exception.OException) OPaginatedClusterException(com.orientechnologies.orient.core.exception.OPaginatedClusterException) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) IOException(java.io.IOException) OPaginatedClusterException(com.orientechnologies.orient.core.exception.OPaginatedClusterException)

Example 10 with OPaginatedClusterException

use of com.orientechnologies.orient.core.exception.OPaginatedClusterException in project orientdb by orientechnologies.

the class OPaginatedCluster method truncate.

@Override
public void truncate() throws IOException {
    startOperation();
    try {
        OAtomicOperation atomicOperation = startAtomicOperation(true);
        acquireExclusiveLock();
        try {
            truncateFile(atomicOperation, fileId);
            clusterPositionMap.truncate();
            pinnedStateEntryIndex = initCusterState(fileId, atomicOperation);
            endAtomicOperation(false, null);
        } catch (Exception e) {
            endAtomicOperation(true, e);
            throw OException.wrapException(new OPaginatedClusterException("Error during cluster truncate", this), e);
        } finally {
            releaseExclusiveLock();
        }
    } finally {
        completeOperation();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) OException(com.orientechnologies.common.exception.OException) OPaginatedClusterException(com.orientechnologies.orient.core.exception.OPaginatedClusterException) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) IOException(java.io.IOException) OPaginatedClusterException(com.orientechnologies.orient.core.exception.OPaginatedClusterException)

Aggregations

OPaginatedClusterException (com.orientechnologies.orient.core.exception.OPaginatedClusterException)15 OAtomicOperation (com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation)9 OCacheEntry (com.orientechnologies.orient.core.storage.cache.OCacheEntry)7 IOException (java.io.IOException)7 ORecordId (com.orientechnologies.orient.core.id.ORecordId)6 OException (com.orientechnologies.common.exception.OException)5 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)5 OSessionStoragePerformanceStatistic (com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic)3 ORecord (com.orientechnologies.orient.core.record.ORecord)1 OPhysicalPosition (com.orientechnologies.orient.core.storage.OPhysicalPosition)1 ORawBuffer (com.orientechnologies.orient.core.storage.ORawBuffer)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 ArrayList (java.util.ArrayList)1