@Test
public void testSnapshot() throws Exception {
InMemoryKeyValueStore store = new InMemoryKeyValueStore(
new KeyValueStoreMetrics("testInMemory", new MetricsRegistryMap()));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
String prefix = "prefix";
for(int i = 0; i < 100; i++) {
store.put(genKey(outputStream, prefix, i), genValue());
}
byte[] firstKey = genKey(outputStream, prefix, 0);
byte[] lastKey = genKey(outputStream, prefix, 100);
KeyValueSnapshot<byte[], byte[]> snapshot = store.snapshot(firstKey, lastKey);
store.put(genKey(outputStream, prefix, 200), genValue());
assertTrue(Iterators.size(snapshot.iterator()) == 100);
List<Integer> keys = new ArrayList<>();
KeyValueIterator<byte[], byte[]> iter = snapshot.iterator();
while (iter.hasNext()) {
Entry<byte[], byte[]> entry = iter.next();
int key = Ints.fromByteArray(Arrays.copyOfRange(entry.getKey(), prefix.getBytes().length, entry.getKey().length));
keys.add(key);
}
assertEquals(keys, IntStream.rangeClosed(0, 99).boxed().collect(Collectors.toList()));
outputStream.close();
store.close();
}