use of com.mysql.cj.xdevapi.AddResult in project aws-mysql-jdbc by awslabs.
the class CollectionModifyTest method testCollectionModifyAsync.
@Test
public void testCollectionModifyAsync() throws Exception {
int i = 0, j = 0, maxrec = 10;
DbDoc doc = null;
AddResult res = null;
Result res2 = null;
CompletableFuture<AddResult> asyncRes = null;
CompletableFuture<Result> asyncRes2 = null;
CompletableFuture<DocResult> asyncDocs = null;
DocResult docs = null;
double d = 100.123;
/* add().executeAsync() maxrec num of records */
for (i = 0; i < maxrec; i++) {
DbDoc newDoc2 = new DbDocImpl();
newDoc2.add("_id", new JsonString().setValue(String.valueOf(i + 1000)));
newDoc2.add("F1", new JsonString().setValue("Field-1-Data-" + i));
newDoc2.add("F2", new JsonNumber().setValue(String.valueOf(d + i)));
newDoc2.add("F3", new JsonNumber().setValue(String.valueOf(100 + i)));
newDoc2.add("T", new JsonNumber().setValue(String.valueOf(i)));
asyncRes = this.collection.add(newDoc2).executeAsync();
res = asyncRes.get();
assertEquals(1, res.getAffectedItemsCount());
newDoc2 = null;
}
assertEquals((maxrec), this.collection.count());
asyncDocs = this.collection.find("F3 >= ? and F3 < ?").bind(100, 100006).fields(expr("{'cnt':count($.F1)}")).executeAsync();
docs = asyncDocs.get();
doc = docs.next();
assertEquals((long) maxrec, (long) (((JsonNumber) doc.get("cnt")).getInteger()));
/* Simple Update with executeAsync */
asyncRes2 = this.collection.modify("$.F3 > 100").unset("$.T").sort("$.F3 desc").executeAsync();
res2 = asyncRes2.get();
assertEquals((maxrec - 1), res2.getAffectedItemsCount());
asyncRes2 = this.collection.modify("$.F3 >= 100").change("$.T", expr("10000+1")).sort("$.F3 desc").executeAsync();
res2 = asyncRes2.get();
assertEquals((1), res2.getAffectedItemsCount());
asyncDocs = this.collection.find("$.T >= ? ").bind(10000).fields(expr("{'cnt':count($.T)}")).executeAsync();
docs = asyncDocs.get();
doc = docs.next();
assertEquals((long) (1), (long) (((JsonNumber) doc.get("cnt")).getInteger()));
asyncRes2 = this.collection.modify("$.F3 >= 100").unset("$.T").sort("$.F3 desc").executeAsync();
res2 = asyncRes.get();
assertEquals(1, res2.getAffectedItemsCount());
asyncRes2 = this.collection.modify("$.F3 >= 100").set("$.T", expr("10000+3")).sort("$.F3 desc").executeAsync();
res2 = asyncRes2.get();
assertEquals(maxrec, res2.getAffectedItemsCount());
asyncDocs = this.collection.find("$.T >= ? ").bind(10000).fields(expr("{'cnt':count($.T)}")).executeAsync();
docs = asyncDocs.get();
doc = docs.next();
assertEquals((long) maxrec, (long) (((JsonNumber) doc.get("cnt")).getInteger()));
CompletableFuture<?>[] futures = new CompletableFuture<?>[501];
// List <Object>futures = new ArrayList<Object>();
j = 0;
for (i = 0; i < 500; ++i, j++) {
if (j >= maxrec) {
j = 0;
}
futures[i] = this.collection.modify("$.F3 = " + (100 + j)).change("$.T", i).executeAsync();
}
for (i = 0; i < 500; ++i, j++) {
// res = ((CompletableFuture<Result>) futures.get(i)).get();
res2 = (Result) futures[i].get();
assertEquals(1, res2.getAffectedItemsCount());
}
futures[i] = this.collection.modify("true").change("$.T", -1).executeAsync();
// wait for them all to finish
CompletableFuture.allOf(futures).get();
asyncDocs = this.collection.find("$.T = ? ").bind(-1).fields(expr("{'cnt':count($.T)}")).executeAsync();
docs = asyncDocs.get();
doc = docs.next();
assertEquals((long) maxrec, (long) (((JsonNumber) doc.get("cnt")).getInteger()));
}
use of com.mysql.cj.xdevapi.AddResult in project aws-mysql-jdbc by awslabs.
the class CollectionModifyTest method testBug24471057.
/**
* Tests fix for BUG#24471057, UPDATE FAILS WHEN THE NEW VALUE IS OF TYPE DBDOC WHICH HAS ARRAY IN IT.
*
* @throws Exception
* if the test fails.
*/
@Test
public void testBug24471057() throws Exception {
String docStr = "{\"B\" : 2, \"ID\" : 1, \"KEY\" : [1]}";
if (!mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
// Inject an _id.
docStr = docStr.replace("{", "{\"_id\": \"1\", ");
}
DbDoc doc1 = JsonParser.parseDoc(new StringReader(docStr));
AddResult res = this.collection.add(doc1).execute();
this.collection.modify("ID=1").set("$.B", doc1).execute();
// expected doc
DbDoc doc2 = JsonParser.parseDoc(new StringReader(docStr));
doc2.put("B", doc1);
if (mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
doc2.put("_id", new JsonString().setValue(res.getGeneratedIds().get(0)));
}
DocResult docs = this.collection.find().execute();
DbDoc doc = docs.next();
assertEquals(doc2.toString(), doc.toString());
// DbDoc as an array member
DbDoc doc3 = JsonParser.parseDoc(new StringReader(docStr));
((JsonArray) doc1.get("KEY")).add(doc3);
this.collection.modify("ID=1").set("$.B", doc1).execute();
// expected doc
doc2.put("B", doc1);
docs = this.collection.find().execute();
doc = docs.next();
assertEquals(doc2.toString(), doc.toString());
}
use of com.mysql.cj.xdevapi.AddResult 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.AddResult 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.AddResult 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());
}
Aggregations