Search in sources :

Example 1 with ConditionTree

use of com.cinchapi.ccl.syntax.ConditionTree in project concourse by cinchapi.

the class Command method init.

/**
 * Inspect the operation and populate the operation variables if they
 * haven't already been {@link #initialized}.
 */
private void init() {
    if (!initialized) {
        String function = method.getName();
        String[] toks = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, function).split("-");
        Association args = Association.of();
        if (function.equals("getServerVersion")) {
            operation = function;
        } else if (function.equals("verifyOrSet")) {
            operation = function;
            args.put("key", params[0]);
            args.put("value", params[1]);
            args.put("record", params[2]);
        } else if (function.equals("verifyAndSwap")) {
            operation = function;
            args.put("key", params[0]);
            args.put("record", params[2]);
            args.put("values", ImmutableList.of(params[1], params[3]));
        } else if (function.equals("findOrInsertCclJson")) {
            operation = "findOrInsert";
            args.put("ccl", params[0]);
        } else if (function.equals("findOrInsertCriteriaJson")) {
            operation = "findOrInsert";
            args.put("criteria", params[0]);
        } else if (function.equals("findOrAddKeyValue")) {
            operation = "findOrAdd";
            args.put("key", params[0]);
            conditionKeys = ImmutableSet.of((String) params[0]);
        } else {
            operation = toks[0];
            for (int i = 1; i < toks.length; ++i) {
                args.put(toks[i].toLowerCase(), params[i - 1]);
            }
        }
        // Parser
        if (args.containsKey("ccl")) {
            String ccl = args.fetch("ccl");
            conditionTree = (ConditionTree) ConcourseCompiler.get().parse(ccl);
        } else if (args.containsKey("criteria")) {
            TCriteria tcriteria = args.fetch("criteria");
            Criteria criteria = Language.translateFromThriftCriteria(tcriteria);
            conditionTree = (ConditionTree) ConcourseCompiler.get().parse(criteria.ccl());
        } else {
            conditionTree = null;
        }
        // operationKeys
        if (args.containsKey("key")) {
            String key = args.fetch("key");
            operationKeys = Sets.newHashSet(key);
        } else if (args.containsKey("keys")) {
            Collection<String> keys = args.fetch("keys");
            operationKeys = Sets.newHashSet(keys);
        } else {
            operationKeys = ImmutableSet.of();
        }
        // operationRecords
        if (args.containsKey("record")) {
            long record = args.fetch("record");
            operationRecords = Sets.newHashSet(record);
        } else if (args.containsKey("records")) {
            Collection<Long> records = args.fetch("records");
            operationRecords = Sets.newHashSet(records);
        } else {
            operationRecords = ImmutableSet.of();
        }
        // order
        if (args.containsKey("order")) {
            TOrder torder = args.fetch("order");
            order = JavaThriftBridge.convert(torder);
        } else {
            order = null;
        }
        // methods above.
        if (conditionKeys == null && conditionTree != null) {
            conditionKeys = ConcourseCompiler.get().analyze(conditionTree).keys();
        } else if (function.startsWith("findKey")) {
            conditionKeys = ImmutableSet.of((String) params[0]);
        } else if (conditionKeys == null) {
            conditionKeys = ImmutableSet.of();
        }
        // operationTimestamp
        if (args.containsKey("time")) {
            operationTimestamp = args.fetch("time");
        }
        initialized = true;
    }
}
Also used : Association(com.cinchapi.common.collect.Association) ConditionTree(com.cinchapi.ccl.syntax.ConditionTree) Collection(java.util.Collection) TCriteria(com.cinchapi.concourse.thrift.TCriteria) TCriteria(com.cinchapi.concourse.thrift.TCriteria) Criteria(com.cinchapi.concourse.lang.Criteria) TOrder(com.cinchapi.concourse.thrift.TOrder)

Example 2 with ConditionTree

use of com.cinchapi.ccl.syntax.ConditionTree in project concourse by cinchapi.

the class Convert method thriftToJava.

/**
 * Return the Java Object that represents {@code object}.
 *
 * @param object
 * @return the Object
 */
public static Object thriftToJava(TObject object) {
    Preconditions.checkState(object.getType() != null, "Cannot read value because it has been " + "created with a newer version of Concourse " + "Server. Please upgrade this client.");
    Object java = object.getJavaFormat();
    if (java == null) {
        ByteBuffer buffer = object.bufferForData();
        switch(object.getType()) {
            case BOOLEAN:
                java = ByteBuffers.getBoolean(buffer);
                break;
            case DOUBLE:
                java = buffer.getDouble();
                break;
            case FLOAT:
                java = buffer.getFloat();
                break;
            case INTEGER:
                java = buffer.getInt();
                break;
            case LINK:
                java = Link.to(buffer.getLong());
                break;
            case LONG:
                java = buffer.getLong();
                break;
            case TAG:
                java = ByteBuffers.getUtf8String(buffer);
                break;
            case TIMESTAMP:
                java = Timestamp.fromMicros(buffer.getLong());
                break;
            case FUNCTION:
                FunctionType type = Enums.parseIgnoreCase(FunctionType.class, buffer.get());
                long timestamp = buffer.getLong();
                int nameLength = buffer.getInt();
                String name = ByteBuffers.getUtf8String(ByteBuffers.get(buffer, nameLength));
                int keyLength;
                String key;
                switch(type) {
                    case INDEX:
                        key = ByteBuffers.getUtf8String(buffer);
                        java = new IndexFunction(name, key, timestamp);
                        break;
                    case KEY_RECORDS:
                        keyLength = buffer.getInt();
                        key = ByteBuffers.getUtf8String(ByteBuffers.get(buffer, keyLength));
                        ArrayBuilder<Long> ab = ArrayBuilder.builder();
                        while (buffer.hasRemaining()) {
                            long record = buffer.getLong();
                            ab.add(record);
                        }
                        java = new KeyRecordsFunction(timestamp, name, key, ab.build());
                        break;
                    case KEY_CONDITION:
                        keyLength = buffer.getInt();
                        key = ByteBuffers.getUtf8String(ByteBuffers.get(buffer, keyLength));
                        String condition = ByteBuffers.getUtf8String(buffer);
                        ConditionTree tree = (ConditionTree) ConcourseCompiler.get().parse(condition);
                        java = new KeyConditionFunction(name, key, tree, timestamp);
                        break;
                }
                break;
            case NULL:
                java = null;
                break;
            default:
                java = ByteBuffers.getUtf8String(buffer);
                break;
        }
        buffer.rewind();
    }
    return java;
}
Also used : IndexFunction(com.cinchapi.ccl.type.function.IndexFunction) KeyRecordsFunction(com.cinchapi.ccl.type.function.KeyRecordsFunction) ConditionTree(com.cinchapi.ccl.syntax.ConditionTree) TObject(com.cinchapi.concourse.thrift.TObject) KeyConditionFunction(com.cinchapi.ccl.type.function.KeyConditionFunction) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ConditionTree (com.cinchapi.ccl.syntax.ConditionTree)2 IndexFunction (com.cinchapi.ccl.type.function.IndexFunction)1 KeyConditionFunction (com.cinchapi.ccl.type.function.KeyConditionFunction)1 KeyRecordsFunction (com.cinchapi.ccl.type.function.KeyRecordsFunction)1 Association (com.cinchapi.common.collect.Association)1 Criteria (com.cinchapi.concourse.lang.Criteria)1 TCriteria (com.cinchapi.concourse.thrift.TCriteria)1 TObject (com.cinchapi.concourse.thrift.TObject)1 TOrder (com.cinchapi.concourse.thrift.TOrder)1 ByteBuffer (java.nio.ByteBuffer)1 Collection (java.util.Collection)1