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();
}
}
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());
}
}
}
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));
}
}
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);
}
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();
}
Aggregations