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());
}
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();
}
}
}
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());
}
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()));
}
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());
}
Aggregations