Search in sources :

Example 1 with AddResult

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

the class CollectionAddTest method testBasicAddMap.

@Test
@Disabled("Collection.add(Map<String, ?> doc) is not implemented yet.")
public void testBasicAddMap() {
    Map<String, Object> doc = new HashMap<>();
    doc.put("x", 1);
    doc.put("y", "this is y");
    doc.put("z", new BigDecimal("44.22"));
    AddResult res = this.collection.add(doc).execute();
    assertTrue(res.getGeneratedIds().get(0).matches("[a-f0-9]{28}"));
    DocResult docs = this.collection.find("z >= 44.22").execute();
    DbDoc d = docs.next();
    JsonString val = (JsonString) d.get("y");
    assertEquals("this is y", val.getString());
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) HashMap(java.util.HashMap) AddResult(com.mysql.cj.xdevapi.AddResult) JsonString(com.mysql.cj.xdevapi.JsonString) JsonString(com.mysql.cj.xdevapi.JsonString) BigDecimal(java.math.BigDecimal) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 2 with AddResult

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

the class CollectionAddTest method testGetGeneratedIds.

@Test
public void testGetGeneratedIds() throws Exception {
    assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
    AddResult res = null;
    DbDoc doc = null;
    DocResult docs = null;
    int i = 0;
    // One record using String
    String json = "{\"FLD1\":\"Data1\"}";
    res = this.collection.add(json).execute();
    List<String> docIds = res.getGeneratedIds();
    assertTrue(docIds.get(0).matches("[a-f0-9]{28}"));
    assertEquals(1, this.collection.count());
    assertEquals(1, docIds.size());
    // More than One record using String
    json = "{\"FLD1\":\"Data2\"}";
    res = this.collection.add(json).add("{}").add("{\"_id\":\"id1\"}").add("{\"FLD1\":\"Data3\"}").execute();
    docIds = res.getGeneratedIds();
    assertEquals(5, this.collection.count());
    assertEquals(3, docIds.size());
    // More than One record using String, and single add()
    json = "{\"FLD1\":\"Data15\"}";
    res = this.collection.add(json, "{}", "{\"_id\":\"id2\"}", "{\"FLD1\":\"Data16\"}").execute();
    docIds = res.getGeneratedIds();
    assertEquals(9, this.collection.count());
    assertEquals(3, docIds.size());
    // One record using DbDoc
    DbDoc newDoc2 = new DbDocImpl();
    newDoc2.add("FLD1", new JsonString().setValue("Data4"));
    res = this.collection.add(newDoc2).execute();
    docIds = res.getGeneratedIds();
    assertEquals(10, this.collection.count());
    assertEquals(1, docIds.size());
    assertTrue(docIds.get(0).matches("[a-f0-9]{28}"));
    // More Than One record using DbDoc
    newDoc2.clear();
    newDoc2.add("FLD1", new JsonString().setValue("Data5"));
    DbDoc newDoc3 = new DbDocImpl();
    newDoc3.add("FLD1", new JsonString().setValue("Data6"));
    res = this.collection.add(newDoc2).add(newDoc3).execute();
    docIds = res.getGeneratedIds();
    assertEquals(12, this.collection.count());
    assertEquals(2, docIds.size());
    assertTrue(docIds.get(0).compareTo(docIds.get(1)) < 0);
    // One record using DbDoc[]
    DbDoc[] jsonlist1 = new DbDocImpl[1];
    newDoc2.clear();
    newDoc2.add("FLD1", new JsonString().setValue("Data7"));
    jsonlist1[0] = newDoc2;
    res = this.collection.add(jsonlist1).execute();
    docIds = res.getGeneratedIds();
    assertEquals(13, this.collection.count());
    assertEquals(1, docIds.size());
    assertTrue(docIds.get(0).matches("[a-f0-9]{28}"));
    // More Than One record using DbDoc[]
    DbDoc[] jsonlist = new DbDocImpl[5];
    for (i = 0; i < 5; i++) {
        DbDoc newDoc = new DbDocImpl();
        newDoc.add("FLD1", new JsonString().setValue("Data" + (i + 8)));
        if (i % 2 == 0) {
            newDoc.add("_id", new JsonString().setValue("id-" + (i + 8)));
        }
        jsonlist[i] = newDoc;
        newDoc = null;
    }
    res = this.collection.add(jsonlist).execute();
    docIds = res.getGeneratedIds();
    assertEquals(18, this.collection.count());
    assertEquals(2, docIds.size());
    json = "{}";
    res = this.collection.add(json).execute();
    docIds = res.getGeneratedIds();
    assertTrue(docIds.get(0).matches("[a-f0-9]{28}"));
    assertEquals(19, this.collection.count());
    assertEquals(1, docIds.size());
    // Verify that when _id is provided by client, getGeneratedIds() will return empty
    res = this.collection.add("{\"_id\":\"00001273834abcdfe\",\"FLD1\":\"Data1\",\"name\":\"name1\"}", "{\"_id\":\"000012738uyie98rjdeje\",\"FLD2\":\"Data2\",\"name\":\"name1\"}", "{\"_id\":\"00001273y834uhf489fe\",\"FLD3\":\"Data3\",\"name\":\"name1\"}").execute();
    docIds = res.getGeneratedIds();
    assertEquals(22, this.collection.count());
    assertEquals(0, docIds.size());
    res = this.collection.add("{\"_id\":null,\"FLD1\":\"nulldata\"}").execute();
    docIds = res.getGeneratedIds();
    assertEquals(23, this.collection.count());
    assertEquals(0, docIds.size());
    docs = this.collection.find("$.FLD1 == 'nulldata'").execute();
    doc = docs.next();
    assertEquals("null", ((JsonLiteral) doc.get("_id")).toString());
    // Try inserting duplicate _ids. User should get error
    assertThrows(XProtocolError.class, "ERROR 5116 \\(HY000\\) Document contains a field value that is not unique but required to be", () -> this.collection.add("{\"_id\":\"abcd1234\",\"FLD1\":\"Data1\"}").add("{\"_id\":\"abcd1234\",\"FLD1\":\"Data2\"}").execute());
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) AddResult(com.mysql.cj.xdevapi.AddResult) 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 3 with AddResult

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

the class CollectionAddTest method testBasicAddDoc.

@Test
public void testBasicAddDoc() {
    DbDoc doc = this.collection.newDoc().add("firstName", new JsonString().setValue("Georgia"));
    doc.add("middleName", new JsonString().setValue("Totto"));
    doc.add("lastName", new JsonString().setValue("O'Keeffe"));
    if (!mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
        // Inject an _id.
        doc.add("_id", new JsonString().setValue("1"));
    }
    AddResult res = this.collection.add(doc).execute();
    if (mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
        assertTrue(res.getGeneratedIds().get(0).matches("[a-f0-9]{28}"));
    } else {
        assertEquals(0, res.getGeneratedIds().size());
    }
    DocResult docs = this.collection.find("lastName like 'O\\'Kee%'").execute();
    DbDoc d = docs.next();
    JsonString val = (JsonString) d.get("lastName");
    assertEquals("O'Keeffe", val.getString());
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) AddResult(com.mysql.cj.xdevapi.AddResult) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 4 with AddResult

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

the class CollectionModifyTest method testCollectionModifyAsyncMany.

@SuppressWarnings("unchecked")
@Test
public void testCollectionModifyAsyncMany() throws Exception {
    int i = 0, maxrec = 10;
    int NUMBER_OF_QUERIES = 1000;
    DbDoc doc = null;
    AddResult res = null;
    Result res2 = null;
    CompletableFuture<AddResult> asyncRes = null;
    CompletableFuture<DocResult> asyncDocs = null;
    DocResult docs = null;
    double d = 100.123;
    /* add().executeAsync() maxrec num of records */
    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(d + i)));
        newDoc2.add("F3", new JsonNumber().setValue(String.valueOf(1000 + i)));
        newDoc2.add("T", new JsonNumber().setValue(String.valueOf(i)));
        asyncRes = this.collection.add(newDoc2).executeAsync();
        res = asyncRes.get();
        assertEquals(1, res.getAffectedItemsCount());
        newDoc2 = null;
    }
    assertEquals((maxrec), this.collection.count());
    List<Object> futures = new ArrayList<>();
    for (i = 0; i < NUMBER_OF_QUERIES; ++i) {
        if (i % 3 == 0) {
            futures.add(this.collection.modify("$.F3 % 2 = 0 ").change("$.T", expr("1000000+" + i)).sort("$.F3 desc").executeAsync());
        } else if (i % 3 == 1) {
            // Error
            futures.add(this.collection.modify("$.F3 = " + (1000 + i)).change("$.T", expr("NON_EXISTING_FUNCTION()")).sort("$.F3 desc").executeAsync());
        } else {
            futures.add(this.collection.modify("$.F3 % 2 = 1 ").change("$.T", expr("$.F3+" + i)).sort("$.F3 desc").executeAsync());
        }
    }
    for (i = 0; i < NUMBER_OF_QUERIES; ++i) {
        if (i % 3 == 0) {
            res2 = ((CompletableFuture<AddResult>) futures.get(i)).get();
            assertEquals((maxrec) / 2, res2.getAffectedItemsCount());
        } else if (i % 3 == 1) {
            int i1 = i;
            assertThrows(ExecutionException.class, ".*FUNCTION " + this.schema.getName() + ".NON_EXISTING_FUNCTION does not exist.*", () -> ((CompletableFuture<Result>) futures.get(i1)).get());
        } else {
            res2 = ((CompletableFuture<Result>) futures.get(i)).get();
            assertEquals((maxrec) / 2, res2.getAffectedItemsCount());
        }
    }
    asyncDocs = this.collection.find("$.T > :X ").bind("X", 1000000).fields(expr("{'cnt':count($.T)}")).executeAsync();
    docs = asyncDocs.get();
    doc = docs.next();
    assertEquals((long) (maxrec) / 2, (long) (((JsonNumber) doc.get("cnt")).getInteger()));
    asyncDocs = this.collection.find("$.T > :X and $.T < :Y").bind("X", 1000).bind("Y", 1000000).fields(expr("{'cnt':count($.T)}")).executeAsync();
    docs = asyncDocs.get();
    doc = docs.next();
    assertEquals((long) (maxrec) / 2, (long) (((JsonNumber) doc.get("cnt")).getInteger()));
}
Also used : ArrayList(java.util.ArrayList) AddResult(com.mysql.cj.xdevapi.AddResult) Result(com.mysql.cj.xdevapi.Result) AddResult(com.mysql.cj.xdevapi.AddResult) DocResult(com.mysql.cj.xdevapi.DocResult) DbDoc(com.mysql.cj.xdevapi.DbDoc) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) CompletableFuture(java.util.concurrent.CompletableFuture) JsonNumber(com.mysql.cj.xdevapi.JsonNumber) JsonString(com.mysql.cj.xdevapi.JsonString) ExecutionException(java.util.concurrent.ExecutionException) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 5 with AddResult

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

the class CompressionTest method uplinkCompression.

/**
 * Tests uplink compression using each one of the compression options.
 */
@Test
public void uplinkCompression() {
    assumeTrue(this.compressionSettings.serverSupportsCompression(), "Server variable mysqlx_compression_algorithms must be configured to run this test.");
    dropCollection("uplinkCompression");
    for (Compression compr : Compression.values()) {
        this.schema.createCollection("uplinkCompression");
        // Replace DISABLED by default value.
        String testCase = "[Compression: " + (compr == Compression.DISABLED ? "<default>" : compr) + "]";
        Session testSession = this.fact.getSession(this.compressFreeBaseUrl + (compr == Compression.DISABLED ? "" : makeParam(PropertyKey.xdevapiCompression, compr)));
        Collection col = testSession.getDefaultSchema().getCollection("uplinkCompression");
        assertTrue(this.counters.resetCounters(), testCase);
        // Enough bytes to trigger compression.
        AddResult res = col.add(longData).execute();
        assertEquals(1, res.getAffectedItemsCount(), testCase);
        assertTrue(this.counters.resetCounters(), testCase);
        // Server compresses small messages anyway.
        assertTrue(this.counters.downlinkCompressionUsed(), testCase);
        assertTrue(this.counters.uplinkCompressionUsed(), testCase);
        testSession.close();
        testSession = this.fact.getSession(this.compressFreeBaseUrl + makeParam(PropertyKey.xdevapiCompression, Compression.DISABLED));
        col = testSession.getDefaultSchema().getCollection("uplinkCompression");
        DocResult docs = col.find().execute();
        assertEquals(1, docs.count(), testCase);
        assertEquals(longDataDoc.get("data").toString(), docs.fetchOne().get("data").toString(), testCase);
        assertTrue(this.counters.resetCounters(), testCase);
        assertFalse(this.counters.usedCompression(), testCase);
        dropCollection("uplinkCompression");
        testSession.close();
    }
}
Also used : Compression(com.mysql.cj.conf.PropertyDefinitions.Compression) Collection(com.mysql.cj.xdevapi.Collection) AddResult(com.mysql.cj.xdevapi.AddResult) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) Session(com.mysql.cj.xdevapi.Session) Test(org.junit.jupiter.api.Test)

Aggregations

AddResult (com.mysql.cj.xdevapi.AddResult)25 Test (org.junit.jupiter.api.Test)25 JsonString (com.mysql.cj.xdevapi.JsonString)23 DbDoc (com.mysql.cj.xdevapi.DbDoc)22 DocResult (com.mysql.cj.xdevapi.DocResult)22 DbDocImpl (com.mysql.cj.xdevapi.DbDocImpl)8 Collection (com.mysql.cj.xdevapi.Collection)7 JsonNumber (com.mysql.cj.xdevapi.JsonNumber)7 Session (com.mysql.cj.xdevapi.Session)7 Result (com.mysql.cj.xdevapi.Result)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 ArrayList (java.util.ArrayList)4 ExecutionException (java.util.concurrent.ExecutionException)4 Compression (com.mysql.cj.conf.PropertyDefinitions.Compression)2 RowResult (com.mysql.cj.xdevapi.RowResult)2 SqlResult (com.mysql.cj.xdevapi.SqlResult)2 BigDecimal (java.math.BigDecimal)2 HashMap (java.util.HashMap)2 JsonArray (com.mysql.cj.xdevapi.JsonArray)1 SessionFactory (com.mysql.cj.xdevapi.SessionFactory)1