use of herddb.client.DMLResult in project herddb by diennea.
the class SimpleClientServerAutoTransactionTest method test.
@Test
public void test() throws Exception {
Path baseDir = folder.newFolder().toPath();
;
String _baseDir = baseDir.toString();
try (Server server = new Server(new ServerConfiguration(baseDir))) {
server.start();
ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
try (HDBClient client = new HDBClient(clientConfiguration);
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", 0, false, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
DMLResult executeUpdateResult = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", TransactionContext.AUTOTRANSACTION_ID, false, Arrays.asList("test", 1, 2));
long tx = executeUpdateResult.transactionId;
long countInsert = executeUpdateResult.updateCount;
Assert.assertEquals(1, countInsert);
GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", tx, Collections.emptyList());
;
Map<String, Object> record = res.data;
Assert.assertNotNull(record);
assertEquals("test", record.get("id"));
assertEquals(Long.valueOf(1), record.get("n1"));
assertEquals(Integer.valueOf(2), record.get("n2"));
connection.commitTransaction(TableSpace.DEFAULT, tx);
try (ScanResultSet scan = connection.executeScan(server.getManager().getVirtualTableSpaceId(), "SELECT * FROM sysconfig", Collections.emptyList(), 0, 0, 10)) {
List<Map<String, Object>> all = scan.consume();
for (Map<String, Object> aa : all) {
String name = (String) aa.get("name");
assertEquals("server.base.dir", name);
String value = (String) aa.get("value");
assertEquals(_baseDir, value);
}
}
try (ScanResultSet scan = connection.executeScan(null, "SELECT * FROM " + server.getManager().getVirtualTableSpaceId() + ".sysclients", Collections.emptyList(), 0, 0, 10)) {
List<Map<String, Object>> all = scan.consume();
for (Map<String, Object> aa : all) {
assertEquals("jvm-local", aa.get("address"));
assertEquals("1", aa.get("id") + "");
assertEquals(ClientConfiguration.PROPERTY_CLIENT_USERNAME_DEFAULT, aa.get("username"));
assertNotNull(aa.get("connectionts"));
}
assertEquals(1, all.size());
}
}
}
}
use of herddb.client.DMLResult in project herddb by diennea.
the class SimpleClientServerTest method test.
@Test
public void test() throws Exception {
Path baseDir = folder.newFolder().toPath();
;
String _baseDir = baseDir.toString();
try (Server server = new Server(new ServerConfiguration(baseDir))) {
server.start();
server.waitForStandaloneBoot();
ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
try (HDBClient client = new HDBClient(clientConfiguration);
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
assertTrue(connection.waitForTableSpace(TableSpace.DEFAULT, 100));
assertFalse(connection.waitForTableSpace("bad-tablespace", 100));
long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", 0, false, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
long tx = connection.beginTransaction(TableSpace.DEFAULT);
long countInsert = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", tx, false, Arrays.asList("test", 1, 2)).updateCount;
Assert.assertEquals(1, countInsert);
long countInsert2 = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", tx, false, Arrays.asList("test2", 2, 3)).updateCount;
Assert.assertEquals(1, countInsert2);
GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", tx, Collections.emptyList());
Map<String, Object> record = res.data;
Assert.assertNotNull(record);
assertEquals("test", record.get("id"));
assertEquals(Long.valueOf(1), record.get("n1"));
assertEquals(Integer.valueOf(2), record.get("n2"));
List<DMLResult> executeUpdates = connection.executeUpdates(TableSpace.DEFAULT, "UPDATE mytable set n2=? WHERE id=?", tx, false, Arrays.asList(Arrays.asList(1, "test"), Arrays.asList(2, "test2"), Arrays.asList(3, "test_not_exists")));
assertEquals(3, executeUpdates.size());
assertEquals(1, executeUpdates.get(0).updateCount);
assertEquals(1, executeUpdates.get(1).updateCount);
assertEquals(0, executeUpdates.get(2).updateCount);
assertEquals(tx, executeUpdates.get(0).transactionId);
assertEquals(tx, executeUpdates.get(1).transactionId);
assertEquals(tx, executeUpdates.get(2).transactionId);
connection.commitTransaction(TableSpace.DEFAULT, tx);
try (ScanResultSet scan = connection.executeScan(server.getManager().getVirtualTableSpaceId(), "SELECT * FROM sysconfig", Collections.emptyList(), 0, 0, 10)) {
List<Map<String, Object>> all = scan.consume();
for (Map<String, Object> aa : all) {
String name = (String) aa.get("name");
assertEquals("server.base.dir", name);
String value = (String) aa.get("value");
assertEquals(_baseDir, value);
}
}
try (ScanResultSet scan = connection.executeScan(null, "SELECT * FROM " + server.getManager().getVirtualTableSpaceId() + ".sysclients", Collections.emptyList(), 0, 0, 10)) {
List<Map<String, Object>> all = scan.consume();
for (Map<String, Object> aa : all) {
assertEquals("jvm-local", aa.get("address"));
assertNotNull(aa.get("id"));
assertEquals(ClientConfiguration.PROPERTY_CLIENT_USERNAME_DEFAULT, aa.get("username"));
assertNotNull(aa.get("connectionts"));
}
assertEquals(1, all.size());
}
}
}
}
use of herddb.client.DMLResult in project herddb by diennea.
the class MultipleConcurrentUpdatesTest method performTest.
private void performTest(boolean useTransactions, long checkPointPeriod, boolean withIndexes) throws Exception {
Path baseDir = folder.newFolder().toPath();
ServerConfiguration serverConfiguration = new ServerConfiguration(baseDir);
serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_LOGICAL_PAGE_SIZE, 10 * 1024);
serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_DATA_MEMORY, 1024 * 1024);
serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_PK_MEMORY, 1024 * 1024);
serverConfiguration.set(ServerConfiguration.PROPERTY_CHECKPOINT_PERIOD, checkPointPeriod);
serverConfiguration.set(ServerConfiguration.PROPERTY_DATADIR, folder.newFolder().getAbsolutePath());
serverConfiguration.set(ServerConfiguration.PROPERTY_LOGDIR, folder.newFolder().getAbsolutePath());
ConcurrentHashMap<String, Long> expectedValue = new ConcurrentHashMap<>();
try (Server server = new Server(serverConfiguration)) {
server.start();
server.waitForStandaloneBoot();
ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
try (HDBClient client = new HDBClient(clientConfiguration);
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", 0, false, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
if (withIndexes) {
long resultCreateIndex = connection.executeUpdate(TableSpace.DEFAULT, "CREATE INDEX theindex ON mytable (n1 long)", 0, false, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateIndex);
}
long tx = connection.beginTransaction(TableSpace.DEFAULT);
for (int i = 0; i < TABLESIZE; i++) {
connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", tx, false, Arrays.asList("test_" + i, 1, 2));
expectedValue.put("test_" + i, 1L);
}
connection.commitTransaction(TableSpace.DEFAULT, tx);
ExecutorService threadPool = Executors.newFixedThreadPool(THREADPOLSIZE);
try {
List<Future> futures = new ArrayList<>();
AtomicLong updates = new AtomicLong();
AtomicLong skipped = new AtomicLong();
AtomicLong gets = new AtomicLong();
for (int i = 0; i < TABLESIZE * MULTIPLIER; i++) {
futures.add(threadPool.submit(new Runnable() {
@Override
public void run() {
try {
boolean update = ThreadLocalRandom.current().nextBoolean();
int k = ThreadLocalRandom.current().nextInt(TABLESIZE);
long value = ThreadLocalRandom.current().nextInt(TABLESIZE);
long transactionId;
String key = "test_" + k;
Long actual = expectedValue.remove(key);
if (actual == null) {
// another thread working on this entry, skip
skipped.incrementAndGet();
return;
}
if (update) {
updates.incrementAndGet();
DMLResult updateResult = connection.executeUpdate(TableSpace.DEFAULT, "UPDATE mytable set n1=? WHERE id=?", useTransactions ? TransactionContext.AUTOTRANSACTION_ID : TransactionContext.NOTRANSACTION_ID, false, Arrays.asList(value, "test_" + k));
long count = updateResult.updateCount;
transactionId = updateResult.transactionId;
if (count <= 0) {
throw new RuntimeException("not updated ?");
}
} else {
gets.incrementAndGet();
GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT * FROM mytable where id=?", useTransactions ? TransactionContext.AUTOTRANSACTION_ID : TransactionContext.NOTRANSACTION_ID, Arrays.asList("test_" + k));
if (res.data == null) {
throw new RuntimeException("not found?");
}
if (!res.data.get("n1").equals(actual)) {
throw new RuntimeException("unspected value " + res.data + ", expected: " + actual);
}
transactionId = res.transactionId;
// value did not change actually
value = actual;
}
if (useTransactions) {
if (transactionId <= 0) {
throw new RuntimeException("no transaction ?");
}
connection.commitTransaction(TableSpace.DEFAULT, transactionId);
}
expectedValue.put(key, value);
} catch (Exception err) {
throw new RuntimeException(err);
}
}
}));
}
for (Future f : futures) {
f.get();
}
System.out.println("stats::updates:" + updates);
System.out.println("stats::get:" + gets);
System.out.println("stats::skipped:" + skipped);
assertTrue(updates.get() > 0);
assertTrue(gets.get() > 0);
List<String> erroredKeys = new ArrayList<>();
for (Map.Entry<String, Long> entry : expectedValue.entrySet()) {
GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT n1 FROM mytable where id=?", TransactionContext.NOTRANSACTION_ID, Arrays.asList(entry.getKey()));
assertNotNull(res.data);
if (!entry.getValue().equals(res.data.get("n1"))) {
if (!entry.getValue().equals(res.data.get("n1"))) {
System.out.println("expected value " + res.data.get("n1") + ", but got " + Long.valueOf(entry.getValue()) + " for key " + entry.getKey());
erroredKeys.add(entry.getKey());
}
}
}
assertTrue(erroredKeys.isEmpty());
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());
assertTrue(stats.getUnloadedPagesCount() > 0);
assertEquals(TABLESIZE, stats.getTablesize());
} finally {
threadPool.shutdown();
threadPool.awaitTermination(1, TimeUnit.MINUTES);
}
}
}
// restart and recovery
try (Server server = new Server(serverConfiguration)) {
server.start();
server.waitForStandaloneBoot();
ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
try (HDBClient client = new HDBClient(clientConfiguration);
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
for (Map.Entry<String, Long> entry : expectedValue.entrySet()) {
GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT n1 FROM mytable where id=?", TransactionContext.NOTRANSACTION_ID, Arrays.asList(entry.getKey()));
assertNotNull(res.data);
assertEquals(entry.getValue(), res.data.get("n1"));
}
}
}
}
use of herddb.client.DMLResult in project herddb by diennea.
the class HerdDBPreparedStatement method executeBatchAsync.
@Override
public CompletableFuture<int[]> executeBatchAsync() {
CompletableFuture<int[]> res = new CompletableFuture<>();
lastUpdateCount = 0;
long tx;
try {
parent.discoverTableSpace(sql);
tx = parent.ensureTransaction();
} catch (SQLException err) {
res.completeExceptionally(err);
return res;
}
parent.getConnection().executeUpdatesAsync(parent.getTableSpace(), sql, tx, false, true, this.batch).whenComplete((dmsresults, error) -> {
if (error != null) {
res.completeExceptionally(SQLExceptionUtils.wrapException(error));
} else {
int[] results = new int[batch.size()];
int i = 0;
for (DMLResult dmlresult : dmsresults) {
results[i++] = (int) dmlresult.updateCount;
parent.bindToTransaction(dmlresult.transactionId);
lastUpdateCount += dmlresult.updateCount;
lastKey = dmlresult.key;
}
res.complete(results);
}
batch.clear();
});
return res;
}
use of herddb.client.DMLResult in project herddb by diennea.
the class ServerSideConnectionPeer method executeUpdates.
public List<DMLResult> executeUpdates(String tableSpace, String query, long transactionId, boolean returnValues, List<List<Object>> originalBatch) throws HDBException {
if (query == null) {
throw new HDBException("bad query null");
}
List<List<Object>> batch = new ArrayList<>(originalBatch.size());
for (int i = 0; i < originalBatch.size(); i++) {
batch.add(PduCodec.normalizeParametersList(originalBatch.get(i)));
}
try {
List<TranslatedQuery> queries = new ArrayList<>();
for (int i = 0; i < batch.size(); i++) {
List<Object> parameters = batch.get(i);
TranslatedQuery translatedQuery = server.getManager().getPlanner().translate(tableSpace, query, parameters, false, true, returnValues, -1);
queries.add(translatedQuery);
}
List<Long> updateCounts = new CopyOnWriteArrayList<>();
List<Map<RawString, Object>> otherDatas = new CopyOnWriteArrayList<>();
CompletableFuture<Long> finalResult = new CompletableFuture<>();
class ComputeNext implements BiConsumer<StatementExecutionResult, Throwable> {
int current;
public ComputeNext(int current) {
this.current = current;
}
@Override
public void accept(StatementExecutionResult result, Throwable error) {
if (error != null) {
finalResult.completeExceptionally(error);
return;
}
if (result instanceof DMLStatementExecutionResult) {
DMLStatementExecutionResult dml = (DMLStatementExecutionResult) result;
final Map<RawString, Object> otherData;
if (returnValues && dml.getKey() != null) {
TranslatedQuery translatedQuery = queries.get(current - 1);
Statement statement = translatedQuery.plan.mainStatement;
TableAwareStatement tableStatement = (TableAwareStatement) statement;
Table table = server.getManager().getTableSpaceManager(statement.getTableSpace()).getTableManager(tableStatement.getTable()).getTable();
Object key = RecordSerializer.deserializePrimaryKey(dml.getKey(), table);
otherData = new HashMap<>();
otherData.put(RAWSTRING_KEY, key);
if (dml.getNewvalue() != null) {
Map<String, Object> newvalue = RecordSerializer.toBean(new Record(dml.getKey(), dml.getNewvalue()), table);
newvalue.forEach((k, v) -> {
otherData.put(RawString.of(k), v);
});
}
} else {
otherData = Collections.emptyMap();
}
updateCounts.add((long) dml.getUpdateCount());
otherDatas.add(otherData);
} else if (result instanceof DDLStatementExecutionResult) {
Map<RawString, Object> otherData = Collections.emptyMap();
updateCounts.add(1L);
otherDatas.add(otherData);
} else {
finalResult.completeExceptionally(new Exception("bad result type " + result.getClass() + " (" + result + ")"));
return;
}
long newTransactionId = result.transactionId;
if (current == queries.size()) {
try {
finalResult.complete(newTransactionId);
} catch (Throwable t) {
finalResult.completeExceptionally(t);
}
return;
}
TranslatedQuery nextPlannedQuery = queries.get(current);
TransactionContext transactionContext = new TransactionContext(newTransactionId);
CompletableFuture<StatementExecutionResult> nextPromise = server.getManager().executePlanAsync(nextPlannedQuery.plan, nextPlannedQuery.context, transactionContext);
nextPromise.whenComplete(new ComputeNext(current + 1));
}
}
TransactionContext transactionContext = new TransactionContext(transactionId);
TranslatedQuery firstTranslatedQuery = queries.get(0);
server.getManager().executePlanAsync(firstTranslatedQuery.plan, firstTranslatedQuery.context, transactionContext).whenComplete(new ComputeNext(1));
long finalTransactionId = finalResult.get();
List<DMLResult> returnedValues = new ArrayList<>();
for (int i = 0; i < updateCounts.size(); i++) {
returnedValues.add(new DMLResult(updateCounts.get(i), otherDatas.get(i).get(RAWSTRING_KEY), otherDatas.get(i), finalTransactionId));
}
return returnedValues;
} catch (HerdDBInternalException err) {
throw new HDBException(err);
} catch (InterruptedException err) {
Thread.currentThread().interrupt();
throw new HDBException(err);
} catch (ExecutionException err) {
throw new HDBException(err.getCause());
}
}
Aggregations