Search in sources :

Example 1 with MigoriDatastore

use of com.google.nigori.client.MigoriDatastore in project nigori by ucam-cl-dtg.

the class MConcurrencyTest method nonInterferenceOfSeparateHistories.

@Test
public void nonInterferenceOfSeparateHistories() throws NigoriCryptographyException, IOException, UnauthorisedException {
    failed = false;
    Thread[] threads = new Thread[THREADS];
    final MigoriDatastore migori = getStore();
    final Index index = new Index("Concurrency");
    assertTrue("Not registered", migori.register());
    try {
        final List<Throwable> exceptionList = Collections.synchronizedList(new LinkedList<Throwable>());
        for (int j = 0; j < THREADS; ++j) {
            final int startFactor = j;
            threads[j] = new Thread() {

                @Override
                public void run() {
                    boolean succeeded = false;
                    try {
                        RevValue last = migori.put(index, Util.int2bin(0));
                        for (int i = startFactor * REPEATS; i < startFactor * REPEATS + REPEATS; ++i) {
                            last = migori.put(index, Util.int2bin(i), last);
                        }
                        succeeded = true;
                    } catch (Throwable e) {
                        exceptionList.add(e);
                    } finally {
                        if (!succeeded && !failed) {
                            failed = true;
                        }
                    }
                }
            };
        }
        startThenJoinThreads(threads);
        ifFailedPrintFailures(failed, exceptionList);
        Collection<RevValue> heads = migori.get(index);
        assertEquals(4, heads.size());
        int total = 0;
        RevValue deleteAt = null;
        for (RevValue head : heads) {
            deleteAt = head;
            int value = Util.bin2int(head.getValue(), 0);
            total += value;
        }
        assertEquals(REPEATS * 10 - 4, total);
        assertNotNull(deleteAt);
        if (deleteAt != null)
            assertTrue(migori.removeIndex(index, deleteAt.getRevision()));
    } finally {
        assertTrue("Not unregistered", migori.unregister());
    }
}
Also used : MigoriDatastore(com.google.nigori.client.MigoriDatastore) RevValue(com.google.nigori.common.RevValue) Index(com.google.nigori.common.Index) Test(org.junit.Test)

Example 2 with MigoriDatastore

use of com.google.nigori.client.MigoriDatastore in project nigori by ucam-cl-dtg.

the class MConcurrencyTest method deterministicEqual.

@Test
public void deterministicEqual() throws NigoriCryptographyException, IOException, UnauthorisedException {
    failed = false;
    Thread[] threads = new Thread[THREADS];
    final MigoriDatastore migori = getStore();
    final Index index = new Index("Concurrency");
    assertTrue("Not registered", migori.register());
    try {
        final List<Throwable> exceptionList = Collections.synchronizedList(new LinkedList<Throwable>());
        for (int j = 0; j < THREADS; ++j) {
            threads[j] = new Thread() {

                @Override
                public void run() {
                    boolean succeeded = false;
                    try {
                        RevValue last = migori.put(index, Util.int2bin(0));
                        for (int i = 0; i < REPEATS; ++i) {
                            last = migori.put(index, Util.int2bin(i), last);
                        }
                        succeeded = true;
                    } catch (Throwable e) {
                        exceptionList.add(e);
                    } finally {
                        if (!succeeded && !failed) {
                            failed = true;
                        }
                    }
                }
            };
        }
        startThenJoinThreads(threads);
        ifFailedPrintFailures(failed, exceptionList);
        Collection<RevValue> heads = migori.get(index);
        assertEquals(1, heads.size());
        RevValue head = heads.toArray(new RevValue[1])[0];
        int total = Util.bin2int(head.getValue(), 0);
        assertEquals(REPEATS - 1, total);
        assertTrue(migori.removeIndex(index, head.getRevision()));
    } finally {
        assertTrue("Not unregistered", migori.unregister());
    }
}
Also used : MigoriDatastore(com.google.nigori.client.MigoriDatastore) RevValue(com.google.nigori.common.RevValue) Index(com.google.nigori.common.Index) Test(org.junit.Test)

Example 3 with MigoriDatastore

use of com.google.nigori.client.MigoriDatastore in project nigori by ucam-cl-dtg.

the class MSetGetTest method putGraph.

@Test
public void putGraph() throws NigoriCryptographyException, IOException, UnauthorisedException {
    MigoriDatastore store = getStore();
    store.register();
    try {
        Index index = new Index("test");
        RevValue a = store.put(index, "a".getBytes());
        RevValue b = store.put(index, "b".getBytes(), a);
        RevValue c = store.put(index, "c".getBytes(), a);
        RevValue d = store.put(index, "d".getBytes(), b, c);
        RevValue e = store.put(index, "e".getBytes(), c, b);
        RevValue f = store.put(index, "f".getBytes(), d);
        RevValue g = store.put(index, "g".getBytes(), e);
        Collection<RevValue> heads = store.get(index);
        assertThat(heads, hasItems(f, g));
        assertTrue(store.removeIndex(index, g.getRevision()));
    } finally {
        store.unregister();
    }
}
Also used : MigoriDatastore(com.google.nigori.client.MigoriDatastore) RevValue(com.google.nigori.common.RevValue) Index(com.google.nigori.common.Index) Test(org.junit.Test)

Aggregations

MigoriDatastore (com.google.nigori.client.MigoriDatastore)3 Index (com.google.nigori.common.Index)3 RevValue (com.google.nigori.common.RevValue)3 Test (org.junit.Test)3