Search in sources :

Example 21 with OSessionStoragePerformanceStatistic

use of com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic 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)

Example 22 with OSessionStoragePerformanceStatistic

use of com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic in project orientdb by orientechnologies.

the class OLocalHashTable method remove.

@Override
public V remove(K key) {
    final OSessionStoragePerformanceStatistic statistic = performanceStatisticManager.getSessionPerformanceStatistic();
    startOperation();
    if (statistic != null)
        statistic.startIndexEntryDeletionTimer();
    try {
        final OAtomicOperation atomicOperation;
        try {
            atomicOperation = startAtomicOperation(true);
        } catch (IOException e) {
            throw OException.wrapException(new OIndexException("Error during hash table entry deletion"), e);
        }
        acquireExclusiveLock();
        try {
            checkNullSupport(key);
            int sizeDiff = 0;
            if (key != null) {
                key = keySerializer.preprocess(key, (Object[]) keyTypes);
                final long hashCode = keyHashFunction.hashCode(key);
                final OHashTable.BucketPath nodePath = getBucket(hashCode);
                final long bucketPointer = directory.getNodePointer(nodePath.nodeIndex, nodePath.itemIndex + nodePath.hashMapOffset);
                final long pageIndex = getPageIndex(bucketPointer);
                final V removed;
                final boolean found;
                final OCacheEntry cacheEntry = loadPage(atomicOperation, fileId, pageIndex, false);
                cacheEntry.acquireExclusiveLock();
                try {
                    final OHashIndexBucket<K, V> bucket = new OHashIndexBucket<K, V>(cacheEntry, keySerializer, valueSerializer, keyTypes, getChanges(atomicOperation, cacheEntry));
                    final int positionIndex = bucket.getIndex(hashCode, key);
                    found = positionIndex >= 0;
                    if (found) {
                        removed = bucket.deleteEntry(positionIndex).value;
                        sizeDiff--;
                    } else
                        removed = null;
                } finally {
                    cacheEntry.releaseExclusiveLock();
                    releasePage(atomicOperation, cacheEntry);
                }
                if (found) {
                    if (nodePath.parent != null) {
                        final int hashMapSize = 1 << nodePath.nodeLocalDepth;
                        final boolean allMapsContainSameBucket = checkAllMapsContainSameBucket(directory.getNode(nodePath.nodeIndex), hashMapSize);
                        if (allMapsContainSameBucket)
                            mergeNodeToParent(nodePath);
                    }
                    changeSize(sizeDiff, atomicOperation);
                }
                endAtomicOperation(false, null);
                return removed;
            } else {
                if (getFilledUpTo(atomicOperation, nullBucketFileId) == 0) {
                    endAtomicOperation(false, null);
                    return null;
                }
                V removed = null;
                OCacheEntry cacheEntry = loadPage(atomicOperation, nullBucketFileId, 0, false);
                if (cacheEntry == null)
                    cacheEntry = addPage(atomicOperation, nullBucketFileId);
                cacheEntry.acquireExclusiveLock();
                try {
                    final ONullBucket<V> nullBucket = new ONullBucket<V>(cacheEntry, getChanges(atomicOperation, cacheEntry), valueSerializer, false);
                    removed = nullBucket.getValue();
                    if (removed != null) {
                        nullBucket.removeValue();
                        sizeDiff--;
                    }
                } finally {
                    cacheEntry.releaseExclusiveLock();
                    releasePage(atomicOperation, cacheEntry);
                }
                changeSize(sizeDiff, atomicOperation);
                endAtomicOperation(false, null);
                return removed;
            }
        } catch (IOException e) {
            rollback(e);
            throw OException.wrapException(new OIndexException("Error during index removal"), e);
        } catch (Exception e) {
            rollback(e);
            throw OException.wrapException(new OStorageException("Error during index removal"), e);
        } finally {
            releaseExclusiveLock();
        }
    } finally {
        if (statistic != null)
            statistic.stopIndexEntryDeletionTimer();
        completeOperation();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) OIndexException(com.orientechnologies.orient.core.index.OIndexException) IOException(java.io.IOException) OIndexException(com.orientechnologies.orient.core.index.OIndexException) OException(com.orientechnologies.common.exception.OException) IOException(java.io.IOException) OLocalHashTableException(com.orientechnologies.orient.core.exception.OLocalHashTableException) OTooBigIndexKeyException(com.orientechnologies.orient.core.exception.OTooBigIndexKeyException) OStorageException(com.orientechnologies.orient.core.exception.OStorageException) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry) OStorageException(com.orientechnologies.orient.core.exception.OStorageException) OSessionStoragePerformanceStatistic(com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic)

Example 23 with OSessionStoragePerformanceStatistic

use of com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic in project orientdb by orientechnologies.

the class OSBTreeBonsaiLocal method loadEntriesMajor.

/**
   * Load all entries with key greater then specified key.
   *
   * @param key          defines
   * @param inclusive    if true entry with given key is included
   * @param ascSortOrder
   * @param listener
   */
@Override
public void loadEntriesMajor(K key, boolean inclusive, boolean ascSortOrder, RangeResultListener<K, V> listener) {
    final OSessionStoragePerformanceStatistic statistic = performanceStatisticManager.getSessionPerformanceStatistic();
    int readEntries = 0;
    startOperation();
    if (statistic != null)
        statistic.startRidBagEntryReadTimer();
    try {
        if (!ascSortOrder)
            throw new IllegalStateException("Descending sort order is not supported.");
        atomicOperationsManager.acquireReadLock(this);
        try {
            final Lock lock = FILE_LOCK_MANAGER.acquireSharedLock(fileId);
            try {
                OAtomicOperation atomicOperation = atomicOperationsManager.getCurrentOperation();
                BucketSearchResult bucketSearchResult = findBucket(key, atomicOperation);
                OBonsaiBucketPointer bucketPointer = bucketSearchResult.getLastPathItem();
                int index;
                if (bucketSearchResult.itemIndex >= 0) {
                    index = inclusive ? bucketSearchResult.itemIndex : bucketSearchResult.itemIndex + 1;
                } else {
                    index = -bucketSearchResult.itemIndex - 1;
                }
                do {
                    final OCacheEntry cacheEntry = loadPage(atomicOperation, fileId, bucketPointer.getPageIndex(), false);
                    cacheEntry.acquireSharedLock();
                    try {
                        OSBTreeBonsaiBucket<K, V> bucket = new OSBTreeBonsaiBucket<K, V>(cacheEntry, bucketPointer.getPageOffset(), keySerializer, valueSerializer, getChanges(atomicOperation, cacheEntry), this);
                        int bucketSize = bucket.size();
                        for (int i = index; i < bucketSize; i++) {
                            if (!listener.addResult(bucket.getEntry(i))) {
                                readEntries++;
                                return;
                            }
                        }
                        bucketPointer = bucket.getRightSibling();
                        index = 0;
                    } finally {
                        cacheEntry.releaseSharedLock();
                        releasePage(atomicOperation, cacheEntry);
                    }
                } while (bucketPointer.getPageIndex() >= 0);
            } finally {
                lock.unlock();
            }
        } catch (IOException ioe) {
            throw OException.wrapException(new OSBTreeBonsaiLocalException("Error during fetch of major values for key " + key + " in sbtree " + getName(), this), ioe);
        } finally {
            atomicOperationsManager.releaseReadLock(this);
        }
    } finally {
        if (statistic != null)
            statistic.stopRidBagEntryReadTimer(readEntries);
        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 24 with OSessionStoragePerformanceStatistic

use of com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic in project orientdb by orientechnologies.

the class LocalCreateBinaryDocumentSpeedTest method cycle.

@Override
@Test(enabled = false)
public void cycle() {
    final OStorage storage = database.getStorage();
    ((OAbstractPaginatedStorage) storage).startGatheringPerformanceStatisticForCurrentThread();
    record = new ORecordBytes(database, payload);
    record.save();
    if (data.getCyclesDone() == data.getCycles() - 1)
        database.commit();
    OSessionStoragePerformanceStatistic sessionStoragePerformanceStatistic = ((OAbstractPaginatedStorage) storage).completeGatheringPerformanceStatisticForCurrentThread();
    System.out.println(sessionStoragePerformanceStatistic.toDocument().toJSON("prettyPrint"));
}
Also used : ORecordBytes(com.orientechnologies.orient.core.record.impl.ORecordBytes) OStorage(com.orientechnologies.orient.core.storage.OStorage) OAbstractPaginatedStorage(com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage) OSessionStoragePerformanceStatistic(com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic) OrientMonoThreadTest(com.orientechnologies.orient.test.database.base.OrientMonoThreadTest) Test(org.testng.annotations.Test)

Example 25 with OSessionStoragePerformanceStatistic

use of com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic in project orientdb by orientechnologies.

the class OLocalHashTable 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 {
                final OAtomicOperation atomicOperation = atomicOperationsManager.getCurrentOperation();
                checkNullSupport(key);
                if (key == null) {
                    if (getFilledUpTo(atomicOperation, nullBucketFileId) == 0)
                        return null;
                    V result = null;
                    OCacheEntry cacheEntry = loadPage(atomicOperation, nullBucketFileId, 0, false);
                    cacheEntry.acquireSharedLock();
                    try {
                        ONullBucket<V> nullBucket = new ONullBucket<V>(cacheEntry, getChanges(atomicOperation, cacheEntry), valueSerializer, false);
                        result = nullBucket.getValue();
                    } finally {
                        cacheEntry.releaseSharedLock();
                        releasePage(atomicOperation, cacheEntry);
                    }
                    return result;
                } else {
                    key = keySerializer.preprocess(key, (Object[]) keyTypes);
                    final long hashCode = keyHashFunction.hashCode(key);
                    OHashTable.BucketPath bucketPath = getBucket(hashCode);
                    final long bucketPointer = directory.getNodePointer(bucketPath.nodeIndex, bucketPath.itemIndex + bucketPath.hashMapOffset);
                    if (bucketPointer == 0)
                        return null;
                    final long pageIndex = getPageIndex(bucketPointer);
                    OCacheEntry cacheEntry = loadPage(atomicOperation, fileId, pageIndex, false);
                    cacheEntry.acquireSharedLock();
                    try {
                        final OHashIndexBucket<K, V> bucket = new OHashIndexBucket<K, V>(cacheEntry, keySerializer, valueSerializer, keyTypes, getChanges(atomicOperation, cacheEntry));
                        OHashIndexBucket.Entry<K, V> entry = bucket.find(key, hashCode);
                        if (entry == null)
                            return null;
                        return entry.value;
                    } finally {
                        cacheEntry.releaseSharedLock();
                        releasePage(atomicOperation, cacheEntry);
                    }
                }
            } finally {
                releaseSharedLock();
            }
        } catch (IOException e) {
            throw OException.wrapException(new OIndexException("Exception during index value retrieval"), 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) OIndexException(com.orientechnologies.orient.core.index.OIndexException) IOException(java.io.IOException) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry) OSessionStoragePerformanceStatistic(com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic)

Aggregations

OSessionStoragePerformanceStatistic (com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic)47 OAtomicOperation (com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation)25 IOException (java.io.IOException)23 OCacheEntry (com.orientechnologies.orient.core.storage.cache.OCacheEntry)20 Lock (java.util.concurrent.locks.Lock)11 OSBTreeBonsaiLocalException (com.orientechnologies.orient.core.exception.OSBTreeBonsaiLocalException)9 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 ORawPair (com.orientechnologies.common.util.ORawPair)5 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)5 OException (com.orientechnologies.common.exception.OException)4 ORecordId (com.orientechnologies.orient.core.id.ORecordId)4 OPaginatedClusterException (com.orientechnologies.orient.core.exception.OPaginatedClusterException)3 OStorageException (com.orientechnologies.orient.core.exception.OStorageException)3 OTooBigIndexKeyException (com.orientechnologies.orient.core.exception.OTooBigIndexKeyException)3 OIndexException (com.orientechnologies.orient.core.index.OIndexException)3 OLocalHashTableException (com.orientechnologies.orient.core.exception.OLocalHashTableException)2 OStorage (com.orientechnologies.orient.core.storage.OStorage)2 OAbstractPaginatedStorage (com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage)2 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 OAllCacheEntriesAreUsedException (com.orientechnologies.orient.core.exception.OAllCacheEntriesAreUsedException)1