Search in sources :

Example 16 with GetStatement

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());
        }
    }
}
Also used : Path(java.nio.file.Path) Table(herddb.model.Table) TranslatedQuery(herddb.sql.TranslatedQuery) GetResult(herddb.model.GetResult) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) CreateIndexStatement(herddb.model.commands.CreateIndexStatement) Index(herddb.model.Index) InsertStatement(herddb.model.commands.InsertStatement) FileCommitLogManager(herddb.file.FileCommitLogManager) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) SecondaryIndexSeek(herddb.index.SecondaryIndexSeek) DataScanner(herddb.model.DataScanner) GetStatement(herddb.model.commands.GetStatement) FileDataStorageManager(herddb.file.FileDataStorageManager) ScanStatement(herddb.model.commands.ScanStatement)

Example 17 with GetStatement

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;
}
Also used : TruncateTableStatement(herddb.model.commands.TruncateTableStatement) Arrays(java.util.Arrays) NullLockManager(herddb.utils.NullLockManager) Table(herddb.model.Table) TruncateTableStatement(herddb.model.commands.TruncateTableStatement) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) TableStatus(herddb.storage.TableStatus) Map(java.util.Map) DataAccessor(herddb.utils.DataAccessor) LogNotAvailableException(herddb.log.LogNotAvailableException) CommitLogResult(herddb.log.CommitLogResult) LogSequenceNumber(herddb.log.LogSequenceNumber) UniqueIndexContraintViolationException(herddb.model.UniqueIndexContraintViolationException) Set(java.util.Set) RecordSerializer(herddb.codec.RecordSerializer) JSQLParserPlanner.delimit(herddb.sql.JSQLParserPlanner.delimit) DataPageMetaData(herddb.core.PageSet.DataPageMetaData) ScanStatement(herddb.model.commands.ScanStatement) Stream(java.util.stream.Stream) StatsLogger(org.apache.bookkeeper.stats.StatsLogger) Bytes(herddb.utils.Bytes) Holder(herddb.utils.Holder) LockHandle(herddb.utils.LockHandle) ForeignKeyViolationException(herddb.model.ForeignKeyViolationException) LogEntry(herddb.log.LogEntry) ArrayList(java.util.ArrayList) TransactionContext(herddb.model.TransactionContext) Transaction(herddb.model.Transaction) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Projection(herddb.model.Projection) ForeignKeyDef(herddb.model.ForeignKeyDef) EnsureLongIncrementAccumulator(herddb.utils.EnsureLongIncrementAccumulator) LogEntryType(herddb.log.LogEntryType) Record(herddb.model.Record) LogEntryFactory(herddb.log.LogEntryFactory) KeyToPageIndex(herddb.index.KeyToPageIndex) DataStorageManager(herddb.storage.DataStorageManager) ColumnTypes(herddb.model.ColumnTypes) ILocalLockManager(herddb.utils.ILocalLockManager) AtomicLong(java.util.concurrent.atomic.AtomicLong) Lock(java.util.concurrent.locks.Lock) IndexOperation(herddb.index.IndexOperation) Column(herddb.model.Column) StampedLock(java.util.concurrent.locks.StampedLock) UpdateStatement(herddb.model.commands.UpdateStatement) ScanLimitsImpl(herddb.model.ScanLimitsImpl) TupleComparator(herddb.model.TupleComparator) ServerConfiguration(herddb.server.ServerConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecordTooBigException(herddb.model.RecordTooBigException) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) LocalLockManager(herddb.utils.LocalLockManager) Futures(herddb.utils.Futures) DataStorageManagerException(herddb.storage.DataStorageManagerException) Index(herddb.model.Index) InsertStatement(herddb.model.commands.InsertStatement) DataScanner(herddb.model.DataScanner) DDLException(herddb.model.DDLException) RecordFunction(herddb.model.RecordFunction) StatementExecutionException(herddb.model.StatementExecutionException) TableContext(herddb.model.TableContext) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) List(java.util.List) FullTableScanConsumer(herddb.storage.FullTableScanConsumer) GetStatement(herddb.model.commands.GetStatement) Entry(java.util.Map.Entry) Statement(herddb.model.Statement) LongAdder(java.util.concurrent.atomic.LongAdder) DataScannerException(herddb.model.DataScannerException) GetResult(herddb.model.GetResult) PrimaryIndexSeek(herddb.index.PrimaryIndexSeek) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) BatchOrderedExecutor(herddb.utils.BatchOrderedExecutor) ConcurrentMap(java.util.concurrent.ConcurrentMap) Level(java.util.logging.Level) HashSet(java.util.HashSet) BooleanHolder(herddb.utils.BooleanHolder) ScanLimits(herddb.model.ScanLimits) DeleteStatement(herddb.model.commands.DeleteStatement) Iterator(java.util.Iterator) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Semaphore(java.util.concurrent.Semaphore) DataPageDoesNotExistException(herddb.storage.DataPageDoesNotExistException) Counter(org.apache.bookkeeper.stats.Counter) StatementExecutionResult(herddb.model.StatementExecutionResult) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CommitLog(herddb.log.CommitLog) AbstractMap(java.util.AbstractMap) TableConsistencyCheckStatement(herddb.model.commands.TableConsistencyCheckStatement) Predicate(herddb.model.Predicate) StatementEvaluationContext(herddb.model.StatementEvaluationContext) Comparator(java.util.Comparator) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) TableManagerStats(herddb.core.stats.TableManagerStats) SystemProperties(herddb.utils.SystemProperties) UpdateStatement(herddb.model.commands.UpdateStatement) DataStorageManagerException(herddb.storage.DataStorageManagerException) DeleteStatement(herddb.model.commands.DeleteStatement) InsertStatement(herddb.model.commands.InsertStatement) StatementExecutionException(herddb.model.StatementExecutionException) GetStatement(herddb.model.commands.GetStatement) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) StatementExecutionResult(herddb.model.StatementExecutionResult) TableConsistencyCheckStatement(herddb.model.commands.TableConsistencyCheckStatement)

Example 18 with GetStatement

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);
}
Also used : PlannerOp(herddb.model.planner.PlannerOp) Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) GetStatement(herddb.model.commands.GetStatement) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) SelectBody(net.sf.jsqlparser.statement.select.SelectBody) StatementExecutionException(herddb.model.StatementExecutionException) BindableTableScanOp(herddb.model.planner.BindableTableScanOp) ScanStatement(herddb.model.commands.ScanStatement) SQLPlannedOperationStatement(herddb.model.commands.SQLPlannedOperationStatement)

Example 19 with GetStatement

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);
}
Also used : Bytes(herddb.utils.Bytes) GetResult(herddb.model.GetResult) HashMap(java.util.HashMap) GetStatement(herddb.model.commands.GetStatement) Record(herddb.model.Record) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Example 20 with GetStatement

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);
}
Also used : Bytes(herddb.utils.Bytes) GetResult(herddb.model.GetResult) HashMap(java.util.HashMap) GetStatement(herddb.model.commands.GetStatement) Record(herddb.model.Record) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Aggregations

GetStatement (herddb.model.commands.GetStatement)100 GetResult (herddb.model.GetResult)92 Test (org.junit.Test)90 InsertStatement (herddb.model.commands.InsertStatement)87 Table (herddb.model.Table)75 CreateTableStatement (herddb.model.commands.CreateTableStatement)72 Record (herddb.model.Record)56 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)50 Bytes (herddb.utils.Bytes)48 Path (java.nio.file.Path)45 TransactionContext (herddb.model.TransactionContext)39 FileCommitLogManager (herddb.file.FileCommitLogManager)30 FileDataStorageManager (herddb.file.FileDataStorageManager)30 FileMetadataStorageManager (herddb.file.FileMetadataStorageManager)30 TransactionResult (herddb.model.TransactionResult)28 CommitTransactionStatement (herddb.model.commands.CommitTransactionStatement)27 BeginTransactionStatement (herddb.model.commands.BeginTransactionStatement)26 UpdateStatement (herddb.model.commands.UpdateStatement)25 HashSet (java.util.HashSet)24 ScanStatement (herddb.model.commands.ScanStatement)23