Search in sources :

Example 86 with OCacheEntry

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

the class OSBTreeBonsaiLocal method size.

@Override
public long size() {
    startOperation();
    try {
        atomicOperationsManager.acquireReadLock(this);
        try {
            final Lock lock = FILE_LOCK_MANAGER.acquireSharedLock(fileId);
            try {
                OAtomicOperation atomicOperation = atomicOperationsManager.getCurrentOperation();
                OCacheEntry rootCacheEntry = loadPage(atomicOperation, fileId, rootBucketPointer.getPageIndex(), false);
                rootCacheEntry.acquireSharedLock();
                try {
                    OSBTreeBonsaiBucket rootBucket = new OSBTreeBonsaiBucket<K, V>(rootCacheEntry, rootBucketPointer.getPageOffset(), keySerializer, valueSerializer, getChanges(atomicOperation, rootCacheEntry), this);
                    return rootBucket.getTreeSize();
                } finally {
                    rootCacheEntry.releaseSharedLock();
                    releasePage(atomicOperation, rootCacheEntry);
                }
            } finally {
                lock.unlock();
            }
        } catch (IOException e) {
            throw OException.wrapException(new OSBTreeBonsaiLocalException("Error during retrieving of size of index " + getName(), this), e);
        } finally {
            atomicOperationsManager.releaseReadLock(this);
        }
    } finally {
        completeOperation();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry) OSBTreeBonsaiLocalException(com.orientechnologies.orient.core.exception.OSBTreeBonsaiLocalException) IOException(java.io.IOException) Lock(java.util.concurrent.locks.Lock)

Example 87 with OCacheEntry

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

the class OSBTreeBonsaiLocal method allocateBucket.

private AllocationResult allocateBucket(OAtomicOperation atomicOperation) throws IOException {
    OCacheEntry sysCacheEntry = loadPage(atomicOperation, fileId, SYS_BUCKET.getPageIndex(), false);
    if (sysCacheEntry == null) {
        sysCacheEntry = addPage(atomicOperation, fileId);
        assert sysCacheEntry.getPageIndex() == SYS_BUCKET.getPageIndex();
    }
    sysCacheEntry.acquireExclusiveLock();
    try {
        final OSysBucket sysBucket = new OSysBucket(sysCacheEntry, getChanges(atomicOperation, sysCacheEntry));
        if ((1.0 * sysBucket.freeListLength()) / ((1.0 * getFilledUpTo(atomicOperation, fileId)) * PAGE_SIZE / OSBTreeBonsaiBucket.MAX_BUCKET_SIZE_BYTES) >= freeSpaceReuseTrigger) {
            final AllocationResult allocationResult = reuseBucketFromFreeList(sysBucket, atomicOperation);
            return allocationResult;
        } else {
            final OBonsaiBucketPointer freeSpacePointer = sysBucket.getFreeSpacePointer();
            if (freeSpacePointer.getPageOffset() + OSBTreeBonsaiBucket.MAX_BUCKET_SIZE_BYTES > PAGE_SIZE) {
                final OCacheEntry cacheEntry = addPage(atomicOperation, fileId);
                final long pageIndex = cacheEntry.getPageIndex();
                sysBucket.setFreeSpacePointer(new OBonsaiBucketPointer(pageIndex, OSBTreeBonsaiBucket.MAX_BUCKET_SIZE_BYTES));
                return new AllocationResult(new OBonsaiBucketPointer(pageIndex, 0), cacheEntry);
            } else {
                sysBucket.setFreeSpacePointer(new OBonsaiBucketPointer(freeSpacePointer.getPageIndex(), freeSpacePointer.getPageOffset() + OSBTreeBonsaiBucket.MAX_BUCKET_SIZE_BYTES));
                final OCacheEntry cacheEntry = loadPage(atomicOperation, fileId, freeSpacePointer.getPageIndex(), false);
                return new AllocationResult(freeSpacePointer, cacheEntry);
            }
        }
    } finally {
        sysCacheEntry.releaseExclusiveLock();
        releasePage(atomicOperation, sysCacheEntry);
    }
}
Also used : OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry)

Example 88 with OCacheEntry

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

the class OSBTreeBonsaiLocal method remove.

@Override
public V remove(K key) {
    final OSessionStoragePerformanceStatistic statistic = performanceStatisticManager.getSessionPerformanceStatistic();
    startOperation();
    if (statistic != null)
        statistic.startRidBagEntryDeletionTimer();
    try {
        final OAtomicOperation atomicOperation;
        try {
            atomicOperation = startAtomicOperation(true);
        } catch (IOException e) {
            throw OException.wrapException(new OSBTreeBonsaiLocalException("Error during sbtree entrie removal", this), e);
        }
        Lock lock = FILE_LOCK_MANAGER.acquireExclusiveLock(fileId);
        try {
            BucketSearchResult bucketSearchResult = findBucket(key, atomicOperation);
            if (bucketSearchResult.itemIndex < 0) {
                endAtomicOperation(false, null);
                return null;
            }
            OBonsaiBucketPointer bucketPointer = bucketSearchResult.getLastPathItem();
            OCacheEntry keyBucketCacheEntry = loadPage(atomicOperation, fileId, bucketPointer.getPageIndex(), false);
            final V removed;
            keyBucketCacheEntry.acquireExclusiveLock();
            try {
                OSBTreeBonsaiBucket<K, V> keyBucket = new OSBTreeBonsaiBucket<K, V>(keyBucketCacheEntry, bucketPointer.getPageOffset(), keySerializer, valueSerializer, getChanges(atomicOperation, keyBucketCacheEntry), this);
                removed = keyBucket.getEntry(bucketSearchResult.itemIndex).value;
                keyBucket.remove(bucketSearchResult.itemIndex);
            } finally {
                keyBucketCacheEntry.releaseExclusiveLock();
                releasePage(atomicOperation, keyBucketCacheEntry);
            }
            setSize(size() - 1, atomicOperation);
            endAtomicOperation(false, null);
            return removed;
        } catch (IOException e) {
            rollback(e);
            throw OException.wrapException(new OSBTreeBonsaiLocalException("Error during removing key " + key + " from sbtree " + getName(), this), e);
        } catch (RuntimeException e) {
            rollback(e);
            throw e;
        } finally {
            lock.unlock();
        }
    } finally {
        if (statistic != null)
            statistic.stopRidBagEntryDeletionTimer();
        completeOperation();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) OSBTreeBonsaiLocalException(com.orientechnologies.orient.core.exception.OSBTreeBonsaiLocalException) IOException(java.io.IOException) Lock(java.util.concurrent.locks.Lock) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry) OSessionStoragePerformanceStatistic(com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic)

Example 89 with OCacheEntry

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

the class OSBTreeBonsaiLocal method initSysBucket.

private void initSysBucket(OAtomicOperation atomicOperation) throws IOException {
    OCacheEntry sysCacheEntry = loadPage(atomicOperation, fileId, SYS_BUCKET.getPageIndex(), false);
    if (sysCacheEntry == null) {
        sysCacheEntry = addPage(atomicOperation, fileId);
        assert sysCacheEntry.getPageIndex() == SYS_BUCKET.getPageIndex();
    }
    sysCacheEntry.acquireExclusiveLock();
    try {
        OSysBucket sysBucket = new OSysBucket(sysCacheEntry, getChanges(atomicOperation, sysCacheEntry));
        if (sysBucket.isInitialized()) {
            sysCacheEntry.releaseExclusiveLock();
            releasePage(atomicOperation, sysCacheEntry);
            sysCacheEntry = loadPage(atomicOperation, fileId, SYS_BUCKET.getPageIndex(), false);
            sysCacheEntry.acquireExclusiveLock();
            sysBucket = new OSysBucket(sysCacheEntry, getChanges(atomicOperation, sysCacheEntry));
            sysBucket.init();
        }
    } finally {
        sysCacheEntry.releaseExclusiveLock();
        releasePage(atomicOperation, sysCacheEntry);
    }
}
Also used : OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry)

Example 90 with OCacheEntry

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

the class OSBTreeBonsaiLocal method firstKey.

@Override
public K firstKey() {
    final OSessionStoragePerformanceStatistic statistic = performanceStatisticManager.getSessionPerformanceStatistic();
    startOperation();
    if (statistic != null)
        statistic.startRidBagEntryReadTimer();
    try {
        atomicOperationsManager.acquireReadLock(this);
        try {
            final Lock lock = FILE_LOCK_MANAGER.acquireSharedLock(fileId);
            try {
                LinkedList<PagePathItemUnit> path = new LinkedList<PagePathItemUnit>();
                OBonsaiBucketPointer bucketPointer = rootBucketPointer;
                OAtomicOperation atomicOperation = atomicOperationsManager.getCurrentOperation();
                OCacheEntry cacheEntry = loadPage(atomicOperation, fileId, rootBucketPointer.getPageIndex(), false);
                int itemIndex = 0;
                cacheEntry.acquireSharedLock();
                try {
                    OSBTreeBonsaiBucket<K, V> bucket = new OSBTreeBonsaiBucket<K, V>(cacheEntry, bucketPointer.getPageOffset(), keySerializer, valueSerializer, getChanges(atomicOperation, cacheEntry), this);
                    while (true) {
                        if (bucket.isLeaf()) {
                            if (bucket.isEmpty()) {
                                if (path.isEmpty()) {
                                    return null;
                                } else {
                                    PagePathItemUnit pagePathItemUnit = path.removeLast();
                                    bucketPointer = pagePathItemUnit.bucketPointer;
                                    itemIndex = pagePathItemUnit.itemIndex + 1;
                                }
                            } else {
                                return bucket.getKey(0);
                            }
                        } else {
                            if (bucket.isEmpty() || itemIndex > bucket.size()) {
                                if (path.isEmpty()) {
                                    return null;
                                } else {
                                    PagePathItemUnit pagePathItemUnit = path.removeLast();
                                    bucketPointer = pagePathItemUnit.bucketPointer;
                                    itemIndex = pagePathItemUnit.itemIndex + 1;
                                }
                            } else {
                                path.add(new PagePathItemUnit(bucketPointer, itemIndex));
                                if (itemIndex < bucket.size()) {
                                    OSBTreeBonsaiBucket.SBTreeEntry<K, V> entry = bucket.getEntry(itemIndex);
                                    bucketPointer = entry.leftChild;
                                } else {
                                    OSBTreeBonsaiBucket.SBTreeEntry<K, V> entry = bucket.getEntry(itemIndex - 1);
                                    bucketPointer = entry.rightChild;
                                }
                                itemIndex = 0;
                            }
                        }
                        cacheEntry.releaseSharedLock();
                        releasePage(atomicOperation, cacheEntry);
                        cacheEntry = loadPage(atomicOperation, fileId, bucketPointer.getPageIndex(), false);
                        cacheEntry.acquireSharedLock();
                        bucket = new OSBTreeBonsaiBucket<K, V>(cacheEntry, bucketPointer.getPageOffset(), keySerializer, valueSerializer, getChanges(atomicOperation, cacheEntry), this);
                    }
                } finally {
                    cacheEntry.releaseSharedLock();
                    releasePage(atomicOperation, cacheEntry);
                }
            } finally {
                lock.unlock();
            }
        } catch (IOException e) {
            throw OException.wrapException(new OSBTreeBonsaiLocalException("Error during finding first key in sbtree [" + getName() + "]", this), e);
        } finally {
            atomicOperationsManager.releaseReadLock(this);
        }
    } finally {
        if (statistic != null)
            statistic.stopRidBagEntryReadTimer(1);
        completeOperation();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) OSBTreeBonsaiLocalException(com.orientechnologies.orient.core.exception.OSBTreeBonsaiLocalException) IOException(java.io.IOException) Lock(java.util.concurrent.locks.Lock) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry) OSessionStoragePerformanceStatistic(com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic)

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