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