public RecordScanner<KeyValue<byte[], T>> createSplitRecordScanner(Split split) { return Scannables.splitRecordScanner(createSplitReader(split), new ObjectRecordMaker()); }
@ReadOnly @Override public T read(byte[] key) { byte[] read = kvTable.read(key); return decode(read); }
@WriteOnly @Override public void write(byte[] key, T object) { kvTable.write(key, encode(object)); }
Schema schema = new ReflectionSchemaGenerator().generate(type); final ObjectStoreDataset<Custom> objectStore = new ObjectStoreDataset<>("kv", kvTable, typeRep, schema, loader); TransactionExecutor txnl = dsFrameworkUtil.newInMemoryTransactionExecutor(objectStore); objectStore.getRecordType(); txnl.execute(new TransactionExecutor.Subroutine() { @Override
@Override public ObjectStoreDataset<?> getDataset(DatasetContext datasetContext, DatasetSpecification spec, Map<String, String> arguments, ClassLoader classLoader) throws IOException { DatasetSpecification kvTableSpec = spec.getSpecification("objects"); KeyValueTable table = tableDef.getDataset(datasetContext, kvTableSpec, arguments, classLoader); TypeRepresentation typeRep = GSON.fromJson(spec.getProperty("type"), TypeRepresentation.class); Schema schema = GSON.fromJson(spec.getProperty("schema"), Schema.class); return new ObjectStoreDataset(spec.getName(), table, typeRep, schema, classLoader); } }
@Override public void apply() throws Exception { Custom custom = store.read(a); Preconditions.checkNotNull(custom); } });
private T decode(byte[] bytes) { if (bytes == null) { return null; } // decode T using schema ByteArrayInputStream bis = new ByteArrayInputStream(bytes); BinaryDecoder decoder = new BinaryDecoder(bis); try { return getReflectionDatumReader().read(decoder, this.schema); } catch (IOException e) { // SHOULD NEVER happen throw new DataSetException("Failed to decode read object: " + e.getMessage(), e); } }
@Override public void apply() throws Exception { // get specific number of splits for a subrange TreeSet<Long> keysToVerify = Sets.newTreeSet(keysWritten.subSet(0x10000000L, 0x40000000L)); List<Split> splits = t.getSplits(5, Bytes.toBytes(0x10000000L), Bytes.toBytes(0x40000000L)); Assert.assertTrue(splits.size() <= 5); // read each split and verify the keys verifySplits(t, splits, keysToVerify); } });
@Override public void apply() throws Exception { CloseableIterator<KeyValue<byte[], String>> objectsIterator = t.scan(Bytes.toBytes(0), Bytes.toBytes(10)); int rowCount = 0; while (objectsIterator.hasNext() && (rowCount < 5)) { rowCount++; } objectsIterator.close(); try { objectsIterator.next(); Assert.fail("Reading after closing Scanner returned result."); } catch (NoSuchElementException e) { } } });
@WriteOnly public void delete(int key) { super.delete(Bytes.toBytes(key)); } }
@Override public ObjectStoreDataset<?> getDataset(DatasetContext datasetContext, DatasetSpecification spec, Map<String, String> arguments, ClassLoader classLoader) throws IOException { DatasetSpecification kvTableSpec = spec.getSpecification("objects"); KeyValueTable table = tableDef.getDataset(datasetContext, kvTableSpec, arguments, classLoader); TypeRepresentation typeRep = GSON.fromJson(spec.getProperty("type"), TypeRepresentation.class); Schema schema = GSON.fromJson(spec.getProperty("schema"), Schema.class); return new ObjectStoreDataset(spec.getName(), table, typeRep, schema, classLoader); } }
@Override public void apply() throws Exception { Custom result = customStore.read(a); Assert.assertEquals(custom, result); } });
private T decode(byte[] bytes) { if (bytes == null) { return null; } // decode T using schema ByteArrayInputStream bis = new ByteArrayInputStream(bytes); BinaryDecoder decoder = new BinaryDecoder(bis); try { return getReflectionDatumReader().read(decoder, this.schema); } catch (IOException e) { // SHOULD NEVER happen throw new DataSetException("Failed to decode read object: " + e.getMessage(), e); } }
public RecordScanner<KeyValue<byte[], T>> createSplitRecordScanner(Split split) { return Scannables.splitRecordScanner(createSplitReader(split), new ObjectRecordMaker()); }
@Override public void apply() throws Exception { ImmutablePair<Integer, String> result = pairStore.read(a); Assert.assertEquals(pair, result); } });
@WriteOnly @Override public void write(byte[] key, T object) { kvTable.write(key, encode(object)); }
@ReadOnly @Override public T read(byte[] key) { byte[] read = kvTable.read(key); return decode(read); }
private void verifySplits(ObjectStoreDataset<String> t, List<Split> splits, SortedSet<Long> keysToVerify) throws InterruptedException { // read each split and verify the keys, remove all read keys from the set for (Split split : splits) { SplitReader<byte[], String> reader = t.createSplitReader(split); reader.initialize(split); while (reader.nextKeyValue()) { byte[] key = reader.getCurrentKey(); String value = reader.getCurrentValue(); // verify each row has the two columns written Assert.assertEquals(Long.toString(Bytes.toLong(key)), value); Assert.assertTrue(keysToVerify.remove(Bytes.toLong(key))); } } // verify all keys have been read if (!keysToVerify.isEmpty()) { System.out.println("Remaining [" + keysToVerify.size() + "]: " + keysToVerify); } Assert.assertTrue(keysToVerify.isEmpty()); }
@Override public void apply() throws Exception { Custom result = customStore.read(a); Assert.assertEquals(custom2, result); } });
@WriteOnly @Override public void write(String key, T object) { kvTable.write(Bytes.toBytes(key), encode(object)); }