Search in sources :

Example 1 with ObjectResultDataset

use of com.cinchapi.concourse.server.plugin.data.ObjectResultDataset 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)1 ObjectResultDataset (com.cinchapi.concourse.server.plugin.data.ObjectResultDataset)1 TObjectResultDataset (com.cinchapi.concourse.server.plugin.data.TObjectResultDataset)1 ComplexTObject (com.cinchapi.concourse.thrift.ComplexTObject)1 Convert (com.cinchapi.concourse.util.Convert)1 ByteBuffer (java.nio.ByteBuffer)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1