use of com.apple.foundationdb.tuple.Tuple in project YCSB by brianfrankcooper.
the class FoundationDBClient method scan.
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
String startRowKey = getRowKey(dbName, table, startkey);
String endRowKey = getEndRowKey(table);
logger.debug("scan key from {} to {} limit {} ", startkey, endRowKey, recordcount);
try (Transaction tr = db.createTransaction()) {
tr.options().setReadYourWritesDisable();
AsyncIterable<KeyValue> entryList = tr.getRange(Tuple.from(startRowKey).pack(), Tuple.from(endRowKey).pack(), recordcount > 0 ? recordcount : 0);
List<KeyValue> entries = entryList.asList().join();
for (int i = 0; i < entries.size(); ++i) {
final HashMap<String, ByteIterator> map = new HashMap<>();
Tuple value = Tuple.fromBytes(entries.get(i).getValue());
if (convTupleToMap(value, fields, map) == Status.OK) {
result.add(map);
} else {
logger.error("Error scanning keys: from {} to {} limit {} ", startRowKey, endRowKey, recordcount);
return Status.ERROR;
}
}
return Status.OK;
} catch (FDBException e) {
logger.error(MessageFormatter.format("Error scanning keys: from {} to {} ", startRowKey, endRowKey).getMessage(), e);
e.printStackTrace();
} catch (Exception e) {
logger.error(MessageFormatter.format("Error scanning keys: from {} to {} ", startRowKey, endRowKey).getMessage(), e);
e.printStackTrace();
}
return Status.ERROR;
}
use of com.apple.foundationdb.tuple.Tuple in project YCSB by brianfrankcooper.
the class FoundationDBClient method update.
@Override
public Status update(String table, String key, Map<String, ByteIterator> values) {
String rowKey = getRowKey(dbName, table, key);
logger.debug("update key = {}", rowKey);
try {
Status s = db.run(tr -> {
byte[] row = tr.get(Tuple.from(rowKey).pack()).join();
Tuple o = Tuple.fromBytes(row);
if (o.size() == 0) {
logger.debug("key not fount: {}", rowKey);
return Status.NOT_FOUND;
}
HashMap<String, ByteIterator> result = new HashMap<>();
if (convTupleToMap(o, null, result) != Status.OK) {
return Status.ERROR;
}
for (String k : values.keySet()) {
if (result.containsKey(k)) {
result.put(k, values.get(k));
} else {
logger.debug("field not fount: {}", k);
return Status.NOT_FOUND;
}
}
Tuple t = new Tuple();
for (Map.Entry<String, String> entry : StringByteIterator.getStringMap(result).entrySet()) {
Tuple v = new Tuple();
v = v.add(entry.getKey());
v = v.add(entry.getValue());
t = t.add(v);
}
tr.set(Tuple.from(rowKey).pack(), t.pack());
return Status.OK;
});
return s;
} catch (FDBException e) {
logger.error(MessageFormatter.format("Error updating key: {}", rowKey).getMessage(), e);
e.printStackTrace();
} catch (Exception e) {
logger.error(MessageFormatter.format("Error updating key: {}", rowKey).getMessage(), e);
e.printStackTrace();
}
return Status.ERROR;
}
use of com.apple.foundationdb.tuple.Tuple in project YCSB by brianfrankcooper.
the class FoundationDBClient method batchInsert.
private void batchInsert() {
try {
db.run(tr -> {
for (int i = 0; i < batchCount; ++i) {
Tuple t = new Tuple();
for (Map.Entry<String, String> entry : StringByteIterator.getStringMap(batchValues.get(i)).entrySet()) {
Tuple v = new Tuple();
v = v.add(entry.getKey());
v = v.add(entry.getValue());
t = t.add(v);
}
tr.set(Tuple.from(batchKeys.get(i)).pack(), t.pack());
}
return null;
});
} catch (FDBException e) {
for (int i = 0; i < batchCount; ++i) {
logger.error(MessageFormatter.format("Error batch inserting key {}", batchKeys.get(i)).getMessage(), e);
}
e.printStackTrace();
} catch (Throwable e) {
for (int i = 0; i < batchCount; ++i) {
logger.error(MessageFormatter.format("Error batch inserting key {}", batchKeys.get(i)).getMessage(), e);
}
e.printStackTrace();
} finally {
batchKeys.clear();
batchValues.clear();
}
}
use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.
the class MapsTest method indexed.
@Test
public void indexed() throws Exception {
final RecordMetaDataHook hook = md -> {
md.addIndex("StringToInt", "mapKeyValue", Key.Expressions.mapKeyValues("map_value"));
};
try (FDBRecordContext context = openContext()) {
openAnyRecordStore(TestRecordsMapsProto.getDescriptor(), context, hook);
TestRecordsMapsProto.StringToInt.Builder recBuilder = TestRecordsMapsProto.StringToInt.newBuilder();
recBuilder.setRecNo(1);
recBuilder.putMapValue("num", 1);
recBuilder.putMapValue("other", 2);
recordStore.saveRecord(recBuilder.build());
recBuilder.setRecNo(2);
recBuilder.clearMapValue();
recBuilder.putMapValue("num", 2);
recordStore.saveRecord(recBuilder.build());
commit(context);
}
final RecordQuery query = RecordQuery.newBuilder().setRecordType("StringToInt").setFilter(Query.field("map_value").mapMatches(k -> k.equalsValue("num"), v -> v.greaterThan(1))).build();
try (FDBRecordContext context = openContext()) {
openAnyRecordStore(TestRecordsMapsProto.getDescriptor(), context, hook);
RecordQueryPlan plan = planner.plan(query);
List<Tuple> results = recordStore.executeQuery(plan).map(FDBQueriedRecord::getPrimaryKey).asList().join();
assertEquals(Collections.singletonList(Tuple.from(2)), results);
MatcherAssert.assertThat(plan, PlanMatchers.primaryKeyDistinct(PlanMatchers.indexScan(Matchers.allOf(indexName("mapKeyValue"), PlanMatchers.bounds(PlanMatchers.hasTupleString("([num, 1],[num]]"))))));
commit(context);
}
}
use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.
the class TupleRangeTest method toRange.
@Test
public void toRange() {
final Tuple a = Tuple.from("a");
final Tuple b = Tuple.from("b");
List<Pair<TupleRange, Range>> testCases = Arrays.asList(Pair.of(TupleRange.allOf(null), new Range(new byte[0], new byte[] { (byte) 0xff })), Pair.of(TupleRange.allOf(PREFIX_TUPLE), Range.startsWith(PREFIX_BYTES)), Pair.of(new TupleRange(a, b, EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_EXCLUSIVE), new Range(a.pack(), b.pack())), Pair.of(new TupleRange(a, b, EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_INCLUSIVE), new Range(a.pack(), ByteArrayUtil.strinc(b.pack()))), Pair.of(new TupleRange(a, b, EndpointType.RANGE_EXCLUSIVE, EndpointType.RANGE_EXCLUSIVE), new Range(ByteArrayUtil.strinc(a.pack()), b.pack())), Pair.of(new TupleRange(a, b, EndpointType.RANGE_EXCLUSIVE, EndpointType.RANGE_INCLUSIVE), new Range(ByteArrayUtil.strinc(a.pack()), ByteArrayUtil.strinc(b.pack()))), Pair.of(new TupleRange(null, b, EndpointType.TREE_START, EndpointType.RANGE_EXCLUSIVE), new Range(new byte[0], b.pack())), Pair.of(new TupleRange(a, null, EndpointType.RANGE_INCLUSIVE, EndpointType.TREE_END), new Range(a.pack(), new byte[] { (byte) 0xff })), Pair.of(new TupleRange(a, b, EndpointType.CONTINUATION, EndpointType.RANGE_EXCLUSIVE), new Range(ByteArrayUtil.join(a.pack(), new byte[] { 0x00 }), b.pack())), Pair.of(new TupleRange(a, b, EndpointType.RANGE_INCLUSIVE, EndpointType.CONTINUATION), new Range(a.pack(), b.pack())), Pair.of(TupleRange.prefixedBy("a"), new Range(new byte[] { 0x02, (byte) 'a' }, new byte[] { 0x02, (byte) 'b' })), Pair.of(new TupleRange(Tuple.from("apple"), a, EndpointType.CONTINUATION, EndpointType.PREFIX_STRING), new Range(ByteArrayUtil.join(Tuple.from("apple").pack(), new byte[] { 0x00 }), new byte[] { 0x02, (byte) 'b' })), Pair.of(new TupleRange(a, Tuple.from("apple"), EndpointType.PREFIX_STRING, EndpointType.CONTINUATION), new Range(new byte[] { 0x02, (byte) 'a' }, Tuple.from("apple").pack())));
testCases.forEach(pair -> {
assertEquals(pair.getRight(), pair.getLeft().toRange());
TupleRange expectedRange = pair.getLeft().prepend(PREFIX_TUPLE);
assertEquals(expectedRange.toRange(), pair.getLeft().toRange(PREFIX_SUBSPACE));
});
}
Aggregations