use of herddb.model.StatementEvaluationContext in project herddb by diennea.
the class TableSpaceManager method executePlannedOperationStatement.
private CompletableFuture<StatementExecutionResult> executePlannedOperationStatement(Statement statement, TransactionContext transactionContext, StatementEvaluationContext context) {
long lockStamp = context.getTableSpaceLock();
boolean lockAcquired = false;
if (lockStamp == 0) {
lockStamp = acquireReadLock(statement);
context.setTableSpaceLock(lockStamp);
lockAcquired = true;
}
SQLPlannedOperationStatement planned = (SQLPlannedOperationStatement) statement;
CompletableFuture<StatementExecutionResult> res;
try {
res = planned.getRootOp().executeAsync(this, transactionContext, context, false, false);
} catch (HerdDBInternalException err) {
// ensure we are able to release locks correctly
LOGGER.log(Level.SEVERE, "Internal error", err);
res = Futures.exception(err);
}
// });
if (lockAcquired) {
res = releaseReadLock(res, lockStamp, statement).thenApply(s -> {
context.setTableSpaceLock(0);
return s;
});
}
return res;
}
use of herddb.model.StatementEvaluationContext in project herddb by diennea.
the class RecordSetSuite method testApplyProjectionSwap.
@Test
public void testApplyProjectionSwap() throws Exception {
RecordSetFactory factory = buildRecordSetFactory(1);
Column[] columns = new Column[2];
columns[0] = Column.column("s1", ColumnTypes.STRING);
columns[1] = Column.column("n1", ColumnTypes.LONG);
Set<String> expected_s2 = new HashSet<>();
Set<Integer> expected_n2 = new HashSet<>();
String[] fieldNames = Column.buildFieldNamesList(columns);
try (MaterializedRecordSet rs = factory.createRecordSet(fieldNames, columns)) {
for (int i = 0; i < 100; i++) {
Map<String, Object> record = new HashMap<>();
String s1 = "test_" + i;
record.put("s1", s1);
record.put("n1", i);
expected_s2.add(s1);
expected_n2.add(i);
rs.add(new Tuple(record, fieldNames));
}
rs.writeFinished();
Column[] columns_projected = new Column[2];
columns_projected[0] = Column.column("n2", ColumnTypes.LONG);
columns_projected[1] = Column.column("s2", ColumnTypes.STRING);
String[] fieldNames_projected = new String[] { "n2", "s2" };
rs.applyProjection(new Projection() {
@Override
public Column[] getColumns() {
return columns_projected;
}
@Override
public String[] getFieldNames() {
return fieldNames_projected;
}
@Override
public Tuple map(DataAccessor tuple, StatementEvaluationContext context) throws StatementExecutionException {
Object[] projected_values = new Object[2];
projected_values[0] = tuple.get("n1");
projected_values[1] = tuple.get("s1");
return new Tuple(fieldNames_projected, projected_values);
}
}, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT());
for (DataAccessor t : rs) {
System.out.println("t:" + t.toMap());
expected_s2.remove(t.get("s2").toString());
expected_n2.remove(t.get("n2"));
}
assertTrue(expected_s2.isEmpty());
assertTrue(expected_n2.isEmpty());
assertEquals("n2", rs.getColumns()[0].name);
assertEquals("s2", rs.getColumns()[1].name);
}
}
use of herddb.model.StatementEvaluationContext in project herddb by diennea.
the class ScanTest method test.
@Test
public void test() throws Exception {
for (int i = 0; i < 100; i++) {
Map<String, Object> data = new HashMap<>();
data.put("id", "key_" + i);
data.put("number", i);
Record record = RecordSerializer.toRecord(data, table);
InsertStatement st = new InsertStatement(tableSpace, tableName, record);
assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
}
{
ScanStatement scan = new ScanStatement(tableSpace, table, new Predicate() {
@Override
public boolean evaluate(Record record, StatementEvaluationContext context) throws StatementExecutionException {
int value = (Integer) record.toBean(table).get("number");
return value >= 50;
}
});
DataScanner scanner = manager.scan(scan, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
List<?> result = scanner.consumeAndClose();
assertEquals(50, result.size());
}
for (int i = 0; i < 20; i++) {
DeleteStatement st = new DeleteStatement(tableSpace, tableName, Bytes.from_string("key_" + i), null);
assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
}
{
ScanStatement scan = new ScanStatement(tableSpace, table, new Predicate() {
@Override
public boolean evaluate(Record record, StatementEvaluationContext context) throws StatementExecutionException {
int value = (Integer) record.toBean(table).get("number");
return value < 50;
}
});
DataScanner scanner = manager.scan(scan, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
List<?> result = scanner.consumeAndClose();
assertEquals(30, result.size());
}
{
ScanStatement scan = new ScanStatement(tableSpace, table, null);
DataScanner scanner = manager.scan(scan, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
List<?> result = scanner.consumeAndClose();
assertEquals(80, result.size());
}
}
use of herddb.model.StatementEvaluationContext in project herddb by diennea.
the class SQLRecordPredicateTest method testEvaluateExpression.
@Test
public void testEvaluateExpression() throws Exception {
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
assertTrue(manager.waitForTablespace(TableSpace.DEFAULT, 10000));
Table table = Table.builder().name("t1").column("pk", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("pk").build();
CreateTableStatement st2 = new CreateTableStatement(table);
manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * " + "FROM t1 " + "where pk >= ?", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate() instanceof SQLRecordPredicate);
SQLRecordPredicate pred = (SQLRecordPredicate) scan.getPredicate();
assertTrue(pred.getPrimaryKeyFilter() != null && (pred.getPrimaryKeyFilter() instanceof CompiledSQLExpression));
assertTrue(pred.getWhere() != null && (pred.getWhere() instanceof CompiledSQLExpression));
StatementEvaluationContext ctx = new SQLStatementEvaluationContext("the-query", Arrays.asList("my-string"), false, false);
Record record = RecordSerializer.makeRecord(table, "pk", "test", "name", "myname");
assertEquals(Boolean.TRUE, pred.evaluate(record, ctx));
long start = System.currentTimeMillis();
int size = 20_000_000;
for (int i = 0; i < size; i++) {
pred.evaluate(record, ctx);
}
long end = System.currentTimeMillis();
double speed = (int) (size * 1000d / (end - start));
System.out.println("speed: " + speed + " eval/s" + " per " + size + " records");
}
}
}
Aggregations