Search in sources :

Example 11 with RevValue

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

the class CryptoNigoriDatastore method get.

/**
 * Retrieve the value associated with {@code index} on the server.
 *
 * @param index
 * @param revision
 * @return a list of RevValues containing the data associated with {@code index} or {@code null}
 *         if no data exists. WARNING: there is no assurance that the value for the revision is a
 *         pair once specified by a valid client - the server can pair any value with any
 *         revision.
 */
private List<RevValue> get(byte[] encKey, Index index, Revision revision) throws IOException, NigoriCryptographyException, UnauthorisedException {
    byte[] encIndex;
    byte[] encRevision = null;
    if (encKey == null) {
        encIndex = keyManager.encryptDeterministically(index.getBytes());
        if (revision != null) {
            encRevision = keyManager.encryptDeterministically(revision.getBytes());
        }
    } else {
        encIndex = keyManager.encryptDeterministically(encKey, index.getBytes());
        if (revision != null) {
            encRevision = keyManager.encryptDeterministically(encKey, revision.getBytes());
        }
    }
    try {
        GetResponse getResponse = protocol.get(MessageLibrary.getRequestAsProtobuf(keyManager.getServerName(), keyManager.signer(), encIndex, encRevision));
        if (getResponse == null) {
            return null;
        }
        List<RevisionValue> revisions = getResponse.getRevisionsList();
        List<RevValue> answer = new ArrayList<RevValue>(revisions.size());
        for (RevisionValue revisionValue : revisions) {
            byte[] revisionciphertext = revisionValue.getRevision().toByteArray();
            byte[] valueciphertext = revisionValue.getValue().toByteArray();
            if (encKey == null) {
                answer.add(new RevValue(keyManager.decrypt(revisionciphertext), keyManager.decrypt(valueciphertext)));
            } else {
                answer.add(new RevValue(keyManager.decrypt(encKey, revisionciphertext), keyManager.decrypt(encKey, valueciphertext)));
            }
        }
        return answer;
    } catch (NotFoundException e) {
        return null;
    }
}
Also used : RevisionValue(com.google.nigori.common.NigoriMessages.RevisionValue) RevValue(com.google.nigori.common.RevValue) ArrayList(java.util.ArrayList) NotFoundException(com.google.nigori.common.NotFoundException) GetResponse(com.google.nigori.common.NigoriMessages.GetResponse)

Example 12 with RevValue

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

the class JEDatabase method getRevision.

@Override
public RevValue getRevision(User user, byte[] key, byte[] revision) throws IOException {
    try {
        DatabaseEntry value = new DatabaseEntry();
        OperationStatus status = db.get(null, makeValueKey(makeLookupBytes(user, key), revision), value, null);
        if (OperationStatus.SUCCESS == status) {
            return new RevValue(revision, value.getData());
        }
        return null;
    } catch (DatabaseException e) {
        throw new IOException(e);
    }
}
Also used : RevValue(com.google.nigori.common.RevValue) OperationStatus(com.sleepycat.je.OperationStatus) DatabaseEntry(com.sleepycat.je.DatabaseEntry) IOException(java.io.IOException) DatabaseException(com.sleepycat.je.DatabaseException)

Example 13 with RevValue

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

the class SQLDatabase method getRecord.

@Override
public Collection<RevValue> getRecord(User user, byte[] key) throws IOException {
    try {
        PreparedStatement queryStatement = con.prepareStatement("SELECT rev, val FROM rev_values, lookups, stores WHERE rev_values.lid = lookups.lid AND lookups.lookup = ? AND lookups.sid = stores.sid AND stores.ph =?");
        try {
            queryStatement.setBytes(1, key);
            queryStatement.setBytes(2, user.getPublicHash());
            ResultSet set = queryStatement.executeQuery();
            List<RevValue> revValues = new ArrayList<RevValue>();
            while (set.next()) {
                byte[] rev = set.getBytes("rev");
                byte[] value = set.getBytes("val");
                revValues.add(new RevValue(rev, value));
            }
            return revValues;
        } finally {
            queryStatement.close();
        }
    } catch (SQLException e) {
        throw new IOException(e);
    }
}
Also used : RevValue(com.google.nigori.common.RevValue) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 14 with RevValue

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

the class NigoriServletTest method testGetRequestKeyExists.

@Test
public void testGetRequestKeyExists() throws Exception {
    final byte[] key = toBytes("a key");
    final byte[] revision = toBytes("a revision");
    final byte[] value = toBytes("a value");
    final byte[] publicHash = keyManager.signer().getPublicHash();
    final String jsonGet = MessageLibrary.getRequestAsJson(serverName, keyManager.signer(), key, null);
    expectedCallsForJsonRequest(jsonGet, MessageLibrary.REQUEST_GET);
    expectedCallsToAuthenticateUser(publicHash);
    expect(database.getRecord(eq(user), aryEq(key))).andReturn(Arrays.asList(new RevValue[] { new RevValue(revision, value) }));
    ServletOutputStream out = expectedCallsForJsonResponse();
    Capture<byte[]> result = new Capture<byte[]>();
    Capture<Integer> size = new Capture<Integer>();
    out.write(capture(result), eq(0), capture(size));
    out.flush();
    runReplayVerifyWithDoPost(out);
    String jsonResponse = new String(result.getValue(), 0, size.getValue(), MessageLibrary.CHARSET);
    GetResponse getResponse = MessageLibrary.getResponseFromJson(jsonResponse);
    List<RevisionValue> revs = getResponse.getRevisionsList();
    assertEquals(1, revs.size());
    for (RevisionValue rev : revs) {
        assertArrayEquals(revision, rev.getRevision().toByteArray());
        assertArrayEquals(value, rev.getValue().toByteArray());
    }
}
Also used : RevValue(com.google.nigori.common.RevValue) ServletOutputStream(javax.servlet.ServletOutputStream) RevisionValue(com.google.nigori.common.NigoriMessages.RevisionValue) GetResponse(com.google.nigori.common.NigoriMessages.GetResponse) Capture(org.easymock.Capture) Test(org.junit.Test)

Example 15 with RevValue

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

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