Search in sources :

Example 21 with OCacheEntry

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

the class OSBTree method firstKey.

public K firstKey() {
    final OSessionStoragePerformanceStatistic statistic = performanceStatisticManager.getSessionPerformanceStatistic();
    startOperation();
    if (statistic != null)
        statistic.startIndexEntryReadTimer();
    try {
        atomicOperationsManager.acquireReadLock(this);
        try {
            acquireSharedLock();
            try {
                OAtomicOperation atomicOperation = atomicOperationsManager.getCurrentOperation();
                final BucketSearchResult searchResult = firstItem(atomicOperation);
                if (searchResult == null)
                    return null;
                final OCacheEntry cacheEntry = loadPage(atomicOperation, fileId, searchResult.getLastPathItem(), false);
                cacheEntry.acquireSharedLock();
                try {
                    OSBTreeBucket<K, V> bucket = new OSBTreeBucket<K, V>(cacheEntry, keySerializer, keyTypes, valueSerializer, getChanges(atomicOperation, cacheEntry));
                    return bucket.getKey(searchResult.itemIndex);
                } finally {
                    cacheEntry.releaseSharedLock();
                    releasePage(atomicOperation, cacheEntry);
                }
            } finally {
                releaseSharedLock();
            }
        } catch (IOException e) {
            throw OException.wrapException(new OSBTreeException("Error during finding first key in sbtree [" + getName() + "]", this), e);
        } finally {
            atomicOperationsManager.releaseReadLock(this);
        }
    } finally {
        if (statistic != null)
            statistic.stopIndexEntryReadTimer();
        completeOperation();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry) IOException(java.io.IOException) OSessionStoragePerformanceStatistic(com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic)

Example 22 with OCacheEntry

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

the class OSBTree method lastKey.

public K lastKey() {
    final OSessionStoragePerformanceStatistic statistic = performanceStatisticManager.getSessionPerformanceStatistic();
    startOperation();
    if (statistic != null)
        statistic.startIndexEntryReadTimer();
    try {
        atomicOperationsManager.acquireReadLock(this);
        try {
            acquireSharedLock();
            try {
                OAtomicOperation atomicOperation = atomicOperationsManager.getCurrentOperation();
                final BucketSearchResult searchResult = lastItem(atomicOperation);
                if (searchResult == null)
                    return null;
                final OCacheEntry cacheEntry = loadPage(atomicOperation, fileId, searchResult.getLastPathItem(), false);
                cacheEntry.acquireSharedLock();
                try {
                    OSBTreeBucket<K, V> bucket = new OSBTreeBucket<K, V>(cacheEntry, keySerializer, keyTypes, valueSerializer, getChanges(atomicOperation, cacheEntry));
                    return bucket.getKey(searchResult.itemIndex);
                } finally {
                    cacheEntry.releaseSharedLock();
                    releasePage(atomicOperation, cacheEntry);
                }
            } finally {
                releaseSharedLock();
            }
        } catch (IOException e) {
            throw OException.wrapException(new OSBTreeException("Error during finding last key in sbtree [" + getName() + "]", this), e);
        } finally {
            atomicOperationsManager.releaseReadLock(this);
        }
    } finally {
        if (statistic != null)
            statistic.stopIndexEntryReadTimer();
        completeOperation();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry) IOException(java.io.IOException) OSessionStoragePerformanceStatistic(com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic)

Example 23 with OCacheEntry

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

the class OSBTree method allocateValuePageFromFreeList.

private long allocateValuePageFromFreeList(OAtomicOperation atomicOperation) throws IOException {
    OCacheEntry rootCacheEntry = loadPage(atomicOperation, fileId, ROOT_INDEX, false);
    assert rootCacheEntry != null;
    rootCacheEntry.acquireSharedLock();
    long freeListFirstIndex;
    OSBTreeBucket<K, V> rootBucket;
    try {
        rootBucket = new OSBTreeBucket<K, V>(rootCacheEntry, keySerializer, keyTypes, valueSerializer, getChanges(atomicOperation, rootCacheEntry));
        freeListFirstIndex = rootBucket.getValuesFreeListFirstIndex();
    } finally {
        rootCacheEntry.releaseSharedLock();
        releasePage(atomicOperation, rootCacheEntry);
    }
    if (freeListFirstIndex >= 0) {
        OCacheEntry freePageEntry = loadPage(atomicOperation, fileId, freeListFirstIndex, false);
        freePageEntry.acquireExclusiveLock();
        try {
            OSBTreeValuePage valuePage = new OSBTreeValuePage(freePageEntry, getChanges(atomicOperation, freePageEntry), false);
            long nextFreeListIndex = valuePage.getNextFreeListPage();
            rootCacheEntry = loadPage(atomicOperation, fileId, ROOT_INDEX, false);
            rootCacheEntry.acquireExclusiveLock();
            rootBucket = new OSBTreeBucket<K, V>(rootCacheEntry, keySerializer, keyTypes, valueSerializer, getChanges(atomicOperation, rootCacheEntry));
            try {
                rootBucket.setValuesFreeListFirstIndex(nextFreeListIndex);
            } finally {
                rootCacheEntry.releaseExclusiveLock();
                releasePage(atomicOperation, rootCacheEntry);
            }
            valuePage.setNextFreeListPage(-1);
        } finally {
            freePageEntry.releaseExclusiveLock();
            releasePage(atomicOperation, freePageEntry);
        }
        return freePageEntry.getPageIndex();
    }
    return -1;
}
Also used : OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry)

Example 24 with OCacheEntry

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

the class OSBTree method get.

public V get(K key) {
    final OSessionStoragePerformanceStatistic statistic = performanceStatisticManager.getSessionPerformanceStatistic();
    startOperation();
    if (statistic != null)
        statistic.startIndexEntryReadTimer();
    try {
        atomicOperationsManager.acquireReadLock(this);
        try {
            acquireSharedLock();
            try {
                checkNullSupport(key);
                OAtomicOperation atomicOperation = atomicOperationsManager.getCurrentOperation();
                if (key != null) {
                    key = keySerializer.preprocess(key, (Object[]) keyTypes);
                    BucketSearchResult bucketSearchResult = findBucket(key, atomicOperation);
                    if (bucketSearchResult.itemIndex < 0)
                        return null;
                    long pageIndex = bucketSearchResult.getLastPathItem();
                    OCacheEntry keyBucketCacheEntry = loadPage(atomicOperation, fileId, pageIndex, false);
                    keyBucketCacheEntry.acquireSharedLock();
                    try {
                        OSBTreeBucket<K, V> keyBucket = new OSBTreeBucket<K, V>(keyBucketCacheEntry, keySerializer, keyTypes, valueSerializer, getChanges(atomicOperation, keyBucketCacheEntry));
                        OSBTreeBucket.SBTreeEntry<K, V> treeEntry = keyBucket.getEntry(bucketSearchResult.itemIndex);
                        return readValue(treeEntry.value, atomicOperation);
                    } finally {
                        keyBucketCacheEntry.releaseSharedLock();
                        releasePage(atomicOperation, keyBucketCacheEntry);
                    }
                } else {
                    if (getFilledUpTo(atomicOperation, nullBucketFileId) == 0)
                        return null;
                    final OCacheEntry nullBucketCacheEntry = loadPage(atomicOperation, nullBucketFileId, 0, false);
                    nullBucketCacheEntry.acquireSharedLock();
                    try {
                        final ONullBucket<V> nullBucket = new ONullBucket<V>(nullBucketCacheEntry, getChanges(atomicOperation, nullBucketCacheEntry), valueSerializer, false);
                        final OSBTreeValue<V> treeValue = nullBucket.getValue();
                        if (treeValue == null)
                            return null;
                        return readValue(treeValue, atomicOperation);
                    } finally {
                        nullBucketCacheEntry.releaseSharedLock();
                        releasePage(atomicOperation, nullBucketCacheEntry);
                    }
                }
            } finally {
                releaseSharedLock();
            }
        } catch (IOException e) {
            throw OException.wrapException(new OSBTreeException("Error during retrieving  of sbtree with name " + getName(), this), e);
        } finally {
            atomicOperationsManager.releaseReadLock(this);
        }
    } finally {
        if (statistic != null)
            statistic.startIndexEntryReadTimer();
        completeOperation();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) IOException(java.io.IOException) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry) OSessionStoragePerformanceStatistic(com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic)

Example 25 with OCacheEntry

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

the class OSBTree method splitNonRootBucket.

private BucketSearchResult splitNonRootBucket(List<Long> path, int keyIndex, K keyToInsert, long pageIndex, OSBTreeBucket<K, V> bucketToSplit, boolean splitLeaf, int indexToSplit, K separationKey, List<OSBTreeBucket.SBTreeEntry<K, V>> rightEntries, OAtomicOperation atomicOperation) throws IOException {
    OCacheEntry rightBucketEntry = addPage(atomicOperation, fileId);
    rightBucketEntry.acquireExclusiveLock();
    try {
        OSBTreeBucket<K, V> newRightBucket = new OSBTreeBucket<K, V>(rightBucketEntry, splitLeaf, keySerializer, keyTypes, valueSerializer, getChanges(atomicOperation, rightBucketEntry));
        newRightBucket.addAll(rightEntries);
        bucketToSplit.shrink(indexToSplit);
        if (splitLeaf) {
            long rightSiblingPageIndex = bucketToSplit.getRightSibling();
            newRightBucket.setRightSibling(rightSiblingPageIndex);
            newRightBucket.setLeftSibling(pageIndex);
            bucketToSplit.setRightSibling(rightBucketEntry.getPageIndex());
            if (rightSiblingPageIndex >= 0) {
                final OCacheEntry rightSiblingBucketEntry = loadPage(atomicOperation, fileId, rightSiblingPageIndex, false);
                rightSiblingBucketEntry.acquireExclusiveLock();
                OSBTreeBucket<K, V> rightSiblingBucket = new OSBTreeBucket<K, V>(rightSiblingBucketEntry, keySerializer, keyTypes, valueSerializer, getChanges(atomicOperation, rightSiblingBucketEntry));
                try {
                    rightSiblingBucket.setLeftSibling(rightBucketEntry.getPageIndex());
                } finally {
                    rightSiblingBucketEntry.releaseExclusiveLock();
                    releasePage(atomicOperation, rightSiblingBucketEntry);
                }
            }
        }
        long parentIndex = path.get(path.size() - 2);
        OCacheEntry parentCacheEntry = loadPage(atomicOperation, fileId, parentIndex, false);
        parentCacheEntry.acquireExclusiveLock();
        try {
            OSBTreeBucket<K, V> parentBucket = new OSBTreeBucket<K, V>(parentCacheEntry, keySerializer, keyTypes, valueSerializer, getChanges(atomicOperation, parentCacheEntry));
            OSBTreeBucket.SBTreeEntry<K, V> parentEntry = new OSBTreeBucket.SBTreeEntry<K, V>(pageIndex, rightBucketEntry.getPageIndex(), separationKey, null);
            int insertionIndex = parentBucket.find(separationKey);
            assert insertionIndex < 0;
            insertionIndex = -insertionIndex - 1;
            while (!parentBucket.addEntry(insertionIndex, parentEntry, true)) {
                parentCacheEntry.releaseExclusiveLock();
                releasePage(atomicOperation, parentCacheEntry);
                BucketSearchResult bucketSearchResult = splitBucket(path.subList(0, path.size() - 1), insertionIndex, separationKey, atomicOperation);
                parentIndex = bucketSearchResult.getLastPathItem();
                parentCacheEntry = loadPage(atomicOperation, fileId, parentIndex, false);
                parentCacheEntry.acquireExclusiveLock();
                insertionIndex = bucketSearchResult.itemIndex;
                parentBucket = new OSBTreeBucket<K, V>(parentCacheEntry, keySerializer, keyTypes, valueSerializer, getChanges(atomicOperation, parentCacheEntry));
            }
        } finally {
            parentCacheEntry.releaseExclusiveLock();
            releasePage(atomicOperation, parentCacheEntry);
        }
    } finally {
        rightBucketEntry.releaseExclusiveLock();
        releasePage(atomicOperation, rightBucketEntry);
    }
    ArrayList<Long> resultPath = new ArrayList<Long>(path.subList(0, path.size() - 1));
    if (comparator.compare(keyToInsert, separationKey) < 0) {
        resultPath.add(pageIndex);
        return new BucketSearchResult(keyIndex, resultPath);
    }
    resultPath.add(rightBucketEntry.getPageIndex());
    if (splitLeaf) {
        return new BucketSearchResult(keyIndex - indexToSplit, resultPath);
    }
    resultPath.add(rightBucketEntry.getPageIndex());
    return new BucketSearchResult(keyIndex - indexToSplit - 1, resultPath);
}
Also used : OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry)

Aggregations

OCacheEntry (com.orientechnologies.orient.core.storage.cache.OCacheEntry)209 ByteBuffer (java.nio.ByteBuffer)77 OAtomicOperation (com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation)76 OLogSequenceNumber (com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber)74 OByteBufferPool (com.orientechnologies.common.directmemory.OByteBufferPool)69 OCachePointer (com.orientechnologies.orient.core.storage.cache.OCachePointer)65 IOException (java.io.IOException)61 OIndexException (com.orientechnologies.orient.core.index.OIndexException)23 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)20 OSessionStoragePerformanceStatistic (com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic)20 OWALChangesTree (com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OWALChangesTree)19 OException (com.orientechnologies.common.exception.OException)17 Test (org.testng.annotations.Test)17 ORecordId (com.orientechnologies.orient.core.id.ORecordId)14 OStorageException (com.orientechnologies.orient.core.exception.OStorageException)13 OLocalHashTableException (com.orientechnologies.orient.core.exception.OLocalHashTableException)12 OSBTreeBonsaiLocalException (com.orientechnologies.orient.core.exception.OSBTreeBonsaiLocalException)11 Lock (java.util.concurrent.locks.Lock)11 Random (java.util.Random)9 HashMap (java.util.HashMap)8