Search in sources :

Example 1 with RevValue

use of com.google.nigori.common.RevValue 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 RevValue

use of com.google.nigori.common.RevValue in project nigori by ucam-cl-dtg.

the class AppEngineDatabase method getRevision.

@Override
public RevValue getRevision(User user, byte[] index, byte[] revision) throws IOException {
    PersistenceManager pm = pmfInstance.getPersistenceManager();
    try {
        Key lookupKey = getLookupKey(user, index);
        Key revisionKey = AppEngineRecord.makeKey(lookupKey, new BytesRevision(revision));
        // If this doesn't exist there is no key so null gets returned by JDOObjectNotFoundException
        AppEngineRecord record = pm.getObjectById(AppEngineRecord.class, revisionKey);
        return new RevValue(revision, record.getValue());
    } catch (JDOObjectNotFoundException e) {
        return null;
    } finally {
        pm.close();
    }
}
Also used : JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) PersistenceManager(javax.jdo.PersistenceManager) RevValue(com.google.nigori.common.RevValue) Key(com.google.appengine.api.datastore.Key)

Example 3 with RevValue

use of com.google.nigori.common.RevValue in project nigori by ucam-cl-dtg.

the class HashMigoriDatastore method toIDByte.

private byte[] toIDByte(RevValue... parents) {
    Arrays.sort(parents);
    byte[] idBytes = new byte[parents.length * HASH_SIZE];
    int insertPoint = 0;
    for (RevValue rev : parents) {
        System.arraycopy(rev.getRevision().getBytes(), 0, idBytes, insertPoint, HASH_SIZE);
        insertPoint += HASH_SIZE;
    }
    return idBytes;
}
Also used : RevValue(com.google.nigori.common.RevValue)

Example 4 with RevValue

use of com.google.nigori.common.RevValue in project nigori by ucam-cl-dtg.

the class HashMigoriDatastore method put.

@Override
public RevValue put(Index index, byte[] value, RevValue... parents) throws IOException, NigoriCryptographyException, UnauthorisedException {
    byte[] revBytes = generateHash(value, toIDByte(parents));
    Revision rev = new Revision(revBytes);
    RevValue rv = new RevValue(rev, value);
    boolean success = store.put(index, rev, value);
    if (!success) {
        throw new IOException("Could not put into the store");
    }
    return rv;
}
Also used : Revision(com.google.nigori.common.Revision) RevValue(com.google.nigori.common.RevValue) IOException(java.io.IOException)

Example 5 with RevValue

use of com.google.nigori.common.RevValue in project nigori by ucam-cl-dtg.

the class HashMigoriDatastore method get.

@Override
public List<RevValue> get(Index index) throws NigoriCryptographyException, IOException, UnauthorisedException {
    DAG<Revision> history = getHistory(index);
    if (history == null) {
        return null;
    }
    Collection<Node<Revision>> heads = history.getHeads();
    List<RevValue> answer = new ArrayList<RevValue>();
    for (Node<Revision> rev : heads) {
        Revision revision = rev.getValue();
        byte[] value = getRevision(index, revision);
        // TODO(drt24) value might be null
        if (value != null) {
            answer.add(new RevValue(revision, value));
        }
    }
    return answer;
}
Also used : Revision(com.google.nigori.common.Revision) RevValue(com.google.nigori.common.RevValue) ArrayList(java.util.ArrayList)

Aggregations

RevValue (com.google.nigori.common.RevValue)23 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)9 IOException (java.io.IOException)7 Index (com.google.nigori.common.Index)6 MigoriDatastore (com.google.nigori.client.MigoriDatastore)3 Revision (com.google.nigori.common.Revision)3 Key (com.google.appengine.api.datastore.Key)2 GetResponse (com.google.nigori.common.NigoriMessages.GetResponse)2 RevisionValue (com.google.nigori.common.NigoriMessages.RevisionValue)2 NotFoundException (com.google.nigori.common.NotFoundException)2 DatabaseEntry (com.sleepycat.je.DatabaseEntry)2 DatabaseException (com.sleepycat.je.DatabaseException)2 OperationStatus (com.sleepycat.je.OperationStatus)2 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)2 PersistenceManager (javax.jdo.PersistenceManager)2 Blob (com.google.appengine.api.datastore.Blob)1 DatastoreService (com.google.appengine.api.datastore.DatastoreService)1 Entity (com.google.appengine.api.datastore.Entity)1 Query (com.google.appengine.api.datastore.Query)1