use of herddb.model.commands.GetStatement in project herddb by diennea.
the class IndexCreationTest method recoverTableAndIndexWithCheckpoint.
private void recoverTableAndIndexWithCheckpoint(String indexType) throws Exception {
Path dataPath = folder.newFolder("data").toPath();
Path logsPath = folder.newFolder("logs").toPath();
Path metadataPath = folder.newFolder("metadata").toPath();
Path tmoDir = folder.newFolder("tmoDir").toPath();
String nodeId = "localhost";
Table table;
Index index;
try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), tmoDir, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
table = Table.builder().tablespace("tblspace1").name("t1").column("id", ColumnTypes.INTEGER).column("name", ColumnTypes.STRING).primaryKey("id").build();
manager.executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.executeStatement(new InsertStatement("tblspace1", table.name, RecordSerializer.makeRecord(table, "id", 1, "name", "uno")), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
GetResult result = manager.get(new GetStatement("tblspace1", table.name, Bytes.from_int(1), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
assertTrue(result.found());
manager.checkpoint();
}
try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), tmoDir, null)) {
manager.start();
manager.waitForTablespace("tblspace1", 10000);
index = Index.builder().onTable(table).column("name", ColumnTypes.STRING).type(indexType).build();
manager.executeStatement(new CreateIndexStatement(index), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
/* Access through index */
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.t1 WHERE name=\'uno\'", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof SecondaryIndexSeek);
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
assertEquals(1, scan1.consume().size());
}
}
}
use of herddb.model.commands.GetStatement in project herddb by diennea.
the class TableManager method executeStatementAsync.
@Override
public CompletableFuture<StatementExecutionResult> executeStatementAsync(Statement statement, Transaction transaction, StatementEvaluationContext context) {
CompletableFuture<StatementExecutionResult> res;
long lockStamp = checkpointLock.readLock();
if (statement instanceof UpdateStatement) {
UpdateStatement update = (UpdateStatement) statement;
res = executeUpdateAsync(update, transaction, context);
} else if (statement instanceof InsertStatement) {
InsertStatement insert = (InsertStatement) statement;
res = executeInsertAsync(insert, transaction, context);
} else if (statement instanceof GetStatement) {
GetStatement get = (GetStatement) statement;
res = executeGetAsync(get, transaction, context);
} else if (statement instanceof DeleteStatement) {
DeleteStatement delete = (DeleteStatement) statement;
res = executeDeleteAsync(delete, transaction, context);
} else if (statement instanceof TruncateTableStatement) {
try {
TruncateTableStatement truncate = (TruncateTableStatement) statement;
res = CompletableFuture.completedFuture(executeTruncate(truncate, transaction, context));
} catch (StatementExecutionException err) {
LOGGER.log(Level.SEVERE, "Truncate table failed", err);
res = Futures.exception(err);
}
} else if (statement instanceof TableConsistencyCheckStatement) {
DBManager manager = this.tableSpaceManager.getDbmanager();
res = CompletableFuture.completedFuture(manager.createTableCheckSum((TableConsistencyCheckStatement) statement, context));
} else {
res = Futures.exception(new StatementExecutionException("not implemented " + statement.getClass()));
}
res = res.whenComplete((r, error) -> {
checkpointLock.unlockRead(lockStamp);
});
if (statement instanceof TruncateTableStatement) {
res = res.whenComplete((r, error) -> {
if (error == null) {
try {
flush();
} catch (DataStorageManagerException err) {
throw new HerdDBInternalException(new StatementExecutionException("internal data error: " + err, err));
}
}
});
}
return res;
}
use of herddb.model.commands.GetStatement in project herddb by diennea.
the class JSQLParserPlanner method buildSelectStatement.
private ExecutionPlan buildSelectStatement(String defaultTableSpace, int maxRows, Select select, boolean forceScan) throws StatementExecutionException {
checkSupported(select.getWithItemsList() == null);
SelectBody selectBody = select.getSelectBody();
PlannerOp op = buildSelectBody(defaultTableSpace, maxRows, selectBody, forceScan).optimize();
// Simplify Scan to Get
if (!forceScan && op instanceof BindableTableScanOp) {
ScanStatement scanStatement = op.unwrap(ScanStatement.class);
if (scanStatement != null && scanStatement.getPredicate() != null) {
Table tableDef = scanStatement.getTableDef();
CompiledSQLExpression where = scanStatement.getPredicate().unwrap(CompiledSQLExpression.class);
SQLRecordKeyFunction keyFunction = IndexUtils.findIndexAccess(where, tableDef.getPrimaryKey(), tableDef, "=", tableDef);
if (keyFunction == null || !keyFunction.isFullPrimaryKey()) {
throw new StatementExecutionException("unsupported GET not on PK (" + keyFunction + ")");
}
GetStatement get = new GetStatement(scanStatement.getTableSpace(), scanStatement.getTable(), keyFunction, scanStatement.getPredicate(), true);
return ExecutionPlan.simple(get);
}
}
return ExecutionPlan.simple(new SQLPlannedOperationStatement(op), op);
}
use of herddb.model.commands.GetStatement in project herddb by diennea.
the class AutoTransactionTest method testAutoTransactionOnGet.
@Test
public void testAutoTransactionOnGet() throws Exception {
int i = 1;
Map<String, Object> data = new HashMap<>();
Bytes key = Bytes.from_string("key_" + i);
data.put("id", "key_" + i);
data.put("number", i);
Record record = RecordSerializer.toRecord(data, table);
InsertStatement st = new InsertStatement(tableSpace, tableName, record);
manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
GetResult get = manager.get(new GetStatement(tableSpace, tableName, key, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.AUTOTRANSACTION_TRANSACTION);
long tx = get.transactionId;
assertTrue(get.found());
TestUtils.commitTransaction(manager, tableSpace, tx);
}
use of herddb.model.commands.GetStatement in project herddb by diennea.
the class AutoTransactionTest method testAutoTransactionOnFailedGet.
@Test
public void testAutoTransactionOnFailedGet() throws Exception {
int i = 1;
Map<String, Object> data = new HashMap<>();
Bytes key_bad = Bytes.from_string("key_bad_" + i);
data.put("id", "key_" + i);
data.put("number", i);
Record record = RecordSerializer.toRecord(data, table);
InsertStatement st = new InsertStatement(tableSpace, tableName, record);
manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
GetResult get = manager.get(new GetStatement(tableSpace, tableName, key_bad, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.AUTOTRANSACTION_TRANSACTION);
long tx = get.transactionId;
assertTrue(!get.found());
TestUtils.commitTransaction(manager, tableSpace, tx);
}
Aggregations