Search in sources :

Example 1 with JsonNumber

use of com.mysql.cj.xdevapi.JsonNumber in project aws-mysql-jdbc by awslabs.

the class DevApiSample method documentWalkthrough.

public static void documentWalkthrough(Schema schema) {
    // document walthrough
    Collection coll = schema.createCollection("myBooks", /* reuseExisting? */
    true);
    DbDoc newDoc = new DbDocImpl().add("isbn", new JsonString().setValue("12345"));
    newDoc.add("title", new JsonString().setValue("Effi Briest"));
    newDoc.add("author", new JsonString().setValue("Theodor Fontane"));
    newDoc.add("currentlyReadingPage", new JsonNumber().setValue(String.valueOf(42)));
    coll.add(newDoc).execute();
    // note: "$" prefix for document paths is optional. "$.title.somethingElse[0]" is the same as "title.somethingElse[0]" in document expressions
    DocResult docs = coll.find("$.title = 'Effi Briest' and $.currentlyReadingPage > 10").execute();
    DbDoc book = docs.next();
    System.err.println("Currently reading " + ((JsonString) book.get("title")).getString() + " on page " + ((JsonNumber) book.get("currentlyReadingPage")).getInteger());
    // increment the page number and fetch it again
    coll.modify("$.isbn = 12345").set("$.currentlyReadingPage", ((JsonNumber) book.get("currentlyReadingPage")).getInteger() + 1).execute();
    docs = coll.find("$.title = 'Effi Briest' and $.currentlyReadingPage > 10").execute();
    book = docs.next();
    System.err.println("Currently reading " + ((JsonString) book.get("title")).getString() + " on page " + ((JsonNumber) book.get("currentlyReadingPage")).getInteger());
    // remove the doc
    coll.remove("true").execute();
    System.err.println("Number of books in collection: " + coll.count());
    schema.dropCollection(coll.getName());
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) JsonNumber(com.mysql.cj.xdevapi.JsonNumber) Collection(com.mysql.cj.xdevapi.Collection) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult)

Example 2 with JsonNumber

use of com.mysql.cj.xdevapi.JsonNumber in project aws-mysql-jdbc by awslabs.

the class RowLockingTest method testSelectRowLockingValid.

@Test
public void testSelectRowLockingValid() throws Exception {
    assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
    int i = 0;
    try {
        /* add(DbDoc[] docs) */
        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());
        this.CheckFlag = 0;
        initException = new Throwable[2];
        SelectRowLock[] Thrd = new SelectRowLock[2];
        /* Two threads with same conditions, select for update in one and update in second */
        Thrd[0] = new SelectRowLock(1, 1, 0, 5, "doc->$.F1 = :bVal");
        Thrd[0].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(0));
        Thrd[0].start();
        Thrd[1] = new SelectRowLock(3, 0, 1, 5, "doc->$.F1 = :bVal");
        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 SelectRowLock(1, 2, 0, 5, "doc->$.F1 = :bVal");
        Thrd[0].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(0));
        Thrd[0].start();
        Thrd[1] = new SelectRowLock(3, 0, 1, 5, "doc->$.F1 = :bVal");
        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 update in second */
        this.CheckFlag = 0;
        initException = new Throwable[2];
        Thrd[0] = new SelectRowLock(2, 1, 0, 5, "doc->$.F1 = :bVal");
        Thrd[0].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(0));
        Thrd[0].start();
        Thrd[1] = new SelectRowLock(3, 0, 1, 5, "doc->$.F1 = :bVal");
        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 SelectRowLock(2, 2, 0, 5, "doc->$.F1 = :bVal");
        Thrd[0].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(0));
        Thrd[0].start();
        Thrd[1] = new SelectRowLock(3, 0, 1, 5, "doc->$.F1 = :bVal");
        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 SelectRowLock(2, 2, 0, 5, "doc->$.F1 < :bVal");
        Thrd[0].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(0));
        Thrd[0].start();
        Thrd[1] = new SelectRowLock(3, 0, 1, 5, "doc->$.F1 < :bVal");
        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 share in one and update in second */
        this.CheckFlag = 0;
        initException = new Throwable[2];
        Thrd[0] = new SelectRowLock(2, 1, 0, 5, "doc->$.F1 < :bVal");
        Thrd[0].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(0));
        Thrd[0].start();
        Thrd[1] = new SelectRowLock(3, 0, 1, 5, "doc->$.F1 < :bVal");
        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 3 with JsonNumber

use of com.mysql.cj.xdevapi.JsonNumber in project aws-mysql-jdbc by awslabs.

the class CollectionAddTest method testCollectionAddBigKeyData.

@Test
public void testCollectionAddBigKeyData() throws Exception {
    int i = 0, j = 0;
    int maxkey = 10;
    int maxrec = 5;
    int keylen = (10);
    int datalen = (1 * 5);
    String key_sub = buildString(keylen, 'X');
    String data_sub = buildString(datalen, 'X');
    /* Insert maxrec records with maxkey (key,value) pairs with key length=keylen and datalength=datalen */
    String key, data, query;
    for (i = 0; i < maxrec; i++) {
        DbDoc newDoc = new DbDocImpl();
        newDoc.add("_id", new JsonNumber().setValue(String.valueOf(i)));
        for (j = 0; j < maxkey; j++) {
            key = key_sub + j;
            data = data_sub + j;
            newDoc.add(key, new JsonString().setValue(data));
        }
        this.collection.add(newDoc).execute();
        newDoc = null;
    }
    assertEquals((maxrec), this.collection.count());
    /* Fetch all keys */
    query = "$._id as _id";
    for (j = 0; j < maxkey; j++) {
        key = key_sub + j;
        query = query + ",$." + key + " as " + key;
    }
    DocResult docs = this.collection.find().orderBy("$._id").fields(query).execute();
    DbDoc doc = null;
    i = 0;
    while (docs.hasNext()) {
        doc = docs.next();
        for (j = 0; j < maxkey; j++) {
            key = key_sub + j;
            data = data_sub + j;
            assertEquals((data), ((JsonString) doc.get(key)).getString());
        }
        i++;
    }
    assertEquals((maxrec), i);
}
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) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 4 with JsonNumber

use of com.mysql.cj.xdevapi.JsonNumber in project aws-mysql-jdbc by awslabs.

the class CollectionAddTest method testCollectionAddBasic.

@Test
public void testCollectionAddBasic() throws Exception {
    assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
    int i = 0, maxrec = 100;
    /* add(DbDoc[] docs) */
    DbDoc[] jsonlist = new DbDocImpl[maxrec];
    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 JsonString().setValue("Field-2-Data-" + i));
        newDoc2.add("F3", new JsonNumber().setValue(String.valueOf(300 + i)));
        jsonlist[i] = newDoc2;
        newDoc2 = null;
    }
    this.collection.add(jsonlist).execute();
    /* add(DbDoc doc) */
    DbDoc newDoc = new DbDocImpl();
    newDoc.add("_id", new JsonString().setValue(String.valueOf(maxrec + 1000)));
    newDoc.add("F1", new JsonString().setValue("Field-1-Data-" + maxrec));
    newDoc.add("F2", new JsonString().setValue("Field-2-Data-" + maxrec));
    newDoc.add("F3", new JsonNumber().setValue(String.valueOf(300 + maxrec)));
    this.collection.add(newDoc).execute();
    /* add(String jsonString) */
    String json = "{'_id':'" + (maxrec + 1000 + 1) + "','F1':'Field-1-Data-" + (maxrec + 1) + "','F2':'Field-2-Data-" + (maxrec + 1) + "','F3':" + (300 + maxrec + 1) + "}";
    json = json.replaceAll("'", "\"");
    this.collection.add(json).execute();
    /* No _Id Field and chained add() */
    json = "{'F1': 'Field-1-Data-9999','F2': 'Field-2-Data-9999','F3': 'Field-3-Data-9999'}".replaceAll("'", "\"");
    this.collection.add(json).add(json.replaceAll("9", "8")).execute();
    assertEquals((maxrec + 4), this.collection.count());
    DocResult docs = this.collection.find("$._id = '1000'").fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3 as f3").execute();
    DbDoc doc = null;
    doc = docs.next();
    assertEquals("1000", ((JsonString) doc.get("_id")).getString());
    System.out.println("ID :" + ((JsonString) doc.get("_id")).getString());
    System.out.println("F1 :" + ((JsonString) doc.get("f1")).getString());
    System.out.println("F2 :" + ((JsonString) doc.get("f2")).getString());
    System.out.println("F3 :" + ((JsonNumber) doc.get("f3")).getInteger());
}
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) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 5 with JsonNumber

use of com.mysql.cj.xdevapi.JsonNumber in project aws-mysql-jdbc by awslabs.

the class CollectionFindTest method testCollectionFindSkipWarning.

@SuppressWarnings("deprecation")
@Test
public void testCollectionFindSkipWarning() throws Exception {
    int i = 0, maxrec = 10;
    DbDoc doc = null;
    DocResult docs = null;
    DbDoc[] jsonlist = new DbDocImpl[maxrec];
    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(10 * (i + 1))));
        newDoc2.add("F3", new JsonNumber().setValue(String.valueOf(i + 1)));
        newDoc2.add("F4", new JsonNumber().setValue(String.valueOf(10000 - i)));
        jsonlist[i] = newDoc2;
        newDoc2 = null;
    }
    this.collection.add(jsonlist).execute();
    assertEquals((maxrec), this.collection.count());
    /* find with order by and condition */
    docs = this.collection.find("$.F3 > 1").orderBy("CAST($.F4 as SIGNED)").execute();
    i = 0;
    while (docs.hasNext()) {
        doc = docs.next();
        i++;
        assertEquals(String.valueOf(1000 + maxrec - i), (((JsonString) doc.get("_id")).getString()));
        assertEquals((long) (10000 - maxrec + i), (long) (((JsonNumber) doc.get("F4")).getInteger()));
    }
    assertEquals(i, (maxrec - 1));
    /* find with order by and limit with condition */
    docs = this.collection.find("$._id > 1001").orderBy("CAST($.F4 as SIGNED)").limit(1).skip(2).execute();
    doc = docs.next();
    assertEquals(String.valueOf(1000 + maxrec - 3), (((JsonString) doc.get("_id")).getString()));
    assertFalse(docs.hasNext());
}
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) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Aggregations

JsonNumber (com.mysql.cj.xdevapi.JsonNumber)59 DbDoc (com.mysql.cj.xdevapi.DbDoc)56 DocResult (com.mysql.cj.xdevapi.DocResult)56 Test (org.junit.jupiter.api.Test)56 JsonString (com.mysql.cj.xdevapi.JsonString)53 DbDocImpl (com.mysql.cj.xdevapi.DbDocImpl)50 AddResult (com.mysql.cj.xdevapi.AddResult)21 Result (com.mysql.cj.xdevapi.Result)20 JsonArray (com.mysql.cj.xdevapi.JsonArray)14 RowResult (com.mysql.cj.xdevapi.RowResult)9 SqlResult (com.mysql.cj.xdevapi.SqlResult)7 Table (com.mysql.cj.xdevapi.Table)7 BigDecimal (java.math.BigDecimal)7 ExecutionException (java.util.concurrent.ExecutionException)7 Row (com.mysql.cj.xdevapi.Row)6 ArrayList (java.util.ArrayList)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 Collection (com.mysql.cj.xdevapi.Collection)3 HashMap (java.util.HashMap)3 WrongArgumentException (com.mysql.cj.exceptions.WrongArgumentException)2