Search in sources :

Example 6 with Holder

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

the class BLinkTest method testScanDotNotExistent.

@Test
public void testScanDotNotExistent() throws Exception {
    BLinkIndexDataStorage<Sized<Long>, Long> storage = new DummyBLinkIndexDataStorage<>();
    try (BLink<Sized<Long>, Long> blink = new BLink<>(2048L, new LongSizeEvaluator(), new RandomPageReplacementPolicy(10), storage)) {
        final long headInserts = 100;
        final long nonExistents = 10;
        final long tailInserts = 100;
        for (long l = 0; l < headInserts; l++) {
            blink.insert(Sized.valueOf(l), l);
        }
        for (long l = headInserts + nonExistents; l < headInserts + nonExistents + tailInserts; l++) {
            blink.insert(Sized.valueOf(l), l);
        }
        BLinkMetadata<Sized<Long>> metadata = blink.checkpoint();
        /* Require at least two nodes! */
        assertNotEquals(1, metadata.nodes.size());
        long offset = 100;
        for (long l = nonExistents; l < headInserts + nonExistents - offset; l++) {
            Stream<Entry<Sized<Long>, Long>> stream = blink.scan(Sized.valueOf(l), Sized.valueOf(l + offset));
            Holder<Long> h = new Holder<>(l);
            Holder<Long> count = new Holder<>(0L);
            StringBuilder builder = new StringBuilder();
            /* Check each value */
            stream.forEach(entry -> {
                assertEquals(h.value, entry.getValue());
                h.value++;
                count.value++;
                builder.append(entry.getValue()).append(", ");
            });
            builder.setLength(builder.length() - 2);
            System.out.println("start " + l + " end " + (l + offset) + " -> " + builder);
            assertEquals(offset - nonExistents, (long) count.value);
        }
    }
}
Also used : Sized(herddb.utils.Sized) Holder(herddb.utils.Holder) RandomPageReplacementPolicy(herddb.core.RandomPageReplacementPolicy) Entry(java.util.Map.Entry) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 7 with Holder

use of herddb.utils.Holder 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 8 with Holder

use of herddb.utils.Holder 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 9 with Holder

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

the class MemoryHashIndexManager method checkpoint.

@Override
public List<PostCheckpointAction> checkpoint(LogSequenceNumber sequenceNumber, boolean pin) throws DataStorageManagerException {
    if (createdInTransaction > 0) {
        LOGGER.log(Level.INFO, "checkpoint for index " + index.name + " skipped, this index is created on transaction " + createdInTransaction + " which is not committed");
        return Collections.emptyList();
    }
    List<PostCheckpointAction> result = new ArrayList<>();
    LOGGER.log(Level.INFO, "flush index {0}", new Object[] { index.name });
    long pageId = newPageId.getAndIncrement();
    Holder<Long> count = new Holder<>();
    dataStorageManager.writeIndexPage(tableSpaceUUID, index.uuid, pageId, (out) -> {
        long entries = 0;
        // version
        out.writeVLong(1);
        // flags for future implementations
        out.writeVLong(0);
        out.writeVInt(data.size());
        for (Map.Entry<Bytes, List<Bytes>> entry : data.entrySet()) {
            out.writeArray(entry.getKey());
            List<Bytes> entrydata = entry.getValue();
            out.writeVInt(entrydata.size());
            for (Bytes v : entrydata) {
                out.writeArray(v);
                ++entries;
            }
        }
        count.value = entries;
    });
    IndexStatus indexStatus = new IndexStatus(index.name, sequenceNumber, newPageId.get(), Collections.singleton(pageId), null);
    result.addAll(dataStorageManager.indexCheckpoint(tableSpaceUUID, index.uuid, indexStatus, pin));
    LOGGER.log(Level.INFO, "checkpoint index {0} finished: logpos {1}, {2} entries, page {3}", new Object[] { index.name, sequenceNumber, Long.toString(count.value), Long.toString(pageId) });
    return result;
}
Also used : Holder(herddb.utils.Holder) ArrayList(java.util.ArrayList) PostCheckpointAction(herddb.core.PostCheckpointAction) Bytes(herddb.utils.Bytes) IndexStatus(herddb.storage.IndexStatus) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 10 with Holder

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

the class BLinkTest method testScanMiddleNotExistent.

@Test
public void testScanMiddleNotExistent() throws Exception {
    BLinkIndexDataStorage<Sized<Long>, Long> storage = new DummyBLinkIndexDataStorage<>();
    try (BLink<Sized<Long>, Long> blink = new BLink<>(2048L, new LongSizeEvaluator(), new RandomPageReplacementPolicy(10), storage)) {
        final long headInserts = 100;
        final long nonExistents = 100;
        final long tailInserts = 100;
        for (long l = 0; l < headInserts; l++) {
            blink.insert(Sized.valueOf(l), l);
        }
        for (long l = headInserts + nonExistents; l < headInserts + nonExistents + tailInserts; l++) {
            blink.insert(Sized.valueOf(l), l);
        }
        BLinkMetadata<Sized<Long>> metadata = blink.checkpoint();
        /* Require at least two nodes! */
        assertNotEquals(1, metadata.nodes.size());
        long offset = 10;
        for (long l = headInserts; l < headInserts + nonExistents - offset; l++) {
            Stream<Entry<Sized<Long>, Long>> stream = blink.scan(Sized.valueOf(l), Sized.valueOf(l + offset));
            Holder<Long> h = new Holder<>(l);
            Holder<Long> count = new Holder<>(0L);
            StringBuilder builder = new StringBuilder();
            /* Check each value */
            stream.forEach(entry -> {
                assertEquals(h.value, entry.getValue());
                h.value++;
                count.value++;
                builder.append(entry.getValue()).append(", ");
            });
            assertEquals(0, (long) count.value);
        }
    }
}
Also used : Sized(herddb.utils.Sized) Holder(herddb.utils.Holder) RandomPageReplacementPolicy(herddb.core.RandomPageReplacementPolicy) Entry(java.util.Map.Entry) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Aggregations

Holder (herddb.utils.Holder)13 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 Entry (java.util.Map.Entry)7 Test (org.junit.Test)7 Record (herddb.model.Record)6 RandomPageReplacementPolicy (herddb.core.RandomPageReplacementPolicy)5 Sized (herddb.utils.Sized)5 CommitLogResult (herddb.log.CommitLogResult)4 LogEntry (herddb.log.LogEntry)4 LogNotAvailableException (herddb.log.LogNotAvailableException)4 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)4 GetResult (herddb.model.GetResult)4 Predicate (herddb.model.Predicate)4 DeleteStatement (herddb.model.commands.DeleteStatement)4 GetStatement (herddb.model.commands.GetStatement)4 InsertStatement (herddb.model.commands.InsertStatement)4 UpdateStatement (herddb.model.commands.UpdateStatement)4 FullTableScanConsumer (herddb.storage.FullTableScanConsumer)4 TableStatus (herddb.storage.TableStatus)4 Bytes (herddb.utils.Bytes)4