use of com.mysql.cj.xdevapi.JsonString in project aws-mysql-jdbc by awslabs.
the class AsyncQueryTest method overlappedAsyncQueries.
@Test
public void overlappedAsyncQueries() throws Exception {
final int NUMBER_OF_QUERIES = 1000;
Session sess = null;
try {
sess = new SessionFactory().getSession(this.baseUrl);
Collection coll = sess.getSchema(this.schema.getName()).getCollection(this.collection.getName());
String json1 = "{'mode': 'sync'}".replaceAll("'", "\"");
String json2 = "{'mode': 'async'}".replaceAll("'", "\"");
if (!mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
// Inject an _id.
json1 = json1.replace("{", "{\"_id\": \"1\", ");
json2 = json2.replace("{", "{\"_id\": \"2\", ");
}
AddResult res = coll.add(json1).add(json2).execute();
if (mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
assertTrue(res.getGeneratedIds().get(0).matches("[a-f0-9]{28}"));
assertTrue(res.getGeneratedIds().get(1).matches("[a-f0-9]{28}"));
} else {
assertEquals(0, res.getGeneratedIds().size());
}
List<CompletableFuture<DocResult>> futures = new ArrayList<>();
for (int i = 0; i < NUMBER_OF_QUERIES; ++i) {
if (i % 5 == 0) {
futures.add(CompletableFuture.completedFuture(coll.find("mode = 'sync'").execute()));
} else {
futures.add(coll.find("mode = 'async'").executeAsync());
}
}
for (int i = 0; i < NUMBER_OF_QUERIES; ++i) {
try {
DocResult docs = futures.get(i).get();
DbDoc d = docs.next();
JsonString mode = (JsonString) d.get("mode");
if (i % 5 == 0) {
assertEquals("sync", mode.getString(), "i = " + i);
} else {
assertEquals("async", mode.getString(), "i = " + i);
}
} catch (Throwable t) {
throw new Exception("Error on i = " + i, t);
}
}
} finally {
if (sess != null) {
sess.close();
sess = null;
}
}
}
use of com.mysql.cj.xdevapi.JsonString in project aws-mysql-jdbc by awslabs.
the class AsyncQueryTest method basicAsyncQuery.
@Test
public void basicAsyncQuery() throws Exception {
String json = "{'firstName':'Frank', 'middleName':'Lloyd', 'lastName':'Wright'}".replaceAll("'", "\"");
if (!mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
// Inject an _id.
json = json.replace("{", "{\"_id\": \"1\", ");
}
AddResult res = this.collection.add(json).execute();
if (mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
assertTrue(res.getGeneratedIds().get(0).matches("[a-f0-9]{28}"));
} else {
assertEquals(0, res.getGeneratedIds().size());
}
CompletableFuture<DocResult> docsF = this.collection.find("firstName like '%Fra%'").executeAsync();
DocResult docs = docsF.get();
DbDoc d = docs.next();
JsonString val = (JsonString) d.get("lastName");
assertEquals("Wright", val.getString());
}
use of com.mysql.cj.xdevapi.JsonString in project aws-mysql-jdbc by awslabs.
the class AsyncQueryTest method insertDocs.
@Test
public void insertDocs() throws Exception {
String json = "{'firstName':'Frank', 'middleName':'Lloyd', 'lastName':'Wright'}".replaceAll("'", "\"");
if (!mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
// Inject an _id.
json = json.replace("{", "{\"_id\": \"1\", ");
}
CompletableFuture<AddResult> resF = this.collection.add(json).executeAsync();
CompletableFuture<DocResult> docF = resF.thenCompose((AddResult res) -> {
if (mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
assertTrue(res.getGeneratedIds().get(0).matches("[a-f0-9]{28}"));
} else {
assertEquals(0, res.getGeneratedIds().size());
}
return this.collection.find("firstName like '%Fra%'").executeAsync();
});
DbDoc d = docF.thenApply((DocResult docs) -> docs.next()).get(5, TimeUnit.SECONDS);
JsonString val = (JsonString) d.get("lastName");
assertEquals("Wright", val.getString());
}
use of com.mysql.cj.xdevapi.JsonString in project aws-mysql-jdbc by awslabs.
the class RowLockingTest method testFindRowLockingValid.
/**
* START collection.find() tests
*
* @throws Exception
*/
@Test
public void testFindRowLockingValid() throws Exception {
assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
int i = 0;
try {
DbDoc[] jsonlist = new DbDocImpl[10];
for (i = 1; i <= 10; i++) {
DbDoc newDoc2 = new DbDocImpl();
newDoc2.add("F1", new JsonNumber().setValue(String.valueOf(i)));
newDoc2.add("F2", new JsonString().setValue("Field-1-Data-" + i));
newDoc2.add("F3", new JsonNumber().setValue(String.valueOf(10 * (i) + 0.1234)));
jsonlist[i - 1] = newDoc2;
newDoc2 = null;
}
this.collection.add(jsonlist).execute();
assertEquals((10), this.collection.count());
/* Two threads with same conditions, select for update in one and update in second */
this.CheckFlag = 0;
initException = new Throwable[2];
FindRowLock[] Thrd = new FindRowLock[2];
Thrd[0] = new FindRowLock(1, 1, 0, 5, "$.F1 = ?");
Thrd[0].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(0));
Thrd[0].start();
Thrd[1] = new FindRowLock(2, 0, 1, 5, "$.F1 = ?");
Thrd[1].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(1));
Thrd[1].start();
for (i = 0; i < 2; i++) {
Thrd[i].join();
}
for (i = 0; i < 2; i++) {
if (initException[i] != null) {
throw new RuntimeException(initException[i]);
}
}
/* Two threads with same conditions, select for share in one and update in second */
this.CheckFlag = 0;
initException = new Throwable[2];
Thrd[0] = new FindRowLock(1, 2, 0, 5, "$.F1 = ?");
Thrd[0].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(0));
Thrd[0].start();
Thrd[1] = new FindRowLock(2, 0, 1, 5, "$.F1 = ?");
Thrd[1].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(1));
Thrd[1].start();
for (i = 0; i < 2; i++) {
Thrd[i].join();
}
for (i = 0; i < 2; i++) {
if (initException[i] != null) {
throw new RuntimeException(initException[i]);
}
}
/* Two threads with same conditions selecting multiple records, select for update in one and update in second */
this.CheckFlag = 0;
initException = new Throwable[2];
Thrd[0] = new FindRowLock(1, 2, 0, 5, "$.F1 < ?");
Thrd[0].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(0));
Thrd[0].start();
Thrd[1] = new FindRowLock(2, 0, 1, 5, "$.F1 < ?");
Thrd[1].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(1));
Thrd[1].start();
for (i = 0; i < 2; i++) {
Thrd[i].join();
}
for (i = 0; i < 2; i++) {
if (initException[i] != null) {
throw new RuntimeException(initException[i]);
}
}
/* Two threads with same conditions selecting multiple rows, select for share in one and update in second */
this.CheckFlag = 0;
initException = new Throwable[2];
Thrd[0] = new FindRowLock(1, 1, 0, 5, "$.F1 < ?");
Thrd[0].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(0));
Thrd[0].start();
Thrd[1] = new FindRowLock(2, 0, 1, 5, "$.F1 < ?");
Thrd[1].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(1));
Thrd[1].start();
for (i = 0; i < 2; i++) {
Thrd[i].join();
}
for (i = 0; i < 2; i++) {
if (initException[i] != null) {
throw new RuntimeException(initException[i]);
}
}
/* Two threads with same conditions, select for update in one and select for share in second */
this.CheckFlag = 0;
initException = new Throwable[2];
Thrd[0] = new FindRowLock(1, 2, 0, 5, "$.F1 < ?");
Thrd[0].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(0));
Thrd[0].start();
Thrd[1] = new FindRowLock(1, 1, 1, 5, "$.F1 < ?");
Thrd[1].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(1));
Thrd[1].start();
for (i = 0; i < 2; i++) {
Thrd[i].join();
}
for (i = 0; i < 2; i++) {
if (initException[i] != null) {
throw new RuntimeException(initException[i]);
}
}
} catch (RuntimeException e) {
System.out.print("**************RuntimeException: " + i);
System.out.println(e.getMessage());
throw e;
} catch (InterruptedException e) {
System.out.print("InterruptedException: " + i);
System.out.println(e.getMessage());
throw e;
}
}
use of com.mysql.cj.xdevapi.JsonString in project aws-mysql-jdbc by awslabs.
the class TableInsertTest method jsonInsert.
@Test
public void jsonInsert() throws IOException {
try {
sqlUpdate("drop table if exists jsonInsert");
sqlUpdate("create table jsonInsert (_id varchar(32), doc JSON)");
Table table = this.schema.getTable("jsonInsert");
table.insert("_id", "doc").values(1, "{\"x\":\"1\"}").execute();
table.insert().values(2, "{\"x\":\"2\"}").execute();
Map<String, Object> row = new HashMap<>();
row.put("_id", 3);
row.put("doc", "{\"x\":\"3\"}");
table.insert(row).execute();
DbDoc doc = new DbDocImpl().add("firstName", new JsonString().setValue("Georgia"));
doc.add("middleName", new JsonString().setValue("Totto"));
doc.add("lastName", new JsonString().setValue("O'Keeffe"));
table.insert("_id", "doc").values(4, doc).execute();
RowResult rows = table.select("_id, doc").orderBy("_id").execute();
Row r = rows.next();
assertEquals("1", r.getString("_id"));
assertEquals(JsonParser.parseDoc(new StringReader("{\"x\":\"1\"}")).toString(), r.getDbDoc("doc").toString());
r = rows.next();
assertEquals("2", r.getString("_id"));
assertEquals("{\"x\": \"2\"}", r.getString("doc"));
r = rows.next();
assertEquals("3", r.getString("_id"));
assertEquals("{\"x\": \"3\"}", r.getString("doc"));
r = rows.next();
assertEquals("4", r.getString("_id"));
assertEquals(doc.toString(), r.getDbDoc("doc").toString());
} finally {
sqlUpdate("drop table if exists jsonInsert");
}
}
Aggregations