use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.
the class EnvironmentTestInMemory method putRandomKeyValue.
private void putRandomKeyValue(Store primary, Store secondary, Transaction txn, int keysCount, int valuesCount, Persistent23TreeMap.MutableMap<Integer, Integer> testMap) {
final int key = rnd.nextInt(keysCount);
final ArrayByteIterable keyEntry = IntegerBinding.intToCompressedEntry(key);
final int value = rnd.nextInt(valuesCount);
testMap.put(key, value);
final ArrayByteIterable valueEntry = IntegerBinding.intToCompressedEntry(value);
final ByteIterable oldValue = primary.get(txn, keyEntry);
primary.put(txn, keyEntry, valueEntry);
if (oldValue != null) {
try (Cursor cursor = secondary.openCursor(txn)) {
Assert.assertTrue(cursor.getSearchBoth(oldValue, keyEntry));
Assert.assertTrue(cursor.deleteCurrent());
}
}
secondary.put(txn, valueEntry, keyEntry);
}
use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.
the class TreeCursorNoDuplicatesTest method testRandomInserts.
@Test
public void testRandomInserts() {
final ByteIterable value = value("value");
final Set<String> keys = new HashSet<>();
for (int i = 0; i < 10000; ++i) {
keys.add(rndString());
}
tm = createMutableTree(false, 1);
for (final String key : keys) {
Assert.assertTrue(tm.add(key(key), value));
}
testCursorOrder(new TreeSet<>(keys));
}
use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.
the class PatriciaCursorDecorator method getSearchKeyRange.
@Nullable
@Override
public ByteIterable getSearchKeyRange(@NotNull ByteIterable key) {
final ByteIterable keyLengthIterable = patriciaCursor.getSearchKeyRange(new EscapingByteIterable(key));
if (keyLengthIterable == null) {
return null;
}
final ByteIterable noDupKey = new UnEscapingByteIterable(patriciaCursor.getKey());
keyBytes = noDupKey.getBytesUnsafe();
keyLength = CompressedUnsignedLongByteIterable.getInt(keyLengthIterable);
valueLength = noDupKey.getLength() - keyLength - 1;
// forget computed next pair
nextKeyLength = UNKNOWN;
// forget computed prev pair
prevKeyLength = UNKNOWN;
return getValue();
}
use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.
the class PatriciaCursorDecorator method getSearchBoth.
@Override
public boolean getSearchBoth(@NotNull ByteIterable key, @NotNull ByteIterable value) {
final ITreeCursor cursor = patriciaCursor.getTree().openCursor();
ITreeCursor cursorToClose = cursor;
try {
ByteIterable keyLengthIterable = cursor.getSearchKey(getEscapedKeyValue(key, value));
if (keyLengthIterable == null) {
return false;
}
final int keyLength = CompressedUnsignedLongByteIterable.getInt(keyLengthIterable);
if (keyLength == key.getLength()) {
keyBytes = new UnEscapingByteIterable(cursor.getKey()).getBytesUnsafe();
this.keyLength = keyLength;
valueLength = value.getLength();
cursorToClose = patriciaCursor;
patriciaCursor = cursor;
// forget computed next pair
nextKeyLength = UNKNOWN;
// forget computed prev pair
prevKeyLength = UNKNOWN;
return true;
}
return false;
} finally {
cursorToClose.close();
}
}
use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.
the class PatriciaCursorDecorator method getNextNoDup.
@Override
public boolean getNextNoDup() {
if (keyBytes == null) {
// init
return getNext();
}
if (getNextLazy() && ByteIterableUtil.compare(keyBytes, keyLength, nextKeyBytes, nextKeyLength) != 0) {
advance();
return true;
}
// we must create new cursor 'cause we don't know if next "no dup" pair exists
final ITreeCursor cursor = patriciaCursor.getTree().openCursor();
ITreeCursor cursorToClose = cursor;
try {
if (cursor.getSearchKeyRange(getEscapedKeyValue(getKey(), getValue())) != null) {
while (cursor.getNext()) {
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 next pair
nextKeyLength = UNKNOWN;
// forget computed prev pair
prevKeyLength = UNKNOWN;
return true;
}
}
}
} finally {
cursorToClose.close();
}
return false;
}
Aggregations