Search in sources :

Example 11 with DMLStatementExecutionResult

use of herddb.model.DMLStatementExecutionResult in project herddb by diennea.

the class TableManager method executeTruncate.

private StatementExecutionResult executeTruncate(TruncateTableStatement truncate, Transaction transaction, StatementEvaluationContext context) throws StatementExecutionException, DataStorageManagerException {
    if (transaction != null) {
        throw new StatementExecutionException("TRUNCATE TABLE cannot be executed within the context of a Transaction");
    }
    try {
        long estimatedSize = keyToPage.size();
        LogEntry entry = LogEntryFactory.truncate(table, null);
        CommitLogResult pos = log.log(entry, entry.transactionId <= 0);
        apply(pos, entry, false);
        return new DMLStatementExecutionResult(0, estimatedSize > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) estimatedSize, null, null);
    } catch (LogNotAvailableException error) {
        throw new StatementExecutionException(error);
    }
}
Also used : DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) CommitLogResult(herddb.log.CommitLogResult) StatementExecutionException(herddb.model.StatementExecutionException) LogEntry(herddb.log.LogEntry) LogNotAvailableException(herddb.log.LogNotAvailableException)

Example 12 with DMLStatementExecutionResult

use of herddb.model.DMLStatementExecutionResult in project herddb by diennea.

the class TableManager method executeUpdate.

private StatementExecutionResult executeUpdate(UpdateStatement update, Transaction transaction, StatementEvaluationContext context) throws StatementExecutionException, DataStorageManagerException {
    AtomicInteger updateCount = new AtomicInteger();
    Holder<Bytes> lastKey = new Holder<>();
    Holder<byte[]> lastValue = new Holder<>();
    /*
         an update can succeed only if the row is valid, the key is contains in the "keys" structure
         the update will simply override the value of the row, assigning a null page to the row
         the update can have a 'where' predicate which is to be evaluated against the decoded row, the update will be executed only if the predicate returns boolean 'true' value  (CAS operation)
         locks: the update  uses a lock on the the key
         */
    RecordFunction function = update.getFunction();
    long transactionId = transaction != null ? transaction.transactionId : 0;
    Predicate predicate = update.getPredicate();
    ScanStatement scan = new ScanStatement(table.tablespace, table, predicate);
    accessTableData(scan, context, new ScanResultOperation() {

        @Override
        public void accept(Record actual) throws StatementExecutionException, LogNotAvailableException, DataStorageManagerException {
            byte[] newValue = function.computeNewValue(actual, context, tableContext);
            final long size = DataPage.estimateEntrySize(actual.key, newValue);
            if (size > maxLogicalPageSize) {
                throw new RecordTooBigException("New version of record " + actual.key + " is to big to be update: new size " + size + ", actual size " + DataPage.estimateEntrySize(actual) + ", max size " + maxLogicalPageSize);
            }
            LogEntry entry = LogEntryFactory.update(table, actual.key.data, newValue, transaction);
            CommitLogResult pos = log.log(entry, entry.transactionId <= 0);
            apply(pos, entry, false);
            lastKey.value = actual.key;
            lastValue.value = newValue;
            updateCount.incrementAndGet();
        }
    }, transaction, true, true);
    return new DMLStatementExecutionResult(transactionId, updateCount.get(), lastKey.value, update.isReturnValues() ? (lastValue.value != null ? Bytes.from_array(lastValue.value) : null) : null);
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException) Holder(herddb.utils.Holder) CommitLogResult(herddb.log.CommitLogResult) RecordTooBigException(herddb.model.RecordTooBigException) StatementExecutionException(herddb.model.StatementExecutionException) Predicate(herddb.model.Predicate) Bytes(herddb.utils.Bytes) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) Record(herddb.model.Record) RecordFunction(herddb.model.RecordFunction) LogEntry(herddb.log.LogEntry) ScanStatement(herddb.model.commands.ScanStatement) LogNotAvailableException(herddb.log.LogNotAvailableException)

Example 13 with DMLStatementExecutionResult

use of herddb.model.DMLStatementExecutionResult in project herddb by diennea.

the class TableManager method executeDelete.

private StatementExecutionResult executeDelete(DeleteStatement delete, Transaction transaction, StatementEvaluationContext context) throws StatementExecutionException, DataStorageManagerException {
    AtomicInteger updateCount = new AtomicInteger();
    Holder<Bytes> lastKey = new Holder<>();
    Holder<byte[]> lastValue = new Holder<>();
    long transactionId = transaction != null ? transaction.transactionId : 0;
    Predicate predicate = delete.getPredicate();
    ScanStatement scan = new ScanStatement(table.tablespace, table, predicate);
    accessTableData(scan, context, new ScanResultOperation() {

        @Override
        public void accept(Record actual) throws StatementExecutionException, LogNotAvailableException, DataStorageManagerException {
            LogEntry entry = LogEntryFactory.delete(table, actual.key.data, transaction);
            CommitLogResult pos = log.log(entry, entry.transactionId <= 0);
            apply(pos, entry, false);
            lastKey.value = actual.key;
            lastValue.value = actual.value.data;
            updateCount.incrementAndGet();
        }
    }, transaction, true, true);
    return new DMLStatementExecutionResult(transactionId, updateCount.get(), lastKey.value, delete.isReturnValues() ? (lastValue.value != null ? Bytes.from_array(lastValue.value) : null) : null);
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException) Holder(herddb.utils.Holder) CommitLogResult(herddb.log.CommitLogResult) StatementExecutionException(herddb.model.StatementExecutionException) Predicate(herddb.model.Predicate) Bytes(herddb.utils.Bytes) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) Record(herddb.model.Record) LogEntry(herddb.log.LogEntry) ScanStatement(herddb.model.commands.ScanStatement) LogNotAvailableException(herddb.log.LogNotAvailableException)

Example 14 with DMLStatementExecutionResult

use of herddb.model.DMLStatementExecutionResult in project herddb by diennea.

the class DeleteOp method execute.

@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) {
    StatementExecutionResult input = this.input.execute(tableSpaceManager, transactionContext, context, true, true);
    ScanResult downstreamScanResult = (ScanResult) input;
    final Table table = tableSpaceManager.getTableManager(tableName).getTable();
    long transactionId = transactionContext.transactionId;
    int updateCount = 0;
    Bytes key = null;
    try (DataScanner inputScanner = downstreamScanResult.dataScanner) {
        while (inputScanner.hasNext()) {
            DataAccessor row = inputScanner.next();
            long transactionIdFromScanner = inputScanner.getTransactionId();
            if (transactionIdFromScanner > 0 && transactionIdFromScanner != transactionId) {
                transactionId = transactionIdFromScanner;
                transactionContext = new TransactionContext(transactionId);
            }
            key = RecordSerializer.serializePrimaryKey(row, table, table.getPrimaryKey());
            DMLStatement deleteStatement = new DeleteStatement(tableSpace, tableName, null, new RawKeyEquals(key));
            DMLStatementExecutionResult _result = (DMLStatementExecutionResult) tableSpaceManager.executeStatement(deleteStatement, context, transactionContext);
            updateCount += _result.getUpdateCount();
            if (_result.transactionId > 0 && _result.transactionId != transactionId) {
                transactionId = _result.transactionId;
                transactionContext = new TransactionContext(transactionId);
            }
            key = _result.getKey();
        }
        return new DMLStatementExecutionResult(transactionId, updateCount, key, null);
    } catch (DataScannerException err) {
        throw new StatementExecutionException(err);
    }
}
Also used : ScanResult(herddb.model.ScanResult) RawKeyEquals(herddb.model.predicates.RawKeyEquals) Table(herddb.model.Table) DataAccessor(herddb.utils.DataAccessor) DeleteStatement(herddb.model.commands.DeleteStatement) StatementExecutionException(herddb.model.StatementExecutionException) Bytes(herddb.utils.Bytes) DataScanner(herddb.model.DataScanner) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) TransactionContext(herddb.model.TransactionContext) StatementExecutionResult(herddb.model.StatementExecutionResult) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) DMLStatement(herddb.model.DMLStatement) DataScannerException(herddb.model.DataScannerException)

Example 15 with DMLStatementExecutionResult

use of herddb.model.DMLStatementExecutionResult in project herddb by diennea.

the class UpdateOp method execute.

@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) {
    StatementExecutionResult input = this.input.execute(tableSpaceManager, transactionContext, context, true, true);
    ScanResult downstreamScanResult = (ScanResult) input;
    final Table table = tableSpaceManager.getTableManager(tableName).getTable();
    long transactionId = transactionContext.transactionId;
    int updateCount = 0;
    Bytes key = null;
    Bytes newValue = null;
    try (DataScanner inputScanner = downstreamScanResult.dataScanner) {
        while (inputScanner.hasNext()) {
            DataAccessor row = inputScanner.next();
            long transactionIdFromScanner = inputScanner.getTransactionId();
            if (transactionIdFromScanner > 0 && transactionIdFromScanner != transactionId) {
                transactionId = transactionIdFromScanner;
                transactionContext = new TransactionContext(transactionId);
            }
            key = RecordSerializer.serializePrimaryKey(row, table, table.getPrimaryKey());
            Predicate pred = new RawKeyEquals(key);
            DMLStatement updateStatement = new UpdateStatement(tableSpace, tableName, null, this.recordFunction, pred).setReturnValues(returnValues);
            DMLStatementExecutionResult _result = (DMLStatementExecutionResult) tableSpaceManager.executeStatement(updateStatement, context, transactionContext);
            updateCount += _result.getUpdateCount();
            if (_result.transactionId > 0 && _result.transactionId != transactionId) {
                transactionId = _result.transactionId;
                transactionContext = new TransactionContext(transactionId);
            }
            key = _result.getKey();
            newValue = _result.getNewvalue();
        }
        return new DMLStatementExecutionResult(transactionId, updateCount, key, newValue);
    } catch (DataScannerException err) {
        throw new StatementExecutionException(err);
    }
}
Also used : ScanResult(herddb.model.ScanResult) RawKeyEquals(herddb.model.predicates.RawKeyEquals) UpdateStatement(herddb.model.commands.UpdateStatement) Table(herddb.model.Table) DataAccessor(herddb.utils.DataAccessor) StatementExecutionException(herddb.model.StatementExecutionException) Predicate(herddb.model.Predicate) Bytes(herddb.utils.Bytes) DataScanner(herddb.model.DataScanner) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) TransactionContext(herddb.model.TransactionContext) StatementExecutionResult(herddb.model.StatementExecutionResult) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) DMLStatement(herddb.model.DMLStatement) DataScannerException(herddb.model.DataScannerException)

Aggregations

DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)26 TransactionContext (herddb.model.TransactionContext)16 Test (org.junit.Test)15 Bytes (herddb.utils.Bytes)13 Table (herddb.model.Table)12 Record (herddb.model.Record)11 StatementExecutionException (herddb.model.StatementExecutionException)11 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)11 InsertStatement (herddb.model.commands.InsertStatement)11 DataScanner (herddb.model.DataScanner)10 DataAccessor (herddb.utils.DataAccessor)10 ScanStatement (herddb.model.commands.ScanStatement)8 DataScannerException (herddb.model.DataScannerException)7 GetResult (herddb.model.GetResult)7 CreateTableStatement (herddb.model.commands.CreateTableStatement)7 Path (java.nio.file.Path)7 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)6 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)6 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)6 GetStatement (herddb.model.commands.GetStatement)6