use of jetbrains.exodus.ByteIterable 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;
// noinspection TryFinallyCanBeTryWithResources
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.ByteIterable in project xodus by JetBrains.
the class PatriciaCursorDecorator method getLast.
@Override
public boolean getLast() {
if (patriciaCursor.getLast()) {
final ByteIterable keyLengthIterable = patriciaCursor.getValue();
final ByteIterable sourceKey = new UnEscapingByteIterable(patriciaCursor.getKey());
keyBytes = sourceKey.getBytesUnsafe();
keyLength = CompressedUnsignedLongByteIterable.getInt(keyLengthIterable);
valueLength = sourceKey.getLength() - keyLength - 1;
// forget computed next pair
nextKeyLength = UNKNOWN;
// forget computed prev pair
prevKeyLength = UNKNOWN;
return true;
}
return false;
}
use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.
the class PatriciaCursorDecorator method getNextLazy.
private boolean getNextLazy() {
if (nextKeyLength < 0) {
// UNKNOWN
if (patriciaCursor.getNext()) {
final ByteIterable keyLengthIterable = patriciaCursor.getValue();
final ByteIterable noDupKey = new UnEscapingByteIterable(patriciaCursor.getKey());
nextKeyBytes = noDupKey.getBytesUnsafe();
nextKeyLength = CompressedUnsignedLongByteIterable.getInt(keyLengthIterable);
nextValueLength = noDupKey.getLength() - nextKeyLength - 1;
return true;
}
return false;
}
return nextKeyBytes != null;
}
use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.
the class PatriciaCursorDecorator method getSearchBothRange.
@Nullable
@Override
public ByteIterable getSearchBothRange(@NotNull ByteIterable key, @NotNull ByteIterable value) {
final ITreeCursor cursor = patriciaCursor.getTree().openCursor();
ITreeCursor cursorToClose = cursor;
// noinspection TryFinallyCanBeTryWithResources
try {
ByteIterable keyLengthIterable = cursor.getSearchKeyRange(new EscapingByteIterable(key));
if (keyLengthIterable != null) {
final int srcKeyLength = key.getLength();
final int valueLength = value.getLength();
while (true) {
int keyLength = CompressedUnsignedLongByteIterable.getInt(keyLengthIterable);
if (srcKeyLength == keyLength) {
final ByteIterable noDupKey = new UnEscapingByteIterable(cursor.getKey());
final byte[] srcKeyBytes = key.getBytesUnsafe();
final byte[] noDupKeyBytes = noDupKey.getBytesUnsafe();
if (ByteIterableUtil.compare(srcKeyBytes, keyLength, noDupKeyBytes, keyLength) == 0) {
// skip separator
final int noDupKeyLength = noDupKey.getLength() - keyLength - 1;
final int cmp = ByteIterableUtil.compare(noDupKeyBytes, noDupKeyLength, keyLength + 1, value.getBytesUnsafe(), valueLength);
if (cmp >= 0) {
keyBytes = noDupKeyBytes;
this.keyLength = keyLength;
this.valueLength = noDupKeyLength;
cursorToClose = patriciaCursor;
patriciaCursor = cursor;
// forget computed next pair
nextKeyLength = UNKNOWN;
// forget computed prev pair
prevKeyLength = UNKNOWN;
return getValue();
}
}
}
if (cursor.getNext()) {
keyLengthIterable = cursor.getValue();
} else {
break;
}
}
}
} finally {
cursorToClose.close();
}
return null;
}
use of jetbrains.exodus.ByteIterable 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;
// noinspection TryFinallyCanBeTryWithResources
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;
}
Aggregations