Search in sources :

Example 1 with Direction

use of com.cinchapi.concourse.lang.sort.Direction 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)

Example 2 with Direction

use of com.cinchapi.concourse.lang.sort.Direction in project concourse by cinchapi.

the class JavaThriftBridge method convert.

/**
 * Return a {@link TOrderComponent} to an {@link OrderComponent}.
 *
 * @param tcomponent
 * @return the analogous {@link OrderComponent}
 */
public static OrderComponent convert(TOrderComponent tcomponent) {
    Object $timestamp = tcomponent.isSetTimestamp() ? Convert.thriftToJava(tcomponent.getTimestamp()) : null;
    if ($timestamp != null && !($timestamp instanceof Number)) {
        // handled in the Sorter.
        try {
            $timestamp = NaturalLanguage.parseMicros($timestamp.toString());
        } catch (Exception e) {
        /* ignore */
        }
    }
    Timestamp timestamp = $timestamp != null ? ($timestamp instanceof Number ? Timestamp.fromMicros(((Number) $timestamp).longValue()) : Timestamp.fromString($timestamp.toString())) : null;
    Direction direction = null;
    for (Direction $direction : Direction.values()) {
        if (tcomponent.getDirection() == $direction.coefficient()) {
            direction = $direction;
            break;
        }
    }
    return new OrderComponent(tcomponent.getKey(), timestamp, direction);
}
Also used : OrderComponent(com.cinchapi.concourse.lang.sort.OrderComponent) Timestamp(com.cinchapi.concourse.Timestamp) Direction(com.cinchapi.concourse.lang.sort.Direction)

Aggregations

Timestamp (com.cinchapi.concourse.Timestamp)2 Direction (com.cinchapi.concourse.lang.sort.Direction)2 OrderComponent (com.cinchapi.concourse.lang.sort.OrderComponent)2 ArrayBuilder (com.cinchapi.common.base.ArrayBuilder)1 Empty (com.cinchapi.common.describe.Empty)1 Sorter (com.cinchapi.concourse.data.sort.Sorter)1 Order (com.cinchapi.concourse.lang.sort.Order)1 Store (com.cinchapi.concourse.server.storage.Store)1 Comparator (java.util.Comparator)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Stream (java.util.stream.Stream)1 Nullable (javax.annotation.Nullable)1