Search in sources :

Example 56 with DbDocImpl

use of com.mysql.cj.xdevapi.DbDocImpl 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 57 with DbDocImpl

use of com.mysql.cj.xdevapi.DbDocImpl 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

DbDocImpl (com.mysql.cj.xdevapi.DbDocImpl)57 JsonString (com.mysql.cj.xdevapi.JsonString)57 DbDoc (com.mysql.cj.xdevapi.DbDoc)56 Test (org.junit.jupiter.api.Test)56 DocResult (com.mysql.cj.xdevapi.DocResult)52 JsonNumber (com.mysql.cj.xdevapi.JsonNumber)50 AddResult (com.mysql.cj.xdevapi.AddResult)21 Result (com.mysql.cj.xdevapi.Result)19 JsonArray (com.mysql.cj.xdevapi.JsonArray)15 RowResult (com.mysql.cj.xdevapi.RowResult)11 Row (com.mysql.cj.xdevapi.Row)9 SqlResult (com.mysql.cj.xdevapi.SqlResult)8 Table (com.mysql.cj.xdevapi.Table)8 BigDecimal (java.math.BigDecimal)7 ExecutionException (java.util.concurrent.ExecutionException)7 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 Collection (com.mysql.cj.xdevapi.Collection)2 Session (com.mysql.cj.xdevapi.Session)2