use of com.orientechnologies.orient.core.exception.OSBTreeBonsaiLocalException in project orientdb by orientechnologies.
the class OSBTreeBonsaiBucket method addEntry.
public boolean addEntry(int index, SBTreeEntry<K, V> treeEntry, boolean updateNeighbors) throws IOException {
final int keySize = keySerializer.getObjectSize(treeEntry.key);
int valueSize = 0;
int entrySize = keySize;
if (isLeaf) {
assert valueSerializer.isFixedLength();
valueSize = valueSerializer.getFixedLength();
entrySize += valueSize;
checkEntreeSize(entrySize);
} else
entrySize += 2 * (OLongSerializer.LONG_SIZE + OIntegerSerializer.INT_SIZE);
int size = size();
int freePointer = getIntValue(offset + FREE_POINTER_OFFSET);
if (freePointer - entrySize < (size + 1) * OIntegerSerializer.INT_SIZE + POSITIONS_ARRAY_OFFSET) {
if (size > 1)
return false;
else
throw new OSBTreeBonsaiLocalException("Entry size ('key + value') is more than is more than allowed " + (freePointer - 2 * OIntegerSerializer.INT_SIZE + POSITIONS_ARRAY_OFFSET) + " bytes, either increase page size using '" + OGlobalConfiguration.SBTREEBONSAI_BUCKET_SIZE.getKey() + "' parameter, or decrease 'key + value' size.", tree);
}
if (index <= size - 1) {
moveData(offset + POSITIONS_ARRAY_OFFSET + index * OIntegerSerializer.INT_SIZE, offset + POSITIONS_ARRAY_OFFSET + (index + 1) * OIntegerSerializer.INT_SIZE, (size - index) * OIntegerSerializer.INT_SIZE);
}
freePointer -= entrySize;
setIntValue(offset + FREE_POINTER_OFFSET, freePointer);
setIntValue(offset + POSITIONS_ARRAY_OFFSET + index * OIntegerSerializer.INT_SIZE, freePointer);
setIntValue(offset + SIZE_OFFSET, size + 1);
if (isLeaf) {
byte[] serializedKey = new byte[keySize];
keySerializer.serializeNativeObject(treeEntry.key, serializedKey, 0);
setBinaryValue(offset + freePointer, serializedKey);
freePointer += keySize;
byte[] serializedValue = new byte[valueSize];
valueSerializer.serializeNativeObject(treeEntry.value, serializedValue, 0);
setBinaryValue(offset + freePointer, serializedValue);
} else {
setBucketPointer(offset + freePointer, treeEntry.leftChild);
freePointer += OLongSerializer.LONG_SIZE + OIntegerSerializer.INT_SIZE;
setBucketPointer(offset + freePointer, treeEntry.rightChild);
freePointer += OLongSerializer.LONG_SIZE + OIntegerSerializer.INT_SIZE;
byte[] serializedKey = new byte[keySize];
keySerializer.serializeNativeObject(treeEntry.key, serializedKey, 0);
setBinaryValue(offset + freePointer, serializedKey);
size++;
if (updateNeighbors && size > 1) {
if (index < size - 1) {
final int nextEntryPosition = getIntValue(offset + POSITIONS_ARRAY_OFFSET + (index + 1) * OIntegerSerializer.INT_SIZE);
setBucketPointer(offset + nextEntryPosition, treeEntry.rightChild);
}
if (index > 0) {
final int prevEntryPosition = getIntValue(offset + POSITIONS_ARRAY_OFFSET + (index - 1) * OIntegerSerializer.INT_SIZE);
setBucketPointer(offset + prevEntryPosition + OLongSerializer.LONG_SIZE + OIntegerSerializer.INT_SIZE, treeEntry.leftChild);
}
}
}
return true;
}
use of com.orientechnologies.orient.core.exception.OSBTreeBonsaiLocalException in project orientdb by orientechnologies.
the class OSBTreeBonsaiLocal method loadEntriesBetween.
@Override
public void loadEntriesBetween(K keyFrom, boolean fromInclusive, K keyTo, boolean toInclusive, RangeResultListener<K, V> listener) {
final OSessionStoragePerformanceStatistic statistic = performanceStatisticManager.getSessionPerformanceStatistic();
int readEntries = 0;
startOperation();
if (statistic != null)
statistic.startRidBagEntryReadTimer();
try {
atomicOperationsManager.acquireReadLock(this);
try {
final Lock lock = FILE_LOCK_MANAGER.acquireSharedLock(fileId);
try {
OAtomicOperation atomicOperation = atomicOperationsManager.getCurrentOperation();
BucketSearchResult bucketSearchResultFrom = findBucket(keyFrom, atomicOperation);
OBonsaiBucketPointer bucketPointerFrom = bucketSearchResultFrom.getLastPathItem();
int indexFrom;
if (bucketSearchResultFrom.itemIndex >= 0) {
indexFrom = fromInclusive ? bucketSearchResultFrom.itemIndex : bucketSearchResultFrom.itemIndex + 1;
} else {
indexFrom = -bucketSearchResultFrom.itemIndex - 1;
}
BucketSearchResult bucketSearchResultTo = findBucket(keyTo, atomicOperation);
OBonsaiBucketPointer bucketPointerTo = bucketSearchResultTo.getLastPathItem();
int indexTo;
if (bucketSearchResultTo.itemIndex >= 0) {
indexTo = toInclusive ? bucketSearchResultTo.itemIndex : bucketSearchResultTo.itemIndex - 1;
} else {
indexTo = -bucketSearchResultTo.itemIndex - 2;
}
int startIndex = indexFrom;
int endIndex;
OBonsaiBucketPointer bucketPointer = bucketPointerFrom;
resultsLoop: while (true) {
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);
if (!bucketPointer.equals(bucketPointerTo))
endIndex = bucket.size() - 1;
else
endIndex = indexTo;
for (int i = startIndex; i <= endIndex; i++) {
if (!listener.addResult(bucket.getEntry(i))) {
readEntries++;
break resultsLoop;
}
}
if (bucketPointer.equals(bucketPointerTo))
break;
bucketPointer = bucket.getRightSibling();
if (bucketPointer.getPageIndex() < 0)
break;
} finally {
cacheEntry.releaseSharedLock();
releasePage(atomicOperation, cacheEntry);
}
startIndex = 0;
}
} finally {
lock.unlock();
}
} catch (IOException ioe) {
throw OException.wrapException(new OSBTreeBonsaiLocalException("Error during fetch of values between key " + keyFrom + " and key " + keyTo + " in sbtree " + getName(), this), ioe);
} finally {
atomicOperationsManager.releaseReadLock(this);
}
} finally {
if (statistic != null)
statistic.stopRidBagEntryReadTimer(readEntries);
completeOperation();
}
}
use of com.orientechnologies.orient.core.exception.OSBTreeBonsaiLocalException 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();
}
}
use of com.orientechnologies.orient.core.exception.OSBTreeBonsaiLocalException 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();
}
}
use of com.orientechnologies.orient.core.exception.OSBTreeBonsaiLocalException 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();
}
}
Aggregations