use of herddb.model.commands.CreateTableSpaceStatement in project herddb by diennea.
the class RawSQLTest method updateMultiColumns.
@Test
public void updateMultiColumns() 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,n1 int,s1 string,t1 timestamp,n2 int,n3 int, primary key (k1) )", Collections.emptyList());
java.sql.Timestamp now = new java.sql.Timestamp(System.currentTimeMillis());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,s1,n1,t1,n2,n3) values(?,?,?,?,?,?)", Arrays.asList("mykey", "a", Integer.valueOf(1234), now, 1, 2)).getUpdateCount());
DataAccessor before = scan(manager, "SELECT n2,n3 FROM tblspace1.tsql WHERE k1=?", Arrays.asList("mykey")).consume().get(0);
assertEquals(1, before.get(0));
assertEquals(2, before.get(1));
assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n2=n2+1,n3=n3+5 WHERE k1=?", Arrays.asList("mykey")).getUpdateCount());
DataAccessor after = scan(manager, "SELECT n2,n3 FROM tblspace1.tsql WHERE k1=?", Arrays.asList("mykey")).consume().get(0);
assertEquals(2, after.get(0));
assertEquals(7, after.get(1));
assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n2=n2+n2,n3=n3+n2 WHERE k1=?", Arrays.asList("mykey")).getUpdateCount());
DataAccessor after2 = scan(manager, "SELECT n2,n3 FROM tblspace1.tsql WHERE k1=?", Arrays.asList("mykey")).consume().get(0);
assertEquals(4, after2.get(0));
assertEquals(9, after2.get(1));
}
}
use of herddb.model.commands.CreateTableSpaceStatement in project herddb by diennea.
the class RawSQLTest method multipleColumnPrimaryKeyTest.
@Test
public void multipleColumnPrimaryKeyTest() 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," + "n1 int," + "s1 string, " + "primary key (k1,n1)" + ")", Collections.emptyList());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
try (DataScanner scan1 = scan(manager, "SELECT k1 as theKey,'one' as theStringConstant,3 LongConstant FROM tblspace1.tsql where k1 ='mykey'", Collections.emptyList())) {
assertEquals(1, scan1.consume().size());
}
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1235))).getUpdateCount());
try (DataScanner scan1 = scan(manager, "SELECT k1 as theKey,'one' as theStringConstant,3 LongConstant FROM tblspace1.tsql where k1 ='mykey'", Collections.emptyList())) {
assertEquals(2, scan1.consume().size());
}
try {
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1235))).getUpdateCount());
fail();
} catch (DuplicatePrimaryKeyException err) {
}
try (DataScanner scan1 = scan(manager, "SELECT k1,n1 FROM tblspace1.tsql where k1 ='mykey' order by n1", Collections.emptyList())) {
List<DataAccessor> rows = scan1.consume();
assertEquals(2, rows.size());
assertEquals(1234, rows.get(0).get("n1"));
assertEquals(1235, rows.get(1).get("n1"));
}
try (DataScanner scan1 = scan(manager, "SELECT k1,n1 FROM tblspace1.tsql where k1 ='mykey' and n1=1234", Collections.emptyList())) {
assertEquals(1, scan1.consume().size());
}
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(n1,k1) values(?,?)", Arrays.asList(Integer.valueOf(1236), "mykey")).getUpdateCount());
try (DataScanner scan1 = scan(manager, "SELECT k1,n1 FROM tblspace1.tsql where k1 ='mykey' order by n1 desc", Collections.emptyList())) {
List<DataAccessor> rows = scan1.consume();
assertEquals(3, rows.size());
assertEquals(1236, rows.get(0).get("n1"));
assertEquals(1235, rows.get(1).get("n1"));
assertEquals(1234, rows.get(2).get("n1"));
}
try (DataScanner scan1 = scan(manager, "SELECT k1,n1 FROM tblspace1.tsql where k1 ='mykey' and n1=1234", Collections.emptyList())) {
assertEquals(1, scan1.consume().size());
}
try (DataScanner scan1 = scan(manager, "SELECT k1,n1 FROM tblspace1.tsql where k1 ='mykey'", Collections.emptyList())) {
assertEquals(3, scan1.consume().size());
}
assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set s1=? where k1 =? and n1=?", Arrays.asList("newvalue", "mykey", Integer.valueOf(1236))).getUpdateCount());
try (DataScanner scan1 = scan(manager, "SELECT k1,n1,s1 FROM tblspace1.tsql where k1 ='mykey' and n1=1236", Collections.emptyList())) {
List<DataAccessor> rows = scan1.consume();
assertEquals(1, rows.size());
assertEquals(RawString.of("newvalue"), rows.get(0).get("s1"));
}
assertEquals(1, executeUpdate(manager, "DELETE FROM tblspace1.tsql where k1 =? and n1=?", Arrays.asList("mykey", Integer.valueOf(1236))).getUpdateCount());
try (DataScanner scan1 = scan(manager, "SELECT k1,n1 FROM tblspace1.tsql where k1 ='mykey' and n1=1236", Collections.emptyList())) {
assertEquals(0, scan1.consume().size());
}
}
}
use of herddb.model.commands.CreateTableSpaceStatement in project herddb by diennea.
the class RawSQLTest method orderByTest.
@Test
public void orderByTest() 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(1))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey2", Integer.valueOf(2))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey3", Integer.valueOf(4))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1) values(?)", Arrays.asList("mykey4")).getUpdateCount());
{
TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT k1 FROM tblspace1.tsql order by n1", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translate1.plan.mainStatement.unwrap(ScanStatement.class);
try (DataScanner scan1 = manager.scan(scan, translate1.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> result = scan1.consume();
assertEquals(4, result.size());
assertEquals(RawString.of("mykey"), result.get(0).get(0));
assertEquals(RawString.of("mykey2"), result.get(1).get(0));
assertEquals(RawString.of("mykey3"), result.get(2).get(0));
// NULLS LAST
assertEquals(RawString.of("mykey4"), result.get(3).get(0));
}
}
{
TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT k1 FROM tblspace1.tsql order by n1 desc", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translate1.plan.mainStatement.unwrap(ScanStatement.class);
try (DataScanner scan1 = manager.scan(scan, translate1.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> result = scan1.consume();
assertEquals(4, result.size());
assertEquals(RawString.of("mykey"), result.get(3).get(0));
assertEquals(RawString.of("mykey2"), result.get(2).get(0));
assertEquals(RawString.of("mykey3"), result.get(1).get(0));
assertEquals(RawString.of("mykey4"), result.get(0).get(0));
}
}
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey5", Integer.valueOf(3))).getUpdateCount());
{
TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT k1 FROM tblspace1.tsql order by n1, k1", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translate1.plan.mainStatement.unwrap(ScanStatement.class);
try (DataScanner scan1 = manager.scan(scan, translate1.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> result = scan1.consume();
assertEquals(5, result.size());
assertEquals(RawString.of("mykey"), result.get(0).get(0));
assertEquals(RawString.of("mykey2"), result.get(1).get(0));
assertEquals(RawString.of("mykey5"), result.get(2).get(0));
assertEquals(RawString.of("mykey3"), result.get(3).get(0));
// NULLS LAST
assertEquals(RawString.of("mykey4"), result.get(4).get(0));
}
}
{
TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT k1 FROM tblspace1.tsql order by n1, k1 desc", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translate1.plan.mainStatement.unwrap(ScanStatement.class);
try (DataScanner scan1 = manager.scan(scan, translate1.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> result = scan1.consume();
assertEquals(5, result.size());
assertEquals(RawString.of("mykey"), result.get(0).get(0));
assertEquals(RawString.of("mykey2"), result.get(1).get(0));
assertEquals(RawString.of("mykey5"), result.get(2).get(0));
assertEquals(RawString.of("mykey3"), result.get(3).get(0));
// NULLS LAST
assertEquals(RawString.of("mykey4"), result.get(4).get(0));
}
}
{
TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT k1 FROM tblspace1.tsql order by n1, k1 desc limit 2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translate1.plan.mainStatement.unwrap(ScanStatement.class);
try (DataScanner scan1 = manager.scan(scan, translate1.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> result = scan1.consume();
assertEquals(2, result.size());
assertEquals(RawString.of("mykey"), result.get(0).get(0));
assertEquals(RawString.of("mykey2"), result.get(1).get(0));
}
}
{
TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT k1 FROM tblspace1.tsql order by n1 asc limit 2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translate1.plan.mainStatement.unwrap(ScanStatement.class);
try (DataScanner scan1 = manager.scan(scan, translate1.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> result = scan1.consume();
assertEquals(2, result.size());
assertEquals(RawString.of("mykey"), result.get(0).get(0));
assertEquals(RawString.of("mykey2"), result.get(1).get(0));
}
}
{
TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT k1 FROM tblspace1.tsql order by n1 desc limit 3", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translate1.plan.mainStatement.unwrap(ScanStatement.class);
try (DataScanner scan1 = manager.scan(scan, translate1.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> result = scan1.consume();
assertEquals(3, result.size());
assertEquals(RawString.of("mykey4"), result.get(0).get(0));
assertEquals(RawString.of("mykey3"), result.get(1).get(0));
assertEquals(RawString.of("mykey5"), result.get(2).get(0));
}
}
}
}
use of herddb.model.commands.CreateTableSpaceStatement in project herddb by diennea.
the class RestartPendingTransactionTest method recoverUpdate.
@Test
public void recoverUpdate() throws Exception {
Path dataPath = folder.newFolder("data").toPath();
Path logsPath = folder.newFolder("logs").toPath();
Path metadataPath = folder.newFolder("metadata").toPath();
Path tmoDir = folder.newFolder("tmoDir").toPath();
Bytes key1 = Bytes.from_string("k1");
Bytes key2 = Bytes.from_string("k2");
Bytes key3 = Bytes.from_string("k3");
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), tmoDir, 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);
Table table = Table.builder().tablespace("tblspace1").name("t1").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
manager.executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.executeStatement(new InsertStatement("tblspace1", table.name, new Record(key1, key1)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.executeStatement(new InsertStatement("tblspace1", table.name, new Record(key2, key2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.executeStatement(new InsertStatement("tblspace1", table.name, new Record(key3, key3)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.checkpoint();
manager.executeStatement(new UpdateStatement("tblspace1", table.name, new ConstValueRecordFunction(key2.data), new ConstValueRecordFunction(key3.data), null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
long tx2 = ((TransactionResult) manager.executeStatement(new BeginTransactionStatement("tblspace1"), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION)).getTransactionId();
manager.executeStatement(new UpdateStatement("tblspace1", table.name, new ConstValueRecordFunction(key2.data), new ConstValueRecordFunction(key3.data), null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx2));
manager.executeStatement(new CommitTransactionStatement("tblspace1", tx2), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
// transactions which contains the update will be replayed at reboot
}
try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), tmoDir, null)) {
manager.start();
manager.waitForTablespace("tblspace1", 10000);
GetResult result = manager.get(new GetStatement("tblspace1", "t1", key1, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
assertTrue(result.found());
}
}
use of herddb.model.commands.CreateTableSpaceStatement in project herddb by diennea.
the class RestartPendingTransactionTest method recoverDelete.
@Test
public void recoverDelete() throws Exception {
Path dataPath = folder.newFolder("data").toPath();
Path logsPath = folder.newFolder("logs").toPath();
Path metadataPath = folder.newFolder("metadata").toPath();
Path tmoDir = folder.newFolder("tmoDir").toPath();
Bytes key1 = Bytes.from_string("k1");
Bytes key2 = Bytes.from_string("k2");
Bytes key3 = Bytes.from_string("k3");
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), tmoDir, 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);
Table table = Table.builder().tablespace("tblspace1").name("t1").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
manager.executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.executeStatement(new InsertStatement("tblspace1", table.name, new Record(key1, key1)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.executeStatement(new InsertStatement("tblspace1", table.name, new Record(key2, key2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.executeStatement(new InsertStatement("tblspace1", table.name, new Record(key3, key3)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.checkpoint();
manager.executeStatement(new DeleteStatement("tblspace1", table.name, key2, null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
}
try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), tmoDir, null)) {
manager.start();
manager.waitForTablespace("tblspace1", 10000);
GetResult result = manager.get(new GetStatement("tblspace1", "t1", key1, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
assertTrue(result.found());
}
}
Aggregations