Search in sources :

Example 16 with OCacheEntry

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

the class OLocalHashTable20 method remove.

@Override
public V remove(K key) {
    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 BucketPath nodePath = getBucket(hashCode);
            final long bucketPointer = directory.getNodePointer(nodePath.nodeIndex, nodePath.itemIndex + nodePath.hashMapOffset);
            final long pageIndex = getPageIndex(bucketPointer);
            final int fileLevel = getFileLevel(bucketPointer);
            final V removed;
            final boolean found;
            final OCacheEntry cacheEntry = loadPageEntry(pageIndex, fileLevel, atomicOperation);
            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--;
                    mergeBucketsAfterDeletion(nodePath, bucket, atomicOperation);
                } 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();
        throw OException.wrapException(new OIndexException("Error during index removal"), e);
    } catch (Exception e) {
        rollback();
        throw OException.wrapException(new OStorageException("Error during index removal"), e);
    } finally {
        releaseExclusiveLock();
    }
}
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) OIndexEngineException(com.orientechnologies.orient.core.index.OIndexEngineException) OStorageException(com.orientechnologies.orient.core.exception.OStorageException) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry) OStorageException(com.orientechnologies.orient.core.exception.OStorageException)

Example 17 with OCacheEntry

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

the class OLocalHashTable20 method setKeySerializer.

@Override
public void setKeySerializer(OBinarySerializer<K> keySerializer) {
    final OAtomicOperation atomicOperation;
    try {
        atomicOperation = startAtomicOperation(true);
    } catch (IOException e) {
        throw OException.wrapException(new OIndexException("Error during hash set serializer for index keys"), e);
    }
    acquireExclusiveLock();
    try {
        this.keySerializer = keySerializer;
        OCacheEntry hashStateEntry = loadPage(atomicOperation, fileStateId, hashStateEntryIndex, true);
        hashStateEntry.acquireExclusiveLock();
        try {
            OHashIndexFileLevelMetadataPage metadataPage = new OHashIndexFileLevelMetadataPage(hashStateEntry, getChanges(atomicOperation, hashStateEntry), false);
            metadataPage.setKeySerializerId(keySerializer.getId());
        } finally {
            hashStateEntry.releaseExclusiveLock();
            releasePage(atomicOperation, hashStateEntry);
        }
        endAtomicOperation(false, null);
    } catch (IOException e) {
        rollback();
        throw OException.wrapException(new OIndexException("Cannot set serializer for index keys"), e);
    } catch (Exception e) {
        rollback();
        throw OException.wrapException(new OStorageException("Cannot set serializer for index keys"), e);
    } finally {
        releaseExclusiveLock();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry) OIndexException(com.orientechnologies.orient.core.index.OIndexException) OStorageException(com.orientechnologies.orient.core.exception.OStorageException) IOException(java.io.IOException) OIndexException(com.orientechnologies.orient.core.index.OIndexException) OException(com.orientechnologies.common.exception.OException) IOException(java.io.IOException) OIndexEngineException(com.orientechnologies.orient.core.index.OIndexEngineException) OStorageException(com.orientechnologies.orient.core.exception.OStorageException)

Example 18 with OCacheEntry

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

the class OLocalHashTable20 method delete.

@Override
public void delete() {
    final OAtomicOperation atomicOperation;
    try {
        atomicOperation = startAtomicOperation(false);
    } catch (IOException e) {
        throw OException.wrapException(new OIndexException("Error during hash table deletion"), e);
    }
    acquireExclusiveLock();
    try {
        final OCacheEntry hashStateEntry = loadPage(atomicOperation, fileStateId, hashStateEntryIndex, true);
        try {
            for (int i = 0; i < HASH_CODE_SIZE; i++) {
                OHashIndexFileLevelMetadataPage metadataPage = new OHashIndexFileLevelMetadataPage(hashStateEntry, getChanges(atomicOperation, hashStateEntry), false);
                if (!metadataPage.isRemoved(i)) {
                    deleteFile(atomicOperation, metadataPage.getFileId(i));
                }
            }
        } finally {
            releasePage(atomicOperation, hashStateEntry);
        }
        directory.delete();
        deleteFile(atomicOperation, fileStateId);
        if (nullKeyIsSupported)
            deleteFile(atomicOperation, nullBucketFileId);
        endAtomicOperation(false, null);
    } catch (IOException e) {
        rollback();
        throw OException.wrapException(new OIndexException("Exception during index deletion"), e);
    } catch (Exception e) {
        rollback();
        throw OException.wrapException(new OIndexException("Exception during index deletion"), e);
    } finally {
        releaseExclusiveLock();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry) 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) OIndexEngineException(com.orientechnologies.orient.core.index.OIndexEngineException) OStorageException(com.orientechnologies.orient.core.exception.OStorageException)

Example 19 with OCacheEntry

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

the class OSBTree method put.

@SuppressWarnings("unchecked")
private boolean put(K key, V value, OIndexEngine.Validator<K, V> validator) {
    final OSessionStoragePerformanceStatistic statistic = performanceStatisticManager.getSessionPerformanceStatistic();
    startOperation();
    if (statistic != null)
        statistic.startIndexEntryUpdateTimer();
    try {
        final OAtomicOperation atomicOperation;
        try {
            atomicOperation = startAtomicOperation(true);
        } catch (IOException e) {
            throw OException.wrapException(new OSBTreeException("Error during sbtree entrie put", this), e);
        }
        acquireExclusiveLock();
        try {
            checkNullSupport(key);
            if (key != null) {
                final int keySize = keySerializer.getObjectSize(key, (Object[]) keyTypes);
                final int valueSize = valueSerializer.getObjectSize(value);
                if (keySize > MAX_KEY_SIZE)
                    throw new OTooBigIndexKeyException("Key size is more than allowed, operation was canceled. Current key size " + keySize + ", allowed  " + MAX_KEY_SIZE, getName());
                final boolean createLinkToTheValue = valueSize > MAX_EMBEDDED_VALUE_SIZE;
                key = keySerializer.preprocess(key, (Object[]) keyTypes);
                long valueLink = -1;
                if (createLinkToTheValue)
                    valueLink = createLinkToTheValue(value, atomicOperation);
                final OSBTreeValue<V> treeValue = new OSBTreeValue<V>(createLinkToTheValue, valueLink, createLinkToTheValue ? null : value);
                BucketSearchResult bucketSearchResult = findBucket(key, atomicOperation);
                OCacheEntry keyBucketCacheEntry = loadPage(atomicOperation, fileId, bucketSearchResult.getLastPathItem(), false);
                keyBucketCacheEntry.acquireExclusiveLock();
                OSBTreeBucket<K, V> keyBucket = new OSBTreeBucket<K, V>(keyBucketCacheEntry, keySerializer, keyTypes, valueSerializer, getChanges(atomicOperation, keyBucketCacheEntry));
                if (validator != null) {
                    // assuming validation throws by default
                    boolean failure = true;
                    boolean ignored = false;
                    try {
                        final V oldValue = bucketSearchResult.itemIndex > -1 ? readValue(keyBucket.getValue(bucketSearchResult.itemIndex), atomicOperation) : null;
                        final Object result = validator.validate(key, oldValue, value);
                        if (result == OIndexEngine.Validator.IGNORE) {
                            ignored = true;
                            failure = false;
                            return false;
                        }
                        value = (V) result;
                        failure = false;
                    } finally {
                        if (failure || ignored) {
                            keyBucketCacheEntry.releaseExclusiveLock();
                            releasePage(atomicOperation, keyBucketCacheEntry);
                        }
                        if (// in case of a failure atomic operation will be ended in a usual way below
                        ignored)
                            endAtomicOperation(false, null);
                    }
                }
                int insertionIndex;
                int sizeDiff;
                if (bucketSearchResult.itemIndex >= 0) {
                    int updateResult = keyBucket.updateValue(bucketSearchResult.itemIndex, treeValue);
                    if (updateResult >= 0) {
                        keyBucketCacheEntry.releaseExclusiveLock();
                        releasePage(atomicOperation, keyBucketCacheEntry);
                        endAtomicOperation(false, null);
                        return true;
                    } else {
                        assert updateResult == -1;
                        long removedLinkedValue = keyBucket.remove(bucketSearchResult.itemIndex);
                        if (removedLinkedValue >= 0)
                            removeLinkedValue(removedLinkedValue, atomicOperation);
                        insertionIndex = bucketSearchResult.itemIndex;
                        sizeDiff = 0;
                    }
                } else {
                    insertionIndex = -bucketSearchResult.itemIndex - 1;
                    sizeDiff = 1;
                }
                while (!keyBucket.addEntry(insertionIndex, new OSBTreeBucket.SBTreeEntry<K, V>(-1, -1, key, treeValue), true)) {
                    keyBucketCacheEntry.releaseExclusiveLock();
                    releasePage(atomicOperation, keyBucketCacheEntry);
                    bucketSearchResult = splitBucket(bucketSearchResult.path, insertionIndex, key, atomicOperation);
                    insertionIndex = bucketSearchResult.itemIndex;
                    keyBucketCacheEntry = loadPage(atomicOperation, fileId, bucketSearchResult.getLastPathItem(), false);
                    keyBucketCacheEntry.acquireExclusiveLock();
                    keyBucket = new OSBTreeBucket<K, V>(keyBucketCacheEntry, keySerializer, keyTypes, valueSerializer, getChanges(atomicOperation, keyBucketCacheEntry));
                }
                keyBucketCacheEntry.releaseExclusiveLock();
                releasePage(atomicOperation, keyBucketCacheEntry);
                if (sizeDiff != 0)
                    setSize(size() + sizeDiff, atomicOperation);
            } else {
                OCacheEntry cacheEntry;
                boolean isNew = false;
                if (getFilledUpTo(atomicOperation, nullBucketFileId) == 0) {
                    cacheEntry = addPage(atomicOperation, nullBucketFileId);
                    isNew = true;
                } else
                    cacheEntry = loadPage(atomicOperation, nullBucketFileId, 0, false);
                final int valueSize = valueSerializer.getObjectSize(value);
                final boolean createLinkToTheValue = valueSize > MAX_EMBEDDED_VALUE_SIZE;
                long valueLink = -1;
                if (createLinkToTheValue)
                    valueLink = createLinkToTheValue(value, atomicOperation);
                final OSBTreeValue<V> treeValue = new OSBTreeValue<V>(createLinkToTheValue, valueLink, createLinkToTheValue ? null : value);
                int sizeDiff = 0;
                boolean ignored = false;
                cacheEntry.acquireExclusiveLock();
                try {
                    final ONullBucket<V> nullBucket = new ONullBucket<V>(cacheEntry, getChanges(atomicOperation, cacheEntry), valueSerializer, isNew);
                    final OSBTreeValue<V> oldValue = nullBucket.getValue();
                    if (validator != null) {
                        final V oldValueValue = oldValue == null ? null : readValue(oldValue, atomicOperation);
                        final Object result = validator.validate(null, oldValueValue, value);
                        if (result == OIndexEngine.Validator.IGNORE) {
                            ignored = true;
                            return false;
                        }
                        value = (V) result;
                    }
                    if (oldValue != null)
                        sizeDiff = -1;
                    nullBucket.setValue(treeValue);
                } finally {
                    cacheEntry.releaseExclusiveLock();
                    releasePage(atomicOperation, cacheEntry);
                    if (ignored)
                        endAtomicOperation(false, null);
                }
                sizeDiff++;
                setSize(size() + sizeDiff, atomicOperation);
            }
            endAtomicOperation(false, null);
            return true;
        } catch (IOException e) {
            rollback(e);
            throw OException.wrapException(new OSBTreeException("Error during index update with key " + key + " and value " + value, this), e);
        } catch (RuntimeException e) {
            rollback(e);
            throw e;
        } finally {
            releaseExclusiveLock();
        }
    } finally {
        if (statistic != null)
            statistic.stopIndexEntryUpdateTimer();
        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) OTooBigIndexKeyException(com.orientechnologies.orient.core.exception.OTooBigIndexKeyException)

Example 20 with OCacheEntry

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

the class OSBTree method size.

public long size() {
    startOperation();
    try {
        atomicOperationsManager.acquireReadLock(this);
        try {
            acquireSharedLock();
            try {
                OAtomicOperation atomicOperation = atomicOperationsManager.getCurrentOperation();
                OCacheEntry rootCacheEntry = loadPage(atomicOperation, fileId, ROOT_INDEX, false);
                rootCacheEntry.acquireSharedLock();
                try {
                    OSBTreeBucket<K, V> rootBucket = new OSBTreeBucket<K, V>(rootCacheEntry, keySerializer, keyTypes, valueSerializer, getChanges(atomicOperation, rootCacheEntry));
                    return rootBucket.getTreeSize();
                } finally {
                    rootCacheEntry.releaseSharedLock();
                    releasePage(atomicOperation, rootCacheEntry);
                }
            } finally {
                releaseSharedLock();
            }
        } catch (IOException e) {
            throw OException.wrapException(new OSBTreeException("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) IOException(java.io.IOException)

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