use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.
the class PatriciaTreeMutable method put.
@Override
public boolean put(@NotNull final ByteIterable key, @NotNull final ByteIterable value) {
final ByteIterator it = key.iterator();
MutableNode node = root;
MutableNode prev = null;
byte prevFirstByte = (byte) 0;
while (true) {
final long matchResult = node.matchesKeySequence(it);
final int matchingLength = NodeBase.MatchResult.getMatchingLength(matchResult);
if (matchingLength < 0) {
final MutableNode prefix = node.splitKey(-matchingLength - 1, NodeBase.MatchResult.getKeyByte(matchResult));
if (NodeBase.MatchResult.hasNext(matchResult)) {
prefix.hang(NodeBase.MatchResult.getNextByte(matchResult), it).setValue(value);
} else {
prefix.setValue(value);
}
if (prev == null) {
root = new MutableRoot(prefix, root.sourceAddress);
} else {
prev.setChild(prevFirstByte, prefix);
}
++size;
return true;
}
if (!it.hasNext()) {
final ByteIterable oldValue = node.getValue();
node.setValue(value);
if (oldValue == null) {
++size;
return true;
}
return !oldValue.equals(value);
}
final byte nextByte = it.next();
final NodeBase child = node.getChild(this, nextByte);
if (child == null) {
if (node.hasChildren() || node.hasKey() || node.hasValue()) {
node.hang(nextByte, it).setValue(value);
} else {
node.setKeySequence(new ArrayByteIterable(nextByte, it));
node.setValue(value);
}
++size;
return true;
}
prev = node;
prevFirstByte = nextByte;
final MutableNode mutableChild = child.getMutableCopy(this);
if (!child.isMutable()) {
node.setChild(nextByte, mutableChild);
}
node = mutableChild;
}
}
use of jetbrains.exodus.ByteIterable 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.ByteIterable in project xodus by JetBrains.
the class PatriciaTreeWithDuplicatesMutable method delete.
@Override
public boolean delete(@NotNull final ByteIterable key) {
boolean wasDeleted = false;
try (ITreeCursor cursor = treeNoDuplicates.openCursor()) {
final byte[] keyBytes = key.getBytesUnsafe();
final int keyLength = key.getLength();
@Nullable ByteIterable value = cursor.getSearchKeyRange(getEscapedKeyWithSeparator(key));
while (value != null) {
if (keyLength != CompressedUnsignedLongByteIterable.getInt(value)) {
break;
}
final ByteIterable noDupKey = new UnEscapingByteIterable(cursor.getKey());
if (ByteIterableUtil.compare(keyBytes, keyLength, noDupKey.getBytesUnsafe(), keyLength) != 0) {
break;
}
cursor.deleteCurrent();
wasDeleted = true;
value = cursor.getNext() ? cursor.getValue() : null;
}
}
return wasDeleted;
}
use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.
the class GarbageCollectorInterleavingTest method fill.
private void fill(@NotNull final String table) {
final ByteIterable val0 = StringBinding.stringToEntry("val0");
env.executeInTransaction(new TransactionalExecutable() {
@Override
public void execute(@NotNull Transaction txn) {
final StoreImpl store = env.openStore(table, getStoreConfig(), txn);
for (int i = 0; i < getRecordsNumber(); ++i) {
final ArrayByteIterable key = StringBinding.stringToEntry("key " + i);
store.put(txn, key, val0);
}
}
});
}
use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.
the class GarbageCollectorInterleavingTest method check.
private void check(@NotNull final String table) {
final ByteIterable val0 = StringBinding.stringToEntry("val0");
env.executeInReadonlyTransaction(new TransactionalExecutable() {
@Override
public void execute(@NotNull Transaction txn) {
final Store store = env.openStore(table, getStoreConfig(), txn);
for (int i = 0; i < getRecordsNumber(); ++i) {
final ArrayByteIterable key = StringBinding.stringToEntry("key " + i);
Assert.assertTrue(store.exists(txn, key, val0));
}
}
});
}
Aggregations