Search in sources :

Example 1 with ArrayBuilder

use of com.cinchapi.common.base.ArrayBuilder 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 2 with ArrayBuilder

use of com.cinchapi.common.base.ArrayBuilder in project concourse by cinchapi.

the class StoreSorter method sort.

@Override
public final Stream<Entry<Long, Map<String, V>>> sort(Map<Long, Map<String, V>> data, @Nullable Long at) {
    ArrayBuilder<Comparator<Entry<Long, Map<String, V>>>> comparators = ArrayBuilder.builder();
    for (OrderComponent component : order.spec()) {
        String key = component.key();
        Timestamp timestamp = component.timestamp();
        Direction direction = component.direction();
        Comparator<Entry<Long, Map<String, V>>> $comparator = (e1, e2) -> {
            long r1 = e1.getKey();
            long r2 = e2.getKey();
            Map<String, V> d1 = e1.getValue();
            Map<String, V> d2 = e2.getValue();
            V v1;
            V v2;
            if (timestamp == null) {
                v1 = d1.get(key);
                if (v1 == null) {
                    v1 = at != null ? lookup(key, r1, at) : lookup(key, r1);
                }
                v2 = d2.get(key);
                if (v2 == null) {
                    v2 = at != null ? lookup(key, r2, at) : lookup(key, r2);
                }
            } else {
                v1 = lookup(key, r1, timestamp.getMicros());
                v2 = lookup(key, r2, timestamp.getMicros());
            }
            if (!Empty.ness().describes(v1) && !Empty.ness().describes(v2)) {
                // end of the sort, regardless of the specified direction
                return direction.coefficient() * compare(v1, v2);
            } else if (!Empty.ness().describes(v1)) {
                return -1;
            } else if (!Empty.ness().describes(v2)) {
                return 1;
            } else {
                return 0;
            }
        };
        comparators.add($comparator);
    }
    comparators.add((e1, e2) -> e1.getKey().compareTo(e2.getKey()));
    Comparator<Entry<Long, Map<String, V>>> comparator = CompoundComparator.of(comparators.build());
    return data.entrySet().stream().sorted(comparator);
}
Also used : ArrayBuilder(com.cinchapi.common.base.ArrayBuilder) Sorter(com.cinchapi.concourse.data.sort.Sorter) Order(com.cinchapi.concourse.lang.sort.Order) Timestamp(com.cinchapi.concourse.Timestamp) Store(com.cinchapi.concourse.server.storage.Store) Direction(com.cinchapi.concourse.lang.sort.Direction) OrderComponent(com.cinchapi.concourse.lang.sort.OrderComponent) Stream(java.util.stream.Stream) Empty(com.cinchapi.common.describe.Empty) Map(java.util.Map) Entry(java.util.Map.Entry) Comparator(java.util.Comparator) Nullable(javax.annotation.Nullable) OrderComponent(com.cinchapi.concourse.lang.sort.OrderComponent) Entry(java.util.Map.Entry) Map(java.util.Map) Timestamp(com.cinchapi.concourse.Timestamp) Direction(com.cinchapi.concourse.lang.sort.Direction) Comparator(java.util.Comparator)

Aggregations

ArrayBuilder (com.cinchapi.common.base.ArrayBuilder)2 Store (com.cinchapi.concourse.server.storage.Store)2 Map (java.util.Map)2 Function (com.cinchapi.ccl.type.Function)1 IndexFunction (com.cinchapi.ccl.type.function.IndexFunction)1 KeyConditionFunction (com.cinchapi.ccl.type.function.KeyConditionFunction)1 KeyRecordsFunction (com.cinchapi.ccl.type.function.KeyRecordsFunction)1 TemporalFunction (com.cinchapi.ccl.type.function.TemporalFunction)1 AdHocIterator (com.cinchapi.common.base.AdHocIterator)1 Empty (com.cinchapi.common.describe.Empty)1 Reflection (com.cinchapi.common.reflect.Reflection)1 Constants (com.cinchapi.concourse.Constants)1 Link (com.cinchapi.concourse.Link)1 Timestamp (com.cinchapi.concourse.Timestamp)1 Sorter (com.cinchapi.concourse.data.sort.Sorter)1 Direction (com.cinchapi.concourse.lang.sort.Direction)1 Order (com.cinchapi.concourse.lang.sort.Order)1 OrderComponent (com.cinchapi.concourse.lang.sort.OrderComponent)1 Calculations (com.cinchapi.concourse.server.calculate.Calculations)1 Source (com.cinchapi.concourse.server.ops.Strategy.Source)1