Search in sources :

Example 36 with TObject

use of com.cinchapi.concourse.thrift.TObject in project concourse by cinchapi.

the class Stores method find.

/**
 * Find the records that contain values that are stored for {@code key} and
 * satisfy {@code operator} in relation to the specified {@code values} at
 * {@code timestamp}.
 * <p>
 * If the {@code key} is primitive, the store lookup is usually a simple
 * {@link Store#find(String, Operator, TObject[]) find}. However, if the key
 * is a navigation key, this method will process it by
 * {@link #browse(Store, String) browsing} the destination values and
 * checking the operator validity of each if and only if the {@code store}
 * is an {@link AtomicOperation} or {@link AtomicSupport supports} starting
 * one.
 * </p>
 *
 * @param store
 * @param timestamp
 * @param key
 * @param operator
 * @param values
 * @return the records that satisfy the query
 */
public static Set<Long> find(Store store, long timestamp, String key, Operator operator, TObject... values) {
    for (int i = 0; i < values.length; ++i) {
        TObject value = values[i];
        if (value.getType() == Type.FUNCTION) {
            Function function = (Function) Convert.thriftToJava(value);
            TemporalFunction func = (TemporalFunction) function;
            String method = Calculations.alias(function.operation());
            ArrayBuilder<Object> args = ArrayBuilder.builder();
            method += "Key";
            args.add(function.key());
            if (function instanceof KeyRecordsFunction || function instanceof KeyConditionFunction) {
                method += "Records";
                Collection<Long> records = function instanceof KeyRecordsFunction ? ((KeyRecordsFunction) function).source() : Finder.instance().visit(((KeyConditionFunction) function).source(), store);
                args.add(records);
            } else if (!(function instanceof IndexFunction)) {
                throw new IllegalStateException("Invalid function value");
            }
            method += "Atomic";
            args.add(func.timestamp());
            args.add(store);
            values[i] = Convert.javaToThrift(Reflection.callStatic(Operations.class, method, args.build()));
        }
    }
    if (Keys.isNavigationKey(key)) {
        Map<TObject, Set<Long>> index = timestamp == Time.NONE ? browse(store, key) : browse(store, key, timestamp);
        Set<Long> records = index.entrySet().stream().filter(e -> e.getKey().is(operator, values)).map(e -> e.getValue()).flatMap(Set::stream).collect(Collectors.toCollection(LinkedHashSet::new));
        return records;
    } else if (Keys.isFunctionKey(key)) {
        Set<Long> records = Sets.newLinkedHashSet();
        for (long record : store.getAllRecords()) {
            Set<TObject> aggregate = select(store, key, record, timestamp);
            for (TObject tobject : aggregate) {
                if (tobject.is(operator, values)) {
                    records.add(record);
                    break;
                }
            }
        }
        return records;
    } else {
        return timestamp == Time.NONE ? store.find(key, operator, values) : store.find(timestamp, key, operator, values);
    }
}
Also used : Function(com.cinchapi.ccl.type.Function) ArrayBuilder(com.cinchapi.common.base.ArrayBuilder) Source(com.cinchapi.concourse.server.ops.Strategy.Source) Store(com.cinchapi.concourse.server.storage.Store) AbstractSet(java.util.AbstractSet) AdHocIterator(com.cinchapi.common.base.AdHocIterator) HashMap(java.util.HashMap) IndexFunction(com.cinchapi.ccl.type.function.IndexFunction) Constants(com.cinchapi.concourse.Constants) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Calculations(com.cinchapi.concourse.server.calculate.Calculations) Reflection(com.cinchapi.common.reflect.Reflection) AtomicSupport(com.cinchapi.concourse.server.storage.AtomicSupport) ImmutableList(com.google.common.collect.ImmutableList) TObject(com.cinchapi.concourse.thrift.TObject) Convert(com.cinchapi.concourse.util.Convert) Keys(com.cinchapi.concourse.validate.Keys) TemporalFunction(com.cinchapi.ccl.type.function.TemporalFunction) Map(java.util.Map) Link(com.cinchapi.concourse.Link) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) KeyType(com.cinchapi.concourse.validate.Keys.KeyType) Time(com.cinchapi.concourse.time.Time) ImmutableSet(com.google.common.collect.ImmutableSet) Iterator(java.util.Iterator) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) KeyConditionFunction(com.cinchapi.ccl.type.function.KeyConditionFunction) MoreObjects(com.google.common.base.MoreObjects) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) AtomicOperation(com.cinchapi.concourse.server.storage.AtomicOperation) Operator(com.cinchapi.concourse.thrift.Operator) ForwardingMap(com.google.common.collect.ForwardingMap) Finder(com.cinchapi.concourse.server.query.Finder) Key(com.cinchapi.concourse.validate.Keys.Key) Type(com.cinchapi.concourse.thrift.Type) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) KeyValue(com.cinchapi.concourse.util.KeyValue) KeyRecordsFunction(com.cinchapi.ccl.type.function.KeyRecordsFunction) TObject(com.cinchapi.concourse.thrift.TObject) AbstractSet(java.util.AbstractSet) LinkedHashSet(java.util.LinkedHashSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) KeyRecordsFunction(com.cinchapi.ccl.type.function.KeyRecordsFunction) TemporalFunction(com.cinchapi.ccl.type.function.TemporalFunction) Function(com.cinchapi.ccl.type.Function) IndexFunction(com.cinchapi.ccl.type.function.IndexFunction) TemporalFunction(com.cinchapi.ccl.type.function.TemporalFunction) KeyConditionFunction(com.cinchapi.ccl.type.function.KeyConditionFunction) KeyRecordsFunction(com.cinchapi.ccl.type.function.KeyRecordsFunction) IndexFunction(com.cinchapi.ccl.type.function.IndexFunction) TObject(com.cinchapi.concourse.thrift.TObject) KeyConditionFunction(com.cinchapi.ccl.type.function.KeyConditionFunction)

Example 37 with TObject

use of com.cinchapi.concourse.thrift.TObject in project concourse by cinchapi.

the class Value method fromByteBuffer.

/**
 * Return the Value encoded in {@code bytes} so long as those bytes adhere
 * to the format specified by the {@link #getBytes()} method.
 *
 * @param bytes
 * @return the Value
 */
public static Value fromByteBuffer(ByteBuffer bytes) {
    Type type = Type.values()[bytes.get()];
    TObject data = createTObject(bytes, type);
    return new Value(data);
}
Also used : Type(com.cinchapi.concourse.thrift.Type) TObject(com.cinchapi.concourse.thrift.TObject)

Example 38 with TObject

use of com.cinchapi.concourse.thrift.TObject in project concourse by cinchapi.

the class Operations method browseNavigationKeyOptionalAtomic.

/**
 * Perform "browse" functionality on a navigation key.
 *
 * @param key
 * @param timestamp
 * @param store
 * @return a mapping from each possible destination value for a given
 *         navigation {@code key} to the records where the navigation could
 *         begin to retrieve the value by selecting the navigation
 *         {@code key}
 */
public static Map<TObject, Set<Long>> browseNavigationKeyOptionalAtomic(String key, long timestamp, Store store) {
    String[] toks = key.split("\\.");
    if (toks.length == 1) {
        return timestamp == Time.NONE ? store.browse(key) : store.browse(key, timestamp);
    } else {
        String start = toks[0];
        StringBuilder $key = new StringBuilder();
        for (int i = 1; i < toks.length - 1; ++i) {
            $key.append(toks[i]).append('.');
        }
        $key.append(toks[toks.length - 1]);
        Map<TObject, Set<Long>> root = timestamp == Time.NONE ? store.browse(start) : store.browse(start, timestamp);
        Map<TObject, Set<Long>> index = Maps.newLinkedHashMap();
        root.entrySet().stream().filter(e -> e.getKey().getType() == Type.LINK).forEach(entry -> {
            Link link = (Link) Convert.thriftToJava(entry.getKey());
            Set<Long> nodes = entry.getValue();
            for (long node : nodes) {
                Set<TObject> values = traverseKeyRecordOptionalAtomic($key.toString(), link.longValue(), timestamp, store);
                for (TObject value : values) {
                    index.computeIfAbsent(value, ignore -> Sets.newLinkedHashSet()).add(node);
                }
            }
        });
        return index;
    }
}
Also used : Navigation(com.cinchapi.concourse.util.Navigation) Page(com.cinchapi.concourse.lang.paginate.Page) Numbers(com.cinchapi.concourse.util.Numbers) AtomicStateException(com.cinchapi.concourse.server.storage.AtomicStateException) Constants(com.cinchapi.concourse.Constants) Paging(com.cinchapi.concourse.server.query.paginate.Paging) Calculations(com.cinchapi.concourse.server.calculate.Calculations) KeyRecordCalculation(com.cinchapi.concourse.server.calculate.KeyRecordCalculation) MultimapViews(com.cinchapi.concourse.util.MultimapViews) Convert(com.cinchapi.concourse.util.Convert) Map(java.util.Map) Link(com.cinchapi.concourse.Link) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) ResolvableLink(com.cinchapi.concourse.util.Convert.ResolvableLink) Set(java.util.Set) AbstractSyntaxTree(com.cinchapi.ccl.syntax.AbstractSyntaxTree) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) AtomicOperation(com.cinchapi.concourse.server.storage.AtomicOperation) List(java.util.List) KeyCalculation(com.cinchapi.concourse.server.calculate.KeyCalculation) JsonArray(com.google.gson.JsonArray) Entry(java.util.Map.Entry) DeferredWrite(com.cinchapi.concourse.server.ConcourseServer.DeferredWrite) Type(com.cinchapi.concourse.thrift.Type) StringSplitter(com.cinchapi.common.base.StringSplitter) Sortable(com.cinchapi.concourse.data.sort.Sortable) Iterables(com.google.common.collect.Iterables) NoOrder(com.cinchapi.concourse.lang.sort.NoOrder) Sorting(com.cinchapi.concourse.server.query.sort.Sorting) Store(com.cinchapi.concourse.server.storage.Store) Multimap(com.google.common.collect.Multimap) Supplier(java.util.function.Supplier) ConcourseCompiler(com.cinchapi.concourse.lang.ConcourseCompiler) NoPage(com.cinchapi.concourse.lang.paginate.NoPage) LinkedHashMap(java.util.LinkedHashMap) JsonElement(com.google.gson.JsonElement) ParseException(com.cinchapi.concourse.thrift.ParseException) Lists(com.google.common.collect.Lists) TObject(com.cinchapi.concourse.thrift.TObject) GlobalState(com.cinchapi.concourse.server.GlobalState) TMaps(com.cinchapi.concourse.util.TMaps) NoSuchElementException(java.util.NoSuchElementException) Time(com.cinchapi.concourse.time.Time) Order(com.cinchapi.concourse.lang.sort.Order) Maps(com.google.common.collect.Maps) Finder(com.cinchapi.concourse.server.query.Finder) DataServices(com.cinchapi.concourse.util.DataServices) TObject(com.cinchapi.concourse.thrift.TObject) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Link(com.cinchapi.concourse.Link) ResolvableLink(com.cinchapi.concourse.util.Convert.ResolvableLink)

Example 39 with TObject

use of com.cinchapi.concourse.thrift.TObject in project concourse by cinchapi.

the class Operations method calculateKeyAtomic.

/**
 * Use the provided {@link AtomicOperation atomic} operation to perform the
 * specified {@code calculation} across the {@code key} at
 * {@code timestamp}.
 *
 * @param key the field name
 * @param timestamp the selection timestamp
 * @param result the running result
 * @param store the {@link AtomicOperation} to use
 * @param calculation the calculation logic
 * @return the result after applying the {@code calculation}
 */
private static Number calculateKeyAtomic(String key, long timestamp, Number result, Store store, KeyCalculation calculation) {
    checkAtomicity(store, timestamp);
    Map<TObject, Set<Long>> data = Stores.browse(store, key, timestamp);
    for (Entry<TObject, Set<Long>> entry : data.entrySet()) {
        TObject tobject = entry.getKey();
        Set<Long> records = entry.getValue();
        Object value = Convert.thriftToJava(tobject);
        Calculations.checkCalculatable(value);
        result = calculation.calculate(result, (Number) value, records);
    }
    return result;
}
Also used : TObject(com.cinchapi.concourse.thrift.TObject) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) TObject(com.cinchapi.concourse.thrift.TObject)

Example 40 with TObject

use of com.cinchapi.concourse.thrift.TObject in project concourse by cinchapi.

the class Operations method clearRecordAtomic.

/**
 * Do the work to remove all the data from {@code record} using the
 * specified {@code atomic} operation.
 *
 * @param record
 * @param atomic
 */
public static void clearRecordAtomic(long record, AtomicOperation atomic) {
    Map<String, Set<TObject>> values = atomic.select(record);
    for (Map.Entry<String, Set<TObject>> entry : values.entrySet()) {
        String key = entry.getKey();
        Set<TObject> valueSet = entry.getValue();
        for (TObject value : valueSet) {
            atomic.remove(key, value, record);
        }
    }
}
Also used : TObject(com.cinchapi.concourse.thrift.TObject) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

TObject (com.cinchapi.concourse.thrift.TObject)242 Test (org.junit.Test)100 ComplexTObject (com.cinchapi.concourse.thrift.ComplexTObject)98 Set (java.util.Set)98 AtomicSupport (com.cinchapi.concourse.server.storage.AtomicSupport)96 TranslateClientExceptions (com.cinchapi.concourse.server.aop.TranslateClientExceptions)91 VerifyAccessToken (com.cinchapi.concourse.server.aop.VerifyAccessToken)91 VerifyReadPermission (com.cinchapi.concourse.server.aop.VerifyReadPermission)88 Map (java.util.Map)76 AbstractSyntaxTree (com.cinchapi.ccl.syntax.AbstractSyntaxTree)72 SortableTable (com.cinchapi.concourse.data.sort.SortableTable)71 Store (com.cinchapi.concourse.server.storage.Store)66 Order (com.cinchapi.concourse.lang.sort.Order)63 SortableColumn (com.cinchapi.concourse.data.sort.SortableColumn)61 SortableSet (com.cinchapi.concourse.data.sort.SortableSet)60 ByteBuffer (java.nio.ByteBuffer)60 Operator (com.cinchapi.concourse.thrift.Operator)59 Entry (java.util.Map.Entry)59 Convert (com.cinchapi.concourse.util.Convert)57 Sets (com.google.common.collect.Sets)57