use of com.cinchapi.concourse.thrift.Diff in project concourse by cinchapi.
the class ConcourseServer method diffKeyStartEnd.
@Override
@TranslateClientExceptions
@VerifyAccessToken
@VerifyReadPermission
public Map<TObject, Map<Diff, Set<Long>>> diffKeyStartEnd(String key, long start, long end, AccessToken creds, TransactionToken transaction, String environment) throws TException {
AtomicSupport store = getStore(transaction, environment);
AtomicReference<Map<TObject, Set<Long>>> startData = new AtomicReference<>(null);
AtomicReference<Map<TObject, Set<Long>>> endData = new AtomicReference<>(null);
AtomicOperations.executeWithRetry(store, (atomic) -> {
startData.set(store.browse(key, start));
endData.set(store.browse(key, end));
});
Set<TObject> startValues = startData.get().keySet();
Set<TObject> endValues = endData.get().keySet();
Set<TObject> xor = Sets.symmetricDifference(startValues, endValues);
Set<TObject> intersection = startValues.size() < endValues.size() ? Sets.intersection(startValues, endValues) : Sets.intersection(endValues, startValues);
Map<TObject, Map<Diff, Set<Long>>> result = TMaps.newLinkedHashMapWithCapacity(xor.size() + intersection.size());
for (TObject value : xor) {
Map<Diff, Set<Long>> entry = Maps.newHashMapWithExpectedSize(1);
if (!startValues.contains(value)) {
entry.put(Diff.ADDED, endData.get().get(value));
} else {
entry.put(Diff.REMOVED, endData.get().get(value));
}
result.put(value, entry);
}
for (TObject value : intersection) {
Set<Long> startRecords = startData.get().get(value);
Set<Long> endRecords = endData.get().get(value);
Set<Long> xorRecords = Sets.symmetricDifference(startRecords, endRecords);
if (!xorRecords.isEmpty()) {
Set<Long> added = Sets.newHashSetWithExpectedSize(xorRecords.size());
Set<Long> removed = Sets.newHashSetWithExpectedSize(xorRecords.size());
for (Long record : xorRecords) {
if (!startRecords.contains(record)) {
added.add(record);
} else {
removed.add(record);
}
}
Map<Diff, Set<Long>> entry = Maps.newHashMapWithExpectedSize(2);
if (!added.isEmpty()) {
entry.put(Diff.ADDED, added);
}
if (!removed.isEmpty()) {
entry.put(Diff.REMOVED, removed);
}
result.put(value, entry);
}
}
return result;
}
use of com.cinchapi.concourse.thrift.Diff in project concourse by cinchapi.
the class ConcourseServer method diffKeyRecordStartEnd.
@Override
@TranslateClientExceptions
@VerifyAccessToken
@VerifyReadPermission
public Map<Diff, Set<TObject>> diffKeyRecordStartEnd(String key, long record, long start, long end, AccessToken creds, TransactionToken transaction, String environment) throws TException {
AtomicSupport store = getStore(transaction, environment);
AtomicReference<Set<TObject>> startValues = new AtomicReference<>(null);
AtomicReference<Set<TObject>> endValues = new AtomicReference<>(null);
AtomicOperations.executeWithRetry(store, (atomic) -> {
startValues.set(store.select(key, record, start));
endValues.set(store.select(key, record, end));
});
Map<Diff, Set<TObject>> result = Maps.newHashMapWithExpectedSize(2);
Set<TObject> xor = Sets.symmetricDifference(startValues.get(), endValues.get());
int expectedSize = xor.size() / 2;
Set<TObject> added = Sets.newHashSetWithExpectedSize(expectedSize);
Set<TObject> removed = Sets.newHashSetWithExpectedSize(expectedSize);
for (TObject current : xor) {
if (!startValues.get().contains(current))
added.add(current);
else {
removed.add(current);
}
}
if (!added.isEmpty()) {
result.put(Diff.ADDED, added);
}
if (!removed.isEmpty()) {
result.put(Diff.REMOVED, removed);
}
return result;
}
use of com.cinchapi.concourse.thrift.Diff in project concourse by cinchapi.
the class ConcourseServer method diffRecordStartEnd.
@Override
@TranslateClientExceptions
@VerifyAccessToken
@VerifyReadPermission
public Map<String, Map<Diff, Set<TObject>>> diffRecordStartEnd(long record, long start, long end, AccessToken creds, TransactionToken transaction, String environment) throws TException {
AtomicSupport store = getStore(transaction, environment);
AtomicReference<Map<String, Set<TObject>>> startData = new AtomicReference<>(null);
AtomicReference<Map<String, Set<TObject>>> endData = new AtomicReference<>(null);
AtomicOperations.executeWithRetry(store, (atomic) -> {
startData.set(store.select(record, start));
endData.set(store.select(record, end));
});
Set<String> startKeys = startData.get().keySet();
Set<String> endKeys = endData.get().keySet();
Set<String> xor = Sets.symmetricDifference(startKeys, endKeys);
Set<String> intersection = Sets.intersection(startKeys, endKeys);
Map<String, Map<Diff, Set<TObject>>> result = TMaps.newLinkedHashMapWithCapacity(xor.size() + intersection.size());
for (String key : xor) {
Map<Diff, Set<TObject>> entry = Maps.newHashMapWithExpectedSize(1);
if (!startKeys.contains(key)) {
entry.put(Diff.ADDED, endData.get().get(key));
} else {
entry.put(Diff.REMOVED, endData.get().get(key));
}
result.put(key, entry);
}
for (String key : intersection) {
Set<TObject> startValues = startData.get().get(key);
Set<TObject> endValues = endData.get().get(key);
Set<TObject> xorValues = Sets.symmetricDifference(startValues, endValues);
if (!xorValues.isEmpty()) {
Set<TObject> added = Sets.newHashSetWithExpectedSize(xorValues.size());
Set<TObject> removed = Sets.newHashSetWithExpectedSize(xorValues.size());
for (TObject value : xorValues) {
if (!startValues.contains(value)) {
added.add(value);
} else {
removed.add(value);
}
}
Map<Diff, Set<TObject>> entry = Maps.newHashMapWithExpectedSize(2);
if (!added.isEmpty()) {
entry.put(Diff.ADDED, added);
}
if (!removed.isEmpty()) {
entry.put(Diff.REMOVED, removed);
}
result.put(key, entry);
}
}
return result;
}
Aggregations