Search in sources :

Example 16 with RevValue

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

Example 17 with RevValue

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

the class SetGetDeleteTest method setGetDelete.

@Test
public void setGetDelete() throws NigoriCryptographyException, IOException, UnauthorisedException {
    NigoriDatastore nigori = getStore();
    for (int i = 0; i < AcceptanceTests.REPEAT; ++i) {
        // check we can do this more than once
        try {
            assertTrue("Not registered" + i, nigori.register());
            assertTrue("Not a clean store " + i, nigori.getIndices().isEmpty());
            for (IndexValue iv : testCases) {
                // once round for each
                final Index index = iv.index;
                final Revision revision = iv.revvalue.getRevision();
                final byte[] value = iv.revvalue.getValue();
                assertTrue("Not put " + i, nigori.put(index, revision, value));
                List<RevValue> revs = nigori.get(index);
                assertFalse("Revisions must exist" + i, revs.isEmpty());
                assertThat(revs, hasItem(iv.revvalue));
                assertEquals("Not one revision " + i, 1, revs.size());
                assertTrue("Not deleted" + i, nigori.delete(index, NULL_DELETE_TOKEN));
                assertNull("Not deleted" + i, nigori.get(index));
                assertFalse("Could redelete", nigori.delete(index, NULL_DELETE_TOKEN));
            }
            // allow them to accumulate
            for (IndexValue iv : testCases) {
                final Index index = iv.index;
                final byte[] value = iv.revvalue.getValue();
                final Revision revision = iv.revvalue.getRevision();
                assertTrue("Not put" + i, nigori.put(index, revision, value));
            }
            try {
                for (IndexValue iv : testCases) {
                    final Index index = iv.index;
                    final byte[] value = iv.revvalue.getValue();
                    final Revision revision = iv.revvalue.getRevision();
                    assertArrayEquals("Got different" + i, value, nigori.getRevision(index, revision));
                }
            } finally {
                for (IndexValue iv : testCases) {
                    final Index index = iv.index;
                    if (!iv.later) {
                        assertTrue("Not deleted" + i, nigori.delete(index, NULL_DELETE_TOKEN));
                    } else {
                        // should have already been deleted
                        assertFalse("Not deleted" + i, nigori.delete(index, NULL_DELETE_TOKEN));
                    }
                }
            }
            for (IndexValue iv : testCases) {
                final Index index = iv.index;
                assertNull("Still there after deletion " + i, nigori.get(index));
            }
        } finally {
            assertTrue("Not unregistered", nigori.unregister());
        }
    }
}
Also used : Revision(com.google.nigori.common.Revision) RevValue(com.google.nigori.common.RevValue) NigoriDatastore(com.google.nigori.client.NigoriDatastore) Index(com.google.nigori.common.Index) Test(org.junit.Test)

Example 18 with RevValue

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

the class AbstractDatabaseTest method setGetDelete.

@Test
public void setGetDelete() throws UserNotFoundException, IOException {
    database.addUser(publicKey, publicHash);
    User user = database.getUser(publicHash);
    try {
        byte[] index = "index".getBytes();
        byte[] revision = "revision".getBytes();
        byte[] value = "value".getBytes();
        assertTrue(database.putRecord(user, index, revision, value));
        Collection<RevValue> revs = database.getRecord(user, index);
        assertEquals(1, revs.size());
        for (RevValue rv : revs) {
            assertArrayEquals(revision, rv.getRevision().getBytes());
            assertArrayEquals(value, rv.getValue());
        }
        assertTrue(database.deleteRecord(user, index));
        assertNull(database.getRecord(user, index));
        assertFalse(database.deleteRecord(user, index));
    } finally {
        assertTrue("User not deleted", database.deleteUser(user));
    }
}
Also used : RevValue(com.google.nigori.common.RevValue) Test(org.junit.Test)

Example 19 with RevValue

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

the class DatabaseNigoriProtocol method get.

@Override
public GetResponse get(GetRequest request) throws IOException, NotFoundException, UnauthorisedException {
    byte[] index = request.getKey().toByteArray();
    AuthenticateRequest auth = request.getAuth();
    User user;
    byte[] revision = null;
    if (request.hasRevision()) {
        revision = request.getRevision().toByteArray();
        user = authenticateUser(auth, MessageLibrary.REQUEST_GET, index, revision);
    } else {
        user = authenticateUser(auth, MessageLibrary.REQUEST_GET, index);
    }
    Collection<RevValue> value;
    if (request.hasRevision()) {
        value = new ArrayList<RevValue>(1);
        RevValue revVal = database.getRevision(user, index, revision);
        if (revVal == null) {
            throw new NotFoundException("Cannot find requested index with revision");
        }
        value.add(revVal);
    } else {
        value = database.getRecord(user, index);
    }
    if (value == null) {
        throw new NotFoundException("No value for that index");
    }
    return MessageLibrary.getResponseAsProtobuf(value);
}
Also used : AuthenticateRequest(com.google.nigori.common.NigoriMessages.AuthenticateRequest) RevValue(com.google.nigori.common.RevValue) NotFoundException(com.google.nigori.common.NotFoundException)

Example 20 with RevValue

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

the class TwoUserDemo method main.

public static void main(String[] args) throws NigoriCryptographyException, IOException, UnauthorisedException {
    MigoriDatastore store;
    Thread secondUser;
    // This is the index in the store which is used to hold the shared data
    final Index sharedIndex = new Index(new byte[] { 1 });
    {
        // This auto-generates a unique username and password used in the store.
        // The username and password is the data which should be shared between two devices.
        final CryptoNigoriDatastore sharedStore = new CryptoNigoriDatastore(HOST, PORT, "nigori");
        final String username = sharedStore.getUsername();
        final String password = sharedStore.getPassword();
        System.out.println("Shared store: Username='" + username + "' Password='" + password + "'");
        store = new HashMigoriDatastore(sharedStore);
        if (!store.register()) {
            System.out.println("Failed to register shared store");
            return;
        }
        secondUser = new SeparateUserAccessesSharedStore(username, password, sharedIndex);
        secondUser.start();
    }
    // First user, possibly on a different device
    byte lastCount = 0;
    for (int i = 0; i < ITERATIONS; ++i) {
        Collection<RevValue> results = store.get(sharedIndex);
        if (results == null || results.isEmpty()) {
            System.out.println("No valid data held");
        } else {
            // System.out.println(String.format("%d head revisions",results.size()));
            for (RevValue rv : results) {
                byte[] result = rv.getValue();
                if (result == null) {
                    System.out.println("No valid data held for " + rv.getRevision());
                } else {
                    byte currentCount = result[0];
                    System.out.println("Count has the value " + currentCount + " for revision " + rv.getRevision());
                    if (currentCount < lastCount) {
                        System.err.println(String.format("Count not increasing, last was (%d) current is (%d)", lastCount, currentCount));
                    }
                    lastCount = currentCount;
                }
            }
        }
        try {
            Thread.sleep(DELAY);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    try {
        secondUser.join();
    } catch (InterruptedException e) {
    }
    // Clean up
    RevValue head = store.getMerging(sharedIndex, new ArbitraryMerger());
    store.removeIndex(sharedIndex, head.getRevision());
    store.unregister();
}
Also used : Index(com.google.nigori.common.Index) RevValue(com.google.nigori.common.RevValue)

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