ValueStoreDb extends AbstractLmdbDb<ValueStoreKey, RefDataValue> { public Optional<RefDataValue> get(final Txn<ByteBuffer> txn, final ByteBuffer keyBuffer, final int typeId) { return getAsBytes(txn, keyBuffer) .map(valueBuffer -> valueSerde.deserialize(valueBuffer, typeId)); } @Inject  ValueStoreDb(@Assisted final Env<ByteBuffer> lmdbEnvironment,
                        final ByteBufferPool byteBufferPool,
                        final ValueStoreKeySerde keySerde,
                        final GenericRefDataValueSerde valueSerde,
                        final ValueStoreHashAlgorithm valueStoreHashAlgorithm); ValueStoreHashAlgorithm getValueStoreHashAlgorithm(); boolean areValuesEqual(final Txn<ByteBuffer> txn,
                                  final ByteBuffer valueStoreKeyBuffer,
                                  final RefDataValue newRefDataValue); ByteBuffer getOrCreateKey(final Txn<ByteBuffer> writeTxn,
                                     final RefDataValue refDataValue,
                                     final PooledByteBuffer valueStoreKeyPooledBuffer,
                                     final boolean isOverwrite,
                                     final EntryConsumer onExistingEntryAction,
                                     final EntryConsumer onNewEntryAction); Optional<RefDataValue> get(final Txn<ByteBuffer> txn,
                                      final ByteBuffer keyBuffer,
                                      final int typeId); static final String DB_NAME; }
@Test void testGet_simple() { assertThat(valueStoreDb.getEntryCount()).isEqualTo(0); final String val1str = "value one"; final String val2str = "value two"; final String val3str = "value three"; final Map<String, ValueStoreKey> valueToKeyMap = new HashMap<>(); LmdbUtils.doWithWriteTxn(lmdbEnv, writeTxn -> { valueToKeyMap.put(val1str, getOrCreate(writeTxn, StringValue.of(val1str))); valueToKeyMap.put(val2str, getOrCreate(writeTxn, StringValue.of(val2str))); valueToKeyMap.put(val3str, getOrCreate(writeTxn, StringValue.of(val3str))); }); assertThat(valueStoreDb.getEntryCount()).isEqualTo(3); ValueStoreKeySerde keySerde = new ValueStoreKeySerde(); LmdbUtils.doWithReadTxn(lmdbEnv, txn -> { valueToKeyMap.forEach((valueStr, valueStoreKey) -> { valueStoreDb.getPooledKeyBuffer().doWithByteBuffer(keyBuffer -> { keySerde.serialize(keyBuffer, valueStoreKey); ByteBuffer valueBuffer = valueStoreDb.getAsBytes(txn, keyBuffer).get(); RefDataValue val = refDataValueSerdeFactory.deserialize(valueBuffer, StringValue.TYPE_ID); assertThat(val).isInstanceOf(StringValue.class); assertThat(((StringValue) val).getValue()).isEqualTo(valueStr); }); }); valueStoreDb.getPooledKeyBuffer().doWithByteBuffer(keyBuffer -> { keySerde.serialize(keyBuffer, new ValueStoreKey(123456, (short) 99)); Optional<RefDataValue> optRefDataValue = valueStoreDb.get( txn, keyBuffer, StringValue.TYPE_ID); assertThat(optRefDataValue).isEmpty(); }); }); }
@Test void testGet_sameHashCodes() { setupValueStoreDb(basicHashAlgorithm); assertThat(valueStoreDb.getEntryCount()).isEqualTo(0); String val1str = "AaAa"; String val2str = "BBBB"; String val3str = "AaBB"; String val4str = "BBAa"; assertThat(val1str.hashCode()).isEqualTo(val2str.hashCode()); assertThat(val1str.hashCode()).isEqualTo(val3str.hashCode()); assertThat(val1str.hashCode()).isEqualTo(val4str.hashCode()); final Map<String, ValueStoreKey> valueToKeyMap = new HashMap<>(); LmdbUtils.doWithWriteTxn(lmdbEnv, writeTxn -> { valueToKeyMap.put(val1str, getOrCreate(writeTxn, StringValue.of(val1str))); valueToKeyMap.put(val2str, getOrCreate(writeTxn, StringValue.of(val2str))); valueToKeyMap.put(val3str, getOrCreate(writeTxn, StringValue.of(val3str))); valueToKeyMap.put(val4str, getOrCreate(writeTxn, StringValue.of(val4str))); }); assertThat( valueToKeyMap.values().stream() .map(ValueStoreKey::getUniqueId) .collect(Collectors.toList())) .contains((short) 0, (short) 1, (short) 2, (short) 3); assertThat(valueStoreDb.getEntryCount()).isEqualTo(valueToKeyMap.size()); ValueStoreKeySerde keySerde = new ValueStoreKeySerde(); LmdbUtils.doWithReadTxn(lmdbEnv, txn -> { List<Integer> ids = new ArrayList<>(); valueStoreDb.getPooledKeyBuffer().doWithByteBuffer(keyBuffer -> { valueToKeyMap.forEach((valueStr, valueStoreKey) -> { keySerde.serialize(keyBuffer, valueStoreKey); ByteBuffer valueBuffer = valueStoreDb.getAsBytes(txn, keyBuffer).get(); RefDataValue val = refDataValueSerdeFactory.deserialize(valueBuffer, StringValue.TYPE_ID); assertThat(val).isInstanceOf(StringValue.class); assertThat(((StringValue) val).getValue()).isEqualTo(valueStr); }); }); }); }