use of com.google.nigori.common.RevValue in project nigori by ucam-cl-dtg.
the class MConcurrencyTest method nonInterferenceOfSeparateHistories.
@Test
public void nonInterferenceOfSeparateHistories() 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) {
final int startFactor = j;
threads[j] = new Thread() {
@Override
public void run() {
boolean succeeded = false;
try {
RevValue last = migori.put(index, Util.int2bin(0));
for (int i = startFactor * REPEATS; i < startFactor * REPEATS + 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(4, heads.size());
int total = 0;
RevValue deleteAt = null;
for (RevValue head : heads) {
deleteAt = head;
int value = Util.bin2int(head.getValue(), 0);
total += value;
}
assertEquals(REPEATS * 10 - 4, total);
assertNotNull(deleteAt);
if (deleteAt != null)
assertTrue(migori.removeIndex(index, deleteAt.getRevision()));
} finally {
assertTrue("Not unregistered", migori.unregister());
}
}
use of com.google.nigori.common.RevValue in project nigori by ucam-cl-dtg.
the class AppEngineDatabase method getRevision.
@Override
public RevValue getRevision(User user, byte[] index, byte[] revision) throws IOException {
PersistenceManager pm = pmfInstance.getPersistenceManager();
try {
Key lookupKey = getLookupKey(user, index);
Key revisionKey = AppEngineRecord.makeKey(lookupKey, new BytesRevision(revision));
// If this doesn't exist there is no key so null gets returned by JDOObjectNotFoundException
AppEngineRecord record = pm.getObjectById(AppEngineRecord.class, revisionKey);
return new RevValue(revision, record.getValue());
} catch (JDOObjectNotFoundException e) {
return null;
} finally {
pm.close();
}
}
use of com.google.nigori.common.RevValue in project nigori by ucam-cl-dtg.
the class HashMigoriDatastore method toIDByte.
private byte[] toIDByte(RevValue... parents) {
Arrays.sort(parents);
byte[] idBytes = new byte[parents.length * HASH_SIZE];
int insertPoint = 0;
for (RevValue rev : parents) {
System.arraycopy(rev.getRevision().getBytes(), 0, idBytes, insertPoint, HASH_SIZE);
insertPoint += HASH_SIZE;
}
return idBytes;
}
use of com.google.nigori.common.RevValue in project nigori by ucam-cl-dtg.
the class HashMigoriDatastore method put.
@Override
public RevValue put(Index index, byte[] value, RevValue... parents) throws IOException, NigoriCryptographyException, UnauthorisedException {
byte[] revBytes = generateHash(value, toIDByte(parents));
Revision rev = new Revision(revBytes);
RevValue rv = new RevValue(rev, value);
boolean success = store.put(index, rev, value);
if (!success) {
throw new IOException("Could not put into the store");
}
return rv;
}
use of com.google.nigori.common.RevValue in project nigori by ucam-cl-dtg.
the class HashMigoriDatastore method get.
@Override
public List<RevValue> get(Index index) throws NigoriCryptographyException, IOException, UnauthorisedException {
DAG<Revision> history = getHistory(index);
if (history == null) {
return null;
}
Collection<Node<Revision>> heads = history.getHeads();
List<RevValue> answer = new ArrayList<RevValue>();
for (Node<Revision> rev : heads) {
Revision revision = rev.getValue();
byte[] value = getRevision(index, revision);
// TODO(drt24) value might be null
if (value != null) {
answer.add(new RevValue(revision, value));
}
}
return answer;
}
Aggregations