@Override public void removeRange(byte[] minKey, byte[] maxKey) { Preconditions.checkState(!this.closed.get(), "transaction closed"); Preconditions.checkState(!this.txType.isReadOnly(), "read-only transaction"); try (final Cursor cursor = this.store.openCursor(this.tx)) { boolean found = minKey != null && minKey.length > 0 ? cursor.getSearchKeyRange(new ArrayByteIterable(minKey)) != null : cursor.getNext(); while (found) { if (maxKey != null && ByteUtil.compare(XodusKVStore.get(cursor.getKey(), false), maxKey) >= 0) break; cursor.deleteCurrent(); found = cursor.getNext(); } } }
@Override public KVPair getAtLeast(byte[] minKey, byte[] maxKey) { Preconditions.checkState(!this.closed.get(), "transaction closed"); try (final Cursor cursor = this.store.openCursor(this.tx)) { final boolean found = minKey != null && minKey.length > 0 ? cursor.getSearchKeyRange(new ArrayByteIterable(minKey)) != null : cursor.getNext(); if (!found) return null; final byte[] key = XodusKVStore.get(cursor.getKey(), true); if (maxKey != null && ByteUtil.compare(key, maxKey) >= 0) return null; return new KVPair(key, XodusKVStore.get(cursor.getValue(), true)); } }
if (this.reverse) { if (this.maxKey != null) { final boolean found = this.cursor.getSearchKeyRange(new ArrayByteIterable(this.maxKey)) != null; assert !found || ByteUtil.compare(XodusKVStore.get(this.cursor.getKey(), false), this.maxKey) >= 0 : "cusor.getSearchKeyRange() returned " + ByteUtil.toString(XodusKVStore.get(this.cursor.getKey(), false)) final boolean found = this.cursor.getSearchKeyRange(new ArrayByteIterable(this.minKey)) != null; if (XodusKVStore.this.log.isTraceEnabled()) XodusKVStore.this.log.trace("initial seek to " + ByteUtil.toString(this.minKey) + " -> " + found);
+ ByteUtil.toString(key) + " >= " + ByteUtil.toString(this.maxKey)); this.cursor.getSearchKeyRange(new ArrayByteIterable(this.maxKey)); // recalibrate and try again continue;
@Override public KVPair getAtMost(byte[] maxKey, byte[] minKey) { Preconditions.checkState(!this.closed.get(), "transaction closed"); try (final Cursor cursor = this.store.openCursor(this.tx)) { // It's possible somebody could be simultaneously inserting keys just after maxKey, in which case we // could be tricked into returning a key > maxKey. This is unlikely, but make sure it can't affect us. while (true) { if (maxKey != null) cursor.getSearchKeyRange(new ArrayByteIterable(maxKey)); if (!cursor.getPrev()) return null; final byte[] key = XodusKVStore.get(cursor.getKey(), true); if (maxKey != null && ByteUtil.compare(key, maxKey) >= 0) continue; if (minKey != null && ByteUtil.compare(key, minKey) < 0) return null; return new KVPair(key, XodusKVStore.get(cursor.getValue(), true)); } } }