Search in sources :

Example 26 with Order

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);
}
Also used : Order(com.cinchapi.concourse.lang.sort.Order) TObject(com.cinchapi.concourse.thrift.TObject) Set(java.util.Set) Store(com.cinchapi.concourse.server.storage.Store) Map(java.util.Map) Queue(com.cinchapi.concourse.server.storage.temp.Queue) Test(org.junit.Test)

Example 27 with Order

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);
}
Also used : Order(com.cinchapi.concourse.lang.sort.Order) TObject(com.cinchapi.concourse.thrift.TObject) Set(java.util.Set) Store(com.cinchapi.concourse.server.storage.Store) Map(java.util.Map) Queue(com.cinchapi.concourse.server.storage.temp.Queue) Test(org.junit.Test)

Example 28 with Order

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);
}
Also used : Order(com.cinchapi.concourse.lang.sort.Order) TObject(com.cinchapi.concourse.thrift.TObject) Set(java.util.Set) Store(com.cinchapi.concourse.server.storage.Store) Map(java.util.Map) Queue(com.cinchapi.concourse.server.storage.temp.Queue) Test(org.junit.Test)

Example 29 with Order

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);
}
Also used : Order(com.cinchapi.concourse.lang.sort.Order) TObject(com.cinchapi.concourse.thrift.TObject) Set(java.util.Set) Store(com.cinchapi.concourse.server.storage.Store) Map(java.util.Map) Queue(com.cinchapi.concourse.server.storage.temp.Queue) Test(org.junit.Test)

Example 30 with Order

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");
    }
}
Also used : Order(com.cinchapi.concourse.lang.sort.Order) Convert(com.cinchapi.concourse.util.Convert) Set(java.util.Set) ComplexTObject(com.cinchapi.concourse.thrift.ComplexTObject) TObjectResultDataset(com.cinchapi.concourse.server.plugin.data.TObjectResultDataset) ByteBuffer(java.nio.ByteBuffer) Entry(java.util.Map.Entry) ObjectResultDataset(com.cinchapi.concourse.server.plugin.data.ObjectResultDataset) TObjectResultDataset(com.cinchapi.concourse.server.plugin.data.TObjectResultDataset) ComplexTObject(com.cinchapi.concourse.thrift.ComplexTObject) List(java.util.List) Map(java.util.Map)

Aggregations

Order (com.cinchapi.concourse.lang.sort.Order)42 Map (java.util.Map)41 Set (java.util.Set)41 Store (com.cinchapi.concourse.server.storage.Store)40 TObject (com.cinchapi.concourse.thrift.TObject)40 AbstractSyntaxTree (com.cinchapi.ccl.syntax.AbstractSyntaxTree)31 NaturalLanguage (com.cinchapi.ccl.util.NaturalLanguage)31 AnyStrings (com.cinchapi.common.base.AnyStrings)31 Array (com.cinchapi.common.base.Array)31 CheckedExceptions (com.cinchapi.common.base.CheckedExceptions)31 Reflection (com.cinchapi.common.reflect.Reflection)31 Constants (com.cinchapi.concourse.Constants)31 Link (com.cinchapi.concourse.Link)31 Timestamp (com.cinchapi.concourse.Timestamp)31 SortableColumn (com.cinchapi.concourse.data.sort.SortableColumn)31 SortableSet (com.cinchapi.concourse.data.sort.SortableSet)31 SortableTable (com.cinchapi.concourse.data.sort.SortableTable)31 ConcourseCompiler (com.cinchapi.concourse.lang.ConcourseCompiler)31 Language (com.cinchapi.concourse.lang.Language)31 Permission (com.cinchapi.concourse.security.Permission)31