Search in sources :

Example 46 with Transaction

use of com.apple.foundationdb.Transaction 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 47 with Transaction

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

the class SplitHelperTest method writeDummyRecord.

@Nonnull
private FDBStoredSizes writeDummyRecord(@Nonnull FDBRecordContext context, @Nonnull Tuple key, @Nullable FDBRecordVersion version, int splits, boolean omitUnsplitSuffix) {
    final Transaction tr = context.ensureActive();
    SplitHelper.SizeInfo sizeInfo = new SplitHelper.SizeInfo();
    if (version != null) {
        assertThat(omitUnsplitSuffix, is(false));
        sizeInfo.setVersionedInline(true);
        byte[] keyBytes = subspace.pack(key.add(SplitHelper.RECORD_VERSION));
        byte[] valueBytes = SplitHelper.packVersion(version);
        tr.set(keyBytes, valueBytes);
        sizeInfo.add(keyBytes, valueBytes);
    }
    if (splits == 1) {
        if (omitUnsplitSuffix) {
            byte[] keyBytes = subspace.pack(key);
            sizeInfo.add(keyBytes, SHORT_STRING);
            tr.set(keyBytes, SHORT_STRING);
        } else {
            byte[] keyBytes = subspace.pack(key.add(SplitHelper.UNSPLIT_RECORD));
            sizeInfo.add(keyBytes, SHORT_STRING);
            tr.set(keyBytes, SHORT_STRING);
        }
        sizeInfo.setSplit(false);
    } else {
        for (int i = 0; i < splits; i++) {
            byte[] keyBytes = subspace.pack(key.add(SplitHelper.START_SPLIT_RECORD + i));
            sizeInfo.add(keyBytes, SHORT_STRING);
            tr.set(keyBytes, SHORT_STRING);
        }
        sizeInfo.setSplit(true);
    }
    return sizeInfo;
}
Also used : Transaction(com.apple.foundationdb.Transaction) ReadTransaction(com.apple.foundationdb.ReadTransaction) Nonnull(javax.annotation.Nonnull)

Example 48 with Transaction

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

the class KeySpaceDirectoryTest method testListDoesNotGoTooDeep.

@Test
public void testListDoesNotGoTooDeep() throws Exception {
    KeySpace root = new KeySpace(new KeySpaceDirectory("a", KeyType.LONG, random.nextLong()).addSubdirectory(new DirectoryLayerDirectory("b").addSubdirectory(new KeySpaceDirectory("c", KeyType.STRING).addSubdirectory(new KeySpaceDirectory("d", KeyType.BYTES).addSubdirectory(new KeySpaceDirectory("e", KeyType.LONG))))));
    final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    try (FDBRecordContext context = database.openContext()) {
        Transaction tr = context.ensureActive();
        for (int i = 0; i < 5; i++) {
            tr.set(root.path("a").add("b", "foo_" + i).add("c", "hi_" + i).add("d", new byte[] { (byte) i }).toTuple(context).pack(), Tuple.from(i).pack());
        }
        context.commit();
    }
    // remainder on the "b" path entry.
    try (FDBRecordContext context = database.openContext()) {
        List<ResolvedKeySpacePath> paths;
        // Check listing from the root
        paths = root.listDirectory(context, "a");
        assertThat("Number of paths in 'a'", paths.size(), is(1));
        assertThat("Value of subdirectory 'a'", paths.get(0).getLogicalValue(), is(root.getDirectory("a").getValue()));
        assertThat("Remainder size of 'a'", paths.get(0).getRemainder().size(), is(3));
        // List from "b"
        paths = root.path("a").listSubdirectory(context, "b");
        assertThat("Number of paths in 'b'", paths.size(), is(5));
        for (ResolvedKeySpacePath path : paths) {
            assertThat("Listing of 'b' directory", path.getDirectoryName(), is("b"));
            Tuple remainder = path.getRemainder();
            assertThat("Remainder of 'b'", remainder.size(), is(2));
            assertThat("Remainder of 'b', first tuple value", remainder.getString(0), startsWith("hi_"));
            assertThat("Remainder of 'b', second tuple value", remainder.getBytes(1), instanceOf(new byte[0].getClass()));
        }
        // List from "c"
        paths = root.path("a").add("b", "foo_0").listSubdirectory(context, "c");
        assertThat("Number of paths in 'c'", paths.size(), is(1));
        for (ResolvedKeySpacePath path : paths) {
            assertThat("Listing of 'c' directory", path.getDirectoryName(), is("c"));
            final Tuple remainder = path.getRemainder();
            assertThat("Remainder of 'c'", remainder.size(), is(1));
            assertThat("Remainder of 'c', first tuple value", remainder.getBytes(0), instanceOf(new byte[0].getClass()));
        }
        // List from "d"
        paths = root.path("a").add("b", "foo_0").add("c", "hi_0").listSubdirectory(context, "d");
        assertThat("Number of paths in 'd'", paths.size(), is(1));
        ResolvedKeySpacePath path = paths.get(0);
        assertThat("Remainder of 'd'", path.getRemainder(), is((Tuple) null));
        // List from "e" (which has no data)
        paths = root.path("a").add("b", "foo_0").add("c", "hi_0").add("d", new byte[] { 0x00 }).listSubdirectory(context, "e");
        assertThat("Number of paths in 'e'", paths.size(), is(0));
    }
}
Also used : Transaction(com.apple.foundationdb.Transaction) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Tuple(com.apple.foundationdb.tuple.Tuple) Test(org.junit.jupiter.api.Test)

Example 49 with Transaction

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

the class KeySpaceDirectoryTest method testListDirectoryLayer.

@Test
public void testListDirectoryLayer() throws Exception {
    KeySpace root = new KeySpace(new KeySpaceDirectory("a", KeyType.LONG, random.nextLong()).addSubdirectory(new DirectoryLayerDirectory("b")).addSubdirectory(new KeySpaceDirectory("c", KeyType.STRING, "c").addSubdirectory(new DirectoryLayerDirectory("d", "d")).addSubdirectory(new DirectoryLayerDirectory("e", "e")).addSubdirectory(new DirectoryLayerDirectory("f", "f"))));
    final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    try (FDBRecordContext context = database.openContext()) {
        Transaction tr = context.ensureActive();
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 5; j++) {
                Tuple key = root.path("a").add("b", "value_" + i).toTuple(context).add(i).add(j);
                tr.set(key.pack(), Tuple.from(i).pack());
            }
        }
        for (int i = 0; i < 5; i++) {
            tr.set(root.path("a").add("c").add("d").toTuple(context).add(i).pack(), Tuple.from(i).pack());
            tr.set(root.path("a").add("c").add("e").toTuple(context).add(i).pack(), Tuple.from(i).pack());
            tr.set(root.path("a").add("c").add("f").toTuple(context).add(i).pack(), Tuple.from(i).pack());
        }
        context.commit();
    }
    try (FDBRecordContext context = database.openContext()) {
        List<ResolvedKeySpacePath> paths = root.path("a").listSubdirectory(context, "b");
        assertEquals(10, paths.size());
        for (ResolvedKeySpacePath path : paths) {
            // The first part of the remainder was the index
            final long index = path.getRemainder().getLong(0);
            // We should always get the "first" key for the value in the directory
            assertEquals(0, path.getRemainder().getLong(1));
            assertTrue(index >= 0 && index < 10);
            assertEquals("a", path.getParent().getDirectoryName());
            assertEquals(root.getDirectory("a").getValue(), path.getParent().getLogicalValue());
            assertEquals("value_" + index, path.getLogicalValue());
        }
        for (String subdir : ImmutableList.of("d", "e", "f")) {
            paths = root.path("a").add("c").listSubdirectory(context, subdir);
            assertEquals(1, paths.size());
            assertEquals(subdir, paths.get(0).getLogicalValue());
            assertEquals(0L, paths.get(0).getRemainder().getLong(0));
            assertEquals("c", paths.get(0).getParent().getDirectoryName());
            assertEquals("a", paths.get(0).getParent().getParent().getDirectoryName());
        }
    }
}
Also used : Transaction(com.apple.foundationdb.Transaction) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Tuple(com.apple.foundationdb.tuple.Tuple) Test(org.junit.jupiter.api.Test)

Example 50 with Transaction

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

the class KeySpaceDirectoryTest method testListAnyValue.

@Test
public void testListAnyValue() throws Exception {
    // Create a root directory called "a" with subdirs of every type (no constants for now)
    Long rootValue = random.nextLong();
    KeySpaceDirectory dirA = new KeySpaceDirectory("a", KeyType.LONG, rootValue);
    for (KeyTypeValue kv : valueOfEveryType) {
        dirA.addSubdirectory(new KeySpaceDirectory(kv.keyType.toString(), kv.keyType));
    }
    KeySpace root = new KeySpace(dirA);
    final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    final Map<KeyType, List<Tuple>> valuesForType = new HashMap<>();
    // Create an entry in the keyspace with a row for every type that we support
    try (FDBRecordContext context = database.openContext()) {
        Transaction tr = context.ensureActive();
        for (KeyTypeValue kv : valueOfEveryType) {
            List<Tuple> values = new ArrayList<>();
            for (int i = 0; i < 5; i++) {
                Object value = kv.generator.get();
                Tuple tupleValue = Tuple.from(value);
                if (!values.contains(tupleValue)) {
                    values.add(tupleValue);
                    // final results.
                    for (int j = 0; j < 5; j++) {
                        tr.set(Tuple.from(rootValue, value, j).pack(), Tuple.from(i).pack());
                    }
                }
            }
            valuesForType.put(kv.keyType, values);
        }
        context.commit();
    }
    try (FDBRecordContext context = database.openContext()) {
        for (KeyTypeValue kv : valueOfEveryType) {
            if (kv.keyType != KeyType.NULL) {
                List<Tuple> values = valuesForType.get(kv.keyType);
                for (Pair<ValueRange<Object>, List<Tuple>> testCase : listRangeTestCases(values)) {
                    testListRange(testCase.getLeft(), testCase.getRight(), context, root, kv.keyType);
                }
            }
        }
    }
}
Also used : KeyType(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory.KeyType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) ValueRange(com.apple.foundationdb.record.ValueRange) Transaction(com.apple.foundationdb.Transaction) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) Tuple(com.apple.foundationdb.tuple.Tuple) Test(org.junit.jupiter.api.Test)

Aggregations

Transaction (com.apple.foundationdb.Transaction)84 ReadTransaction (com.apple.foundationdb.ReadTransaction)34 Tuple (com.apple.foundationdb.tuple.Tuple)34 Test (org.junit.jupiter.api.Test)33 Nonnull (javax.annotation.Nonnull)28 ArrayList (java.util.ArrayList)26 List (java.util.List)26 CompletableFuture (java.util.concurrent.CompletableFuture)26 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)22 Subspace (com.apple.foundationdb.subspace.Subspace)21 Collectors (java.util.stream.Collectors)19 Nullable (javax.annotation.Nullable)19 KeyValue (com.apple.foundationdb.KeyValue)18 Map (java.util.Map)18 KeyValueLogMessage (com.apple.foundationdb.record.logging.KeyValueLogMessage)17 Range (com.apple.foundationdb.Range)16 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)16 AtomicReference (java.util.concurrent.atomic.AtomicReference)16 Collections (java.util.Collections)15 RecordCursor (com.apple.foundationdb.record.RecordCursor)14