use of com.apple.foundationdb.KeyValue in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method historyCompat.
@Test
public void historyCompat() {
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaDataProto.MetaData.Builder metaData = RecordMetaDataProto.MetaData.newBuilder();
metaData.setRecords(TestRecords1Proto.getDescriptor().toProto());
metaData.addRecordTypesBuilder().setName("MySimpleRecord").getPrimaryKeyBuilder().getFieldBuilder().setFieldName("rec_no").setFanType(RecordMetaDataProto.Field.FanType.SCALAR);
metaData.addRecordTypesBuilder().setName("MyOtherRecord").getPrimaryKeyBuilder().getFieldBuilder().setFieldName("rec_no").setFanType(RecordMetaDataProto.Field.FanType.SCALAR);
metaData.setVersion(101);
metaDataStore.saveRecordMetaData(metaData.build());
{
// Adjust to look like old format store by moving everything under CURRENT_KEY up under root.
Transaction tr = context.ensureActive();
List<KeyValue> kvs = context.asyncToSync(FDBStoreTimer.Waits.WAIT_LOAD_META_DATA, tr.getRange(metaDataStore.getSubspace().range(FDBMetaDataStore.CURRENT_KEY)).asList());
context.ensureActive().clear(metaDataStore.getSubspace().range());
for (KeyValue kv : kvs) {
Tuple tuple = Tuple.fromBytes(kv.getKey());
List<Object> items = tuple.getItems();
assertEquals(null, items.remove(items.size() - 2));
tuple = Tuple.fromList(items);
tr.set(tuple.pack(), kv.getValue());
}
}
context.commit();
}
RecordMetaData before;
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
before = metaDataStore.getRecordMetaData();
context.commit();
}
assertNotNull(before.getRecordType("MySimpleRecord"));
assertFalse(before.hasIndex("MyIndex"));
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaDataProto.MetaData.Builder metaData = RecordMetaDataProto.MetaData.newBuilder();
metaData.setRecords(TestRecords1Proto.getDescriptor().toProto());
metaData.addRecordTypesBuilder().setName("MySimpleRecord").getPrimaryKeyBuilder().getFieldBuilder().setFieldName("rec_no").setFanType(RecordMetaDataProto.Field.FanType.SCALAR);
metaData.addIndexesBuilder().setName("MyIndex").addRecordType("MySimpleRecord").setAddedVersion(102).setLastModifiedVersion(102).getRootExpressionBuilder().getFieldBuilder().setFieldName("num_value_2").setFanType(RecordMetaDataProto.Field.FanType.SCALAR);
metaData.addRecordTypesBuilder().setName("MyOtherRecord").getPrimaryKeyBuilder().getFieldBuilder().setFieldName("rec_no").setFanType(RecordMetaDataProto.Field.FanType.SCALAR);
metaData.setVersion(102);
metaDataStore.saveRecordMetaData(metaData.build());
context.commit();
}
RecordMetaData after;
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
after = metaDataStore.getRecordMetaData();
context.commit();
}
assertNotNull(after.getRecordType("MySimpleRecord"));
assertTrue(after.hasIndex("MyIndex"));
RecordMetaData beforeAgain;
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
beforeAgain = context.asyncToSync(FDBStoreTimer.Waits.WAIT_LOAD_META_DATA, metaDataStore.loadVersion(before.getVersion()));
context.commit();
}
assertEquals(before.getVersion(), beforeAgain.getVersion());
assertNotNull(beforeAgain.getRecordType("MySimpleRecord"));
assertFalse(beforeAgain.hasIndex("MyIndex"));
}
use of com.apple.foundationdb.KeyValue in project fdb-record-layer by FoundationDB.
the class SplitHelperTest method saveSuccessfully.
private SplitHelper.SizeInfo saveSuccessfully(@Nonnull FDBRecordContext context, @Nonnull Tuple key, byte[] serialized, @Nullable FDBRecordVersion version, boolean splitLongRecords, boolean omitUnsplitSuffix, @Nullable FDBStoredSizes previousSizeInfo) {
final SplitHelper.SizeInfo sizeInfo = new SplitHelper.SizeInfo();
SplitHelper.saveWithSplit(context, subspace, key, serialized, version, splitLongRecords, omitUnsplitSuffix, previousSizeInfo != null, previousSizeInfo, sizeInfo);
int dataKeyCount = (serialized.length - 1) / SplitHelper.SPLIT_RECORD_SIZE + 1;
boolean isSplit = dataKeyCount > 1;
int keyCount = dataKeyCount;
if (version != null) {
keyCount += 1;
}
int keySize = (subspace.pack().length + key.pack().length) * keyCount;
assertEquals(isSplit, sizeInfo.isSplit());
assertEquals(keyCount, sizeInfo.getKeyCount());
if (!omitUnsplitSuffix || splitLongRecords) {
// Add in the the counters the split points.
if (!isSplit) {
// As 0 requires 1 byte when Tuple packed
keySize += 1;
} else {
// As each split point is two bytes when tuple packed
keySize += dataKeyCount * 2;
}
}
if (version != null) {
keySize += 2;
}
int valueSize = serialized.length + (version != null ? 1 + FDBRecordVersion.VERSION_LENGTH : 0);
assertEquals(keySize, sizeInfo.getKeySize());
assertEquals(valueSize, sizeInfo.getValueSize());
assertEquals(version != null, sizeInfo.isVersionedInline());
final Subspace keySubspace = subspace.subspace(key);
RecordCursorIterator<KeyValue> kvCursor = KeyValueCursor.Builder.withSubspace(keySubspace).setContext(context).setScanProperties(ScanProperties.FORWARD_SCAN).build().asIterator();
List<Long> indexes = new ArrayList<>(keyCount);
byte[] versionBytes = null;
byte[] valueBytes = null;
while (kvCursor.hasNext()) {
KeyValue kv = kvCursor.next();
Tuple suffix = keySubspace.unpack(kv.getKey());
if (omitUnsplitSuffix) {
assertThat(suffix.isEmpty(), is(true));
valueBytes = kv.getValue();
} else {
Long index = suffix.getLong(0);
indexes.add(index);
if (index == SplitHelper.RECORD_VERSION) {
versionBytes = kv.getValue();
} else {
if (valueBytes == null) {
valueBytes = kv.getValue();
} else {
valueBytes = ByteArrayUtil.join(valueBytes, kv.getValue());
}
}
}
}
List<Long> expectedIndexes;
if (omitUnsplitSuffix) {
expectedIndexes = Collections.emptyList();
} else {
expectedIndexes = new ArrayList<>(keyCount);
if (version != null && version.isComplete()) {
expectedIndexes.add(SplitHelper.RECORD_VERSION);
}
if (!isSplit) {
expectedIndexes.add(SplitHelper.UNSPLIT_RECORD);
} else {
LongStream.range(SplitHelper.START_SPLIT_RECORD, SplitHelper.START_SPLIT_RECORD + dataKeyCount).forEach(expectedIndexes::add);
}
}
assertEquals(expectedIndexes, indexes);
assertNotNull(valueBytes);
assertArrayEquals(serialized, valueBytes);
if (version != null) {
if (!version.isComplete()) {
assertNull(versionBytes);
} else {
assertNotNull(versionBytes);
assertEquals(version, FDBRecordVersion.fromVersionstamp(Tuple.fromBytes(versionBytes).getVersionstamp(0)));
}
} else {
assertNull(versionBytes);
}
return sizeInfo;
}
use of com.apple.foundationdb.KeyValue in project fdb-record-layer by FoundationDB.
the class KeyValueCursorTest method beginsWith.
@Test
public void beginsWith() {
fdb.run(context -> {
KeyValueCursor cursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setRange(TupleRange.allOf(Tuple.from(3))).setContinuation(null).setScanProperties(ScanProperties.FORWARD_SCAN).build();
for (int j = 0; j < 5; j++) {
KeyValue kv = cursor.getNext().get();
assertArrayEquals(subspace.pack(Tuple.from(3, j)), kv.getKey());
assertArrayEquals(Tuple.from(3, j).pack(), kv.getValue());
}
assertThat(cursor.getNext().hasNext(), is(false));
cursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setRange(TupleRange.allOf(Tuple.from(3))).setContinuation(null).setScanProperties(new ScanProperties(ExecuteProperties.newBuilder().setReturnedRowLimit(2).build())).build();
assertEquals(2, (int) cursor.getCount().join());
cursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setRange(TupleRange.allOf(Tuple.from(3))).setContinuation(cursor.getNext().getContinuation().toBytes()).setScanProperties(new ScanProperties(ExecuteProperties.newBuilder().setReturnedRowLimit(3).build())).build();
assertEquals(3, (int) cursor.getCount().join());
return null;
});
}
use of com.apple.foundationdb.KeyValue in project fdb-record-layer by FoundationDB.
the class KeyValueCursorTest method emptyScanSplit.
@Test
public void emptyScanSplit() {
fdb.run(context -> {
RecordCursor<KeyValue> kvCursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setRange(TupleRange.allOf(Tuple.from(9))).setContinuation(null).setScanProperties(ScanProperties.FORWARD_SCAN).build();
RecordCursor<?> cursor = new SplitHelper.KeyValueUnsplitter(context, subspace, kvCursor, false, null, false, new CursorLimitManager(context, ScanProperties.FORWARD_SCAN));
RecordCursorResult<?> result = cursor.getNext();
assertFalse(result.hasNext());
assertEquals(RecordCursor.NoNextReason.SOURCE_EXHAUSTED, result.getNoNextReason());
assertTrue(result.getContinuation().isEnd());
return null;
});
}
use of com.apple.foundationdb.KeyValue in project fdb-record-layer by FoundationDB.
the class KeyValueCursorTest method all.
@Test
public void all() {
fdb.run(context -> {
KeyValueCursor cursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setRange(TupleRange.ALL).setContinuation(null).setScanProperties(ScanProperties.FORWARD_SCAN).build();
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
KeyValue kv = cursor.getNext().get();
assertArrayEquals(subspace.pack(Tuple.from(i, j)), kv.getKey());
assertArrayEquals(Tuple.from(i, j).pack(), kv.getValue());
}
}
assertThat(cursor.getNext().hasNext(), is(false));
cursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setRange(TupleRange.ALL).setContinuation(null).setScanProperties(new ScanProperties(ExecuteProperties.newBuilder().setReturnedRowLimit(10).build())).build();
assertEquals(10, (int) cursor.getCount().join());
cursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setRange(TupleRange.ALL).setContinuation(cursor.getNext().getContinuation().toBytes()).setScanProperties(ScanProperties.FORWARD_SCAN).build();
assertEquals(15, (int) cursor.getCount().join());
return null;
});
}
Aggregations