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;
}
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);
}
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)));
}
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));
}
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);
}
}
Aggregations