Search in sources :

Example 76 with JsonString

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;
        }
    }
}
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 77 with JsonString

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());
}
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 78 with JsonString

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());
}
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 79 with JsonString

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

Example 80 with JsonString

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");
    }
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) RowResult(com.mysql.cj.xdevapi.RowResult) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) Table(com.mysql.cj.xdevapi.Table) HashMap(java.util.HashMap) StringReader(java.io.StringReader) JsonString(com.mysql.cj.xdevapi.JsonString) JsonString(com.mysql.cj.xdevapi.JsonString) Row(com.mysql.cj.xdevapi.Row) Test(org.junit.jupiter.api.Test)

Aggregations

JsonString (com.mysql.cj.xdevapi.JsonString)80 Test (org.junit.jupiter.api.Test)79 DbDoc (com.mysql.cj.xdevapi.DbDoc)78 DocResult (com.mysql.cj.xdevapi.DocResult)74 DbDocImpl (com.mysql.cj.xdevapi.DbDocImpl)58 JsonNumber (com.mysql.cj.xdevapi.JsonNumber)53 AddResult (com.mysql.cj.xdevapi.AddResult)33 Result (com.mysql.cj.xdevapi.Result)21 JsonArray (com.mysql.cj.xdevapi.JsonArray)18 RowResult (com.mysql.cj.xdevapi.RowResult)15 Table (com.mysql.cj.xdevapi.Table)12 Row (com.mysql.cj.xdevapi.Row)11 SqlResult (com.mysql.cj.xdevapi.SqlResult)10 ExecutionException (java.util.concurrent.ExecutionException)10 BigDecimal (java.math.BigDecimal)9 Collection (com.mysql.cj.xdevapi.Collection)8 Session (com.mysql.cj.xdevapi.Session)8 SessionFactory (com.mysql.cj.xdevapi.SessionFactory)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6