use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.
the class ScanHugeTableTest method testBatch.
@Test
public void testBatch() throws Exception {
try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
server.getManager().setMaxDataUsedMemory(750 * 1024 * 1024);
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 s = con.createStatement();
PreparedStatement ps = con.prepareStatement("INSERT INTO mytable (n1, name) values(?,?)")) {
s.execute("CREATE TABLE mytable (n1 int primary key, name string)");
String bigPrefix = StringUtils.repeat("Test", 300);
// int size = 1_000_000;
int size = 10_000;
{
long _start = System.currentTimeMillis();
con.setAutoCommit(false);
for (int i = 0; i < size; i++) {
ps.setInt(1, i);
ps.setString(2, bigPrefix + i);
ps.addBatch();
if (i % 6000 == 0) {
ps.executeBatch();
con.commit();
long _stop = System.currentTimeMillis();
System.out.println("written " + i + " records_ " + (_stop - _start) + " ms");
}
}
ps.executeBatch();
con.commit();
long _stop = System.currentTimeMillis();
System.out.println("insert: " + (_stop - _start) + " ms");
}
server.getManager().checkpoint();
con.setAutoCommit(true);
{
long _start = System.currentTimeMillis();
try (ResultSet rs = s.executeQuery("SELECT COUNT(*) from mytable")) {
assertTrue(rs.next());
long res = rs.getLong(1);
assertEquals(size, res);
}
long _stop = System.currentTimeMillis();
System.out.println("count: " + (_stop - _start) + " ms");
}
{
long _start = System.currentTimeMillis();
try (ResultSet rs = s.executeQuery("SELECT * from mytable")) {
int i = 0;
while (rs.next()) {
if (i % 1000 == 0) {
System.out.println("read " + i + " records");
}
i++;
}
assertEquals(size, i);
}
long _stop = System.currentTimeMillis();
System.out.println("read: " + (_stop - _start) + " ms");
}
}
}
}
}
use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.
the class SimpleDataSourceTest 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 string primary key, name string)");
assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,NamE) values('k1','name1')"));
try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
boolean found = false;
while (rs.next()) {
String key = rs.getString("key");
String name = rs.getString("name");
assertEquals("k1", key);
assertEquals("name1", name);
found = true;
}
assertTrue(found);
}
assertEquals(1, statement.executeUpdate("UPDATE MYTaBLE set name='name2' where key='k1'"));
try (ResultSet rs = statement.executeQuery("SELECT KeY,nAme FROM mytable")) {
boolean found = false;
while (rs.next()) {
String key = rs.getString("key");
String name = rs.getString("naMe");
assertEquals("k1", key);
assertEquals("name2", name);
found = true;
}
assertTrue(found);
}
try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
boolean found = false;
while (rs.next()) {
String key = rs.getString("key");
String name = rs.getString("name");
assertEquals("k1", key);
assertEquals("name2", name);
found = true;
}
assertTrue(found);
}
assertEquals(1, statement.executeUpdate("DELETE FROM mytable where name='name2' and key='k1'"));
assertEquals(0, statement.executeUpdate("UPDATE mytable set name='name2' where key='k1'"));
try (PreparedStatement ps = con.prepareStatement("INSERT INTO mytable (key,name) values(?,?)")) {
for (int i = 0; i < 10; i++) {
ps.setString(1, "kp_" + i);
ps.setString(2, "n2_" + i);
assertEquals(1, ps.executeUpdate());
}
}
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM mytable");
ResultSet rs = ps.executeQuery()) {
Set<String> foundKeys = new HashSet<>();
while (rs.next()) {
foundKeys.add(rs.getString("key"));
}
for (int i = 0; i < 10; i++) {
assertTrue(foundKeys.contains("kp_" + i));
}
}
try (PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) as total FROM mytable");
ResultSet rs = ps.executeQuery()) {
assertTrue(rs.next());
assertEquals(10, rs.getLong("total"));
}
try (PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) as cc FROM mytable");
ResultSet rs = ps.executeQuery()) {
assertTrue(rs.next());
assertEquals(10, rs.getLong("cc"));
assertEquals(10, rs.getLong(1));
assertEquals(Long.valueOf(10), rs.getObject(1));
assertEquals(Long.valueOf(10), rs.getObject("cc"));
}
}
}
}
}
use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.
the class SimpleExpressionsTest 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)) {
try (Connection con = dataSource.getConnection();
Statement statement = con.createStatement()) {
statement.execute("CREATE TABLE mytable (k1 string primary key, n1 int, l1 long, t1 timestamp, nu string, b1 bool, d1 double)");
}
try (Connection con = dataSource.getConnection();
PreparedStatement statement = con.prepareStatement("INSERT INTO mytable(k1,n1,l1,t1,nu,b1,d1) values(?,?,?,?,?,?,?)")) {
int i = 1;
statement.setString(i++, "mykey");
statement.setInt(i++, 1);
statement.setLong(i++, 2);
statement.setTimestamp(i++, new java.sql.Timestamp(System.currentTimeMillis()));
statement.setString(i++, null);
statement.setBoolean(i++, true);
statement.setDouble(i++, 1.5);
int rows = statement.executeUpdate();
Assert.assertEquals(1, rows);
}
// Types
try (Connection con = dataSource.getConnection()) {
// String
try (PreparedStatement statement = con.prepareStatement("SELECT k1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals("mykey", rs.getObject(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT upper(k1) FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals("MYKEY", rs.getObject(1));
}
}
// Number
try (PreparedStatement statement = con.prepareStatement("SELECT n1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(1, rs.getObject(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT n1*5 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(5L, rs.getObject(1));
assertEquals(5, rs.getLong(1));
assertEquals(5, rs.getInt(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT n1*5.0 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(5d, rs.getObject(1));
assertEquals(5, rs.getLong(1));
assertEquals(5, rs.getInt(1));
}
}
// Long
try (PreparedStatement statement = con.prepareStatement("SELECT l1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(2L, rs.getObject(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT l1*5 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(10L, rs.getObject(1));
assertEquals(10, rs.getLong(1));
assertEquals(10, rs.getInt(1));
assertEquals(10f, rs.getFloat(1), 0);
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT l1*5.1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(10.2, rs.getObject(1));
assertEquals(10, rs.getLong(1));
assertEquals(10, rs.getInt(1));
assertEquals(10.2f, rs.getFloat(1), 0);
}
}
// Null
try (PreparedStatement statement = con.prepareStatement("SELECT nu FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(null, rs.getObject(1));
}
}
// Double
try (PreparedStatement statement = con.prepareStatement("SELECT 3/2 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(1.5, rs.getObject(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT d1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(1.5, rs.getObject(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT d1*5/2 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(3.75, rs.getObject(1));
assertEquals(3.75, rs.getDouble(1), 0);
assertEquals(3, rs.getLong(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT -n1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(-1, rs.getObject(1));
assertEquals(-1, rs.getDouble(1), 0);
assertEquals(-1, rs.getInt(1), 0);
assertEquals(-1, rs.getLong(1), 0);
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT -n1-n1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(-2L, rs.getObject(1));
assertEquals(-2, rs.getDouble(1), 0);
assertEquals(-2, rs.getInt(1), 0);
assertEquals(-2, rs.getLong(1), 0);
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT +n1+n1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(2L, rs.getObject(1));
assertEquals(2, rs.getDouble(1), 0);
assertEquals(2, rs.getInt(1), 0);
assertEquals(2, rs.getLong(1), 0);
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT n1*n1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(1L, rs.getObject(1));
assertEquals(1, rs.getDouble(1), 0);
assertEquals(1, rs.getInt(1), 0);
assertEquals(1, rs.getLong(1), 0);
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT -d1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(-1.5, rs.getObject(1));
assertEquals(-1.5, rs.getDouble(1), 0);
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT +d1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(1.5, rs.getObject(1));
assertEquals(1.5, rs.getDouble(1), 0);
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT +d1+d1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(3, rs.getDouble(1), 0);
assertEquals(3.0, rs.getObject(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT +d1*d1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(2.25, rs.getDouble(1), 0);
assertEquals(2.25, rs.getObject(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT -d1+d1 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(0, rs.getDouble(1), 0);
assertEquals(0.0, rs.getObject(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT +d1+4 FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(5.5, rs.getDouble(1), 0);
assertEquals(5.5, rs.getObject(1));
}
}
// Boolean
try (PreparedStatement statement = con.prepareStatement("SELECT true FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(true, rs.getObject(1));
assertEquals(true, rs.getBoolean(1));
}
}
try (PreparedStatement statement = con.prepareStatement("SELECT false FROM mytable")) {
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertEquals(false, rs.getObject(1));
assertEquals(false, rs.getBoolean(1));
}
}
}
}
}
}
}
use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.
the class SimpleScanTest method test.
@Test
public void test() throws Exception {
try (Server server = new Server(herddb.jdbc.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)) {
try (Connection con = dataSource.getConnection();
Statement statement = con.createStatement()) {
statement.execute("CREATE TABLE mytable (k1 string primary key, n1 int, l1 long, t1 timestamp, nu string, b1 bool, d1 double, s1 smallint)");
}
try (Connection con = dataSource.getConnection();
PreparedStatement statement = con.prepareStatement("INSERT INTO mytable(k1,n1,l1,t1,nu,b1,d1,s1) values(?,?,?,?,?,?,?,?)")) {
for (int n = 0; n < 10; ++n) {
int i = 1;
statement.setString(i++, "mykey_" + n);
statement.setInt(i++, n);
statement.setLong(i++, n);
statement.setTimestamp(i++, new java.sql.Timestamp(System.currentTimeMillis()));
statement.setString(i++, null);
statement.setBoolean(i++, true);
statement.setDouble(i++, n + 0.5);
statement.setShort(i++, (short) n);
statement.addBatch();
}
int[] batches = statement.executeBatch();
Assert.assertEquals(10, batches.length);
for (int batch : batches) {
Assert.assertEquals(1, batch);
}
}
try (Connection con = dataSource.getConnection()) {
// Get and projection
try (PreparedStatement statement = con.prepareStatement("SELECT n1, k1 FROM mytable WHERE k1 = 'mykey_1'")) {
int count = 0;
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
assertEquals(1, rs.getInt(1));
assertNotNull(rs.getString(2));
++count;
}
}
assertEquals(1, count);
}
// Scan and projection
try (PreparedStatement statement = con.prepareStatement("SELECT n1, k1 FROM mytable WHERE n1 > 1")) {
int count = 0;
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
assertTrue(rs.getInt(1) > 1);
assertNotNull(rs.getString(2));
++count;
}
}
assertEquals(8, count);
}
// Scan, sort and projection
try (PreparedStatement statement = con.prepareStatement("SELECT n1, k1 FROM mytable WHERE n1 > 1 ORDER BY n1")) {
int count = 0;
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
assertTrue(rs.getInt(1) > 1);
assertNotNull(rs.getString(2));
++count;
}
}
assertEquals(8, count);
}
// verify all data types and table contents
try (PreparedStatement statement = con.prepareStatement("SELECT * FROM mytable ORDER BY n1")) {
try (ResultSet rs = statement.executeQuery()) {
for (int n = 0; n < 10; ++n) {
int i = 1;
assertTrue(rs.next());
assertEquals("mykey_" + n, rs.getString(i++));
assertEquals(n, rs.getInt(i++));
assertEquals(n, rs.getLong(i++));
assertNotNull(rs.getTimestamp(i++));
assertNull(rs.getString(i++));
assertTrue(rs.getBoolean(i++));
assertEquals(n + 0.5, rs.getDouble(i++), 0d);
assertEquals((short) n, rs.getShort(i++));
}
}
}
}
}
}
}
}
use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.
the class SimpleScanTest method testSingleColumnHeapTable.
@Test
public void testSingleColumnHeapTable() throws Exception {
try (Server server = new Server(herddb.jdbc.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)) {
try (Connection con = dataSource.getConnection();
Statement statement = con.createStatement()) {
// no primary key
statement.execute("CREATE TABLE mytable (id string)");
}
try (Connection con = dataSource.getConnection();
PreparedStatement statement = con.prepareStatement("INSERT INTO mytable(id) values(?)")) {
for (int n = 0; n < 10; ++n) {
int i = 1;
statement.setString(i++, "mykey_" + n);
statement.addBatch();
}
int[] batches = statement.executeBatch();
Assert.assertEquals(10, batches.length);
for (int batch : batches) {
Assert.assertEquals(1, batch);
}
}
try (Connection con = dataSource.getConnection()) {
try (PreparedStatement statement = con.prepareStatement("SELECT * FROM mytable")) {
int count = 0;
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
assertNotNull(rs.getString(1));
++count;
}
}
assertEquals(10, count);
}
}
}
}
}
}
Aggregations