Search in sources :

Example 6 with RevValue

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

the class ComparableMergerTest method mergeTwoEquiv.

@Test
public void mergeTwoEquiv() throws IOException, NigoriCryptographyException, UnauthorisedException {
    Collection<RevValue> heads = new ArrayList<RevValue>();
    byte[] test = MessageLibrary.toBytes("test");
    RevValue rv = new RevValue(Revision.EMPTY, test);
    RevValue rv1 = new RevValue(Revision.EMPTY, test);
    heads.add(rv);
    heads.add(rv1);
    MigoriDatastore store = createMock(MigoriDatastore.class);
    expect(store.put(eq(INDEX), aryEq(test), eq(rv), eq(rv1))).andReturn(rv);
    replay(store);
    RevValue value = merger.merge(store, INDEX, heads);
    verify(store);
    assertEquals(rv, value);
}
Also used : RevValue(com.google.nigori.common.RevValue) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with RevValue

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

the class JEDatabase method getRecord.

@Override
public Collection<RevValue> getRecord(User user, byte[] key) throws IOException {
    Transaction txn = null;
    try {
        txn = env.beginTransaction(null, null);
        OperationStatus lookupExists = db.getSearchBoth(txn, makeStoresKey(user), new DatabaseEntry(key), null);
        if (OperationStatus.SUCCESS != lookupExists) {
            txn.commit();
            return null;
        }
        Collection<RevValue> collection = new ArrayList<RevValue>();
        Cursor cursor = db.openCursor(txn, null);
        try {
            byte[] lookup = makeLookupBytes(user, key);
            DatabaseEntry lookupKey = new DatabaseEntry(lookup);
            DatabaseEntry revision = new DatabaseEntry();
            DatabaseEntry value = new DatabaseEntry();
            DatabaseEntry revisionKey;
            OperationStatus status = cursor.getSearchKey(lookupKey, revision, null);
            for (; OperationStatus.SUCCESS == status; status = cursor.getNextDup(lookupKey, revision, null)) {
                revisionKey = makeValueKey(lookup, revision.getData());
                OperationStatus valStatus = db.get(txn, revisionKey, value, null);
                if (OperationStatus.SUCCESS == valStatus) {
                    collection.add(new RevValue(revision.getData(), value.getData()));
                } else {
                // TODO(drt24) revision exists but value does not.
                }
            }
        } finally {
            cursor.close();
        }
        txn.commit();
        return collection;
    } catch (DatabaseException e) {
        severe("Exception while getting record", e);
        try {
            if (txn != null)
                txn.abort();
        } catch (DatabaseException e1) {
        // we already had a failure, ignore this one.
        }
        throw new IOException(e);
    }
}
Also used : Transaction(com.sleepycat.je.Transaction) RevValue(com.google.nigori.common.RevValue) OperationStatus(com.sleepycat.je.OperationStatus) ArrayList(java.util.ArrayList) DatabaseEntry(com.sleepycat.je.DatabaseEntry) IOException(java.io.IOException) Cursor(com.sleepycat.je.Cursor) DatabaseException(com.sleepycat.je.DatabaseException)

Example 8 with RevValue

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

the class CommandLineExample method main.

public static void main(String[] args) throws Exception {
    if (args.length < 5 || args.length > 8) {
        usage();
        return;
    }
    final String server = args[0];
    final int port = Integer.parseInt(args[1]);
    final String action = args[2];
    final String username = args[3];
    final String password = args[4];
    MigoriDatastore nigori = new HashMigoriDatastore(new CryptoNigoriDatastore(server, port, "nigori", username, password));
    if (action.equals("register")) {
        boolean success = nigori.register();
        System.out.println("Success: " + success);
    } else if (action.equals("unregister")) {
        boolean success = nigori.unregister();
        System.out.println("Success: " + success);
    } else if (action.equals("authenticate")) {
        boolean success = nigori.authenticate();
        System.out.println("Success: " + success);
    } else if (action.equals("put")) {
        if (args.length != 7) {
            System.err.println("*** Error: exactly seven elements needed for a put action");
            usage();
            return;
        }
        RevValue success = nigori.put(new Index(args[5]), MessageLibrary.toBytes(args[6]));
        System.out.println("Success: " + success);
    } else if (action.equals("get")) {
        if (args.length != 6) {
            System.err.println("*** Error: exactly six elements needed for a get action");
            usage();
            return;
        }
        try {
            Collection<RevValue> data = nigori.get(new Index(args[5]));
            if (data != null) {
                for (RevValue datum : data) {
                    System.out.println(datum.getRevision() + " : " + new String(datum.getValue()));
                }
            } else {
                System.out.println("No values for index.");
            }
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        }
    } else if (action.equals("delete")) {
        if (args.length != 6) {
            System.err.println("*** Error: exactly six elements needed for a delete action");
            usage();
            return;
        }
        boolean success = nigori.removeIndex(new Index(args[5]), Revision.EMPTY);
        System.out.println("Success: " + success);
    } else {
        System.err.println("*** Error: Unknown action " + action);
        usage();
    }
}
Also used : RevValue(com.google.nigori.common.RevValue) Index(com.google.nigori.common.Index) IOException(java.io.IOException)

Example 9 with RevValue

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

the class ComparableMerger method merge.

@Override
public RevValue merge(MigoriDatastore store, Index index, Collection<RevValue> heads) throws IOException, NigoriCryptographyException, UnauthorisedException {
    // caller must ensure that this is true
    assert heads.size() > 1;
    List<T> items = new ArrayList<T>(heads.size());
    Map<T, RevValue> mapBack = new HashMap<T, RevValue>();
    for (RevValue value : heads) {
        T item;
        try {
            item = converter.fromBytes(index, value.getValue());
        } catch (ConversionException e) {
            throw new IOException(e);
        }
        items.add(item);
        mapBack.put(item, value);
    }
    findEquivalences(store, index, items, mapBack);
    T latest = items.get(items.size() - 1);
    if (items.size() == 1) {
        // we have already reduced it to one so don't need to do anything more
        return mapBack.get(latest);
    }
    T newItem = converter.fromLatest(latest);
    return store.put(index, converter.toBytes(newItem), map(items, mapBack).toArray(new RevValue[0]));
}
Also used : RevValue(com.google.nigori.common.RevValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 10 with RevValue

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

the class ComparableMerger method putEquivalence.

private TypeValue<T> putEquivalence(MigoriDatastore store, Index index, Collection<T> equivalence, Map<T, RevValue> mapBack) throws IOException, NigoriCryptographyException, UnauthorisedException {
    assert equivalence.size() > 1;
    List<RevValue> values = new ArrayList<RevValue>();
    T value = null;
    for (T item : equivalence) {
        value = item;
        values.add(mapBack.get(item));
    }
    T item = converter.fromLatest(value);
    RevValue rv = store.put(index, converter.toBytes(item), values.toArray(new RevValue[0]));
    return new TypeValue<T>(item, rv);
}
Also used : 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