Search in sources :

Example 1 with OPaginatedClusterException

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

the class OPaginatedCluster method deleteRecord.

public boolean deleteRecord(long clusterPosition) throws IOException {
    startOperation();
    OSessionStoragePerformanceStatistic statistic = performanceStatisticManager.getSessionPerformanceStatistic();
    if (statistic != null)
        statistic.startRecordDeletionTimer();
    try {
        OAtomicOperation atomicOperation = startAtomicOperation(true);
        acquireExclusiveLock();
        try {
            OClusterPositionMapBucket.PositionEntry positionEntry = clusterPositionMap.get(clusterPosition, 1);
            if (positionEntry == null) {
                endAtomicOperation(false, null);
                return false;
            }
            long pageIndex = positionEntry.getPageIndex();
            int recordPosition = positionEntry.getRecordPosition();
            if (getFilledUpTo(atomicOperation, fileId) <= pageIndex) {
                endAtomicOperation(false, null);
                return false;
            }
            long nextPagePointer;
            int removedContentSize = 0;
            do {
                boolean cacheEntryReleased = false;
                OCacheEntry cacheEntry = loadPage(atomicOperation, fileId, pageIndex, false);
                cacheEntry.acquireExclusiveLock();
                int initialFreePageIndex;
                try {
                    OClusterPage localPage = new OClusterPage(cacheEntry, false, getChanges(atomicOperation, cacheEntry));
                    initialFreePageIndex = calculateFreePageIndex(localPage);
                    if (localPage.isDeleted(recordPosition)) {
                        if (removedContentSize == 0) {
                            cacheEntryReleased = true;
                            try {
                                cacheEntry.releaseExclusiveLock();
                                releasePage(atomicOperation, cacheEntry);
                            } finally {
                                endAtomicOperation(false, null);
                            }
                            return false;
                        } else
                            throw new OPaginatedClusterException("Content of record " + new ORecordId(id, clusterPosition) + " was broken", this);
                    } else if (removedContentSize == 0) {
                        cacheEntry.releaseExclusiveLock();
                        releasePage(atomicOperation, cacheEntry);
                        cacheEntry = loadPage(atomicOperation, fileId, pageIndex, false);
                        cacheEntry.acquireExclusiveLock();
                        localPage = new OClusterPage(cacheEntry, false, getChanges(atomicOperation, cacheEntry));
                    }
                    byte[] content = localPage.getRecordBinaryValue(recordPosition, 0, localPage.getRecordSize(recordPosition));
                    int initialFreeSpace = localPage.getFreeSpace();
                    localPage.deleteRecord(recordPosition);
                    removedContentSize += localPage.getFreeSpace() - initialFreeSpace;
                    nextPagePointer = OLongSerializer.INSTANCE.deserializeNative(content, content.length - OLongSerializer.LONG_SIZE);
                } finally {
                    if (!cacheEntryReleased) {
                        cacheEntry.releaseExclusiveLock();
                        releasePage(atomicOperation, cacheEntry);
                    }
                }
                updateFreePagesIndex(fileId, pinnedStateEntryIndex, initialFreePageIndex, pageIndex, atomicOperation);
                pageIndex = getPageIndex(nextPagePointer);
                recordPosition = getRecordPosition(nextPagePointer);
            } while (nextPagePointer >= 0);
            updateClusterState(fileId, pinnedStateEntryIndex, -1, -removedContentSize, atomicOperation);
            clusterPositionMap.remove(clusterPosition);
            addAtomicOperationMetadata(new ORecordId(id, clusterPosition), atomicOperation);
            endAtomicOperation(false, null);
            return true;
        } catch (IOException e) {
            endAtomicOperation(true, e);
            throw OException.wrapException(new OPaginatedClusterException("Error during record deletion", this), e);
        } catch (RuntimeException e) {
            endAtomicOperation(true, e);
            throw OException.wrapException(new OPaginatedClusterException("Error during record deletion", this), e);
        } finally {
            releaseExclusiveLock();
        }
    } finally {
        if (statistic != null)
            statistic.stopRecordDeletionTimer();
        completeOperation();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) IOException(java.io.IOException) OPaginatedClusterException(com.orientechnologies.orient.core.exception.OPaginatedClusterException) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry) OSessionStoragePerformanceStatistic(com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic)

Example 2 with OPaginatedClusterException

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

the class OPaginatedCluster method setRecordGrowFactorInternal.

private void setRecordGrowFactorInternal(String stringValue) {
    try {
        float growFactor = Float.parseFloat(stringValue);
        if (growFactor < 1)
            throw new OPaginatedClusterException(OCluster.ATTRIBUTES.RECORD_GROW_FACTOR + " cannot be less than 1", this);
        config.recordGrowFactor = growFactor;
        storageLocal.getConfiguration().update();
    } catch (NumberFormatException nfe) {
        throw OException.wrapException(new OPaginatedClusterException("Invalid value for cluster attribute " + OCluster.ATTRIBUTES.RECORD_GROW_FACTOR + " was passed [" + stringValue + "]", this), nfe);
    }
}
Also used : OPaginatedClusterException(com.orientechnologies.orient.core.exception.OPaginatedClusterException)

Example 3 with OPaginatedClusterException

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

the class OPaginatedCluster method setRecordOverflowGrowFactorInternal.

private void setRecordOverflowGrowFactorInternal(final String stringValue) {
    try {
        float growFactor = Float.parseFloat(stringValue);
        if (growFactor < 1)
            throw new OPaginatedClusterException(OCluster.ATTRIBUTES.RECORD_OVERFLOW_GROW_FACTOR + " cannot be less than 1", this);
        config.recordOverflowGrowFactor = growFactor;
        storageLocal.getConfiguration().update();
    } catch (NumberFormatException nfe) {
        throw OException.wrapException(new OPaginatedClusterException("Invalid value for cluster attribute " + OCluster.ATTRIBUTES.RECORD_OVERFLOW_GROW_FACTOR + " was passed [" + stringValue + "]", this), nfe);
    }
}
Also used : OPaginatedClusterException(com.orientechnologies.orient.core.exception.OPaginatedClusterException)

Example 4 with OPaginatedClusterException

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

the class OPaginatedCluster method updateRecord.

public void updateRecord(final long clusterPosition, byte[] content, final int recordVersion, final byte recordType) throws IOException {
    startOperation();
    OSessionStoragePerformanceStatistic statistic = performanceStatisticManager.getSessionPerformanceStatistic();
    if (statistic != null)
        statistic.startRecordUpdateTimer();
    try {
        content = compression.compress(content);
        content = encryption.encrypt(content);
        OAtomicOperation atomicOperation = startAtomicOperation(true);
        acquireExclusiveLock();
        try {
            final OClusterPositionMapBucket.PositionEntry positionEntry = clusterPositionMap.get(clusterPosition, 1);
            if (positionEntry == null) {
                endAtomicOperation(false, null);
                return;
            }
            int nextRecordPosition = positionEntry.getRecordPosition();
            long nextPageIndex = positionEntry.getPageIndex();
            int newRecordPosition = -1;
            long newPageIndex = -1;
            long prevPageIndex = -1;
            int prevRecordPosition = -1;
            long nextEntryPointer = -1;
            int from = 0;
            int to;
            long sizeDiff = 0;
            byte[] updateEntry = null;
            do {
                final int entrySize;
                final int updatedEntryPosition;
                if (updateEntry == null) {
                    if (from == 0) {
                        entrySize = Math.min(getEntryContentLength(content.length), OClusterPage.MAX_RECORD_SIZE);
                        to = entrySize - (2 * OByteSerializer.BYTE_SIZE + OIntegerSerializer.INT_SIZE + OLongSerializer.LONG_SIZE);
                    } else {
                        entrySize = Math.min(content.length - from + OByteSerializer.BYTE_SIZE + OLongSerializer.LONG_SIZE, OClusterPage.MAX_RECORD_SIZE);
                        to = from + entrySize - (OByteSerializer.BYTE_SIZE + OLongSerializer.LONG_SIZE);
                    }
                    updateEntry = new byte[entrySize];
                    int entryPosition = 0;
                    if (from == 0) {
                        updateEntry[entryPosition] = recordType;
                        entryPosition++;
                        OIntegerSerializer.INSTANCE.serializeNative(content.length, updateEntry, entryPosition);
                        entryPosition += OIntegerSerializer.INT_SIZE;
                    }
                    System.arraycopy(content, from, updateEntry, entryPosition, to - from);
                    entryPosition += to - from;
                    if (nextPageIndex == positionEntry.getPageIndex())
                        updateEntry[entryPosition] = 1;
                    entryPosition++;
                    OLongSerializer.INSTANCE.serializeNative(-1, updateEntry, entryPosition);
                    if (to < content.length) {
                        assert entrySize == OClusterPage.MAX_RECORD_SIZE;
                    }
                } else {
                    entrySize = updateEntry.length;
                    if (from == 0) {
                        to = entrySize - (2 * OByteSerializer.BYTE_SIZE + OIntegerSerializer.INT_SIZE + OLongSerializer.LONG_SIZE);
                    } else {
                        to = from + entrySize - (OByteSerializer.BYTE_SIZE + OLongSerializer.LONG_SIZE);
                    }
                }
                int freePageIndex = -1;
                if (nextPageIndex < 0) {
                    FindFreePageResult findFreePageResult = findFreePage(fileId, pinnedStateEntryIndex, entrySize, atomicOperation);
                    nextPageIndex = findFreePageResult.pageIndex;
                    freePageIndex = findFreePageResult.freePageIndex;
                }
                boolean isNew = false;
                OCacheEntry cacheEntry = loadPage(atomicOperation, fileId, nextPageIndex, false);
                if (cacheEntry == null) {
                    cacheEntry = addPage(atomicOperation, fileId);
                    isNew = true;
                }
                cacheEntry.acquireExclusiveLock();
                try {
                    final OClusterPage localPage = new OClusterPage(cacheEntry, isNew, getChanges(atomicOperation, cacheEntry));
                    final int pageFreeSpace = localPage.getFreeSpace();
                    if (freePageIndex < 0)
                        freePageIndex = calculateFreePageIndex(localPage);
                    else
                        assert isNew || freePageIndex == calculateFreePageIndex(localPage);
                    if (nextRecordPosition >= 0) {
                        if (localPage.isDeleted(nextRecordPosition))
                            throw new OPaginatedClusterException("Record with rid " + new ORecordId(id, clusterPosition) + " was deleted", this);
                        int currentEntrySize = localPage.getRecordSize(nextRecordPosition);
                        nextEntryPointer = localPage.getRecordLongValue(nextRecordPosition, currentEntrySize - OLongSerializer.LONG_SIZE);
                        if (currentEntrySize == entrySize) {
                            localPage.replaceRecord(nextRecordPosition, updateEntry, recordVersion);
                            updatedEntryPosition = nextRecordPosition;
                        } else {
                            localPage.deleteRecord(nextRecordPosition);
                            if (localPage.getMaxRecordSize() >= entrySize) {
                                updatedEntryPosition = localPage.appendRecord(recordVersion, updateEntry);
                                if (updatedEntryPosition < 0) {
                                    throw new IllegalStateException("Page " + cacheEntry.getPageIndex() + " does not have enough free space to add record content");
                                }
                            } else {
                                updatedEntryPosition = -1;
                            }
                        }
                        if (nextEntryPointer >= 0) {
                            nextRecordPosition = getRecordPosition(nextEntryPointer);
                            nextPageIndex = getPageIndex(nextEntryPointer);
                        } else {
                            nextPageIndex = -1;
                            nextRecordPosition = -1;
                        }
                    } else {
                        assert localPage.getFreeSpace() >= entrySize;
                        updatedEntryPosition = localPage.appendRecord(recordVersion, updateEntry);
                        if (updatedEntryPosition < 0) {
                            throw new IllegalStateException("Page " + cacheEntry.getPageIndex() + " does not have enough free space to add record content");
                        }
                        nextPageIndex = -1;
                        nextRecordPosition = -1;
                    }
                    sizeDiff += pageFreeSpace - localPage.getFreeSpace();
                } finally {
                    cacheEntry.releaseExclusiveLock();
                    releasePage(atomicOperation, cacheEntry);
                }
                updateFreePagesIndex(fileId, pinnedStateEntryIndex, freePageIndex, cacheEntry.getPageIndex(), atomicOperation);
                if (updatedEntryPosition >= 0) {
                    if (from == 0) {
                        newPageIndex = cacheEntry.getPageIndex();
                        newRecordPosition = updatedEntryPosition;
                    }
                    from = to;
                    if (prevPageIndex >= 0) {
                        OCacheEntry prevCacheEntry = loadPage(atomicOperation, fileId, prevPageIndex, false);
                        prevCacheEntry.acquireExclusiveLock();
                        try {
                            OClusterPage prevPage = new OClusterPage(prevCacheEntry, false, getChanges(atomicOperation, prevCacheEntry));
                            prevPage.setRecordLongValue(prevRecordPosition, -OLongSerializer.LONG_SIZE, createPagePointer(cacheEntry.getPageIndex(), updatedEntryPosition));
                        } finally {
                            prevCacheEntry.releaseExclusiveLock();
                            releasePage(atomicOperation, prevCacheEntry);
                        }
                    }
                    prevPageIndex = cacheEntry.getPageIndex();
                    prevRecordPosition = updatedEntryPosition;
                    updateEntry = null;
                }
            } while (to < content.length || updateEntry != null);
            // clear unneeded pages
            while (nextEntryPointer >= 0) {
                nextPageIndex = getPageIndex(nextEntryPointer);
                nextRecordPosition = getRecordPosition(nextEntryPointer);
                final int freePagesIndex;
                final int freeSpace;
                OCacheEntry cacheEntry = loadPage(atomicOperation, fileId, nextPageIndex, false);
                cacheEntry.acquireExclusiveLock();
                try {
                    final OClusterPage localPage = new OClusterPage(cacheEntry, false, getChanges(atomicOperation, cacheEntry));
                    freeSpace = localPage.getFreeSpace();
                    freePagesIndex = calculateFreePageIndex(localPage);
                    nextEntryPointer = localPage.getRecordLongValue(nextRecordPosition, -OLongSerializer.LONG_SIZE);
                    localPage.deleteRecord(nextRecordPosition);
                    sizeDiff += freeSpace - localPage.getFreeSpace();
                } finally {
                    cacheEntry.releaseExclusiveLock();
                    releasePage(atomicOperation, cacheEntry);
                }
                updateFreePagesIndex(fileId, pinnedStateEntryIndex, freePagesIndex, nextPageIndex, atomicOperation);
            }
            assert newPageIndex >= 0;
            assert newRecordPosition >= 0;
            if (newPageIndex != positionEntry.getPageIndex() || newRecordPosition != positionEntry.getRecordPosition()) {
                clusterPositionMap.update(clusterPosition, new OClusterPositionMapBucket.PositionEntry(newPageIndex, newRecordPosition));
            }
            updateClusterState(fileId, pinnedStateEntryIndex, 0, sizeDiff, atomicOperation);
            addAtomicOperationMetadata(new ORecordId(id, clusterPosition), atomicOperation);
            endAtomicOperation(false, null);
        } catch (RuntimeException e) {
            endAtomicOperation(true, e);
            throw OException.wrapException(new OPaginatedClusterException("Error during record update", 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) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry) OSessionStoragePerformanceStatistic(com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic)

Example 5 with OPaginatedClusterException

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

the class OPaginatedCluster method delete.

@Override
public void delete() throws IOException {
    startOperation();
    try {
        final OAtomicOperation atomicOperation = startAtomicOperation(false);
        acquireExclusiveLock();
        try {
            deleteFile(atomicOperation, fileId);
            clusterPositionMap.delete();
            endAtomicOperation(false, null);
        } catch (IOException ioe) {
            endAtomicOperation(true, ioe);
            throw ioe;
        } catch (Exception e) {
            endAtomicOperation(true, e);
            throw OException.wrapException(new OPaginatedClusterException("Error during deletion of cluster " + getName(), this), e);
        } finally {
            releaseExclusiveLock();
        }
    } finally {
        completeOperation();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) IOException(java.io.IOException) 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