Search in sources :

Example 61 with DataAccessor

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

the class SimpleScanZeroCopyTest method testPkNotFirst.

@Test
public void testPkNotFirst() throws Exception {
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        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);
        execute(manager, "CREATE TABLE tblspace1.tsql (n1 int,k1 string primary key, s1 string)", Collections.emptyList());
        execute(manager, "INSERT INTO tblspace1.tsql (k1,n1 ,s1) values (?,?,?)", Arrays.asList("a", 1, "b"));
        try (DataScanner scan = TestUtils.scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
            List<DataAccessor> data = scan.consume();
            assertEquals(1, data.size());
            // read from the full record
            assertTrue(data.get(0) instanceof DataAccessorForFullRecord);
            assertEquals(1, data.get(0).get("n1"));
            assertEquals(1, data.get(0).get(0));
            assertEquals(RawString.of("a"), data.get(0).get("k1"));
            assertEquals(RawString.of("a"), data.get(0).get(1));
            assertEquals(RawString.of("b"), data.get(0).get("s1"));
            assertEquals(RawString.of("b"), data.get(0).get(2));
            String[] fieldNames = scan.getFieldNames();
            for (String fName : fieldNames) {
                Object value = data.get(0).get(fName);
                System.out.println("FIELD " + fName + " -> " + value + " (" + value.getClass() + ")");
            }
            IntHolder current = new IntHolder();
            data.get(0).forEach((fName, value) -> {
                System.out.println("FIELD2 " + fName + " -> " + value + " (" + value.getClass() + ")");
                assertEquals(fName, fieldNames[current.value++]);
            });
        }
        try (DataScanner scan = TestUtils.scan(manager, "SELECT k1 FROM tblspace1.tsql ", Collections.emptyList())) {
            List<DataAccessor> data = scan.consume();
            assertEquals(1, data.size());
            // read from the full record, keeping only some field
            assertTrue(data.get(0) instanceof ProjectedDataAccessor || data.get(0) instanceof ZeroCopyProjection.RuntimeProjectedDataAccessor);
            assertEquals(RawString.of("a"), data.get(0).get("k1"));
            assertEquals(RawString.of("a"), data.get(0).get(0));
            String[] fieldNames = scan.getFieldNames();
            for (String fName : fieldNames) {
                Object value = data.get(0).get(fName);
                System.out.println("FIELD " + fName + " -> " + value + " (" + value.getClass() + ")");
            }
            IntHolder current = new IntHolder();
            data.get(0).forEach((fName, value) -> {
                System.out.println("FIELD2 " + fName + " -> " + value + " (" + value.getClass() + ")");
                assertEquals(fName, fieldNames[current.value++]);
            });
        }
        try (DataScanner scan = TestUtils.scan(manager, "SELECT COUNT(*) FROM tblspace1.tsql ", Collections.emptyList())) {
            List<DataAccessor> data = scan.consume();
            assertEquals(1, data.size());
            assertTrue(data.get(0) instanceof Tuple);
        }
    }
}
Also used : MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) ProjectedDataAccessor(herddb.utils.ProjectedDataAccessor) RawString(herddb.utils.RawString) ProjectedDataAccessor(herddb.utils.ProjectedDataAccessor) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) IntHolder(herddb.utils.IntHolder) DataAccessorForFullRecord(herddb.codec.DataAccessorForFullRecord) Tuple(herddb.model.Tuple) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 62 with DataAccessor

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

the class SimplerPlannerTest method plannerUpdateTest.

@Test
public void plannerUpdateTest() throws Exception {
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        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);
        execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=1111", Collections.emptyList()).getUpdateCount());
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql where n1=1111", Collections.emptyList())) {
            List<DataAccessor> results = scan.consume();
            assertEquals(1, results.size());
        }
        assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=1112 where k1='mykey'", Collections.emptyList()).getUpdateCount());
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql where n1=1112", Collections.emptyList())) {
            List<DataAccessor> results = scan.consume();
            assertEquals(1, results.size());
        }
        assertEquals(0, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=1112 where k1='no'", Collections.emptyList()).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=n1+1 where k1='mykey'", Collections.emptyList()).getUpdateCount());
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql where n1=1113", Collections.emptyList())) {
            List<DataAccessor> results = scan.consume();
            assertEquals(1, results.size());
        }
        assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=? where k1=?", Arrays.asList(1114, "mykey")).getUpdateCount());
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql where n1=1114", Collections.emptyList())) {
            List<DataAccessor> results = scan.consume();
            assertEquals(1, results.size());
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) RawString(herddb.utils.RawString) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 63 with DataAccessor

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

the class UpdateTest method upsertTest.

@Test
public void upsertTest() throws Exception {
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        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);
        execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
        try (DataScanner scan = scan(manager, "SELECT n1 from tblspace1.tsql where k1=?", Arrays.asList("mykey"))) {
            List<DataAccessor> recordSet = scan.consumeAndClose();
            assertEquals(1, recordSet.size());
            assertEquals(1234, recordSet.get(0).get(0));
        }
        assertEquals(1, executeUpdate(manager, "UPSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1235))).getUpdateCount());
        try (DataScanner scan = scan(manager, "SELECT n1 from tblspace1.tsql where k1=?", Arrays.asList("mykey"))) {
            List<DataAccessor> recordSet = scan.consumeAndClose();
            assertEquals(1, recordSet.size());
            assertEquals(1235, recordSet.get(0).get(0));
        }
        assertEquals(4, executeUpdate(manager, "UPSERT INTO tblspace1.tsql(k1,n1)" + "values(?,?),(?,?),(?,?),(?,?)", Arrays.asList("mykey", Integer.valueOf(1235), "mykey", Integer.valueOf(1236), "mykey", Integer.valueOf(1237), "mykey", Integer.valueOf(1238))).getUpdateCount());
        try (DataScanner scan = scan(manager, "SELECT n1 from tblspace1.tsql where k1=?", Arrays.asList("mykey"))) {
            List<DataAccessor> recordSet = scan.consumeAndClose();
            assertEquals(1, recordSet.size());
            assertEquals(1238, recordSet.get(0).get(0));
        }
        assertEquals(1, executeUpdate(manager, "DELETE FROM tblspace1.tsql", Collections.emptyList()).getUpdateCount());
        execute(manager, "ALTER TABLE tblspace1.tsql MODIFY s1 string not null", Collections.emptyList());
        // assert that UPSERT fails
        StatementExecutionException error = herddb.utils.TestUtils.expectThrows(StatementExecutionException.class, () -> {
            executeUpdate(manager, "UPSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1235)));
        });
        assertThat(error.getMessage(), containsString("Column 's1' has no default value and does not allow NULLs"));
        // insert a value, n1 has a value
        assertEquals(1, executeUpdate(manager, "UPSERT INTO tblspace1.tsql(k1,n1,s1) values(?,?,'non-empty')", Arrays.asList("mykey", Integer.valueOf(1235))).getUpdateCount());
        try (DataScanner scan = scan(manager, "SELECT n1 from tblspace1.tsql where k1=?", Arrays.asList("mykey"))) {
            List<DataAccessor> recordSet = scan.consumeAndClose();
            assertEquals(1, recordSet.size());
            assertEquals(1235, recordSet.get(0).get(0));
        }
        // upsert, making n1 null now, because it has not been named in the INSERT clause
        // use non uppercase casing in UPSERT keyword
        assertEquals(1, executeUpdate(manager, "UPsert INTO tblspace1.tsql(k1,s1) values(?,'non-empty')", Arrays.asList("mykey")).getUpdateCount());
        try (DataScanner scan = scan(manager, "SELECT n1 from tblspace1.tsql where k1=?", Arrays.asList("mykey"))) {
            List<DataAccessor> recordSet = scan.consumeAndClose();
            assertEquals(1, recordSet.size());
            assertNull(recordSet.get(0).get(0));
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) StatementExecutionException(herddb.model.StatementExecutionException) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 64 with DataAccessor

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

the class UpdateTest method simpleManualSequence.

@Test
public void simpleManualSequence() throws Exception {
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        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);
        execute(manager, "CREATE TABLE tblspace1.myseq (k1 int, k2 int, current_value int, primary key(k1, k2))", Collections.emptyList());
        DMLStatementExecutionResult insertSequence1 = executeUpdate(manager, "INSERT INTO tblspace1.myseq(k1,k2,current_value) values(?,?,?)", Arrays.asList(Integer.valueOf(78), Integer.valueOf(0), Integer.valueOf(1)), TransactionContext.AUTOTRANSACTION_TRANSACTION);
        assertTrue(insertSequence1.transactionId > 0);
        try (DataScanner scan = scan(manager, "SELECT current_value from tblspace1.myseq where k1=?", Arrays.asList(78), new TransactionContext(insertSequence1.transactionId))) {
            List<DataAccessor> recordSet = scan.consumeAndClose();
            assertEquals(1, recordSet.size());
            assertEquals(1, recordSet.get(0).get(0));
        }
        // set current_value = 2 if current_value = 1
        DMLStatementExecutionResult updateSequence1 = executeUpdate(manager, "UPDATE tblspace1.myseq set current_value=? where k1=78 and k2=0 and current_value=?", Arrays.asList(Integer.valueOf(2), Integer.valueOf(1)), new TransactionContext(insertSequence1.transactionId));
        assertEquals(insertSequence1.transactionId, updateSequence1.transactionId);
        assertEquals(1, updateSequence1.getUpdateCount());
        try (DataScanner scan = scan(manager, "SELECT current_value from tblspace1.myseq where k1=?", Arrays.asList(78), new TransactionContext(updateSequence1.transactionId))) {
            List<DataAccessor> recordSet = scan.consumeAndClose();
            assertEquals(1, recordSet.size());
            assertEquals(2, recordSet.get(0).get(0));
        }
        DMLStatementExecutionResult updateSequence2 = executeUpdate(manager, "UPDATE tblspace1.myseq set current_value=? where k1=78 and k2=0 and current_value=?", Arrays.asList(Integer.valueOf(3), Integer.valueOf(2)), new TransactionContext(updateSequence1.transactionId));
        assertEquals(updateSequence2.transactionId, updateSequence1.transactionId);
        assertEquals(1, updateSequence2.getUpdateCount());
        try (DataScanner scan = scan(manager, "SELECT current_value from tblspace1.myseq where k1=?", Arrays.asList(78), new TransactionContext(updateSequence1.transactionId))) {
            List<DataAccessor> recordSet = scan.consumeAndClose();
            assertEquals(1, recordSet.size());
            assertEquals(3, recordSet.get(0).get(0));
        }
        DMLStatementExecutionResult deleteSequence2 = executeUpdate(manager, "DELETE FROM tblspace1.myseq where k1=78 and k2=0 ", Arrays.asList(), new TransactionContext(updateSequence1.transactionId));
        assertEquals(deleteSequence2.transactionId, updateSequence1.transactionId);
        assertEquals(1, deleteSequence2.getUpdateCount());
        try (DataScanner scan = scan(manager, "SELECT current_value from tblspace1.myseq where k1=?", Arrays.asList(78), new TransactionContext(updateSequence1.transactionId))) {
            List<DataAccessor> recordSet = scan.consumeAndClose();
            assertEquals(0, recordSet.size());
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) TransactionContext(herddb.model.TransactionContext) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 65 with DataAccessor

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

the class UpdateTest method updatePrimaryKeyTest.

@Test
public void updatePrimaryKeyTest() throws Exception {
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        assumeTrue(manager.isFullSQLSupportEnabled());
        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);
        execute(manager, "CREATE TABLE tblspace1.tsql (k1 int not null primary key, k2 int)", Collections.emptyList());
        executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,k2) values(?,?)", Arrays.asList(Integer.valueOf(78), Integer.valueOf(99)), TransactionContext.NO_TRANSACTION);
        try (DataScanner scan = scan(manager, "SELECT * from tblspace1.tsql where k1=?", Arrays.asList(78), TransactionContext.NO_TRANSACTION)) {
            List<DataAccessor> recordSet = scan.consume();
            assertEquals(1, recordSet.size());
        }
        executeUpdate(manager, "UPDATE tblspace1.tsql set k1=? where k1=?", Arrays.asList(Integer.valueOf(72), Integer.valueOf(78)), TransactionContext.NO_TRANSACTION);
        try (DataScanner scan = scan(manager, "SELECT * from tblspace1.tsql where k1=?", Arrays.asList(78), TransactionContext.NO_TRANSACTION)) {
            List<DataAccessor> recordSet = scan.consume();
            assertEquals(0, recordSet.size());
        }
        try (DataScanner scan = scan(manager, "SELECT * from tblspace1.tsql where k1=?", Arrays.asList(72), TransactionContext.NO_TRANSACTION)) {
            List<DataAccessor> recordSet = scan.consume();
            assertEquals(1, recordSet.size());
        }
        try (DataScanner scan = scan(manager, "SELECT * from tblspace1.tsql where k1=? and k2=?", Arrays.asList(72, 99), TransactionContext.NO_TRANSACTION)) {
            List<DataAccessor> recordSet = scan.consume();
            assertEquals(1, recordSet.size());
        }
        dump(manager, "SELECT * from tblspace1.tsql", null, TransactionContext.NO_TRANSACTION);
        // complicate the problem, use the old value to build the new one
        executeUpdate(manager, "UPDATE tblspace1.tsql set k1=k1 * 2 + k2 where k2 >= ?", Arrays.asList(Integer.valueOf(99)), TransactionContext.NO_TRANSACTION);
        dump(manager, "SELECT * from tblspace1.tsql", null, TransactionContext.NO_TRANSACTION);
        try (DataScanner scan = scan(manager, "SELECT * from tblspace1.tsql where k1=? and k2=?", Arrays.asList(243, 99), TransactionContext.NO_TRANSACTION)) {
            List<DataAccessor> recordSet = scan.consume();
            assertEquals(1, recordSet.size());
        }
        // update other values not in pk, using old pk value
        executeUpdate(manager, "UPDATE tblspace1.tsql set k1=k1 - 10, k2=k1 * 2", Collections.emptyList(), TransactionContext.NO_TRANSACTION);
        dump(manager, "SELECT * from tblspace1.tsql", null, TransactionContext.NO_TRANSACTION);
        try (DataScanner scan = scan(manager, "SELECT * from tblspace1.tsql where k1=? and k2=?", Arrays.asList(233, 486), TransactionContext.NO_TRANSACTION)) {
            List<DataAccessor> recordSet = scan.consume();
            assertEquals(1, recordSet.size());
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Aggregations

DataAccessor (herddb.utils.DataAccessor)164 DataScanner (herddb.model.DataScanner)118 Test (org.junit.Test)110 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)83 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)82 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)82 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)76 RawString (herddb.utils.RawString)70 DBManager (herddb.core.DBManager)52 TransactionContext (herddb.model.TransactionContext)44 StatementExecutionException (herddb.model.StatementExecutionException)38 Table (herddb.model.Table)34 List (java.util.List)31 DataScannerException (herddb.model.DataScannerException)28 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)27 RuntimeProjectedDataAccessor (herddb.model.planner.ProjectOp.ZeroCopyProjection.RuntimeProjectedDataAccessor)27 Column (herddb.model.Column)22 ScanResult (herddb.model.ScanResult)22 ScanStatement (herddb.model.commands.ScanStatement)22 Bytes (herddb.utils.Bytes)22