use of com.cinchapi.concourse.server.storage.AtomicStateException in project concourse by cinchapi.
the class AtomicOperations method supplyWithRetry.
/**
* Run the {@link AtomicSupplier} within an {@link AtomicOperation} from the
* provided {@code store} and continue to retry execution of the supplier
* until it terminates without failure and a value can be returned.
*
* @param store the {@link AtomicSupport} store from which the
* {@link AtomicOperation} is started
* @param supplier the {@link AtomicSupplier} to run until it succeeds
* @return the return value
*/
public static <T> T supplyWithRetry(AtomicSupport store, AtomicSupplier<T> supplier) {
AtomicOperation atomic = null;
T value = null;
while (atomic == null || !atomic.commit(CommitVersions.next())) {
atomic = store.startAtomicOperation();
try {
value = supplier.supply(atomic);
} catch (AtomicStateException e) {
atomic = null;
}
}
return value;
}
use of com.cinchapi.concourse.server.storage.AtomicStateException in project concourse by cinchapi.
the class ConcourseServer method insertJsonRecord.
@Override
@TranslateClientExceptions
@VerifyAccessToken
@VerifyWritePermission
public boolean insertJsonRecord(String json, long record, AccessToken creds, TransactionToken transaction, String environment) throws TException {
AtomicSupport store = getStore(transaction, environment);
try {
Multimap<String, Object> data = Convert.jsonToJava(json);
AtomicOperation atomic = store.startAtomicOperation();
List<DeferredWrite> deferred = Lists.newArrayList();
return Operations.insertAtomic(data, record, atomic, deferred) && Operations.insertDeferredAtomic(deferred, atomic) && atomic.commit(CommitVersions.next());
} catch (TransactionStateException e) {
throw new TransactionException();
} catch (AtomicStateException e) {
return false;
}
}
use of com.cinchapi.concourse.server.storage.AtomicStateException in project concourse by cinchapi.
the class ConcourseServer method consolidateRecords.
@Override
@TranslateClientExceptions
@VerifyAccessToken
@VerifyWritePermission
public boolean consolidateRecords(List<Long> records, AccessToken creds, TransactionToken transaction, String environment) throws TException {
Set<Long> $records = Sets.newLinkedHashSet(records);
if ($records.size() >= 2) {
AtomicSupport store = getStore(transaction, environment);
AtomicOperation atomic = store.startAtomicOperation();
try {
Iterator<Long> it = $records.iterator();
long destination = it.next();
while (it.hasNext()) {
// 1. Copy all data from the #source to the #destination
long source = it.next();
Map<String, Set<TObject>> data = store.select(source);
for (Entry<String, Set<TObject>> entry : data.entrySet()) {
String key = entry.getKey();
for (TObject value : entry.getValue()) {
if (!atomic.verify(key, value, destination) && !atomic.add(key, value, destination)) {
return false;
}
}
}
// 2. Replace all incoming links to #source with links to
// #destination
Map<String, Set<Long>> incoming = Operations.traceRecordAtomic(source, Time.NONE, atomic);
for (Entry<String, Set<Long>> entry : incoming.entrySet()) {
String key = entry.getKey();
for (long record : entry.getValue()) {
if (!atomic.remove(key, Convert.javaToThrift(Link.to(source)), record)) {
return false;
}
if (!atomic.add(key, Convert.javaToThrift(Link.to(destination)), record)) {
return false;
}
}
}
// 3. Clear the #source
Operations.clearRecordAtomic(source, atomic);
}
return atomic.commit(CommitVersions.next());
} catch (TransactionStateException e) {
throw new TransactionException();
} catch (AtomicStateException e) {
return false;
}
} else {
// don't return a truthy value.
return false;
}
}
Aggregations