Search in sources :

Example 1 with Symbol

use of com.cinchapi.ccl.grammar.Symbol in project concourse by cinchapi.

the class CriteriaTest method testTimestampPinningSomeTimestamps.

@Test
public void testTimestampPinningSomeTimestamps() {
    Criteria criteria = Criteria.where().key("name").operator(Operator.EQUALS).value("Jeff Nelson").and().group(Criteria.where().key("company").operator(Operator.EQUALS).value("Cinchapi").at(Timestamp.now()).or().key("company").operator(Operator.EQUALS).value("Blavity")).build();
    Timestamp timestamp = Timestamp.now();
    criteria = criteria.at(timestamp);
    List<Symbol> symbols = Parsing.groupExpressions(criteria.symbols());
    symbols.forEach((symbol) -> {
        if (symbol instanceof ExpressionSymbol) {
            ExpressionSymbol expression = (ExpressionSymbol) symbol;
            Assert.assertEquals(expression.raw().timestamp(), timestamp.getMicros());
        }
    });
}
Also used : KeySymbol(com.cinchapi.ccl.grammar.KeySymbol) ExpressionSymbol(com.cinchapi.ccl.grammar.ExpressionSymbol) Symbol(com.cinchapi.ccl.grammar.Symbol) NavigationKeySymbol(com.cinchapi.ccl.grammar.NavigationKeySymbol) ExpressionSymbol(com.cinchapi.ccl.grammar.ExpressionSymbol) Timestamp(com.cinchapi.concourse.Timestamp) Test(org.junit.Test)

Example 2 with Symbol

use of com.cinchapi.ccl.grammar.Symbol in project concourse by cinchapi.

the class CriteriaTest method testTimestampPinning.

@Test
public void testTimestampPinning() {
    Criteria criteria = Criteria.where().key("name").operator(Operator.EQUALS).value("Jeff Nelson").and().group(Criteria.where().key("company").operator(Operator.EQUALS).value("Cinchapi").or().key("company").operator(Operator.EQUALS).value("Blavity")).build();
    Timestamp timestamp = Timestamp.now();
    criteria = criteria.at(timestamp);
    List<Symbol> symbols = Parsing.groupExpressions(criteria.symbols());
    symbols.forEach((symbol) -> {
        if (symbol instanceof ExpressionSymbol) {
            ExpressionSymbol expression = (ExpressionSymbol) symbol;
            Assert.assertEquals(expression.raw().timestamp(), timestamp.getMicros());
        }
    });
}
Also used : KeySymbol(com.cinchapi.ccl.grammar.KeySymbol) ExpressionSymbol(com.cinchapi.ccl.grammar.ExpressionSymbol) Symbol(com.cinchapi.ccl.grammar.Symbol) NavigationKeySymbol(com.cinchapi.ccl.grammar.NavigationKeySymbol) ExpressionSymbol(com.cinchapi.ccl.grammar.ExpressionSymbol) Timestamp(com.cinchapi.concourse.Timestamp) Test(org.junit.Test)

Example 3 with Symbol

use of com.cinchapi.ccl.grammar.Symbol in project concourse by cinchapi.

the class Convert method javaToThrift.

/**
 * Return the Thrift Object that represents {@code object}.
 *
 * @param object
 * @return the TObject
 */
public static TObject javaToThrift(Object object) {
    if (object == null) {
        return TObject.NULL;
    } else {
        ByteBuffer bytes;
        Type type = null;
        if (object instanceof Boolean) {
            bytes = ByteBuffer.allocate(1);
            bytes.put((boolean) object ? (byte) 1 : (byte) 0);
            type = Type.BOOLEAN;
        } else if (object instanceof Double) {
            bytes = ByteBuffer.allocate(8);
            bytes.putDouble((double) object);
            type = Type.DOUBLE;
        } else if (object instanceof Float) {
            bytes = ByteBuffer.allocate(4);
            bytes.putFloat((float) object);
            type = Type.FLOAT;
        } else if (object instanceof Link) {
            bytes = ByteBuffer.allocate(8);
            bytes.putLong(((Link) object).longValue());
            type = Type.LINK;
        } else if (object instanceof Long) {
            bytes = ByteBuffer.allocate(8);
            bytes.putLong((long) object);
            type = Type.LONG;
        } else if (object instanceof Integer) {
            bytes = ByteBuffer.allocate(4);
            bytes.putInt((int) object);
            type = Type.INTEGER;
        } else if (object instanceof BigDecimal) {
            bytes = ByteBuffer.allocate(8);
            bytes.putDouble((double) ((BigDecimal) object).doubleValue());
            type = Type.DOUBLE;
        } else if (object instanceof Tag) {
            bytes = ByteBuffer.wrap(object.toString().getBytes(StandardCharsets.UTF_8));
            type = Type.TAG;
        } else if (object instanceof Timestamp) {
            try {
                bytes = ByteBuffer.allocate(8);
                bytes.putLong(((Timestamp) object).getMicros());
                type = Type.TIMESTAMP;
            } catch (IllegalStateException e) {
                throw new UnsupportedOperationException("Cannot convert string based Timestamp to a TObject");
            }
        } else if (object instanceof Function) {
            type = Type.FUNCTION;
            Function function = (Function) object;
            byte[] nameBytes = function.operation().getBytes(StandardCharsets.UTF_8);
            byte[] keyBytes = function.key().getBytes(StandardCharsets.UTF_8);
            if (function instanceof IndexFunction) {
                /*
                     * Schema:
                     * | type (1) | timestamp(8) | nameLength (4) | name
                     * (nameLength) | key |
                     */
                bytes = ByteBuffer.allocate(1 + 8 + 4 + nameBytes.length + keyBytes.length);
                bytes.put((byte) FunctionType.INDEX.ordinal());
                bytes.putLong(((TemporalFunction) function).timestamp());
                bytes.putInt(nameBytes.length);
                bytes.put(nameBytes);
                bytes.put(keyBytes);
            } else if (function instanceof KeyRecordsFunction) {
                /*
                     * Schema:
                     * | type (1) | timestamp(8) | nameLength (4) | name
                     * (nameLength) | keyLength (4) | key (keyLength) | records
                     * (8 each) |
                     */
                KeyRecordsFunction func = (KeyRecordsFunction) function;
                bytes = ByteBuffer.allocate(1 + 8 + 4 + nameBytes.length + 4 + keyBytes.length + 8 * func.source().size());
                bytes.put((byte) FunctionType.KEY_RECORDS.ordinal());
                bytes.putLong(((TemporalFunction) function).timestamp());
                bytes.putInt(nameBytes.length);
                bytes.put(nameBytes);
                bytes.putInt(keyBytes.length);
                bytes.put(keyBytes);
                for (long record : func.source()) {
                    bytes.putLong(record);
                }
            } else if (function instanceof KeyConditionFunction) {
                /*
                     * Schema:
                     * | type (1) | timestamp(8) | nameLength (4) | name
                     * (nameLength) | keyLength (4) | key (keyLength) |
                     * condition |
                     */
                KeyConditionFunction func = (KeyConditionFunction) function;
                String condition = ConcourseCompiler.get().tokenize(func.source()).stream().map(Symbol::toString).collect(Collectors.joining(" "));
                bytes = ByteBuffer.allocate(1 + 9 + 4 + nameBytes.length + 4 + keyBytes.length + condition.length());
                bytes.put((byte) FunctionType.KEY_CONDITION.ordinal());
                bytes.putLong(((TemporalFunction) function).timestamp());
                bytes.putInt(nameBytes.length);
                bytes.put(nameBytes);
                bytes.putInt(keyBytes.length);
                bytes.put(keyBytes);
                bytes.put(condition.getBytes(StandardCharsets.UTF_8));
            } else {
                throw new UnsupportedOperationException("Cannot convert the following function to a TObject: " + function);
            }
        } else {
            bytes = ByteBuffer.wrap(object.toString().getBytes(StandardCharsets.UTF_8));
            type = Type.STRING;
        }
        bytes.rewind();
        return new TObject(bytes, type).setJavaFormat(object);
    }
}
Also used : TObject(com.cinchapi.concourse.thrift.TObject) KeyRecordsFunction(com.cinchapi.ccl.type.function.KeyRecordsFunction) FunctionValueSymbol(com.cinchapi.ccl.grammar.FunctionValueSymbol) Symbol(com.cinchapi.ccl.grammar.Symbol) ByteBuffer(java.nio.ByteBuffer) Timestamp(com.cinchapi.concourse.Timestamp) BigDecimal(java.math.BigDecimal) TemporalFunction(com.cinchapi.ccl.type.function.TemporalFunction) IndexFunction(com.cinchapi.ccl.type.function.IndexFunction) TemporalFunction(com.cinchapi.ccl.type.function.TemporalFunction) KeyRecordsFunction(com.cinchapi.ccl.type.function.KeyRecordsFunction) Function(com.cinchapi.ccl.type.Function) KeyConditionFunction(com.cinchapi.ccl.type.function.KeyConditionFunction) Type(com.cinchapi.concourse.thrift.Type) IndexFunction(com.cinchapi.ccl.type.function.IndexFunction) Tag(com.cinchapi.concourse.Tag) KeyConditionFunction(com.cinchapi.ccl.type.function.KeyConditionFunction) Link(com.cinchapi.concourse.Link)

Example 4 with Symbol

use of com.cinchapi.ccl.grammar.Symbol in project concourse by cinchapi.

the class BuiltCriteria method at.

/**
 * Return this {@link Criteria} with each expression (e.g. {key} {operator}
 * {values}) pinned to the specified {@code timestamp}.
 *
 * <strong>NOTE:</strong> Any timestamps that are pinned to any expressions
 * within this Criteria will be replaced by the specified {@code timestamp}.
 *
 * @param timestamp the {@link Timestamp} to which the returned
 *            {@link Criteria} is pinned
 *
 * @return this {@link Criteria} pinned to {@code timestamp}
 */
public Criteria at(Timestamp timestamp) {
    AbstractSyntaxTree ast = ConcourseCompiler.get().parse(ccl());
    List<Symbol> symbols = Parsing.groupExpressions(ConcourseCompiler.get().tokenize(ast));
    TimestampSymbol ts = new TimestampSymbol(timestamp.getMicros());
    symbols.forEach((symbol) -> {
        if (symbol instanceof ExpressionSymbol) {
            ExpressionSymbol expression = (ExpressionSymbol) symbol;
            expression.timestamp(ts);
        }
    });
    BuiltCriteria criteria = new BuiltCriteria();
    symbols = Parsing.ungroupExpressions(symbols);
    criteria.symbols = symbols;
    return criteria;
}
Also used : AbstractSyntaxTree(com.cinchapi.ccl.syntax.AbstractSyntaxTree) ExpressionSymbol(com.cinchapi.ccl.grammar.ExpressionSymbol) TimestampSymbol(com.cinchapi.ccl.grammar.TimestampSymbol) ParenthesisSymbol(com.cinchapi.ccl.grammar.ParenthesisSymbol) Symbol(com.cinchapi.ccl.grammar.Symbol) ExpressionSymbol(com.cinchapi.ccl.grammar.ExpressionSymbol) TimestampSymbol(com.cinchapi.ccl.grammar.TimestampSymbol)

Example 5 with Symbol

use of com.cinchapi.ccl.grammar.Symbol in project concourse by cinchapi.

the class ConcourseCompilerTest method testGroupSingle.

@Test
public void testGroupSingle() {
    String key = TestData.getString();
    Operator operator = Operator.EQUALS;
    Object value = TestData.getObject();
    Criteria criteria = Criteria.where().key(key).operator(operator).value(value).build();
    List<Symbol> symbols = Parsing.groupExpressions(criteria.symbols());
    ExpressionSymbol exp = (ExpressionSymbol) symbols.get(0);
    Assert.assertEquals(1, symbols.size());
    Assert.assertEquals(exp.raw().key(), key);
    Assert.assertEquals(exp.raw().operator(), operator);
    Assert.assertEquals(exp.values().get(0).value(), value);
}
Also used : Operator(com.cinchapi.concourse.thrift.Operator) KeySymbol(com.cinchapi.ccl.grammar.KeySymbol) PostfixNotationSymbol(com.cinchapi.ccl.grammar.PostfixNotationSymbol) Symbol(com.cinchapi.ccl.grammar.Symbol) OperatorSymbol(com.cinchapi.ccl.grammar.OperatorSymbol) ValueSymbol(com.cinchapi.ccl.grammar.ValueSymbol) ExpressionSymbol(com.cinchapi.ccl.grammar.ExpressionSymbol) ConjunctionSymbol(com.cinchapi.ccl.grammar.ConjunctionSymbol) ParenthesisSymbol(com.cinchapi.ccl.grammar.ParenthesisSymbol) ExpressionSymbol(com.cinchapi.ccl.grammar.ExpressionSymbol) Test(org.junit.Test)

Aggregations

Symbol (com.cinchapi.ccl.grammar.Symbol)12 ExpressionSymbol (com.cinchapi.ccl.grammar.ExpressionSymbol)11 ParenthesisSymbol (com.cinchapi.ccl.grammar.ParenthesisSymbol)9 KeySymbol (com.cinchapi.ccl.grammar.KeySymbol)8 Test (org.junit.Test)8 ConjunctionSymbol (com.cinchapi.ccl.grammar.ConjunctionSymbol)6 OperatorSymbol (com.cinchapi.ccl.grammar.OperatorSymbol)6 PostfixNotationSymbol (com.cinchapi.ccl.grammar.PostfixNotationSymbol)6 ValueSymbol (com.cinchapi.ccl.grammar.ValueSymbol)6 Operator (com.cinchapi.concourse.thrift.Operator)5 TimestampSymbol (com.cinchapi.ccl.grammar.TimestampSymbol)3 Timestamp (com.cinchapi.concourse.Timestamp)3 NavigationKeySymbol (com.cinchapi.ccl.grammar.NavigationKeySymbol)2 FunctionValueSymbol (com.cinchapi.ccl.grammar.FunctionValueSymbol)1 AbstractSyntaxTree (com.cinchapi.ccl.syntax.AbstractSyntaxTree)1 Function (com.cinchapi.ccl.type.Function)1 IndexFunction (com.cinchapi.ccl.type.function.IndexFunction)1 KeyConditionFunction (com.cinchapi.ccl.type.function.KeyConditionFunction)1 KeyRecordsFunction (com.cinchapi.ccl.type.function.KeyRecordsFunction)1 TemporalFunction (com.cinchapi.ccl.type.function.TemporalFunction)1