use of jetbrains.exodus.tree.ITreeCursor in project xodus by JetBrains.
the class PatriciaCursorDecorator method getPrevNoDup.
@Override
public boolean getPrevNoDup() {
if (keyBytes == null) {
// init
return getPrev();
}
if (getPrevLazy() && ByteIterableUtil.compare(keyBytes, keyLength, prevKeyBytes, prevKeyLength) != 0) {
advance();
return true;
}
// we must create new cursor 'cause we don't know if prev "no dup" pair exists
final ITreeCursor cursor = patriciaCursor.getTree().openCursor();
ITreeCursor cursorToClose = cursor;
try {
if (cursor.getSearchKeyRange(getEscapedKeyValue(getKey(), getValue())) != null) {
while (cursor.getPrev()) {
final ByteIterable keyLengthIterable = cursor.getValue();
final ByteIterable noDupKey = new UnEscapingByteIterable(cursor.getKey());
final int keyLength = CompressedUnsignedLongByteIterable.getInt(keyLengthIterable);
final byte[] noDupKeyBytes = noDupKey.getBytesUnsafe();
if (ByteIterableUtil.compare(keyBytes, this.keyLength, noDupKeyBytes, keyLength) != 0) {
keyBytes = noDupKey.getBytesUnsafe();
this.keyLength = keyLength;
valueLength = noDupKey.getLength() - keyLength - 1;
cursorToClose = patriciaCursor;
patriciaCursor = cursor;
// forget computed prev pair
prevKeyLength = UNKNOWN;
// forget computed next pair
nextKeyLength = UNKNOWN;
return true;
}
}
}
} finally {
cursorToClose.close();
}
return false;
}
use of jetbrains.exodus.tree.ITreeCursor in project xodus by JetBrains.
the class PatriciaCursorDecorator method getSearchKey.
@Nullable
@Override
public ByteIterable getSearchKey(@NotNull ByteIterable key) {
final ITreeCursor cursor = patriciaCursor.getTree().openCursor();
ITreeCursor cursorToClose = cursor;
try {
ByteIterable keyLengthIterable = cursor.getSearchKeyRange(new EscapingByteIterable(key));
if (keyLengthIterable != null) {
int keyLength = CompressedUnsignedLongByteIterable.getInt(keyLengthIterable);
if (key.getLength() == keyLength) {
final ByteIterable noDupKey = new UnEscapingByteIterable(cursor.getKey());
final byte[] keyBytes = key.getBytesUnsafe();
final byte[] noDupKeyBytes = noDupKey.getBytesUnsafe();
if (ByteIterableUtil.compare(keyBytes, keyLength, noDupKeyBytes, keyLength) == 0) {
this.keyBytes = noDupKeyBytes;
this.keyLength = keyLength;
valueLength = noDupKey.getLength() - keyLength - 1;
cursorToClose = patriciaCursor;
patriciaCursor = cursor;
// forget computed next pair
nextKeyLength = UNKNOWN;
// forget computed prev pair
prevKeyLength = UNKNOWN;
return getValue();
}
}
}
} finally {
cursorToClose.close();
}
return null;
}
use of jetbrains.exodus.tree.ITreeCursor in project xodus by JetBrains.
the class PatriciaTreeWithDuplicates method get.
@Nullable
@Override
public ByteIterable get(@NotNull final ByteIterable key) {
try (ITreeCursor cursor = treeNoDuplicates.openCursor()) {
final ByteIterable value = cursor.getSearchKeyRange(getEscapedKeyWithSeparator(key));
if (value != null && value != ByteIterable.EMPTY) {
int keyLength = CompressedUnsignedLongByteIterable.getInt(value);
if (key.getLength() == keyLength) {
final ByteIterable noDupKey = new UnEscapingByteIterable(cursor.getKey());
final byte[] noDupKeyBytes = noDupKey.getBytesUnsafe();
if (ByteIterableUtil.compare(key.getBytesUnsafe(), keyLength, noDupKeyBytes, keyLength) == 0) {
return new ArrayByteIterable(Arrays.copyOfRange(noDupKeyBytes, // skip separator
keyLength + 1, noDupKey.getLength()));
}
}
}
return null;
}
}
use of jetbrains.exodus.tree.ITreeCursor in project xodus by JetBrains.
the class BTreePutSpecificTest method testNextDup.
@Test
public void testNextDup() {
tm = createEmptyTreeForCursor(1).getMutableCopy();
getTreeMutable().put(kv("1", "1"));
getTreeMutable().put(kv("1", "2"));
getTreeMutable().put(kv("1", "3"));
getTreeMutable().put(kv("1", "4"));
getTreeMutable().put(kv("1", "5"));
final ITreeCursor cursor = tm.openCursor();
assertTrue(cursor.getNextDup());
assertTrue(cursor.getNextDup());
assertTrue(cursor.getNextDup());
assertTrue(cursor.getNextDup());
assertTrue(cursor.getNextDup());
}
use of jetbrains.exodus.tree.ITreeCursor in project xodus by JetBrains.
the class BTreeCursorDupConcurrentCursorModificationTest method deleteImpl.
@Override
protected void deleteImpl(@NotNull final ByteIterable key, @NotNull final ByteIterable value) {
final ITreeCursor cursor = tm.openCursor();
assertTrue(cursor.getSearchBoth(key, value));
assertTrue(cursor.deleteCurrent());
}
Aggregations