Search in sources :

Example 31 with KeyValue

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"));
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) KeyValue(com.apple.foundationdb.KeyValue) Transaction(com.apple.foundationdb.Transaction) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) List(java.util.List) ArrayList(java.util.ArrayList) Tuple(com.apple.foundationdb.tuple.Tuple) Test(org.junit.jupiter.api.Test) MetaDataProtoTest(com.apple.foundationdb.record.metadata.MetaDataProtoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 32 with KeyValue

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;
}
Also used : KeyValue(com.apple.foundationdb.KeyValue) ArrayList(java.util.ArrayList) Subspace(com.apple.foundationdb.subspace.Subspace) Tuple(com.apple.foundationdb.tuple.Tuple)

Example 33 with KeyValue

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;
    });
}
Also used : KeyValue(com.apple.foundationdb.KeyValue) ScanProperties(com.apple.foundationdb.record.ScanProperties) Test(org.junit.jupiter.api.Test)

Example 34 with KeyValue

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;
    });
}
Also used : KeyValue(com.apple.foundationdb.KeyValue) CursorLimitManager(com.apple.foundationdb.record.cursors.CursorLimitManager) Test(org.junit.jupiter.api.Test)

Example 35 with KeyValue

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;
    });
}
Also used : KeyValue(com.apple.foundationdb.KeyValue) ScanProperties(com.apple.foundationdb.record.ScanProperties) Test(org.junit.jupiter.api.Test)

Aggregations

KeyValue (com.apple.foundationdb.KeyValue)51 Test (org.junit.jupiter.api.Test)23 Subspace (com.apple.foundationdb.subspace.Subspace)22 Nonnull (javax.annotation.Nonnull)22 CompletableFuture (java.util.concurrent.CompletableFuture)19 List (java.util.List)18 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)16 ArrayList (java.util.ArrayList)16 Transaction (com.apple.foundationdb.Transaction)15 ScanProperties (com.apple.foundationdb.record.ScanProperties)15 Tuple (com.apple.foundationdb.tuple.Tuple)14 Map (java.util.Map)14 Collectors (java.util.stream.Collectors)12 Nullable (javax.annotation.Nullable)12 ByteArrayUtil (com.apple.foundationdb.tuple.ByteArrayUtil)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 AsyncIterator (com.apple.foundationdb.async.AsyncIterator)10 ExecuteProperties (com.apple.foundationdb.record.ExecuteProperties)10 Collections (java.util.Collections)10 ReadTransaction (com.apple.foundationdb.ReadTransaction)9