独自の順序付けを提供するカスタムのComparableBinding/ByteIterableの組み合わせを実装することは可能ですか?そして私はどのようにこれをシステムに登録しますか? また、キーに使用する場合はByteIterable.subIterable(final int offset、final int length)メソッドを実装しない方が安全でしょうか?私のユースケースでは、順序を壊すので、有効なサブタイトルはありません。カスタムComparableBindingの実装
次のTestStore.test()メソッドでは、下位のassert文が失敗するため、カーソルを昇順に移動させるには不十分です。
import jetbrains.exodus.ArrayByteIterable;
import jetbrains.exodus.ByteIterable;
import jetbrains.exodus.ByteIterator;
import org.jetbrains.annotations.NotNull;
import java.nio.charset.Charset;
public class TestKey implements ByteIterable {
private final int value;
private final byte[] bytes;
public TestKey(int value) {
this.value = value;
this.bytes = Integer.toString(value).getBytes(Charset.forName("utf-8"));
}
@Override
public int compareTo(@NotNull ByteIterable o) {
return Integer.compare(value, ((TestKey)o).value);
}
@Override
public ByteIterator iterator() {
return new ArrayByteIterable(bytes).iterator();
}
@Override
public byte[] getBytesUnsafe() {
return bytes;
}
@Override
public int getLength() {
return bytes.length;
}
@Override
public @NotNull ByteIterable subIterable(int offset, int length) {
throw new UnsupportedOperationException("subIterable");
}
}
import jetbrains.exodus.ByteIterable;
import jetbrains.exodus.bindings.IntegerBinding;
import jetbrains.exodus.bindings.StringBinding;
import jetbrains.exodus.env.Cursor;
import jetbrains.exodus.env.Environment;
import jetbrains.exodus.env.Environments;
import jetbrains.exodus.env.Store;
import jetbrains.exodus.env.StoreConfig;
import jetbrains.exodus.env.Transaction;
import jetbrains.exodus.env.TransactionalExecutable;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.Arrays;
import java.util.UUID;
public class TestStore {
private Store store;
private Environment environment;
public TestStore(File folder) {
environment = Environments.newContextualInstance(folder);
environment.executeInTransaction(new TransactionalExecutable() {
@Override
public void execute(@NotNull Transaction txn) {
store = environment.openStore(
UUID.randomUUID().toString(),
StoreConfig.WITHOUT_DUPLICATES,
txn,
true);
}
});
}
public void test() {
int count = 1000;
int[] orig = new int[count];
int[] iterated = new int[count];
for(int i = 0; i < count; i++) {
final int index = i;
environment.executeInTransaction(new TransactionalExecutable() {
@Override
public void execute(@NotNull Transaction txn) {
orig[index] = index;
store.put(txn,
new TestKey(index),
// IntegerBinding.intToEntry(index),
StringBinding.stringToEntry(Integer.toString(index))
);
}
});
}
environment.executeInTransaction(new TransactionalExecutable() {
@Override
public void execute(@NotNull Transaction txn) {
int offset = 0;
try(Cursor cursor = store.openCursor(txn)) {
while(cursor.getNext()) {
ByteIterable key = cursor.getKey();
ByteIterable value = cursor.getValue();
iterated[offset++] = Integer.parseInt(StringBinding.entryToString(value));
}
}
}
});
assert Arrays.equals(orig, iterated);
}
}
|あなたはこのとの結合使用しようとしている(環境EntityStores)? –
環境のみ。 – wolpers