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());
}
});
}
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());
}
});
}
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);
}
}
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;
}
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);
}
Aggregations