Search in sources :

Example 11 with Predicate

use of herddb.model.Predicate 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 12 with Predicate

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

the class TableManager method accessRecord.

public Record accessRecord(Map.Entry<Bytes, Long> entry, Predicate predicate, StatementEvaluationContext context, Transaction transaction, LocalScanPageCache lastPageRead, boolean primaryIndexSeek, boolean forWrite, boolean acquireLock) {
    Bytes key = entry.getKey();
    boolean keep_lock = false;
    boolean already_locked = transaction != null && transaction.lookupLock(table.name, key) != null;
    LockHandle lock = acquireLock ? (forWrite ? lockForWrite(key, transaction) : lockForRead(key, transaction)) : null;
    try {
        if (transaction != null) {
            if (transaction.recordDeleted(table.name, key)) {
                // skip this record. inside current transaction it has been deleted
                return null;
            }
            Record record = transaction.recordUpdated(table.name, key);
            if (record != null) {
                // use current transaction version of the record
                if (predicate == null || predicate.evaluate(record, context)) {
                    keep_lock = true;
                    return record;
                }
                return null;
            }
        }
        Long pageId = entry.getValue();
        if (pageId != null) {
            boolean pkFilterCompleteMatch = false;
            if (!primaryIndexSeek && predicate != null) {
                Predicate.PrimaryKeyMatchOutcome outcome = predicate.matchesRawPrimaryKey(key, context);
                if (outcome == Predicate.PrimaryKeyMatchOutcome.FAILED) {
                    return null;
                } else if (outcome == Predicate.PrimaryKeyMatchOutcome.FULL_CONDITION_VERIFIED) {
                    pkFilterCompleteMatch = true;
                }
            }
            Record record = fetchRecord(key, pageId, lastPageRead);
            if (record != null && (pkFilterCompleteMatch || predicate == null || predicate.evaluate(record, context))) {
                keep_lock = true;
                return record;
            }
        }
        return null;
    } finally {
        // release the lock on the key if it did not match scan criteria
        if (transaction == null) {
            if (lock != null) {
                if (forWrite) {
                    locksManager.releaseWriteLockForKey(key, lock);
                } else {
                    locksManager.releaseReadLockForKey(key, lock);
                }
            }
        } else if (!keep_lock && !already_locked) {
            transaction.releaseLockOnKey(table.name, key, locksManager);
        }
    }
}
Also used : Bytes(herddb.utils.Bytes) LockHandle(herddb.utils.LockHandle) AtomicLong(java.util.concurrent.atomic.AtomicLong) Record(herddb.model.Record) Predicate(herddb.model.Predicate)

Example 13 with Predicate

use of herddb.model.Predicate 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)

Example 14 with Predicate

use of herddb.model.Predicate 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.consume();
        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.consume();
        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.consume();
        assertEquals(80, result.size());
    }
}
Also used : HashMap(java.util.HashMap) DeleteStatement(herddb.model.commands.DeleteStatement) InsertStatement(herddb.model.commands.InsertStatement) Predicate(herddb.model.Predicate) DataScanner(herddb.model.DataScanner) Record(herddb.model.Record) List(java.util.List) StatementEvaluationContext(herddb.model.StatementEvaluationContext) ScanStatement(herddb.model.commands.ScanStatement) Test(org.junit.Test)

Aggregations

Predicate (herddb.model.Predicate)14 Record (herddb.model.Record)10 ScanStatement (herddb.model.commands.ScanStatement)8 StatementExecutionException (herddb.model.StatementExecutionException)7 Bytes (herddb.utils.Bytes)7 Table (herddb.model.Table)6 DataScanner (herddb.model.DataScanner)5 LogEntry (herddb.log.LogEntry)4 StatementEvaluationContext (herddb.model.StatementEvaluationContext)4 DeleteStatement (herddb.model.commands.DeleteStatement)4 InsertStatement (herddb.model.commands.InsertStatement)4 DataStorageManagerException (herddb.storage.DataStorageManagerException)4 LockHandle (herddb.utils.LockHandle)4 HashMap (java.util.HashMap)4 List (java.util.List)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)3 RecordFunction (herddb.model.RecordFunction)3 UpdateStatement (herddb.model.commands.UpdateStatement)3