Search in sources :

Example 6 with AtomicOperation

use of com.cinchapi.concourse.server.storage.AtomicOperation 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;
    }
}
Also used : ComplexTObject(com.cinchapi.concourse.thrift.ComplexTObject) TObject(com.cinchapi.concourse.thrift.TObject) SortableSet(com.cinchapi.concourse.data.sort.SortableSet) Set(java.util.Set) TransactionStateException(com.cinchapi.concourse.server.storage.TransactionStateException) AtomicSupport(com.cinchapi.concourse.server.storage.AtomicSupport) AtomicStateException(com.cinchapi.concourse.server.storage.AtomicStateException) TransactionException(com.cinchapi.concourse.thrift.TransactionException) AtomicOperation(com.cinchapi.concourse.server.storage.AtomicOperation) VerifyWritePermission(com.cinchapi.concourse.server.aop.VerifyWritePermission) VerifyAccessToken(com.cinchapi.concourse.server.aop.VerifyAccessToken) TranslateClientExceptions(com.cinchapi.concourse.server.aop.TranslateClientExceptions)

Example 7 with AtomicOperation

use of com.cinchapi.concourse.server.storage.AtomicOperation in project concourse by cinchapi.

the class OperationsTest method testTraceRecordsAtomic.

@Test
public void testTraceRecordsAtomic() {
    AtomicSupport store = getStore();
    try {
        setupGraph(store);
        AtomicOperation atomic = store.startAtomicOperation();
        Map<Long, Map<String, Set<Long>>> incoming = Operations.traceRecordsAtomic(ImmutableList.of(1L, 2L, 3L), Time.NONE, atomic);
        Assert.assertEquals(ImmutableMap.of(2L, ImmutableMap.of("foo", ImmutableSet.of(1L, 4L), "bar", ImmutableSet.of(3L), "baz", ImmutableSet.of(3L)), 1L, ImmutableMap.of("bar", ImmutableSet.of(2L), "baz", ImmutableSet.of(3L), "foo", ImmutableSet.of(4L)), 3L, ImmutableMap.of("baz", ImmutableSet.of(1L, 4L), "foo", ImmutableSet.of(2L), "bar", ImmutableSet.of(4L))), incoming);
    } finally {
        store.stop();
    }
}
Also used : AtomicOperation(com.cinchapi.concourse.server.storage.AtomicOperation) AtomicSupport(com.cinchapi.concourse.server.storage.AtomicSupport) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

AtomicOperation (com.cinchapi.concourse.server.storage.AtomicOperation)7 AtomicSupport (com.cinchapi.concourse.server.storage.AtomicSupport)6 TObject (com.cinchapi.concourse.thrift.TObject)4 Set (java.util.Set)4 AtomicStateException (com.cinchapi.concourse.server.storage.AtomicStateException)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 Function (com.cinchapi.ccl.type.Function)2 IndexFunction (com.cinchapi.ccl.type.function.IndexFunction)2 KeyConditionFunction (com.cinchapi.ccl.type.function.KeyConditionFunction)2 KeyRecordsFunction (com.cinchapi.ccl.type.function.KeyRecordsFunction)2 TemporalFunction (com.cinchapi.ccl.type.function.TemporalFunction)2 AdHocIterator (com.cinchapi.common.base.AdHocIterator)2 ArrayBuilder (com.cinchapi.common.base.ArrayBuilder)2 Reflection (com.cinchapi.common.reflect.Reflection)2 Constants (com.cinchapi.concourse.Constants)2 Link (com.cinchapi.concourse.Link)2 Calculations (com.cinchapi.concourse.server.calculate.Calculations)2 Source (com.cinchapi.concourse.server.ops.Strategy.Source)2 Finder (com.cinchapi.concourse.server.query.Finder)2