use of herddb.server.Server in project herddb by diennea.
the class MultiDMLOnSameRecordTest method testWithIndexSeek.
@Test
public void testWithIndexSeek() throws Exception {
Path baseDir = folder.newFolder().toPath();
ServerConfiguration serverConfiguration = newServerConfigurationWithAutoPort(baseDir);
serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_LOGICAL_PAGE_SIZE, 10 * 1024);
serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_DATA_MEMORY, 1024 * 1024 / 4);
serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_PK_MEMORY, 1024 * 1024);
serverConfiguration.set(ServerConfiguration.PROPERTY_CHECKPOINT_PERIOD, 0);
serverConfiguration.set(ServerConfiguration.PROPERTY_DATADIR, folder.newFolder().getAbsolutePath());
serverConfiguration.set(ServerConfiguration.PROPERTY_LOGDIR, folder.newFolder().getAbsolutePath());
try (Server server = new Server(serverConfiguration)) {
server.start();
server.waitForStandaloneBoot();
DBManager manager = server.getManager();
execute(manager, "CREATE TABLE mytable (id string primary key, n1 long, k2 string)", Collections.emptyList());
execute(manager, "CREATE INDEX tt ON mytable (k2)", Collections.emptyList());
ExecutorService threadPool = Executors.newFixedThreadPool(THREADPOLSIZE);
try {
List<Future> futures = new ArrayList<>();
AtomicLong updates = new AtomicLong();
AtomicLong deletes = new AtomicLong();
AtomicLong inserts = new AtomicLong();
AtomicLong duplicatePkErrors = new AtomicLong();
for (int i = 0; i < TESTSIZE; i++) {
futures.add(threadPool.submit(new Runnable() {
@Override
public void run() {
try {
int k = ThreadLocalRandom.current().nextInt(10);
long value = ThreadLocalRandom.current().nextInt(100) + 1000;
String key = "test_" + k;
boolean update = ThreadLocalRandom.current().nextBoolean();
boolean insert = ThreadLocalRandom.current().nextBoolean();
boolean delete = ThreadLocalRandom.current().nextBoolean();
if (update) {
updates.incrementAndGet();
execute(manager, "UPDATE mytable set n1=? WHERE k2=?", Arrays.asList(value, key));
} else if (insert) {
inserts.incrementAndGet();
try {
execute(manager, "INSERT INTO mytable(n1, id, k2) values(?,?,?)", Arrays.asList(value, key, key));
} catch (DuplicatePrimaryKeyException ok) {
duplicatePkErrors.incrementAndGet();
}
} else if (delete) {
deletes.incrementAndGet();
execute(manager, "DELETE FROM mytable WHERE k2=?", Arrays.asList(key));
}
} catch (Exception err) {
throw new RuntimeException(err);
}
}
}));
}
for (Future f : futures) {
f.get();
}
System.out.println("stats::updates:" + updates);
System.out.println("stats::deletes:" + deletes);
System.out.println("stats::inserts:" + inserts);
System.out.println("stats::duplicatePkErrors:" + duplicatePkErrors);
TableManagerStats stats = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTableManager("mytable").getStats();
System.out.println("stats::tablesize:" + stats.getTablesize());
System.out.println("stats::dirty records:" + stats.getDirtyrecords());
System.out.println("stats::unload count:" + stats.getUnloadedPagesCount());
System.out.println("stats::load count:" + stats.getLoadedPagesCount());
System.out.println("stats::buffers used mem:" + stats.getBuffersUsedMemory());
} finally {
threadPool.shutdown();
threadPool.awaitTermination(1, TimeUnit.MINUTES);
}
}
// restart and recovery
try (Server server = new Server(serverConfiguration)) {
server.start();
server.waitForTableSpaceBoot(TableSpace.DEFAULT, 300000, true);
}
}
use of herddb.server.Server in project herddb by diennea.
the class MultiDMLOnSameRecordTest method testWithPrimaryKeyIndexSeek.
@Test
public void testWithPrimaryKeyIndexSeek() throws Exception {
Path baseDir = folder.newFolder().toPath();
ServerConfiguration serverConfiguration = newServerConfigurationWithAutoPort(baseDir);
serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_LOGICAL_PAGE_SIZE, 10 * 1024);
serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_DATA_MEMORY, 1024 * 1024 / 4);
serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_PK_MEMORY, 1024 * 1024);
serverConfiguration.set(ServerConfiguration.PROPERTY_CHECKPOINT_PERIOD, 0);
serverConfiguration.set(ServerConfiguration.PROPERTY_DATADIR, folder.newFolder().getAbsolutePath());
serverConfiguration.set(ServerConfiguration.PROPERTY_LOGDIR, folder.newFolder().getAbsolutePath());
try (Server server = new Server(serverConfiguration)) {
server.start();
server.waitForStandaloneBoot();
DBManager manager = server.getManager();
execute(manager, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", Collections.emptyList());
ExecutorService threadPool = Executors.newFixedThreadPool(THREADPOLSIZE);
try {
List<Future> futures = new ArrayList<>();
AtomicLong updates = new AtomicLong();
AtomicLong deletes = new AtomicLong();
AtomicLong inserts = new AtomicLong();
AtomicLong duplicatePkErrors = new AtomicLong();
for (int i = 0; i < TESTSIZE; i++) {
futures.add(threadPool.submit(new Runnable() {
@Override
public void run() {
try {
int k = ThreadLocalRandom.current().nextInt(10);
long value = ThreadLocalRandom.current().nextInt(100) + 1000;
String key = "test_" + k;
boolean update = ThreadLocalRandom.current().nextBoolean();
boolean insert = ThreadLocalRandom.current().nextBoolean();
boolean delete = ThreadLocalRandom.current().nextBoolean();
if (update) {
System.err.println("do " + Thread.currentThread() + " update on " + Bytes.from_string(key));
updates.incrementAndGet();
execute(manager, "UPDATE mytable set n1=? WHERE id=?", Arrays.asList(value, key));
} else if (insert) {
System.err.println("do " + Thread.currentThread() + " insert on " + Bytes.from_string(key));
inserts.incrementAndGet();
try {
execute(manager, "INSERT INTO mytable(n1, id) values(?,?)", Arrays.asList(value, key));
} catch (DuplicatePrimaryKeyException ok) {
duplicatePkErrors.incrementAndGet();
}
} else if (delete) {
System.err.println("do " + Thread.currentThread() + " delete on " + Bytes.from_string(key));
deletes.incrementAndGet();
execute(manager, "DELETE FROM mytable WHERE id=?", Arrays.asList(key));
}
} catch (Exception err) {
throw new RuntimeException(err);
}
}
}));
}
for (Future f : futures) {
f.get();
}
System.out.println("stats::updates:" + updates);
System.out.println("stats::deletes:" + deletes);
System.out.println("stats::inserts:" + inserts);
System.out.println("stats::duplicatePkErrors:" + duplicatePkErrors);
TableManagerStats stats = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTableManager("mytable").getStats();
System.out.println("stats::tablesize:" + stats.getTablesize());
System.out.println("stats::dirty records:" + stats.getDirtyrecords());
System.out.println("stats::unload count:" + stats.getUnloadedPagesCount());
System.out.println("stats::load count:" + stats.getLoadedPagesCount());
System.out.println("stats::buffers used mem:" + stats.getBuffersUsedMemory());
} finally {
threadPool.shutdown();
threadPool.awaitTermination(1, TimeUnit.MINUTES);
}
}
// restart and recovery
try (Server server = new Server(serverConfiguration)) {
server.start();
server.waitForTableSpaceBoot(TableSpace.DEFAULT, 300000, true);
}
}
use of herddb.server.Server in project herddb by diennea.
the class PrettySQLExceptionTest method test.
@Test
public void test() throws Exception {
ServerConfiguration serverConfiguration = herddb.jdbc.TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath());
try (Server server = new Server(serverConfiguration)) {
server.start();
server.waitForStandaloneBoot();
try (Connection connection = DriverManager.getConnection(server.getJdbcUrl());
Statement statement = connection.createStatement()) {
try (ResultSet rs = statement.executeQuery("SELECT * FROM bad_table")) {
fail();
} catch (SQLException errorExpected) {
System.out.println("EXCEPTION MESSAGE: " + errorExpected.getMessage());
assertTrue("herddb.model.TableDoesNotExistException: no such table bad_table in tablespace default".equals(errorExpected.getMessage()) || errorExpected.getMessage().contains("Object 'BAD_TABLE' not found"));
}
statement.executeUpdate("CREATE TABLE mytable(pk string primary key)");
statement.executeUpdate("INSERT INTO mytable(pk) values('myvalue')");
// test pretty print errors
try (ResultSet rs = statement.executeQuery("SELECT pk FROM mytable")) {
assertTrue(rs.next());
assertEquals("Value 'myvalue' (class herddb.utils.RawString) cannot be converted to an integer value, call was getInt(1), column names: [pk]", TestUtils.expectThrows(SQLException.class, () -> {
rs.getInt("pk");
}).getMessage());
assertEquals("Value 'myvalue' (class herddb.utils.RawString) cannot be converted to an integer value, call was getDate(1), column names: [pk]", TestUtils.expectThrows(SQLException.class, () -> {
rs.getDate("pk");
}).getMessage());
assertEquals("Value 'myvalue' (class herddb.utils.RawString) cannot be converted to an integer value, call was getTimestamp(1), column names: [pk]", TestUtils.expectThrows(SQLException.class, () -> {
rs.getTimestamp("pk");
}).getMessage());
assertEquals("Value 'myvalue' (class herddb.utils.RawString) cannot be converted to an integer value, call was getLong(1), column names: [pk]", TestUtils.expectThrows(SQLException.class, () -> {
rs.getLong("pk");
}).getMessage());
}
}
}
}
use of herddb.server.Server in project herddb by diennea.
the class SelectColumnTest method test.
@Test
public void test() throws Exception {
try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
server.start();
server.waitForStandaloneBoot();
try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
Connection con = dataSource.getConnection();
Statement statement = con.createStatement()) {
statement.execute("CREATE TABLE mytable (key " + data.keyType + " primary key, val " + data.valueType + ")");
try (PreparedStatement insert = con.prepareStatement("INSERT INTO mytable (key,val) values(?,?)")) {
for (Object[] odata : data.data) {
insert.setObject(1, odata[0]);
setParameterAccordingToJavaType(insert, 2, odata[1]);
assertEquals(1, insert.executeUpdate());
}
}
if (data.checkKey()) {
try (PreparedStatement selectKey = con.prepareStatement("SELECT key FROM mytable WHERE key = ?")) {
for (Object[] odata : data.data) {
selectKey.setObject(1, odata[0]);
try (ResultSet rs = selectKey.executeQuery()) {
int count = 0;
while (rs.next()) {
data.keyChecker.accept(rs, odata[0]);
count++;
}
assertEquals(1, count);
}
}
}
}
try (PreparedStatement selectKey = con.prepareStatement("SELECT val FROM mytable WHERE key = ?")) {
for (Object[] odata : data.data) {
selectKey.setObject(1, odata[0]);
try (ResultSet rs = selectKey.executeQuery()) {
int count = 0;
while (rs.next()) {
data.valueChecker.accept(rs, odata[1]);
count++;
}
assertEquals(1, count);
}
}
}
}
}
}
}
use of herddb.server.Server in project herddb by diennea.
the class SimpleJoinTest method testSubQuery.
@Test
public void testSubQuery() throws Exception {
try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
server.start();
server.waitForStandaloneBoot();
try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
Connection con = dataSource.getConnection();
Statement statement = con.createStatement()) {
statement.execute("CREATE TABLE mytable (key string primary key, name string)");
statement.execute("CREATE TABLE table1 (k1 string primary key,n1 int,s1 string)");
statement.execute("CREATE TABLE table2 (k2 string primary key,n2 int,s2 string)");
statement.execute("INSERT INTO table1 (k1,n1,s1) values('a',1,'A')");
statement.execute("INSERT INTO table1 (k1,n1,s1) values('b',2,'B')");
statement.execute("INSERT INTO table2 (k2,n2,s2) values('c',3,'A')");
statement.execute("INSERT INTO table2 (k2,n2,s2) values('d',4,'A')");
try (ResultSet rs = statement.executeQuery("SELECT t1.k1, max(n1) as maxn1, max(select n2 from table2 t2 WHERE t1.s1=t2.s2) as maxn2 FROM " + " table1 t1 " + " group by k1")) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(2, count);
}
}
}
}
}
Aggregations