use of com.cinchapi.concourse.lang.sort.OrderComponent 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);
}
use of com.cinchapi.concourse.lang.sort.OrderComponent 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);
}
use of com.cinchapi.concourse.lang.sort.OrderComponent in project concourse by cinchapi.
the class JavaThriftBridgeTest method testOrderComponentWithStringTimestampConvert.
@Test
public void testOrderComponentWithStringTimestampConvert() {
OrderComponent component = new OrderComponent("a", Timestamp.fromString("yesterday"), Direction.ASCENDING);
TOrderComponent tcomponent = JavaThriftBridge.convert(component);
Assert.assertTrue(tcomponent.getTimestamp().getType() == Type.STRING);
}
Aggregations