Search in sources :

Example 61 with Bytes

use of herddb.utils.Bytes 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 62 with Bytes

use of herddb.utils.Bytes 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 63 with Bytes

use of herddb.utils.Bytes in project herddb by diennea.

the class MemoryHashIndexManager method recordDeleted.

@Override
public void recordDeleted(Bytes key, DataAccessor values) {
    Bytes indexKey = RecordSerializer.serializePrimaryKey(values, index, index.columnNames);
    removeValueFromIndex(indexKey, key);
}
Also used : Bytes(herddb.utils.Bytes)

Example 64 with Bytes

use of herddb.utils.Bytes in project herddb by diennea.

the class MemoryHashIndexManager method recordUpdated.

@Override
public void recordUpdated(Bytes key, DataAccessor previousValues, DataAccessor newValues) {
    Bytes indexKeyRemoved = RecordSerializer.serializePrimaryKey(previousValues, index, index.columnNames);
    Bytes indexKeyAdded = RecordSerializer.serializePrimaryKey(newValues, index, index.columnNames);
    if (Objects.equals(indexKeyRemoved, indexKeyAdded)) {
        return;
    }
    // BEWARE that this operation is not atomic
    if (indexKeyAdded != null) {
        addValueToIndex(indexKeyAdded, key);
    }
    if (indexKeyRemoved != null) {
        removeValueFromIndex(indexKeyRemoved, key);
    }
}
Also used : Bytes(herddb.utils.Bytes)

Example 65 with Bytes

use of herddb.utils.Bytes in project herddb by diennea.

the class MemoryHashIndexManager method recordInserted.

@Override
public void recordInserted(Bytes key, DataAccessor values) {
    Bytes indexKey = RecordSerializer.serializePrimaryKey(values, index, index.columnNames);
    addValueToIndex(indexKey, key);
}
Also used : Bytes(herddb.utils.Bytes)

Aggregations

Bytes (herddb.utils.Bytes)139 Record (herddb.model.Record)77 Test (org.junit.Test)71 Table (herddb.model.Table)68 TransactionContext (herddb.model.TransactionContext)62 InsertStatement (herddb.model.commands.InsertStatement)61 GetResult (herddb.model.GetResult)53 GetStatement (herddb.model.commands.GetStatement)51 CreateTableStatement (herddb.model.commands.CreateTableStatement)50 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)49 Path (java.nio.file.Path)34 TransactionResult (herddb.model.TransactionResult)28 BeginTransactionStatement (herddb.model.commands.BeginTransactionStatement)28 CommitTransactionStatement (herddb.model.commands.CommitTransactionStatement)28 DataStorageManagerException (herddb.storage.DataStorageManagerException)28 DataScanner (herddb.model.DataScanner)26 DeleteStatement (herddb.model.commands.DeleteStatement)24 UpdateStatement (herddb.model.commands.UpdateStatement)24 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)23 ArrayList (java.util.ArrayList)23