Search in sources :

Example 31 with Identifier

use of com.cinchapi.concourse.server.model.Identifier in project concourse by cinchapi.

the class ByteableCollectionsTest method testStreamIteratorThreadSafety.

@Test
public void testStreamIteratorThreadSafety() throws InterruptedException, ExecutionException {
    List<Identifier> values = Lists.newArrayList();
    int count = 10;
    for (int i = 0; i < count; ++i) {
        values.add(Identifier.of(i));
    }
    Path file = Paths.get(TestData.getTemporaryTestFile());
    ByteBuffer bytes = ByteableCollections.toByteBuffer(values);
    FileSystem.writeBytes(bytes, file.toString());
    FileChannel channel = FileSystem.getFileChannel(file);
    ExecutorService executor = Executors.newCachedThreadPool();
    List<Future<?>> futures = Lists.newArrayList();
    for (int i = 0; i < 10; ++i) {
        futures.add(executor.submit(() -> {
            List<Identifier> actual = Lists.newArrayList();
            Iterator<ByteBuffer> it = ByteableCollections.stream(channel, 0, FileSystem.getFileSize(file.toString()), 20);
            while (it.hasNext()) {
                actual.add(Identifier.fromByteBuffer(it.next()));
            }
        }));
    }
    for (Future<?> future : futures) {
        future.get();
    }
}
Also used : Path(java.nio.file.Path) Identifier(com.cinchapi.concourse.server.model.Identifier) FileChannel(java.nio.channels.FileChannel) ExecutorService(java.util.concurrent.ExecutorService) Iterator(java.util.Iterator) CloseableIterator(com.cinchapi.concourse.collect.CloseableIterator) Future(java.util.concurrent.Future) List(java.util.List) ByteBuffer(java.nio.ByteBuffer) ConcourseBaseTest(com.cinchapi.concourse.test.ConcourseBaseTest) Test(org.junit.Test)

Example 32 with Identifier

use of com.cinchapi.concourse.server.model.Identifier in project concourse by cinchapi.

the class ManifestTest method testManifestWorksAfterBeingFlushed.

@Test
public void testManifestWorksAfterBeingFlushed() {
    int count = TestData.getScaleCount() * 2;
    Manifest manifest = Manifest.create(count);
    Identifier key = Identifier.of(count);
    manifest.putStart(count, key);
    manifest.putEnd(count * 2, key);
    Range range = manifest.lookup(key);
    Assert.assertEquals(count, range.start());
    Assert.assertEquals(count * 2, range.end());
    manifest.transfer(file);
    range = manifest.lookup(key);
    Assert.assertEquals(count, range.start());
    Assert.assertEquals(count * 2, range.end());
}
Also used : Identifier(com.cinchapi.concourse.server.model.Identifier) Range(com.cinchapi.concourse.server.storage.db.kernel.Manifest.Range) Test(org.junit.Test) ConcourseBaseTest(com.cinchapi.concourse.test.ConcourseBaseTest)

Example 33 with Identifier

use of com.cinchapi.concourse.server.model.Identifier in project concourse by cinchapi.

the class ManifestTest method testManifestStreamedEntriesAccuracy.

@Test
public void testManifestStreamedEntriesAccuracy() {
    int threshold = Manifest.MANIFEST_LENGTH_ENTRY_STREAMING_THRESHOLD;
    Manifest.MANIFEST_LENGTH_ENTRY_STREAMING_THRESHOLD = (int) Math.pow(2, 16);
    try {
        Manifest manifest = Manifest.create(100000);
        Text text = Text.wrap(Random.getString());
        int count = 0;
        int start = 0;
        Map<Composite, Range> expected = Maps.newHashMap();
        while (manifest.length() < Manifest.MANIFEST_LENGTH_ENTRY_STREAMING_THRESHOLD) {
            Identifier record = Identifier.of(count);
            int $start = start;
            int end = start + TestData.getScaleCount();
            Range range = new Manifest.Range() {

                @Override
                public long start() {
                    return $start;
                }

                @Override
                public long end() {
                    return end;
                }
            };
            Composite composite = Composite.create(text, record);
            manifest.putStart(start, composite);
            manifest.putEnd(end, composite);
            expected.put(composite, range);
            start = end + 1;
            ++count;
        }
        Path file = Paths.get(TestData.getTemporaryTestFile());
        manifest.transfer(file);
        Manifest $manifest = Manifest.load(file, 0, FileSystem.getFileSize(file.toString()));
        expected.forEach((composite, range) -> {
            Range actual = $manifest.lookup(composite);
            Assert.assertEquals(range.start(), actual.start());
            Assert.assertEquals(range.end(), actual.end());
        });
    } finally {
        Manifest.MANIFEST_LENGTH_ENTRY_STREAMING_THRESHOLD = threshold;
    }
}
Also used : Path(java.nio.file.Path) Identifier(com.cinchapi.concourse.server.model.Identifier) Composite(com.cinchapi.concourse.server.io.Composite) Text(com.cinchapi.concourse.server.model.Text) Range(com.cinchapi.concourse.server.storage.db.kernel.Manifest.Range) Test(org.junit.Test) ConcourseBaseTest(com.cinchapi.concourse.test.ConcourseBaseTest)

Example 34 with Identifier

use of com.cinchapi.concourse.server.model.Identifier in project concourse by cinchapi.

the class SegmentTest method testDataDeduplication.

@Test
public void testDataDeduplication() {
    String key = "name";
    TObject value = Convert.javaToThrift("Fonamey");
    long record = 1;
    // Simulate adding and removing while server is running, but creating
    // new intermediate TObjects
    Write w1 = Write.add(key, Convert.javaToThrift("Fonamey"), record);
    Write w2 = Write.remove(key, Convert.javaToThrift("Fonamey"), record);
    Assert.assertNotSame(w1.getValue(), w2.getValue());
    segment.acquire(w1);
    segment.acquire(w2);
    // Simulate loading data from disk and creating new intermediate because
    // values are not cached when read
    w1 = Write.fromByteBuffer(w1.getBytes());
    w2 = Write.fromByteBuffer(w2.getBytes());
    Assert.assertNotSame(w1.getValue(), w2.getValue());
    segment.acquire(w1);
    segment.acquire(w2);
    int count = TestData.getScaleCount();
    for (int i = 0; i < count; ++i) {
        Write write = Numbers.isEven(i) ? Write.remove(key, value, record) : Write.add(key, value, record);
        write = Write.fromByteBuffer(write.getBytes());
        segment.acquire(write);
    }
    Text name = null;
    Value fonamey = null;
    Identifier one = null;
    Position position = null;
    Iterator<Revision<Identifier, Text, Value>> tit = segment.table().iterator();
    while (tit.hasNext()) {
        Revision<Identifier, Text, Value> revision = tit.next();
        if (one == null) {
            one = revision.getLocator();
        }
        if (name == null) {
            name = revision.getKey();
        }
        if (fonamey == null) {
            fonamey = revision.getValue();
        }
        Assert.assertSame(one, revision.getLocator());
        Assert.assertSame(name, revision.getKey());
        Assert.assertSame(fonamey, revision.getValue());
    }
    Iterator<Revision<Text, Value, Identifier>> iit = segment.index().iterator();
    while (iit.hasNext()) {
        Revision<Text, Value, Identifier> revision = iit.next();
        if (one == null) {
            one = revision.getValue();
        }
        if (name == null) {
            name = revision.getLocator();
        }
        if (fonamey == null) {
            fonamey = revision.getKey();
        }
        Assert.assertSame(one, revision.getValue());
        Assert.assertSame(name, revision.getLocator());
        Assert.assertSame(fonamey, revision.getKey());
    }
    Iterator<Revision<Text, Text, Position>> cit = segment.corpus().iterator();
    while (cit.hasNext()) {
        Revision<Text, Text, Position> revision = cit.next();
        if (position == null) {
            position = revision.getValue();
        }
        if (name == null) {
            name = revision.getLocator();
        }
        Assert.assertSame(position, revision.getValue());
        Assert.assertSame(name, revision.getLocator());
        if (revision.getKey().toString().equals("name")) {
            Assert.assertSame(name, revision.getKey());
        }
    }
}
Also used : Write(com.cinchapi.concourse.server.storage.temp.Write) TObject(com.cinchapi.concourse.thrift.TObject) Position(com.cinchapi.concourse.server.model.Position) Text(com.cinchapi.concourse.server.model.Text) Identifier(com.cinchapi.concourse.server.model.Identifier) Revision(com.cinchapi.concourse.server.storage.db.Revision) Value(com.cinchapi.concourse.server.model.Value) ConcourseBaseTest(com.cinchapi.concourse.test.ConcourseBaseTest) Test(org.junit.Test)

Example 35 with Identifier

use of com.cinchapi.concourse.server.model.Identifier in project concourse by cinchapi.

the class SegmentTest method testConcurrency.

@Test
public void testConcurrency() throws InterruptedException {
    AtomicBoolean succeeded = new AtomicBoolean(true);
    AwaitableExecutorService executor = new AwaitableExecutorService(Executors.newCachedThreadPool());
    try {
        for (int i = 0; i < 1000; ++i) {
            AtomicBoolean done = new AtomicBoolean(false);
            long record = i;
            String key = Long.toString(record);
            TObject value = Convert.javaToThrift(Long.toString(record));
            Write write = Write.add(key, value, record);
            Identifier pk = Identifier.of(record);
            Text text = Text.wrap(key);
            Thread reader = new Thread(() -> {
                while (!done.get()) {
                    TableRecord tr = TableRecord.create(pk);
                    IndexRecord ir = IndexRecord.create(text);
                    CorpusRecord cr = CorpusRecord.create(text);
                    segment.table().seek(Composite.create(pk), tr);
                    segment.index().seek(Composite.create(text), ir);
                    segment.corpus().seek(Composite.create(text), cr);
                    if (!done.get() && tr.isEmpty() != ir.isEmpty()) {
                        if (!tr.isEmpty() && ir.isEmpty()) {
                            // Later read is empty
                            succeeded.set(false);
                            System.out.println(AnyStrings.format("table empty = {} and index empty = {} for {}", tr.isEmpty(), ir.isEmpty(), record));
                        }
                    }
                    if (!done.get() && ir.isEmpty() != cr.isEmpty()) {
                        if (!ir.isEmpty() && cr.isEmpty()) {
                            // Later read is empty
                            succeeded.set(false);
                            System.out.println(AnyStrings.format("index empty = {} and corpus empty = {} for {}", tr.isEmpty(), cr.isEmpty(), record));
                        }
                    }
                }
                TableRecord tr = TableRecord.create(pk);
                IndexRecord ir = IndexRecord.create(text);
                CorpusRecord cr = CorpusRecord.create(text);
                segment.table().seek(Composite.create(pk), tr);
                segment.index().seek(Composite.create(text), ir);
                segment.corpus().seek(Composite.create(text), cr);
                if (tr.isEmpty()) {
                    succeeded.set(false);
                    System.out.println("After write finished, table still empty for " + record);
                }
                if (ir.isEmpty()) {
                    succeeded.set(false);
                    System.out.println("After write finished, index still empty for " + record);
                }
                if (cr.isEmpty()) {
                    succeeded.set(false);
                    System.out.println("After write finished, corpus still empty for " + record);
                }
            });
            Thread writer = new Thread(() -> {
                try {
                    segment.acquire(write, executor);
                    done.set(true);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            reader.start();
            writer.start();
            writer.join();
            reader.join();
        }
        Assert.assertTrue(succeeded.get());
    } finally {
        executor.shutdown();
    }
}
Also used : Write(com.cinchapi.concourse.server.storage.temp.Write) TObject(com.cinchapi.concourse.thrift.TObject) Text(com.cinchapi.concourse.server.model.Text) IndexRecord(com.cinchapi.concourse.server.storage.db.IndexRecord) TableRecord(com.cinchapi.concourse.server.storage.db.TableRecord) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Identifier(com.cinchapi.concourse.server.model.Identifier) CorpusRecord(com.cinchapi.concourse.server.storage.db.CorpusRecord) AwaitableExecutorService(com.cinchapi.concourse.server.concurrent.AwaitableExecutorService) ConcourseBaseTest(com.cinchapi.concourse.test.ConcourseBaseTest) Test(org.junit.Test)

Aggregations

Identifier (com.cinchapi.concourse.server.model.Identifier)40 Text (com.cinchapi.concourse.server.model.Text)28 Value (com.cinchapi.concourse.server.model.Value)25 Test (org.junit.Test)18 Set (java.util.Set)13 ConcourseBaseTest (com.cinchapi.concourse.test.ConcourseBaseTest)9 LinkedHashSet (java.util.LinkedHashSet)7 ByteBuffer (java.nio.ByteBuffer)6 Path (java.nio.file.Path)6 Write (com.cinchapi.concourse.server.storage.temp.Write)4 List (java.util.List)4 LazyTransformSet (com.cinchapi.common.collect.lazy.LazyTransformSet)3 Position (com.cinchapi.concourse.server.model.Position)3 Action (com.cinchapi.concourse.server.storage.Action)3 Range (com.cinchapi.concourse.server.storage.db.kernel.Manifest.Range)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 NavigableSet (java.util.NavigableSet)3 CoalescableTreeMap (com.cinchapi.common.collect.CoalescableTreeMap)2 Composite (com.cinchapi.concourse.server.io.Composite)2 CorpusRecord (com.cinchapi.concourse.server.storage.db.CorpusRecord)2