Search in sources :

Example 21 with AddResult

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()));
}
Also used : AddResult(com.mysql.cj.xdevapi.AddResult) Result(com.mysql.cj.xdevapi.Result) AddResult(com.mysql.cj.xdevapi.AddResult) DocResult(com.mysql.cj.xdevapi.DocResult) DbDoc(com.mysql.cj.xdevapi.DbDoc) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) CompletableFuture(java.util.concurrent.CompletableFuture) JsonNumber(com.mysql.cj.xdevapi.JsonNumber) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 22 with AddResult

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());
}
Also used : JsonArray(com.mysql.cj.xdevapi.JsonArray) DbDoc(com.mysql.cj.xdevapi.DbDoc) StringReader(java.io.StringReader) AddResult(com.mysql.cj.xdevapi.AddResult) JsonString(com.mysql.cj.xdevapi.JsonString) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 23 with AddResult

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;
        }
    }
}
Also used : SessionFactory(com.mysql.cj.xdevapi.SessionFactory) ArrayList(java.util.ArrayList) AddResult(com.mysql.cj.xdevapi.AddResult) JsonString(com.mysql.cj.xdevapi.JsonString) ExecutionException(java.util.concurrent.ExecutionException) DbDoc(com.mysql.cj.xdevapi.DbDoc) CompletableFuture(java.util.concurrent.CompletableFuture) Collection(com.mysql.cj.xdevapi.Collection) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) Session(com.mysql.cj.xdevapi.Session) Test(org.junit.jupiter.api.Test)

Example 24 with AddResult

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());
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) AddResult(com.mysql.cj.xdevapi.AddResult) JsonString(com.mysql.cj.xdevapi.JsonString) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 25 with AddResult

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());
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) AddResult(com.mysql.cj.xdevapi.AddResult) JsonString(com.mysql.cj.xdevapi.JsonString) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Aggregations

AddResult (com.mysql.cj.xdevapi.AddResult)25 Test (org.junit.jupiter.api.Test)25 JsonString (com.mysql.cj.xdevapi.JsonString)23 DbDoc (com.mysql.cj.xdevapi.DbDoc)22 DocResult (com.mysql.cj.xdevapi.DocResult)22 DbDocImpl (com.mysql.cj.xdevapi.DbDocImpl)8 Collection (com.mysql.cj.xdevapi.Collection)7 JsonNumber (com.mysql.cj.xdevapi.JsonNumber)7 Session (com.mysql.cj.xdevapi.Session)7 Result (com.mysql.cj.xdevapi.Result)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 ArrayList (java.util.ArrayList)4 ExecutionException (java.util.concurrent.ExecutionException)4 Compression (com.mysql.cj.conf.PropertyDefinitions.Compression)2 RowResult (com.mysql.cj.xdevapi.RowResult)2 SqlResult (com.mysql.cj.xdevapi.SqlResult)2 BigDecimal (java.math.BigDecimal)2 HashMap (java.util.HashMap)2 JsonArray (com.mysql.cj.xdevapi.JsonArray)1 SessionFactory (com.mysql.cj.xdevapi.SessionFactory)1