Search in sources :

Example 41 with Tuple

use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.

the class BunchedTupleSerializer method deserializeEntries.

@Nonnull
@Override
public List<Map.Entry<Tuple, Tuple>> deserializeEntries(@Nonnull Tuple key, @Nonnull byte[] data) {
    if (!ByteArrayUtil.startsWith(data, PREFIX)) {
        throw new BunchedSerializationException("data did not begin with expected prefix").setData(data);
    }
    Tuple entriesTuple = Tuple.fromBytes(data, PREFIX.length, data.length - PREFIX.length);
    if (entriesTuple.size() % 2 == 0) {
        throw new BunchedSerializationException("deserialized entry list had incorrect number of elements");
    }
    Iterator<Object> tupleIterator = entriesTuple.iterator();
    List<Map.Entry<Tuple, Tuple>> entryList = new ArrayList<>(entriesTuple.size() / 2);
    boolean first = true;
    while (tupleIterator.hasNext()) {
        Tuple entryKey = null;
        Tuple entryValue = null;
        try {
            if (first) {
                entryKey = key;
                first = false;
            } else {
                entryKey = toTuple(tupleIterator.next());
            }
            entryValue = toTuple(tupleIterator.next());
            entryList.add(new AbstractMap.SimpleImmutableEntry<>(entryKey, entryValue));
        } catch (ClassCastException e) {
            throw new BunchedSerializationException("deserialized entry list did not consist only of tuples", e);
        }
    }
    return entryList;
}
Also used : AbstractMap(java.util.AbstractMap) ArrayList(java.util.ArrayList) Tuple(com.apple.foundationdb.tuple.Tuple) Nonnull(javax.annotation.Nonnull)

Example 42 with Tuple

use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.

the class BunchedTupleSerializerTest method serializeKey.

@Test
public void serializeKey() {
    List<Tuple> sortedTuples = TEST_TUPLES.stream().sorted().collect(Collectors.toList());
    List<Tuple> sortedKeys = TEST_TUPLES.stream().map(serializer::serializeKey).sorted(ByteArrayUtil::compareUnsigned).map(serializer::deserializeKey).collect(Collectors.toList());
    assertEquals(sortedTuples, sortedKeys);
    // Add a subspace and make sure unpacking by length works.
    Subspace subspace = new Subspace(Tuple.from("fake", "subspace"));
    List<Tuple> prefixedTuples = TEST_TUPLES.stream().map(serializer::serializeKey).map(b -> ByteArrayUtil.join(subspace.getKey(), b)).map(data -> serializer.deserializeKey(data, subspace.getKey().length)).collect(Collectors.toList());
    assertEquals(TEST_TUPLES, prefixedTuples);
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ByteArrayUtil(com.apple.foundationdb.tuple.ByteArrayUtil) Arrays(java.util.Arrays) Collectors(java.util.stream.Collectors) Subspace(com.apple.foundationdb.subspace.Subspace) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) Tuple(com.apple.foundationdb.tuple.Tuple) AbstractMap(java.util.AbstractMap) List(java.util.List) Map(java.util.Map) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Collections(java.util.Collections) Subspace(com.apple.foundationdb.subspace.Subspace) Tuple(com.apple.foundationdb.tuple.Tuple) ByteArrayUtil(com.apple.foundationdb.tuple.ByteArrayUtil) Test(org.junit.jupiter.api.Test)

Example 43 with Tuple

use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.

the class TupleRangeTest method illegalRanges.

@Test
public void illegalRanges() {
    final Tuple a = Tuple.from("a");
    final Tuple b = Tuple.from("b");
    List<TupleRange> testRanges = Arrays.asList(new TupleRange(a, b, EndpointType.RANGE_INCLUSIVE, EndpointType.TREE_START), new TupleRange(a, b, EndpointType.TREE_END, EndpointType.RANGE_EXCLUSIVE), new TupleRange(null, b, EndpointType.RANGE_EXCLUSIVE, EndpointType.RANGE_EXCLUSIVE), new TupleRange(a, null, EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_INCLUSIVE), new TupleRange(null, a, EndpointType.PREFIX_STRING, EndpointType.PREFIX_STRING), new TupleRange(a, b, EndpointType.PREFIX_STRING, EndpointType.PREFIX_STRING), new TupleRange(a, a, EndpointType.RANGE_INCLUSIVE, EndpointType.PREFIX_STRING), new TupleRange(a, a, EndpointType.PREFIX_STRING, EndpointType.RANGE_INCLUSIVE), new TupleRange(a, b, EndpointType.CONTINUATION, EndpointType.PREFIX_STRING), new TupleRange(a, b, EndpointType.PREFIX_STRING, EndpointType.CONTINUATION), new TupleRange(Tuple.from(1066), Tuple.from(1066), EndpointType.PREFIX_STRING, EndpointType.PREFIX_STRING));
    testRanges.forEach(range -> assertThrows(RecordCoreException.class, range::toRange, () -> String.format("range %s should have thrown an error", range)));
}
Also used : Tuple(com.apple.foundationdb.tuple.Tuple) Test(org.junit.jupiter.api.Test)

Example 44 with Tuple

use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.

the class TupleTypeUtilTest method listEquivalence.

@Test
public void listEquivalence() {
    List<Object> packableValues = VALUES.stream().filter(value -> {
        if (value instanceof FDBRecordVersion) {
            return ((FDBRecordVersion) value).isComplete();
        } else if (value instanceof Versionstamp) {
            return ((Versionstamp) value).isComplete();
        } else {
            return true;
        }
    }).collect(Collectors.toList());
    List<Object> equivalentList = TupleTypeUtil.toTupleEquivalentList(packableValues);
    Tuple tupleWithNormalizing = Tuple.fromList(TupleTypeUtil.toTupleAppropriateList(equivalentList));
    Tuple tupleWithoutNormalizing = Tuple.fromList(TupleTypeUtil.toTupleAppropriateList(packableValues));
    assertThat(TupleHelpers.equals(tupleWithNormalizing, tupleWithoutNormalizing), is(true));
    assertArrayEquals(tupleWithoutNormalizing.pack(), tupleWithNormalizing.pack());
    Tuple deserializedTuple = Tuple.fromBytes(tupleWithNormalizing.pack());
    assertThat(TupleHelpers.equals(tupleWithNormalizing, deserializedTuple), is(true));
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) FDBRecordVersion(com.apple.foundationdb.record.provider.foundationdb.FDBRecordVersion) FDBTestBase(com.apple.foundationdb.record.provider.foundationdb.FDBTestBase) Tuple(com.apple.foundationdb.tuple.Tuple) BigInteger(java.math.BigInteger) Tag(org.junit.jupiter.api.Tag) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) MethodSource(org.junit.jupiter.params.provider.MethodSource) Charsets(com.google.common.base.Charsets) Versionstamp(com.apple.foundationdb.tuple.Versionstamp) Tags(com.apple.test.Tags) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Stream(java.util.stream.Stream) TupleHelpers(com.apple.foundationdb.tuple.TupleHelpers) TestRecordsEnumProto(com.apple.foundationdb.record.TestRecordsEnumProto) Matchers.is(org.hamcrest.Matchers.is) Versionstamp(com.apple.foundationdb.tuple.Versionstamp) FDBRecordVersion(com.apple.foundationdb.record.provider.foundationdb.FDBRecordVersion) Tuple(com.apple.foundationdb.tuple.Tuple) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 45 with Tuple

use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.

the class FDBRecordStoreIndexTest method scanIndexWithValue.

/**
 * Verify that explicit (i.e. bypassing the planner) index scans work .
 */
@Test
public void scanIndexWithValue() throws Exception {
    RecordMetaDataHook hook = metaData -> {
        metaData.removeIndex("MySimpleRecord$num_value_unique");
        metaData.addIndex("MySimpleRecord", new Index("multi_index_value", Key.Expressions.field("num_value_unique"), Key.Expressions.field("num_value_2"), IndexTypes.VALUE, IndexOptions.UNIQUE_OPTIONS));
    };
    complexQuerySetup(hook);
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, hook);
        int i = 0;
        try (RecordCursorIterator<IndexEntry> cursor = recordStore.scanIndex(recordStore.getRecordMetaData().getIndex("multi_index_value"), IndexScanType.BY_VALUE, new TupleRange(Tuple.from(900L), Tuple.from(950L), EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_INCLUSIVE), null, ScanProperties.FORWARD_SCAN).asIterator()) {
            while (cursor.hasNext()) {
                IndexEntry tuples = cursor.next();
                Tuple key = tuples.getKey();
                Tuple value = tuples.getValue();
                assertEquals(2, key.size());
                assertEquals(1, value.size());
                assertTrue(key.getLong(0) >= 900);
                assertTrue(key.getLong(0) <= 950);
                assertTrue(value.getLong(0) == (999 - i) % 3);
                i++;
            }
        }
        assertEquals(50, i);
        assertDiscardedNone(context);
    }
}
Also used : IndexEntry(com.apple.foundationdb.record.IndexEntry) Arrays(java.util.Arrays) Descriptors(com.google.protobuf.Descriptors) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Function(java.util.function.Function) Key(com.apple.foundationdb.record.metadata.Key) TestHelpers.assertDiscardedNone(com.apple.foundationdb.record.TestHelpers.assertDiscardedNone) IndexScanType(com.apple.foundationdb.record.IndexScanType) Tuple(com.apple.foundationdb.tuple.Tuple) TestHelpers(com.apple.foundationdb.record.TestHelpers) EndpointType(com.apple.foundationdb.record.EndpointType) ScanProperties(com.apple.foundationdb.record.ScanProperties) TestHelpers.assertDiscardedAtLeast(com.apple.foundationdb.record.TestHelpers.assertDiscardedAtLeast) RecordCursorIterator(com.apple.foundationdb.record.RecordCursorIterator) Tag(org.junit.jupiter.api.Tag) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Nullable(javax.annotation.Nullable) FDBRecordStoreBase(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreBase) TestRecords4Proto(com.apple.foundationdb.record.TestRecords4Proto) Query(com.apple.foundationdb.record.query.expressions.Query) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) IndexOptions(com.apple.foundationdb.record.metadata.IndexOptions) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) Tags(com.apple.test.Tags) TupleRange(com.apple.foundationdb.record.TupleRange) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) Index(com.apple.foundationdb.record.metadata.Index) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Message(com.google.protobuf.Message) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) IndexEntry(com.apple.foundationdb.record.IndexEntry) Index(com.apple.foundationdb.record.metadata.Index) TupleRange(com.apple.foundationdb.record.TupleRange) Tuple(com.apple.foundationdb.tuple.Tuple) 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