use of com.cinchapi.concourse.lang.sort.Order in project concourse by cinchapi.
the class SortingTest method testSortMultipleKeysSort.
@Test
public void testSortMultipleKeysSort() {
Map<Long, Map<String, Set<TObject>>> records = Maps.newLinkedHashMap();
List<String> keys = Lists.newArrayList("name", "company", "age");
Map<String, Set<TObject>> entry = TMaps.newLinkedHashMapWithCapacity(keys.size());
Set<TObject> values = Sets.newHashSet(Convert.javaToThrift("jeff"));
entry.put("name", values);
values = Sets.newHashSet(Convert.javaToThrift("Cinchapi"));
entry.put("company", values);
values = Sets.newHashSet(Convert.javaToThrift(100));
entry.put("age", values);
TMaps.putResultDatasetOptimized(records, Integer.toUnsignedLong(1), entry);
entry = TMaps.newLinkedHashMapWithCapacity(keys.size());
values = Sets.newHashSet(Convert.javaToThrift("ashleah"));
entry.put("name", values);
values = Sets.newHashSet(Convert.javaToThrift("ARMN Inc."));
entry.put("company", values);
values = Sets.newHashSet(Convert.javaToThrift(100));
entry.put("age", values);
TMaps.putResultDatasetOptimized(records, Integer.toUnsignedLong(2), entry);
entry = TMaps.newLinkedHashMapWithCapacity(keys.size());
values = Sets.newHashSet(Convert.javaToThrift("mark"));
entry.put("name", values);
values = Sets.newHashSet(Convert.javaToThrift("Cinchapi"));
entry.put("company", values);
values = Sets.newHashSet(Convert.javaToThrift(50));
entry.put("age", values);
TMaps.putResultDatasetOptimized(records, Integer.toUnsignedLong(3), entry);
entry = TMaps.newLinkedHashMapWithCapacity(keys.size());
values = Sets.newHashSet(Convert.javaToThrift("jeffB"));
entry.put("name", values);
values = Sets.newHashSet(Convert.javaToThrift("Blavity"));
entry.put("company", values);
values = Sets.newHashSet(Convert.javaToThrift(1));
entry.put("age", values);
TMaps.putResultDatasetOptimized(records, Integer.toUnsignedLong(4), entry);
Order order = Order.by("age").ascending().then("company").ascending().build();
Store store = new Queue(1);
Map<Long, Map<String, Set<TObject>>> result = Sorting.byValues(order, store).organize(records);
List<Long> expectedSort = Lists.newArrayList();
expectedSort.add(Integer.toUnsignedLong(4));
expectedSort.add(Integer.toUnsignedLong(3));
expectedSort.add(Integer.toUnsignedLong(2));
expectedSort.add(Integer.toUnsignedLong(1));
List<Long> sort = Lists.newArrayList(result.keySet());
Assert.assertEquals(expectedSort, sort);
}
use of com.cinchapi.concourse.lang.sort.Order in project concourse by cinchapi.
the class SortingTest method testIntAscendingSort.
@Test
public void testIntAscendingSort() {
Map<Long, Map<String, Set<TObject>>> records = Maps.newLinkedHashMap();
List<String> keys = Lists.newArrayList("name", "company", "age");
Map<String, Set<TObject>> entry = TMaps.newLinkedHashMapWithCapacity(keys.size());
Set<TObject> values = Sets.newHashSet(Convert.javaToThrift("jeff"));
entry.put("name", values);
values = Sets.newHashSet(Convert.javaToThrift("Cinchapi"));
entry.put("company", values);
values = Sets.newHashSet(Convert.javaToThrift(50));
entry.put("age", values);
TMaps.putResultDatasetOptimized(records, Integer.toUnsignedLong(1), entry);
entry = TMaps.newLinkedHashMapWithCapacity(keys.size());
values = Sets.newHashSet(Convert.javaToThrift("jeffB"));
entry.put("name", values);
values = Sets.newHashSet(Convert.javaToThrift("Blavity"));
entry.put("company", values);
values = Sets.newHashSet(Convert.javaToThrift(100));
entry.put("age", values);
TMaps.putResultDatasetOptimized(records, Integer.toUnsignedLong(2), entry);
entry = TMaps.newLinkedHashMapWithCapacity(keys.size());
values = Sets.newHashSet(Convert.javaToThrift("ashleah"));
entry.put("name", values);
values = Sets.newHashSet(Convert.javaToThrift("ARMN Inc."));
entry.put("company", values);
values = Sets.newHashSet(Convert.javaToThrift(100));
entry.put("age", values);
TMaps.putResultDatasetOptimized(records, Integer.toUnsignedLong(3), entry);
entry = TMaps.newLinkedHashMapWithCapacity(keys.size());
values = Sets.newHashSet(Convert.javaToThrift("mark"));
entry.put("name", values);
values = Sets.newHashSet(Convert.javaToThrift("Cinchapi"));
entry.put("company", values);
values = Sets.newHashSet(Convert.javaToThrift(25));
entry.put("age", values);
TMaps.putResultDatasetOptimized(records, Integer.toUnsignedLong(4), entry);
Order order = Order.by("age").ascending().build();
Store store = new Queue(1);
Map<Long, Map<String, Set<TObject>>> result = Sorting.byValues(order, store).organize(records);
List<Long> expectedSort = Lists.newArrayList();
expectedSort.add(Integer.toUnsignedLong(4));
expectedSort.add(Integer.toUnsignedLong(1));
expectedSort.add(Integer.toUnsignedLong(2));
expectedSort.add(Integer.toUnsignedLong(3));
List<Long> sort = Lists.newArrayList(result.keySet());
Assert.assertEquals(expectedSort, sort);
}
use of com.cinchapi.concourse.lang.sort.Order in project concourse by cinchapi.
the class SortingTest method testUnequalValueQuantityAscendingSort.
@Test
public void testUnequalValueQuantityAscendingSort() {
Map<Long, Map<String, Set<TObject>>> records = Maps.newLinkedHashMap();
List<String> keys = Lists.newArrayList("name", "company", "age");
Map<String, Set<TObject>> entry = TMaps.newLinkedHashMapWithCapacity(keys.size());
Set<TObject> values = Sets.newHashSet(Convert.javaToThrift("jeff"));
entry.put("name", values);
values = Sets.newHashSet(Convert.javaToThrift("Cinchapi"), Convert.javaToThrift("Blavity"));
entry.put("company", values);
values = Sets.newHashSet(Convert.javaToThrift(50));
entry.put("age", values);
TMaps.putResultDatasetOptimized(records, Integer.toUnsignedLong(1), entry);
entry = TMaps.newLinkedHashMapWithCapacity(keys.size());
values = Sets.newHashSet(Convert.javaToThrift("ashleah"));
entry.put("name", values);
values = Sets.newHashSet(Convert.javaToThrift("ARMN Inc."));
entry.put("company", values);
values = Sets.newHashSet(Convert.javaToThrift(25));
entry.put("age", values);
TMaps.putResultDatasetOptimized(records, Integer.toUnsignedLong(2), entry);
entry = TMaps.newLinkedHashMapWithCapacity(keys.size());
values = Sets.newHashSet(Convert.javaToThrift("mark"));
entry.put("name", values);
values = Sets.newHashSet(Convert.javaToThrift("Cinchapi"));
entry.put("company", values);
values = Sets.newHashSet(Convert.javaToThrift(100));
entry.put("age", values);
TMaps.putResultDatasetOptimized(records, Integer.toUnsignedLong(3), entry);
Order order = Order.by("company").ascending().build();
Store store = new Queue(1);
Map<Long, Map<String, Set<TObject>>> result = Sorting.byValues(order, store).organize(records);
List<Long> expectedSort = Lists.newArrayList();
expectedSort.add(Integer.toUnsignedLong(2));
expectedSort.add(Integer.toUnsignedLong(1));
expectedSort.add(Integer.toUnsignedLong(3));
List<Long> sort = Lists.newArrayList(result.keySet());
Assert.assertEquals(expectedSort, sort);
}
use of com.cinchapi.concourse.lang.sort.Order in project concourse by cinchapi.
the class SortingTest method testStringAscendingSort.
@Test
public void testStringAscendingSort() {
Map<Long, Map<String, Set<TObject>>> records = Maps.newLinkedHashMap();
List<String> keys = Lists.newArrayList("name", "company", "age");
Map<String, Set<TObject>> entry = TMaps.newLinkedHashMapWithCapacity(keys.size());
Set<TObject> values = Sets.newHashSet(Convert.javaToThrift("jeff"));
entry.put("name", values);
values = Sets.newHashSet(Convert.javaToThrift("Cinchapi"));
entry.put("company", values);
values = Sets.newHashSet(Convert.javaToThrift(50));
entry.put("age", values);
TMaps.putResultDatasetOptimized(records, Integer.toUnsignedLong(1), entry);
entry = TMaps.newLinkedHashMapWithCapacity(keys.size());
values = Sets.newHashSet(Convert.javaToThrift("jeffB"));
entry.put("name", values);
values = Sets.newHashSet(Convert.javaToThrift("Blavity"));
entry.put("company", values);
values = Sets.newHashSet(Convert.javaToThrift(100));
entry.put("age", values);
TMaps.putResultDatasetOptimized(records, Integer.toUnsignedLong(2), entry);
entry = TMaps.newLinkedHashMapWithCapacity(keys.size());
values = Sets.newHashSet(Convert.javaToThrift("ashleah"));
entry.put("name", values);
values = Sets.newHashSet(Convert.javaToThrift("ARMN Inc."));
entry.put("company", values);
values = Sets.newHashSet(Convert.javaToThrift(100));
entry.put("age", values);
TMaps.putResultDatasetOptimized(records, Integer.toUnsignedLong(3), entry);
entry = TMaps.newLinkedHashMapWithCapacity(keys.size());
values = Sets.newHashSet(Convert.javaToThrift("mark"));
entry.put("name", values);
values = Sets.newHashSet(Convert.javaToThrift("Cinchapi"));
entry.put("company", values);
values = Sets.newHashSet(Convert.javaToThrift(25));
entry.put("age", values);
TMaps.putResultDatasetOptimized(records, Integer.toUnsignedLong(4), entry);
Order order = Order.by("name").ascending().build();
Store store = new Queue(1);
Map<Long, Map<String, Set<TObject>>> result = Sorting.byValues(order, store).organize(records);
List<Long> expectedSort = Lists.newArrayList();
expectedSort.add(Integer.toUnsignedLong(3));
expectedSort.add(Integer.toUnsignedLong(1));
expectedSort.add(Integer.toUnsignedLong(2));
expectedSort.add(Integer.toUnsignedLong(4));
List<Long> sort = Lists.newArrayList(result.keySet());
Assert.assertEquals(expectedSort, sort);
}
use of com.cinchapi.concourse.lang.sort.Order in project concourse by cinchapi.
the class ConcourseRuntime method invokeServer.
/**
* Invoke {@code method} with {@code args} on the local Concourse Server
* instance that is associated with this {@link ConcourseRuntime runtime}.
*
* @param method the name of the method to invoke
* @param args the args to pass to the method
* @return the result of the method invocation
*/
@NoGuice
@SuppressWarnings("unchecked")
private static <T> T invokeServer(String method, Object... args) {
try {
ConcourseRuntimeAuthorized thread = (ConcourseRuntimeAuthorized) Thread.currentThread();
List<ComplexTObject> targs = Lists.newArrayListWithCapacity(args.length);
Collection<Integer> valueTransform = VALUE_TRANSFORM.get(method);
Collection<Integer> criteriaTransform = CRITERIA_TRANSFORM.get(method);
Collection<Integer> orderTransform = ORDER_TRANSFORM.get(method);
Collection<Integer> pageTransform = PAGE_TRANSFORM.get(method);
for (int i = 0; i < args.length; ++i) {
// Must go through each parameters and transform generic value
// objects into TObjects; all Criteria into TCriteria and all
// Order into TOrder
Object arg = args[i];
if (valueTransform.contains(i)) {
if (arg instanceof List) {
arg = ((List<Object>) arg).stream().map(Convert::javaToThrift).collect(Collectors.toList());
} else if (arg instanceof Set) {
arg = ((Set<Object>) arg).stream().map(Convert::javaToThrift).collect(Collectors.toSet());
} else if (arg instanceof Map) {
arg = ((Map<?, Object>) arg).entrySet().stream().collect(Collectors.toMap(Entry::getKey, e -> Convert.javaToThrift(e)));
} else {
arg = Convert.javaToThrift(arg);
}
} else if (criteriaTransform.contains(i)) {
arg = Language.translateToThriftCriteria((Criteria) arg);
} else if (orderTransform.contains(i)) {
arg = JavaThriftBridge.convert((Order) arg);
} else if (pageTransform.contains(i)) {
arg = JavaThriftBridge.convert((Page) arg);
}
targs.add(ComplexTObject.fromJavaObject(arg));
}
// Send a RemoteMethodRequest to the server, asking that the locally
// invoked method be executed. The result will be placed on the
// current thread's response queue
RemoteMethodResponse response;
synchronized (thread.accessToken()) {
RemoteMethodRequest request = new RemoteMethodRequest(method, thread.accessToken(), thread.transactionToken(), thread.environment(), targs);
ByteBuffer buffer = serializer.serialize(request);
thread.outgoing().write(buffer);
response = ConcurrentMaps.waitAndRemove(thread.responses(), thread.accessToken());
}
if (!response.isError()) {
Object ret = response.response.getJavaObject();
if (ret instanceof ByteBuffer) {
// CON-509: PluginSerializable objects will be wrapped
// within a ComplexTObject as BINARY data
ret = serializer.deserialize((ByteBuffer) ret);
}
if (RETURN_TRANSFORM.contains(method)) {
// StatefulConcourseService interface.
if (ret instanceof TObjectResultDataset) {
ret = new ObjectResultDataset((TObjectResultDataset) ret);
} else {
ret = Convert.possibleThriftToJava(ret);
}
}
return (T) ret;
} else {
throw CheckedExceptions.wrapAsRuntimeException(response.error);
}
} catch (ClassCastException e) {
throw new RuntimeException("Illegal attempt to use " + ConcourseRuntime.class.getSimpleName() + " from an unsupported thread");
}
}
Aggregations