Search in sources :

Example 36 with DeleteStatement

use of herddb.model.commands.DeleteStatement 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.consumeAndClose();
        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.consumeAndClose();
        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.consumeAndClose();
        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)

Example 37 with DeleteStatement

use of herddb.model.commands.DeleteStatement in project herddb by diennea.

the class NewPageTest method checkpointEmptyNewPage.

/**
 * Attempt to executre a checkpoint with empty new pages
 */
@Test
public void checkpointEmptyNewPage() throws Exception {
    String nodeId = "localhost";
    ServerConfiguration config1 = newServerConfigurationWithAutoPort();
    /* Smaller pages to avoid too many record creations */
    config1.set(ServerConfiguration.PROPERTY_MAX_LOGICAL_PAGE_SIZE, 10 * 1024);
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null, config1, null)) {
        manager.start();
        CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
        manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        execute(manager, "CREATE TABLE tblspace1.tsql (K1 string, S1 string, primary key(k1))", Collections.emptyList());
        manager.checkpoint();
        final TableManager tableManager = (TableManager) manager.getTableSpaceManager("tblspace1").getTableManager("tsql");
        final Table table = tableManager.getTable();
        int records = 0;
        /*
             * Fill table until there are some loaded page (not only kept in memory and unknown to page
             * replacement policy).
             */
        while (tableManager.getLoadedPages().size() < 2) {
            Bytes key = Bytes.from_string("ins_key_" + records++);
            assertEquals(1, manager.executeUpdate(new InsertStatement(table.tablespace, table.name, new Record(key, key)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
        }
        /* Checks that every page is still writable and filled */
        for (DataPage page : tableManager.getLoadedPages()) {
            assertTrue(page.writable);
            assertFalse(page.immutable);
            assertFalse(page.isEmpty());
            assertNotEquals(0, page.getUsedMemory());
        }
        /* Fully remove all the data, existing (still mutable) pages should be empty now */
        for (int i = 0; i < records; ++i) {
            Bytes key = Bytes.from_string("ins_key_" + i);
            assertEquals(1, manager.executeUpdate(new DeleteStatement(table.tablespace, table.name, key, null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
        }
        /* Checks that every page is still writable and empty */
        for (DataPage page : tableManager.getLoadedPages()) {
            assertTrue(page.writable);
            assertFalse(page.immutable);
            assertTrue(page.isEmpty());
            assertEquals(0, page.getUsedMemory());
        }
        manager.checkpoint();
        /* Expect just one (empty) new page remaining */
        assertEquals(1, tableManager.getLoadedPages().size());
    }
}
Also used : Table(herddb.model.Table) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) ServerConfiguration(herddb.server.ServerConfiguration) DeleteStatement(herddb.model.commands.DeleteStatement) InsertStatement(herddb.model.commands.InsertStatement) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) Bytes(herddb.utils.Bytes) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) Record(herddb.model.Record) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 38 with DeleteStatement

use of herddb.model.commands.DeleteStatement in project herddb by diennea.

the class NewPageTest method unloadEmptyNewPage.

/**
 * Attempt to unload an empty new page from memory
 */
@Test
public void unloadEmptyNewPage() throws Exception {
    String nodeId = "localhost";
    ServerConfiguration config1 = newServerConfigurationWithAutoPort();
    /* Smaller pages to avoid too many record creations */
    config1.set(ServerConfiguration.PROPERTY_MAX_LOGICAL_PAGE_SIZE, 10 * 1024);
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null, config1, null)) {
        manager.start();
        CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
        manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        execute(manager, "CREATE TABLE tblspace1.tsql (K1 string, S1 string, primary key(k1))", Collections.emptyList());
        manager.checkpoint();
        final TableManager tableManager = (TableManager) manager.getTableSpaceManager("tblspace1").getTableManager("tsql");
        final Table table = tableManager.getTable();
        int records = 0;
        /*
             * Fill table until there are some loaded page (not only kept in memory and unknown to page
             * replacement policy).
             */
        while (tableManager.getLoadedPages().size() < 2) {
            Bytes key = Bytes.from_string("ins_key_" + records++);
            assertEquals(1, manager.executeUpdate(new InsertStatement(table.tablespace, table.name, new Record(key, key)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
        }
        /* Checks that every page is still writable and filled */
        for (DataPage page : tableManager.getLoadedPages()) {
            assertTrue(page.writable);
            assertFalse(page.immutable);
            assertFalse(page.isEmpty());
            assertNotEquals(0, page.getUsedMemory());
        }
        /* Fully remove all the data, existing (still mutable) pages should be empty now */
        for (int i = 0; i < records; ++i) {
            Bytes key = Bytes.from_string("ins_key_" + i);
            assertEquals(1, manager.executeUpdate(new DeleteStatement(table.tablespace, table.name, key, null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
        }
        /* Checks that every page is still writable and empty */
        for (DataPage page : tableManager.getLoadedPages()) {
            assertTrue(page.writable);
            assertFalse(page.immutable);
            assertTrue(page.isEmpty());
            assertEquals(0, page.getUsedMemory());
        }
        /* Expect that every empty new page remains */
        assertEquals(2, tableManager.getLoadedPages().size());
        /* Attempt to unload new pages known to page replacement policy */
        boolean unloadedAny = false;
        for (DataPage page : tableManager.getLoadedPages()) {
            if (manager.getMemoryManager().getDataPageReplacementPolicy().remove(page)) {
                unloadedAny = true;
                tableManager.unload(page.pageId);
            }
        }
        /* Ensure that some page was unloaded */
        assertTrue(unloadedAny);
    }
}
Also used : Table(herddb.model.Table) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) ServerConfiguration(herddb.server.ServerConfiguration) DeleteStatement(herddb.model.commands.DeleteStatement) InsertStatement(herddb.model.commands.InsertStatement) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) Bytes(herddb.utils.Bytes) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) Record(herddb.model.Record) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Aggregations

DeleteStatement (herddb.model.commands.DeleteStatement)38 InsertStatement (herddb.model.commands.InsertStatement)33 Test (org.junit.Test)30 Record (herddb.model.Record)29 GetResult (herddb.model.GetResult)23 Table (herddb.model.Table)23 GetStatement (herddb.model.commands.GetStatement)23 Bytes (herddb.utils.Bytes)22 TransactionContext (herddb.model.TransactionContext)20 CreateTableStatement (herddb.model.commands.CreateTableStatement)13 UpdateStatement (herddb.model.commands.UpdateStatement)12 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)11 Path (java.nio.file.Path)9 TransactionResult (herddb.model.TransactionResult)8 BeginTransactionStatement (herddb.model.commands.BeginTransactionStatement)8 FileDataStorageManager (herddb.file.FileDataStorageManager)7 DataScanner (herddb.model.DataScanner)7 ServerConfiguration (herddb.server.ServerConfiguration)7 FileCommitLogManager (herddb.file.FileCommitLogManager)6 FileMetadataStorageManager (herddb.file.FileMetadataStorageManager)6