Search in sources :

Example 86 with Tuple

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;
}
Also used : Tuple(com.apple.foundationdb.tuple.Tuple)

Example 87 with Tuple

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;
}
Also used : Tuple(com.apple.foundationdb.tuple.Tuple)

Example 88 with Tuple

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();
    }
}
Also used : Tuple(com.apple.foundationdb.tuple.Tuple)

Example 89 with Tuple

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);
    }
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Query(com.apple.foundationdb.record.query.expressions.Query) Tags(com.apple.test.Tags) Matchers(org.hamcrest.Matchers) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) TestRecordsMapsProto(com.apple.foundationdb.record.TestRecordsMapsProto) Key(com.apple.foundationdb.record.metadata.Key) PlanMatchers(com.apple.foundationdb.record.query.plan.match.PlanMatchers) Test(org.junit.jupiter.api.Test) Tuple(com.apple.foundationdb.tuple.Tuple) List(java.util.List) MatcherAssert(org.hamcrest.MatcherAssert) PlanMatchers.indexName(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexName) Message(com.google.protobuf.Message) Tag(org.junit.jupiter.api.Tag) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Collections(java.util.Collections) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Tuple(com.apple.foundationdb.tuple.Tuple) Test(org.junit.jupiter.api.Test)

Example 90 with Tuple

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));
    });
}
Also used : Range(com.apple.foundationdb.Range) Tuple(com.apple.foundationdb.tuple.Tuple) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.jupiter.api.Test)

Aggregations

Tuple (com.apple.foundationdb.tuple.Tuple)163 Nonnull (javax.annotation.Nonnull)80 List (java.util.List)66 Test (org.junit.jupiter.api.Test)66 TupleRange (com.apple.foundationdb.record.TupleRange)62 ArrayList (java.util.ArrayList)57 Message (com.google.protobuf.Message)56 Nullable (javax.annotation.Nullable)50 IndexEntry (com.apple.foundationdb.record.IndexEntry)48 Index (com.apple.foundationdb.record.metadata.Index)47 Subspace (com.apple.foundationdb.subspace.Subspace)47 CompletableFuture (java.util.concurrent.CompletableFuture)46 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)45 ScanProperties (com.apple.foundationdb.record.ScanProperties)43 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)42 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)41 Collectors (java.util.stream.Collectors)41 Collections (java.util.Collections)40 Transaction (com.apple.foundationdb.Transaction)38 RecordCursor (com.apple.foundationdb.record.RecordCursor)38