use of herddb.mem.MemoryMetadataStorageManager in project herddb by diennea.
the class AlterTableSQLTest method addColumn.
@Test
public void addColumn() 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());
Table table = manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable();
assertEquals(0, table.getColumn("k1").serialPosition);
assertEquals(1, table.getColumn("n1").serialPosition);
assertEquals(2, table.getColumn("s1").serialPosition);
execute(manager, "INSERT INTO tblspace1.tsql (k1,n1,s1) values('a',1,'b')", Collections.emptyList());
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList()).consume();
assertEquals(1, tuples.size());
assertEquals(3, tuples.get(0).getFieldNames().length);
}
execute(manager, "ALTER TABLE tblspace1.tsql add column k2 string", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.tsql (k1,n1,s1,k2) values('b',1,'b','c')", Collections.emptyList());
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql WHERE k2='c'", Collections.emptyList()).consume();
assertEquals(1, tuples.size());
assertEquals(4, tuples.get(0).getFieldNames().length);
}
table = manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable();
assertEquals(0, table.getColumn("k1").serialPosition);
assertEquals(1, table.getColumn("n1").serialPosition);
assertEquals(2, table.getColumn("s1").serialPosition);
assertEquals(3, table.getColumn("k2").serialPosition);
}
}
use of herddb.mem.MemoryMetadataStorageManager in project herddb by diennea.
the class AlterTablespaceSQLTest method createAlterTableSpace.
@Test
public void createAlterTableSpace() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager(nodeId, new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
assertTrue(manager.waitForTablespace(TableSpace.DEFAULT, 10000));
execute(manager, "EXECUTE CREATETABLESPACE 'ttt'", Collections.emptyList());
execute(manager, "CREATE TABLESPACE 'ttt2','leader:" + nodeId + "'", Collections.emptyList());
try {
execute(manager, "EXECUTE CREATETABLESPACE 'ttt2','leader:othernode'", Collections.emptyList());
fail();
} catch (TableSpaceAlreadyExistsException err) {
}
execute(manager, "EXECUTE CREATETABLESPACE 'ttt3','leader:othernode'", Collections.emptyList());
execute(manager, "EXECUTE CREATETABLESPACE 'ttt4','leader:othernode','replica:" + nodeId + ",othernode'", Collections.emptyList());
execute(manager, "EXECUTE ALTERTABLESPACE 'ttt3','replica:" + nodeId + ",othernode','expectedReplicaCount:2'", Collections.emptyList());
execute(manager, "EXECUTE ALTERTABLESPACE 'ttt3','leader:othernode'", Collections.emptyList());
execute(manager, "EXECUTE ALTERTABLESPACE 'ttt3','expectedReplicaCount:12'", Collections.emptyList());
TableSpace ttt3 = manager.getMetadataStorageManager().describeTableSpace("ttt3");
assertEquals("othernode", ttt3.leaderId);
assertEquals(12, ttt3.expectedReplicaCount);
assertTrue(ttt3.replicas.contains("othernode"));
assertTrue(ttt3.replicas.contains(nodeId));
try (DataScanner scan = scan(manager, "SELECT * FROM SYSTABLESPACES", Collections.emptyList())) {
List<DataAccessor> tuples = scan.consume();
assertEquals(5, tuples.size());
for (DataAccessor t : tuples) {
System.out.println("tablespace: " + t.toMap());
assertNotNull(t.get("expectedreplicacount"));
assertNotNull(t.get("tablespace_name"));
assertNotNull(t.get("replica"));
assertNotNull(t.get("leader"));
}
}
try (DataScanner scan = scan(manager, "SELECT expectedreplicacount FROM SYSTABLESPACES where tablespace_name='ttt3'", Collections.emptyList())) {
List<DataAccessor> tuples = scan.consume();
assertEquals(1, tuples.size());
for (DataAccessor t : tuples) {
System.out.println("tablespace: " + t.toMap());
assertEquals(12, t.get("expectedreplicacount"));
}
}
}
}
use of herddb.mem.MemoryMetadataStorageManager in project herddb by diennea.
the class AlterTablespaceSQLTest method escapeTableSpaceName.
@Test
public void escapeTableSpaceName() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager(nodeId, new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
assertTrue(manager.waitForTablespace(TableSpace.DEFAULT, 10000));
execute(manager, "CREATE TABLESPACE `default`,'leader:" + nodeId + "'", Collections.emptyList());
try {
execute(manager, "EXECUTE CREATETABLESPACE `default`,'leader:othernode'", Collections.emptyList());
fail();
} catch (TableSpaceAlreadyExistsException err) {
}
execute(manager, "EXECUTE ALTERTABLESPACE `default`,`replica:" + nodeId + ",othernode`,'expectedReplicaCount:2'", Collections.emptyList());
execute(manager, "EXECUTE ALTERTABLESPACE 'default','leader:othernode'", Collections.emptyList());
execute(manager, "EXECUTE ALTERTABLESPACE 'default','expectedReplicaCount:12'", Collections.emptyList());
TableSpace ttt3 = manager.getMetadataStorageManager().describeTableSpace("default");
assertEquals("othernode", ttt3.leaderId);
assertEquals(12, ttt3.expectedReplicaCount);
assertTrue(ttt3.replicas.contains("othernode"));
assertTrue(ttt3.replicas.contains(nodeId));
try (DataScanner scan = scan(manager, "SELECT * FROM SYSTABLESPACES", Collections.emptyList())) {
List<DataAccessor> tuples = scan.consume();
assertEquals(2, tuples.size());
for (DataAccessor t : tuples) {
System.out.println("tablespace: " + t.toMap());
assertNotNull(t.get("expectedreplicacount"));
assertNotNull(t.get("tablespace_name"));
assertNotNull(t.get("replica"));
assertNotNull(t.get("leader"));
}
}
try (DataScanner scan = scan(manager, "SELECT expectedreplicacount FROM SYSTABLESPACES where tablespace_name='default'", Collections.emptyList())) {
List<DataAccessor> tuples = scan.consume();
assertEquals(1, tuples.size());
for (DataAccessor t : tuples) {
System.out.println("tablespace: " + t.toMap());
assertEquals(12, t.get("expectedreplicacount"));
}
}
}
}
use of herddb.mem.MemoryMetadataStorageManager in project herddb by diennea.
the class AutoIncrementTest method testAutoIncrement_other_sintax.
@Test
public void testAutoIncrement_other_sintax() 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 auto_increment, s1 string, primary key (n1))", Collections.emptyList());
{
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa")).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa")).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa")).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa")).getUpdateCount());
assertEquals(2, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?),(?)", Arrays.asList("aa", "aa")).getUpdateCount());
List<DataAccessor> rows = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList()).consume();
assertEquals(6, rows.size());
for (int i = 1; i <= 6; i++) {
int _i = i;
assertTrue(rows.stream().filter(t -> t.get("n1").equals(Integer.valueOf(_i))).findAny().isPresent());
}
DMLStatementExecutionResult result = executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"));
assertEquals(1, result.getUpdateCount());
assertEquals(Bytes.from_int(7), result.getKey());
assertEquals(7, executeUpdate(manager, "delete from tblspace1.tsql", Collections.emptyList()).getUpdateCount());
}
// the same but in transaction
{
DMLStatementExecutionResult res = executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), TransactionContext.AUTOTRANSACTION_TRANSACTION);
assertEquals(1, res.getUpdateCount());
long tx = res.transactionId;
assertTrue(tx > 0);
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx)).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx)).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx)).getUpdateCount());
assertEquals(2, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?),(?)", Arrays.asList("aa", "aa"), new TransactionContext(tx)).getUpdateCount());
List<DataAccessor> rows = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList(), new TransactionContext(tx)).consume();
assertEquals(6, rows.size());
for (int i = 8; i <= 13; i++) {
int _i = i;
assertTrue("can't find " + i, rows.stream().filter(t -> t.get("n1").equals(Integer.valueOf(_i))).findAny().isPresent());
}
DMLStatementExecutionResult result = executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx));
assertEquals(1, result.getUpdateCount());
assertEquals(Bytes.from_int(14), result.getKey());
assertEquals(tx, res.transactionId);
// transaction not committed
}
// the same but in transaction, multi values as first
{
DMLStatementExecutionResult res = executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?),(?)", Arrays.asList("aa", "aa"), TransactionContext.AUTOTRANSACTION_TRANSACTION);
assertEquals(2, res.getUpdateCount());
long tx = res.transactionId;
assertTrue(tx > 0);
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx)).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx)).getUpdateCount());
assertEquals(2, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?),(?)", Arrays.asList("aa", "aa"), new TransactionContext(tx)).getUpdateCount());
List<DataAccessor> rows = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList(), new TransactionContext(tx)).consume();
assertEquals(6, rows.size());
for (int i = 15; i <= 20; i++) {
int _i = i;
assertTrue(rows.stream().filter(t -> t.get("n1").equals(Integer.valueOf(_i))).findAny().isPresent());
}
DMLStatementExecutionResult result = executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx));
assertEquals(1, result.getUpdateCount());
assertEquals(Bytes.from_int(21), result.getKey());
assertEquals(tx, res.transactionId);
assertEquals(7, executeUpdate(manager, "delete from tblspace1.tsql", Collections.emptyList(), new TransactionContext(tx)).getUpdateCount());
}
}
}
use of herddb.mem.MemoryMetadataStorageManager in project herddb by diennea.
the class BetterExecuteSyntaxTest method betterSyntax.
@Test
public void betterSyntax() throws Exception {
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
execute(manager, "CREATE TABLESPACE 'tblspace1'", Collections.emptyList());
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "ALTER TABLESPACE 'tblspace1','expectedreplicacount:2'", Collections.emptyList());
long tx = ((TransactionResult) execute(manager, "BEGIN TRANSACTION 'tblspace1'", Collections.emptyList())).getTransactionId();
execute(manager, "COMMIT TRANSACTION 'tblspace1'," + tx, Collections.emptyList());
long tx2 = ((TransactionResult) execute(manager, "BEGIN TRANSACTION 'tblspace1'", Collections.emptyList())).getTransactionId();
execute(manager, "ROLLBACK TRANSACTION 'tblspace1'," + tx2, Collections.emptyList());
execute(manager, "DROP TABLESPACE 'tblspace1'", Collections.emptyList());
try (DataScanner scan = TestUtils.scan(manager, "SELECT COUNT(*) FROM systablespaces WHERE tablespace_name=?", Arrays.asList("tblspace1"))) {
DataAccessor first = scan.consume().get(0);
Number count = (Number) first.get(first.getFieldNames()[0]);
assertEquals(0, count.intValue());
}
}
}
Aggregations