Search in sources :

Example 51 with InsertStatement

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

the class BigTableScanTest method bigTableScan.

@Test
public void bigTableScan() throws Exception {
    int testSize = 5000;
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        manager.setMaxMemoryReference(128 * 1024);
        manager.setMaxLogicalPageSize(32 * 1024);
        manager.setMaxPKUsedMemory(manager.getMaxLogicalPageSize() * 2);
        manager.start();
        CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
        manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        Table table = Table.builder().tablespace("tblspace1").name("t1").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
        CreateTableStatement st2 = new CreateTableStatement(table);
        manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        for (int i = 0; i < testSize; i++) {
            InsertStatement insert = new InsertStatement(table.tablespace, table.name, RecordSerializer.makeRecord(table, "id", "k" + i, "name", "testname" + i));
            assertEquals(1, manager.executeUpdate(insert, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
        }
        TableManagerStats stats = manager.getTableSpaceManager(table.tablespace).getTableManager(table.name).getStats();
        assertEquals(testSize, stats.getTablesize());
        assertEquals(2, stats.getLoadedpages());
        manager.checkpoint();
        try (DataScanner scan = manager.scan(new ScanStatement(table.tablespace, table, new FullTableScanPredicate()), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION)) {
            AtomicInteger count = new AtomicInteger();
            scan.forEach(tuple -> {
                count.incrementAndGet();
            });
            assertEquals(testSize, count.get());
        }
        assertEquals(testSize, stats.getTablesize());
    }
}
Also used : Table(herddb.model.Table) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) InsertStatement(herddb.model.commands.InsertStatement) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DataScanner(herddb.model.DataScanner) TableManagerStats(herddb.core.stats.TableManagerStats) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) FullTableScanPredicate(herddb.model.FullTableScanPredicate) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) ScanStatement(herddb.model.commands.ScanStatement) Test(org.junit.Test)

Example 52 with InsertStatement

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

the class ColumnTimestampTest method test2.

@Test
public void test2() throws Exception {
    String tableName2 = "t2";
    Table table2 = Table.builder().tablespace("tblspace1").name(tableName2).tablespace(tableSpace).column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).column("ts1", ColumnTypes.TIMESTAMP).primaryKey("id").build();
    CreateTableStatement st2 = new CreateTableStatement(table2);
    manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    java.sql.Timestamp ts1 = new java.sql.Timestamp(System.currentTimeMillis());
    {
        Map<String, Object> bean = new HashMap<>();
        bean.put("id", "key1");
        bean.put("ts1", ts1);
        Record record = RecordSerializer.toRecord(bean, table2);
        InsertStatement st = new InsertStatement(tableSpace, "t2", record);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    {
        GetResult result = manager.get(new GetStatement(tableSpace, tableName2, Bytes.from_string("key1"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertTrue(result.found());
        Map<String, Object> resultbean = result.getRecord().toBean(table2);
        assertEquals(Bytes.from_string("key1"), result.getRecord().key);
        assertEquals(2, resultbean.entrySet().size());
        assertEquals(RawString.of("key1"), resultbean.get("id"));
        assertEquals(ts1, resultbean.get("ts1"));
    }
    {
        Map<String, Object> bean = new HashMap<>();
        bean.put("id", "key1");
        bean.put("ts1", null);
        Record record = RecordSerializer.toRecord(bean, table2);
        UpdateStatement st = new UpdateStatement(tableSpace, tableName2, record, null);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    {
        GetResult result = manager.get(new GetStatement(tableSpace, tableName2, Bytes.from_string("key1"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertTrue(result.found());
        Map<String, Object> resultbean = result.getRecord().toBean(table2);
        assertEquals(Bytes.from_string("key1"), result.getRecord().key);
        assertEquals(1, resultbean.entrySet().size());
        assertEquals(RawString.of("key1"), resultbean.get("id"));
        assertEquals(null, resultbean.get("ts1"));
    }
}
Also used : UpdateStatement(herddb.model.commands.UpdateStatement) Table(herddb.model.Table) GetResult(herddb.model.GetResult) CreateTableStatement(herddb.model.commands.CreateTableStatement) RawString(herddb.utils.RawString) InsertStatement(herddb.model.commands.InsertStatement) GetStatement(herddb.model.commands.GetStatement) Record(herddb.model.Record) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 53 with InsertStatement

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

the class DropOldPagesTest method test.

@Test
public void test() throws Exception {
    FileDataStorageManager fileDataStorageManager = (FileDataStorageManager) dataStorageManager;
    {
        Record record = new Record(Bytes.from_string("key1"), Bytes.from_string("0"));
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    String tableUuid = manager.getTableSpaceManager(tableSpace).getTableManager(tableName).getTable().uuid;
    assertEquals(0, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableUuid));
    assertEquals(0, fileDataStorageManager.getTablePageFiles(tableSpaceUUID, tableUuid).size());
    manager.checkpoint();
    assertEquals(1, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableUuid));
    assertEquals(1, fileDataStorageManager.getTablePageFiles(tableSpaceUUID, tableUuid).size());
    {
        Record record = new Record(Bytes.from_string("key2"), Bytes.from_string("0"));
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
        assertEquals(1, manager.executeUpdate(new DeleteStatement(tableSpace, tableName, Bytes.from_string("key1"), null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    manager.checkpoint();
    assertEquals(1, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableUuid));
    assertEquals(1, fileDataStorageManager.getTablePageFiles(tableSpaceUUID, tableUuid).size());
}
Also used : FileDataStorageManager(herddb.file.FileDataStorageManager) Record(herddb.model.Record) DeleteStatement(herddb.model.commands.DeleteStatement) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Example 54 with InsertStatement

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

the class MultipleColumnPrimaryKeyTest method test.

@Test
public void test() throws Exception {
    {
        Record record = RecordSerializer.makeRecord(table, "id", "r1", "name", "k1");
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    {
        Map<String, Object> key = new HashMap<>();
        key.put("id", "r1");
        key.put("name", "k1");
        GetResult result = manager.get(new GetStatement(tableSpace, tableName, RecordSerializer.serializePrimaryKey(key, table, table.primaryKey), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertTrue(result.found());
    }
    {
        Map<String, Object> key = new HashMap<>();
        key.put("id", "r1");
        key.put("name", "k2");
        GetResult result = manager.get(new GetStatement(tableSpace, tableName, RecordSerializer.serializePrimaryKey(key, table, table.primaryKey), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertTrue(!result.found());
    }
    {
        Map<String, Object> key = new HashMap<>();
        key.put("id", "r1");
        key.put("name", "k1");
        {
            GetResult result = manager.get(new GetStatement(tableSpace, tableName, RecordSerializer.serializePrimaryKey(key, table, table.primaryKey), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertTrue(result.found());
        }
        {
            DeleteStatement st = new DeleteStatement(tableSpace, tableName, RecordSerializer.serializePrimaryKey(key, table, table.primaryKey), null);
            assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
        }
        {
            GetResult result = manager.get(new GetStatement(tableSpace, tableName, RecordSerializer.serializePrimaryKey(key, table, table.primaryKey), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertFalse(result.found());
        }
    }
}
Also used : GetResult(herddb.model.GetResult) GetStatement(herddb.model.commands.GetStatement) Record(herddb.model.Record) HashMap(java.util.HashMap) Map(java.util.Map) DeleteStatement(herddb.model.commands.DeleteStatement) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Example 55 with InsertStatement

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

the class SimpleClusterTest method test.

@Test
public void test() throws Exception {
    {
        Record record = new Record(Bytes.from_string("key1"), Bytes.from_string("0"));
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    assertEquals(0, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableName));
    manager.checkpoint();
    String tableUuid = manager.getTableSpaceManager(tableSpace).getTableManager(tableName).getTable().uuid;
    assertNotNull(dataStorageManager.readPage(tableSpaceUUID, tableUuid, 1L));
    assertEquals(1, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableUuid));
    {
        GetResult result = manager.get(new GetStatement(tableSpace, tableName, Bytes.from_string("key1"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertTrue(result.found());
    }
    manager.checkpoint();
    assertEquals(1, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableUuid));
    {
        Record record = new Record(Bytes.from_string("key1"), Bytes.from_string("5"));
        UpdateStatement st = new UpdateStatement(tableSpace, tableName, record, null);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    // a new page must be allocated
    manager.checkpoint();
    assertEquals(1, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableUuid));
    {
        Record record = new Record(Bytes.from_string("key1"), Bytes.from_string("6"));
        UpdateStatement st = new UpdateStatement(tableSpace, tableName, record, null);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    {
        Record record = new Record(Bytes.from_string("key1"), Bytes.from_string("7"));
        UpdateStatement st = new UpdateStatement(tableSpace, tableName, record, null);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    // only a new page must be allocated, not two more
    manager.checkpoint();
    assertEquals(1, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableUuid));
    {
        DeleteStatement st = new DeleteStatement(tableSpace, tableName, Bytes.from_string("key1"), null);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
        GetResult result = manager.get(new GetStatement(tableSpace, tableName, Bytes.from_string("key1"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertFalse(result.found());
    }
    // a delete does not trigger new pages in this case
    manager.checkpoint();
    assertEquals(0, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableUuid));
    {
        assertEquals(1, manager.executeUpdate(new InsertStatement(tableSpace, tableName, new Record(Bytes.from_string("key2"), Bytes.from_string("50"))), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
        assertEquals(1, manager.executeUpdate(new InsertStatement(tableSpace, tableName, new Record(Bytes.from_string("key3"), Bytes.from_string("60"))), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    manager.checkpoint();
    assertEquals(1, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableUuid));
    {
        DeleteStatement st = new DeleteStatement(tableSpace, tableName, Bytes.from_string("key2"), null);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    // a new page, containg the key3 record is needed
    manager.checkpoint();
    assertEquals(1, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableUuid));
    Holder<TableStatus> _tableStatus = new Holder<>();
    dataStorageManager.fullTableScan(tableSpaceUUID, tableUuid, new FullTableScanConsumer() {

        @Override
        public void acceptTableStatus(TableStatus tableStatus) {
            _tableStatus.value = tableStatus;
        }

        @Override
        public void startPage(long pageId) {
        }

        @Override
        public void acceptRecord(Record record) {
        }

        @Override
        public void endPage() {
        }

        @Override
        public void endTable() {
        }
    });
    for (long pageId : _tableStatus.value.activePages.keySet()) {
        List<Record> records = dataStorageManager.readPage(tableSpaceUUID, tableUuid, pageId);
        System.out.println("PAGE #" + pageId + " records :" + records);
    }
    assertEquals(1, _tableStatus.value.activePages.size());
}
Also used : UpdateStatement(herddb.model.commands.UpdateStatement) GetResult(herddb.model.GetResult) Holder(herddb.utils.Holder) DeleteStatement(herddb.model.commands.DeleteStatement) InsertStatement(herddb.model.commands.InsertStatement) FullTableScanConsumer(herddb.storage.FullTableScanConsumer) GetStatement(herddb.model.commands.GetStatement) TableStatus(herddb.storage.TableStatus) Record(herddb.model.Record) Test(org.junit.Test)

Aggregations

InsertStatement (herddb.model.commands.InsertStatement)78 Test (org.junit.Test)73 Record (herddb.model.Record)53 GetStatement (herddb.model.commands.GetStatement)51 GetResult (herddb.model.GetResult)50 Table (herddb.model.Table)50 CreateTableStatement (herddb.model.commands.CreateTableStatement)48 Bytes (herddb.utils.Bytes)39 TransactionContext (herddb.model.TransactionContext)38 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)28 Path (java.nio.file.Path)25 FileDataStorageManager (herddb.file.FileDataStorageManager)24 FileCommitLogManager (herddb.file.FileCommitLogManager)23 FileMetadataStorageManager (herddb.file.FileMetadataStorageManager)23 DeleteStatement (herddb.model.commands.DeleteStatement)21 TransactionResult (herddb.model.TransactionResult)19 BeginTransactionStatement (herddb.model.commands.BeginTransactionStatement)19 CommitTransactionStatement (herddb.model.commands.CommitTransactionStatement)17 UpdateStatement (herddb.model.commands.UpdateStatement)16 DataScanner (herddb.model.DataScanner)15