Search in sources :

Example 11 with JsonString

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

the class CollectionFindTest method testCollectionFindWithIntervalOperation.

/* interval, In, NOT IN */
@Test
public void testCollectionFindWithIntervalOperation() throws Exception {
    int i = 0, maxrec = 10;
    int SLen = 1;
    DbDoc doc = 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 + 1 + 1000)));
        newDoc2.add("dt", new JsonString().setValue((2000 + i) + "-02-" + (i * 2 + 10)));
        newDoc2.add("dtime", new JsonString().setValue((2000 + i) + "-02-01 12:" + (i + 10) + ":01"));
        newDoc2.add("str", new JsonString().setValue(buildString(SLen + i, 'q')));
        newDoc2.add("ival", new JsonNumber().setValue(String.valueOf((int) Math.pow(2, (i + 1)))));
        if (maxrec - 1 == i) {
            newDoc2.add("ival", new JsonNumber().setValue(String.valueOf(-2147483648)));
        }
        jsonlist[i] = newDoc2;
        newDoc2 = null;
    }
    this.collection.add(jsonlist).execute();
    assertEquals((maxrec), this.collection.count());
    /* find With bitwise | Condition */
    DocResult docs = this.collection.find("CAST($.ival as SIGNED)>1 ").fields("$._id as _id, $.dt as f1, $.dtime as f2, $.str as f3 , $.ival  as f4,$.dt - interval 25 day as tmp").execute();
    i = 0;
    while (docs.hasNext()) {
        doc = docs.next();
        assertEquals(String.valueOf(i + 1 + 1000), (((JsonString) doc.get("_id")).getString()));
        assertEquals(((2000 + i) + "-02-" + (i * 2 + 10)), (((JsonString) doc.get("f1")).getString()));
        assertEquals((2000 + i) + "-02-01 12:" + (i + 10) + ":01", (((JsonString) doc.get("f2")).getString()));
        assertEquals(buildString(SLen + i, 'q'), (((JsonString) doc.get("f3")).getString()));
        assertEquals((long) ((int) Math.pow(2, (i + 1))), (long) (((JsonNumber) doc.get("f4")).getInteger()));
        i++;
    }
    assertEquals((maxrec - 1), i);
    /* find With bitwise interval Condition */
    docs = this.collection.find("$.dt + interval 6 day = '2007-03-02' ").fields("$._id as _id, $.dt as f1 ").execute();
    doc = docs.next();
    assertEquals("2007-02-24", (((JsonString) doc.get("f1")).getString()));
    assertFalse(docs.hasNext());
    docs = this.collection.find("$.dt + interval 1 month = '2006-03-22' ").fields("$._id as _id, $.dt as f1 ").execute();
    doc = docs.next();
    assertEquals("2006-02-22", (((JsonString) doc.get("f1")).getString()));
    assertFalse(docs.hasNext());
    docs = this.collection.find("$.dt + interval 1 year = '2010-02-28' ").fields("$._id as _id, $.dt as f1 ").execute();
    doc = docs.next();
    assertEquals("2009-02-28", (((JsonString) doc.get("f1")).getString()));
    assertFalse(docs.hasNext());
    docs = this.collection.find("$.dt - interval 1 year = '2008-02-28' ").fields("$._id as _id, $.dt as f1 ").execute();
    doc = docs.next();
    assertEquals("2009-02-28", (((JsonString) doc.get("f1")).getString()));
    assertFalse(docs.hasNext());
    docs = this.collection.find("$.dt - interval 25 day = '2007-01-30' ").fields("$._id as _id, $.dt as f1 ").execute();
    doc = docs.next();
    assertEquals("2007-02-24", (((JsonString) doc.get("f1")).getString()));
    assertFalse(docs.hasNext());
    /* Between */
    docs = this.collection.find("CAST($.ival as SIGNED) between 65 and 128 ").fields("$._id as _id, $.ival as f1 ").execute();
    doc = docs.next();
    assertEquals((long) 128, (long) (((JsonNumber) doc.get("f1")).getInteger()));
    assertFalse(docs.hasNext());
    docs = this.collection.find("$.dt between '2006-01-28' and '2007-02-01' ").fields("$._id as _id, $.dt as f1 ").execute();
    doc = docs.next();
    assertEquals("2006-02-22", (((JsonString) doc.get("f1")).getString()));
    assertFalse(docs.hasNext());
    docs = this.collection.find("CAST($.ival as SIGNED) <0 ").fields("$._id as _id, $.ival as f1 ").execute();
    doc = docs.next();
    assertEquals((long) -2147483648, (long) (((JsonNumber) doc.get("f1")).getInteger()));
    assertFalse(docs.hasNext());
    docs = this.collection.find("(CAST($.ival as SIGNED) between 9 and 31) or (CAST($.ival as SIGNED) between 65 and 128)").fields("$._id as _id, $.ival as f1 ").orderBy("CAST($.ival as SIGNED) asc").execute();
    doc = docs.next();
    assertEquals((long) 16, (long) (((JsonNumber) doc.get("f1")).getInteger()));
    doc = docs.next();
    assertEquals((long) 128, (long) (((JsonNumber) doc.get("f1")).getInteger()));
    assertFalse(docs.hasNext());
    docs = this.collection.find("(CAST($.ival as SIGNED) between 9 and 31) and (CAST($.ival as SIGNED) between 65 and 128)").fields("$._id as _id, $.ival as f1 ").orderBy("CAST($.ival as SIGNED)").execute();
    assertFalse(docs.hasNext());
    docs = this.collection.find("CAST($.ival as SIGNED) in (20,NULL,31.5,'17',16,CAST($.ival as SIGNED)+1) ").fields("$._id as _id, $.ival as f1 ").execute();
    doc = docs.next();
    assertEquals((long) 16, (long) (((JsonNumber) doc.get("f1")).getInteger()));
    assertFalse(docs.hasNext());
    docs = this.collection.find("(CAST($.ival as SIGNED) in (16,32,4,256,512)) and ($.dt - interval 25 day = '2007-01-30' ) and  ($.ival not in(2,4))").fields("$._id as _id, $.ival as f1 ").execute();
    doc = docs.next();
    assertEquals((long) 256, (long) (((JsonNumber) doc.get("f1")).getInteger()));
    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)

Example 12 with JsonString

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

the class CollectionFindTest method testCollectionRowLockOptions.

@Test
public void testCollectionRowLockOptions() throws Exception {
    assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5")), "MySQL 8.0.5+ is required to run this test.");
    Function<DocResult, List<String>> asStringList = rr -> rr.fetchAll().stream().map(d -> ((JsonString) d.get("_id")).getString()).collect(Collectors.toList());
    this.collection.add("{\"_id\":\"1\", \"a\":1}").add("{\"_id\":\"2\", \"a\":1}").add("{\"_id\":\"3\", \"a\":1}").execute();
    Session session1 = null;
    Session session2 = null;
    try {
        session1 = new SessionFactory().getSession(this.testProperties);
        Collection col1 = session1.getDefaultSchema().getCollection(this.collectionName);
        session2 = new SessionFactory().getSession(this.testProperties);
        Collection col2 = session2.getDefaultSchema().getCollection(this.collectionName);
        DocResult res;
        CompletableFuture<DocResult> futRes;
        /*
             * 1. Shared Lock in both sessions.
             */
        // session2.lockShared() returns data immediately.
        session1.startTransaction();
        col1.find("_id = '1'").lockShared().execute();
        session2.startTransaction();
        res = col2.find("_id < '3'").lockShared().execute();
        assertEquals(2, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), hasItems("1", "2"));
        session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockShared().executeAsync();
        res = futRes.get(3, TimeUnit.SECONDS);
        assertTrue(futRes.isDone());
        assertEquals(2, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("1", "2"));
        session2.rollback();
        session1.rollback();
        // session2.lockShared(NOWAIT) returns data immediately.
        session1.startTransaction();
        col1.find("_id = '1'").lockShared().execute();
        session2.startTransaction();
        res = col2.find("_id < '3'").lockShared(Statement.LockContention.NOWAIT).execute();
        assertEquals(2, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("1", "2"));
        session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockShared(Statement.LockContention.NOWAIT).executeAsync();
        res = futRes.get(3, TimeUnit.SECONDS);
        assertTrue(futRes.isDone());
        assertEquals(2, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("1", "2"));
        session2.rollback();
        session1.rollback();
        // session2.lockShared(SKIP_LOCK) returns data immediately.
        session1.startTransaction();
        col1.find("_id = '1'").lockShared().execute();
        session2.startTransaction();
        res = col2.find("_id < '3'").lockShared(Statement.LockContention.SKIP_LOCKED).execute();
        assertEquals(2, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("1", "2"));
        session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockShared(Statement.LockContention.SKIP_LOCKED).executeAsync();
        res = futRes.get(3, TimeUnit.SECONDS);
        assertTrue(futRes.isDone());
        assertEquals(2, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("1", "2"));
        session2.rollback();
        session1.rollback();
        /*
             * 2. Shared Lock in first session and exclusive lock in second.
             */
        // session2.lockExclusive() blocks until session1 ends.
        session1.startTransaction();
        col1.find("_id = '1'").lockShared().execute();
        // session2.startTransaction();
        // res = col2.find("_id < '3'").lockExclusive().execute(); (Can't test)
        // session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockExclusive().executeAsync();
        final CompletableFuture<DocResult> fr1 = futRes;
        assertThrows(TimeoutException.class, () -> fr1.get(3, TimeUnit.SECONDS));
        // Unlocks session2.
        session1.rollback();
        res = futRes.get(3, TimeUnit.SECONDS);
        assertTrue(futRes.isDone());
        assertEquals(2, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("1", "2"));
        session2.rollback();
        // session2.lockExclusive(NOWAIT) should return locking error.
        session1.startTransaction();
        col1.find("_id = '1'").lockShared().execute();
        session2.startTransaction();
        assertThrows(XProtocolError.class, "ERROR 3572 \\(HY000\\) Statement aborted because lock\\(s\\) could not be acquired immediately and NOWAIT is set\\.", () -> col2.find("_id < '3'").lockExclusive(Statement.LockContention.NOWAIT).execute());
        session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockExclusive(Statement.LockContention.NOWAIT).executeAsync();
        final CompletableFuture<DocResult> fr2 = futRes;
        assertThrows(ExecutionException.class, ".*XProtocolError: ERROR 3572 \\(HY000\\) Statement aborted because lock\\(s\\) could not be acquired immediately and NOWAIT is set\\.", () -> fr2.get(3, TimeUnit.SECONDS));
        session2.rollback();
        session1.rollback();
        // session2.lockExclusive(SKIP_LOCK) should return (unlocked) data immediately.
        session1.startTransaction();
        col1.find("_id = '1'").lockShared().execute();
        session2.startTransaction();
        res = col2.find("_id < '3'").lockExclusive(Statement.LockContention.SKIP_LOCKED).execute();
        assertEquals(1, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("2"));
        session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockExclusive(Statement.LockContention.SKIP_LOCKED).executeAsync();
        res = futRes.get(3, TimeUnit.SECONDS);
        assertTrue(futRes.isDone());
        assertEquals(1, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("2"));
        session2.rollback();
        session1.rollback();
        /*
             * 3. Exclusive Lock in first session and shared lock in second.
             */
        // session2.lockShared() blocks until session1 ends.
        session1.startTransaction();
        col1.find("_id = '1'").lockExclusive().execute();
        // session2.startTransaction();
        // res = col2.find("_id < '3'").lockShared().execute(); (Can't test)
        // session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockShared().executeAsync();
        final CompletableFuture<DocResult> fr3 = futRes;
        assertThrows(TimeoutException.class, () -> fr3.get(3, TimeUnit.SECONDS));
        // Unlocks session2.
        session1.rollback();
        res = futRes.get(3, TimeUnit.SECONDS);
        assertTrue(futRes.isDone());
        assertEquals(2, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("1", "2"));
        session2.rollback();
        // session2.lockShared(NOWAIT) should return locking error.
        session1.startTransaction();
        col1.find("_id = '1'").lockExclusive().execute();
        session2.startTransaction();
        assertThrows(XProtocolError.class, "ERROR 3572 \\(HY000\\) Statement aborted because lock\\(s\\) could not be acquired immediately and NOWAIT is set\\.", () -> col2.find("_id < '3'").lockShared(Statement.LockContention.NOWAIT).execute());
        session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockShared(Statement.LockContention.NOWAIT).executeAsync();
        final CompletableFuture<DocResult> fr4 = futRes;
        assertThrows(ExecutionException.class, ".*XProtocolError: ERROR 3572 \\(HY000\\) Statement aborted because lock\\(s\\) could not be acquired immediately and NOWAIT is set\\.", () -> fr4.get(3, TimeUnit.SECONDS));
        session2.rollback();
        session1.rollback();
        // session2.lockShared(SKIP_LOCK) should return (unlocked) data immediately.
        session1.startTransaction();
        col1.find("_id = '1'").lockExclusive().execute();
        session2.startTransaction();
        res = col2.find("_id < '3'").lockShared(Statement.LockContention.SKIP_LOCKED).execute();
        assertEquals(1, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("2"));
        session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockShared(Statement.LockContention.SKIP_LOCKED).executeAsync();
        res = futRes.get(3, TimeUnit.SECONDS);
        assertTrue(futRes.isDone());
        assertEquals(1, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("2"));
        session2.rollback();
        session1.rollback();
        /*
             * 4. Exclusive Lock in both sessions.
             */
        // session2.lockExclusive() blocks until session1 ends.
        session1.startTransaction();
        col1.find("_id = '1'").lockExclusive().execute();
        // session2.startTransaction();
        // res = col2.find("_id < '3'").lockExclusive().execute(); (Can't test)
        // session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockExclusive().executeAsync();
        final CompletableFuture<DocResult> fr5 = futRes;
        assertThrows(TimeoutException.class, () -> fr5.get(3, TimeUnit.SECONDS));
        // Unlocks session2.
        session1.rollback();
        res = futRes.get(3, TimeUnit.SECONDS);
        assertTrue(futRes.isDone());
        assertEquals(2, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("1", "2"));
        session2.rollback();
        // session2.lockExclusive(NOWAIT) should return locking error.
        session1.startTransaction();
        col1.find("_id = '1'").lockExclusive().execute();
        session2.startTransaction();
        assertThrows(XProtocolError.class, "ERROR 3572 \\(HY000\\) Statement aborted because lock\\(s\\) could not be acquired immediately and NOWAIT is set\\.", () -> col2.find("_id < '3'").lockExclusive(Statement.LockContention.NOWAIT).execute());
        session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockExclusive(Statement.LockContention.NOWAIT).executeAsync();
        final CompletableFuture<DocResult> fr6 = futRes;
        assertThrows(ExecutionException.class, ".*XProtocolError: ERROR 3572 \\(HY000\\) Statement aborted because lock\\(s\\) could not be acquired immediately and NOWAIT is set\\.", () -> fr6.get(3, TimeUnit.SECONDS));
        session2.rollback();
        session1.rollback();
        // session2.lockExclusive(SKIP_LOCK) should return (unlocked) data immediately.
        session1.startTransaction();
        col1.find("_id = '1'").lockExclusive().execute();
        session2.startTransaction();
        res = col2.find("_id < '3'").lockExclusive(Statement.LockContention.SKIP_LOCKED).execute();
        assertEquals(1, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("2"));
        session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockExclusive(Statement.LockContention.SKIP_LOCKED).executeAsync();
        res = futRes.get(3, TimeUnit.SECONDS);
        assertTrue(futRes.isDone());
        assertEquals(1, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("2"));
        session2.rollback();
        session1.rollback();
    } finally {
        if (session1 != null) {
            session1.close();
        }
        if (session2 != null) {
            session2.close();
        }
    }
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) RowResult(com.mysql.cj.xdevapi.RowResult) JsonNumber(com.mysql.cj.xdevapi.JsonNumber) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) TimeoutException(java.util.concurrent.TimeoutException) Collection(com.mysql.cj.xdevapi.Collection) Statement(com.mysql.cj.xdevapi.Statement) BigDecimal(java.math.BigDecimal) Expression.expr(com.mysql.cj.xdevapi.Expression.expr) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) FindStatementImpl(com.mysql.cj.xdevapi.FindStatementImpl) Map(java.util.Map) Table(com.mysql.cj.xdevapi.Table) ServerVersion(com.mysql.cj.ServerVersion) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) Warning(com.mysql.cj.xdevapi.Warning) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) FindStatement(com.mysql.cj.xdevapi.FindStatement) Assertions.fail(org.junit.jupiter.api.Assertions.fail) MysqlErrorNumbers(com.mysql.cj.exceptions.MysqlErrorNumbers) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Result(com.mysql.cj.xdevapi.Result) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) JsonArray(com.mysql.cj.xdevapi.JsonArray) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) JsonString(com.mysql.cj.xdevapi.JsonString) Function(java.util.function.Function) ArrayList(java.util.ArrayList) AddResult(com.mysql.cj.xdevapi.AddResult) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SessionImpl(com.mysql.cj.xdevapi.SessionImpl) Session(com.mysql.cj.xdevapi.Session) Iterator(java.util.Iterator) CoreMatchers.hasItems(org.hamcrest.CoreMatchers.hasItems) DbDoc(com.mysql.cj.xdevapi.DbDoc) DocResult(com.mysql.cj.xdevapi.DocResult) XProtocolError(com.mysql.cj.protocol.x.XProtocolError) SessionFactory(com.mysql.cj.xdevapi.SessionFactory) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Row(com.mysql.cj.xdevapi.Row) SqlResult(com.mysql.cj.xdevapi.SqlResult) JsonLiteral(com.mysql.cj.xdevapi.JsonLiteral) SessionFactory(com.mysql.cj.xdevapi.SessionFactory) Collection(com.mysql.cj.xdevapi.Collection) List(java.util.List) ArrayList(java.util.ArrayList) 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 13 with JsonString

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

the class CollectionFindTest method testCollectionFindInDelete.

@SuppressWarnings("deprecation")
@Test
public void testCollectionFindInDelete() throws Exception {
    assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
    int i = 0, maxrec = 10;
    DocResult docs = null;
    Result res = null;
    /* 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 JsonNumber().setValue(String.valueOf(10 * (i + 1) + 0.1234)));
        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 without Condition */
    docs = this.collection.find().fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3 as f3,$.F2/10 as tmp1,1/2 as tmp2").orderBy("$.F3").execute();
    i = 0;
    while (docs.hasNext()) {
        docs.next();
        i++;
    }
    assertEquals((maxrec), i);
    /* remove with single IN */
    res = this.collection.remove("'1001' in $._id").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("'1001' in $._id").execute();
    assertFalse(docs.hasNext());
    /* remove with mulltiple IN */
    String findCond = "";
    for (i = 1; i < maxrec; i++) {
        findCond = findCond + "'";
        findCond = findCond + String.valueOf(i + 1000) + "' not in $._id";
        if (i != maxrec - 1) {
            findCond = findCond + " and ";
        }
    }
    res = this.collection.remove(findCond).execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find(findCond).execute();
    assertFalse(docs.hasNext());
    /* remove with single IN */
    res = this.collection.remove("10004 in $.F4").orderBy("CAST($.F4 as SIGNED)").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("10004 in $.F4").orderBy("CAST($.F4 as SIGNED)").execute();
    assertFalse(docs.hasNext());
    /* remove with single IN for float */
    res = this.collection.remove("30.1234 in $.F2").orderBy("CAST($.F4 as SIGNED)").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("30.1234 in $.F2").orderBy("CAST($.F4 as SIGNED)").execute();
    assertFalse(docs.hasNext());
    res = this.collection.remove("true").execute();
    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) + 0.1234)));
        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());
    Table table = this.schema.getCollectionAsTable(this.collectionName);
    /* delete with single IN */
    res = table.delete().where("'1001' in doc->$._id").execute();
    assertEquals(res.getAffectedItemsCount(), 1);
    RowResult rows = table.select("doc->$.F1 as _id").where("'1001' in doc->$._id").execute();
    assertFalse(rows.hasNext());
    /* delete with multiple IN */
    findCond = "";
    for (i = 1; i < maxrec; i++) {
        findCond = findCond + "'";
        findCond = findCond + String.valueOf(i + 1000) + "' not in doc->$._id";
        if (i != maxrec - 1) {
            findCond = findCond + " and ";
        }
    }
    res = table.delete().where(findCond).execute();
    assertEquals(res.getAffectedItemsCount(), 1);
    rows = table.select("doc->$.F1 as _id").where(findCond).execute();
    assertFalse(rows.hasNext());
    /* delete with single IN for float */
    res = table.delete().where("30.1234 in doc->$.F2").execute();
    assertEquals(res.getAffectedItemsCount(), 1);
    rows = table.select("doc->$.F1 as F1").where("30.1234 in doc->$.F2").execute();
    assertFalse(rows.hasNext());
    /* delete with single IN for int */
    res = table.delete().where("10004 in doc->$.F4").execute();
    assertEquals(res.getAffectedItemsCount(), 1);
    rows = table.select("doc->$.F1 as F1").where("10004 in doc->$.F4").execute();
    assertFalse(rows.hasNext());
}
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) JsonNumber(com.mysql.cj.xdevapi.JsonNumber) JsonString(com.mysql.cj.xdevapi.JsonString) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) RowResult(com.mysql.cj.xdevapi.RowResult) Result(com.mysql.cj.xdevapi.Result) AddResult(com.mysql.cj.xdevapi.AddResult) DocResult(com.mysql.cj.xdevapi.DocResult) SqlResult(com.mysql.cj.xdevapi.SqlResult) Test(org.junit.jupiter.api.Test)

Example 14 with JsonString

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

the class CollectionFindTest method testCollectionFindStress_004.

/*
     * Many Keys
     * Issue : Hangs when maxKey > 7K
     */
@Test
public void testCollectionFindStress_004() throws Exception {
    int i = 0, j = 0, maxrec = 5;
    int maxKey = 1024 * 8;
    String key, key_sub = "key_", query;
    String data, data_sub = "data_";
    DocResult docs = null;
    DbDoc doc = null;
    String json = "";
    for (i = 0; i < maxrec; i++) {
        StringBuilder b = new StringBuilder("{'_id':'" + (1000 + i) + "'");
        for (j = 0; j < maxKey; j++) {
            key = key_sub + j;
            data = data_sub + i + "_" + j;
            b.append(",'").append(key).append("':'").append(data).append("'");
        }
        json = b.append("}").toString();
        json = json.replaceAll("'", "\"");
        this.collection.add(json).execute();
        b = null;
    }
    /* Inserting maxKey (key,data) pair */
    for (i = 0; i < maxrec; i++) {
        DbDoc newDoc2 = new DbDocImpl();
        newDoc2.add("_id", new JsonString().setValue(String.valueOf(i + 1 + 2000)));
        for (j = 0; j < maxKey; j++) {
            key = key_sub + j;
            data = data_sub + i + "_" + j;
            newDoc2.add(key, new JsonString().setValue(data));
        }
        this.collection.add(newDoc2).execute();
        newDoc2 = null;
    }
    assertEquals((maxrec * 2), this.collection.count());
    /* Select All Keys */
    query = "$._id as _id";
    for (j = 0; j < maxKey; j++) {
        key = key_sub + j;
        query = query + ",$." + key + " as " + key;
    }
    docs = this.collection.find("$._id= '1001'").fields(query).orderBy(key_sub + (maxKey - 1)).execute();
    doc = docs.next();
    assertEquals(String.valueOf(1001), (((JsonString) doc.get("_id")).getString()));
    assertEquals(data_sub + "1_" + (maxKey - 1), (((JsonString) doc.get(key_sub + (maxKey - 1))).getString()));
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) 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 15 with JsonString

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

the class CollectionFindTest method testCollectionFindInValidMix.

@Test
public void testCollectionFindInValidMix() throws Exception {
    assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
    int i = 0, j = 0, maxrec = 8, minArraySize = 3;
    DbDoc doc = null;
    DocResult docs = null;
    for (i = 0; i < maxrec; i++) {
        DbDoc newDoc2 = new DbDocImpl();
        newDoc2.add("_id", new JsonString().setValue(String.valueOf(i + 1000)));
        newDoc2.add("F1", new JsonNumber().setValue(String.valueOf(i + 1)));
        JsonArray jarray = new JsonArray();
        for (j = 0; j < (minArraySize + i); j++) {
            jarray.addValue(new JsonString().setValue("Field-1-Data-" + i));
        }
        newDoc2.add("ARR1", jarray);
        this.collection.add(newDoc2).execute();
        newDoc2 = null;
        jarray = null;
    }
    assertEquals((maxrec), this.collection.count());
    /* 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 + 1100)));
        newDoc2.add("ARR1", new JsonString().setValue("Field-1-Data-" + i));
        newDoc2.add("F2", new JsonString().setValue("10-15-201" + i));
        jsonlist[i] = newDoc2;
        newDoc2 = null;
    }
    this.collection.add(jsonlist).execute();
    /* find with IN for key having mixture of value and array */
    docs = this.collection.find("\"10-15-2017\" in $.F2").execute();
    doc = docs.next();
    assertEquals("1107", (((JsonString) doc.get("_id")).getString()));
    assertFalse(docs.hasNext());
    /* find with NULL IN in key having mix of array and string */
    docs = this.collection.find("NULL in $.ARR1").execute();
    assertFalse(docs.hasNext());
}
Also used : JsonArray(com.mysql.cj.xdevapi.JsonArray) 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

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