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