Search in sources :

Example 1 with DocFilterParams

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

the class XProtocolTest method testDocUpdate.

@Test
public void testDocUpdate() {
    assumeTrue(this.isSetForXTests, PropertyDefinitions.SYSP_testsuite_url_mysqlx + " must be set to run this test.");
    try {
        String collName = createTempTestCollection(this.protocol);
        String json = "{'_id': '85983efc2a9a11e5b345feff819cdc9f', 'testVal': '1', 'insertedBy': 'Jess'}".replaceAll("'", "\"");
        this.protocol.send(this.messageBuilder.buildDocInsert(getTestDatabase(), collName, Arrays.asList(new String[] { json }), false), 0);
        this.protocol.readQueryResult(new StatementExecuteOkBuilder());
        List<UpdateSpec> updates = new ArrayList<>();
        updates.add(new UpdateSpec(UpdateType.ITEM_SET, "$.a").setValue("lemon"));
        updates.add(new UpdateSpec(UpdateType.ITEM_REMOVE, "$.insertedBy"));
        this.protocol.send(this.messageBuilder.buildDocUpdate(new DocFilterParams(getTestDatabase(), collName), updates), 0);
        this.protocol.readQueryResult(new StatementExecuteOkBuilder());
        // verify
        this.protocol.send(this.messageBuilder.buildFind(new DocFilterParams(getTestDatabase(), collName)), 0);
        ColumnDefinition metadata = this.protocol.readMetadata();
        Iterator<Row> ris = new XProtocolRowInputStream(metadata, this.protocol, null);
        Row r = ris.next();
        assertEquals("{\"a\": \"lemon\", \"_id\": \"85983efc2a9a11e5b345feff819cdc9f\", \"testVal\": \"1\"}", r.getValue(0, new StringValueFactory(this.protocol.getPropertySet())));
        this.protocol.readQueryResult(new StatementExecuteOkBuilder());
    } finally {
        dropTempTestCollection(this.protocol);
    }
}
Also used : XProtocolRowInputStream(com.mysql.cj.protocol.x.XProtocolRowInputStream) StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) StringValueFactory(com.mysql.cj.result.StringValueFactory) ArrayList(java.util.ArrayList) UpdateSpec(com.mysql.cj.xdevapi.UpdateSpec) Row(com.mysql.cj.result.Row) DocFilterParams(com.mysql.cj.xdevapi.DocFilterParams) ColumnDefinition(com.mysql.cj.protocol.ColumnDefinition) Test(org.junit.jupiter.api.Test)

Example 2 with DocFilterParams

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

the class MysqlxSessionTest method testInterleavedResults.

@Test
public void testInterleavedResults() {
    assumeTrue(this.isSetForXTests, PropertyDefinitions.SYSP_testsuite_url_mysqlx + " must be set to run this test.");
    XMessageBuilder builder = (XMessageBuilder) this.session.<XMessage>getMessageBuilder();
    String collName = "testInterleavedResults";
    try {
        try {
            this.session.query(builder.buildDropCollection(getTestDatabase(), collName), new UpdateResultBuilder<>());
        } catch (XProtocolError e) {
            if (e.getErrorCode() != MysqlErrorNumbers.ER_BAD_TABLE_ERROR) {
                throw e;
            }
        }
        this.session.query(builder.buildCreateCollection(getTestDatabase(), collName), new UpdateResultBuilder<>());
        List<String> stringDocs = new ArrayList<>();
        stringDocs.add("{'_id':'0'}");
        stringDocs.add("{'_id':'1'}");
        stringDocs.add("{'_id':'2'}");
        stringDocs.add("{'_id':'3'}");
        stringDocs.add("{'_id':'4'}");
        stringDocs = stringDocs.stream().map(s -> s.replaceAll("'", "\"")).collect(Collectors.toList());
        this.session.query(builder.buildDocInsert(getTestDatabase(), collName, stringDocs, false), new UpdateResultBuilder<>());
        FilterParams filterParams = new DocFilterParams(getTestDatabase(), collName);
        filterParams.setOrder("$._id");
        DocResult docs1 = this.session.query(((XMessageBuilder) this.session.<XMessage>getMessageBuilder()).buildFind(filterParams), new StreamingDocResultBuilder(this.session));
        DocResult docs2 = this.session.query(((XMessageBuilder) this.session.<XMessage>getMessageBuilder()).buildFind(filterParams), new StreamingDocResultBuilder(this.session));
        DocResult docs3 = this.session.query(((XMessageBuilder) this.session.<XMessage>getMessageBuilder()).buildFind(filterParams), new StreamingDocResultBuilder(this.session));
        DocResult docs4 = this.session.query(((XMessageBuilder) this.session.<XMessage>getMessageBuilder()).buildFind(filterParams), new StreamingDocResultBuilder(this.session));
        DocResult docs5 = this.session.query(((XMessageBuilder) this.session.<XMessage>getMessageBuilder()).buildFind(filterParams), new StreamingDocResultBuilder(this.session));
        assertTrue(docs5.hasNext());
        assertTrue(docs4.hasNext());
        assertTrue(docs3.hasNext());
        assertTrue(docs2.hasNext());
        assertTrue(docs1.hasNext());
        for (int i = 0; i < 5; ++i) {
            assertEquals("{\n\"_id\" : \"" + i + "\"\n}", docs1.next().toFormattedString());
            assertEquals("{\n\"_id\" : \"" + i + "\"\n}", docs2.next().toFormattedString());
            assertEquals("{\n\"_id\" : \"" + i + "\"\n}", docs3.next().toFormattedString());
            assertEquals("{\n\"_id\" : \"" + i + "\"\n}", docs4.next().toFormattedString());
            assertEquals("{\n\"_id\" : \"" + i + "\"\n}", docs5.next().toFormattedString());
        }
        assertFalse(docs5.hasNext());
        assertFalse(docs4.hasNext());
        assertFalse(docs3.hasNext());
        assertFalse(docs2.hasNext());
        assertFalse(docs1.hasNext());
    // let the session be closed with all of these "open"
    } finally {
        try {
            this.session.query(builder.buildDropCollection(getTestDatabase(), collName), new UpdateResultBuilder<>());
        } catch (XProtocolError e) {
            if (e.getErrorCode() != MysqlErrorNumbers.ER_BAD_TABLE_ERROR) {
                throw e;
            }
        }
    }
}
Also used : XMessageBuilder(com.mysql.cj.protocol.x.XMessageBuilder) XMessage(com.mysql.cj.protocol.x.XMessage) XProtocolError(com.mysql.cj.protocol.x.XProtocolError) DocFilterParams(com.mysql.cj.xdevapi.DocFilterParams) FilterParams(com.mysql.cj.xdevapi.FilterParams) ArrayList(java.util.ArrayList) StreamingDocResultBuilder(com.mysql.cj.xdevapi.StreamingDocResultBuilder) DocResult(com.mysql.cj.xdevapi.DocResult) DocFilterParams(com.mysql.cj.xdevapi.DocFilterParams) Test(org.junit.jupiter.api.Test)

Example 3 with DocFilterParams

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

the class XProtocolAsyncTest method simpleSuccessfulQuery.

@Test
public void simpleSuccessfulQuery() throws Exception {
    assumeTrue(this.isSetForXTests, PropertyDefinitions.SYSP_testsuite_url_mysqlx + " must be set to run this test.");
    try {
        String collName = createTempTestCollection(this.protocol);
        String json = "{'_id': '85983efc2a9a11e5b345feff819cdc9f', 'testVal': 1, 'insertedBy': 'Jess'}".replaceAll("'", "\"");
        this.protocol.send(this.messageBuilder.buildDocInsert(getTestDatabase(), collName, Arrays.asList(new String[] { json }), false), 0);
        this.protocol.readQueryResult(new StatementExecuteOkBuilder());
        final ValueHolder<ColumnDefinition> metadataHolder = new ValueHolder<>();
        final ValueHolder<ArrayList<Row>> rowHolder = new ValueHolder<>();
        rowHolder.accept(new ArrayList<>());
        final ValueHolder<StatementExecuteOk> okHolder = new ValueHolder<>();
        final ValueHolder<Throwable> excHolder = new ValueHolder<>();
        this.protocol.queryAsync(this.messageBuilder.buildFind(new DocFilterParams(getTestDatabase(), collName)), new ResultBuilder<RowResult>() {

            private ArrayList<Field> fields = new ArrayList<>();

            private ColumnDefinition metadata;

            @Override
            public boolean addProtocolEntity(ProtocolEntity entity) {
                if (entity instanceof Field) {
                    this.fields.add((Field) entity);
                } else if (entity instanceof ColumnDefinition) {
                    this.metadata = (ColumnDefinition) entity;
                    metadataHolder.accept(this.metadata);
                } else if (entity instanceof Row) {
                    if (this.metadata == null) {
                        this.metadata = new DefaultColumnDefinition(this.fields.toArray(new Field[] {}));
                        metadataHolder.accept(this.metadata);
                    }
                    rowHolder.get().add((Row) entity);
                } else if (entity instanceof StatementExecuteOk) {
                    okHolder.accept((StatementExecuteOk) entity);
                    synchronized (XProtocolAsyncTest.this) {
                        XProtocolAsyncTest.this.notify();
                    }
                    return true;
                }
                return false;
            }

            @Override
            public RowResult build() {
                return null;
            }
        });
        synchronized (this) {
            // timeout in case we get stuck
            this.wait(5000);
        }
        assertEquals(1, metadataHolder.get().getFields().length);
        assertEquals(1, rowHolder.get().size());
        assertNotNull(okHolder.get());
        assertNull(excHolder.get());
    } finally {
        dropTempTestCollection(this.protocol);
    }
}
Also used : StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) ArrayList(java.util.ArrayList) DefaultColumnDefinition(com.mysql.cj.result.DefaultColumnDefinition) ColumnDefinition(com.mysql.cj.protocol.ColumnDefinition) StatementExecuteOk(com.mysql.cj.protocol.x.StatementExecuteOk) RowResult(com.mysql.cj.xdevapi.RowResult) Field(com.mysql.cj.result.Field) DefaultColumnDefinition(com.mysql.cj.result.DefaultColumnDefinition) Row(com.mysql.cj.result.Row) DocFilterParams(com.mysql.cj.xdevapi.DocFilterParams) ProtocolEntity(com.mysql.cj.protocol.ProtocolEntity) Test(org.junit.jupiter.api.Test)

Example 4 with DocFilterParams

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

the class XProtocolTest method testMultiInsert.

@Test
public void testMultiInsert() {
    assumeTrue(this.isSetForXTests, PropertyDefinitions.SYSP_testsuite_url_mysqlx + " must be set to run this test.");
    try {
        String collName = createTempTestCollection(this.protocol);
        List<String> stringDocs = new ArrayList<>();
        stringDocs.add("{'a': 'A', 'a1': 'A1', '_id': 'a'}");
        stringDocs.add("{'b': 'B', 'b2': 'B2', '_id': 'b'}");
        stringDocs.add("{'c': 'C', 'c3': 'C3', '_id': 'c'}");
        stringDocs = stringDocs.stream().map(s -> s.replaceAll("'", "\"")).collect(Collectors.toList());
        this.protocol.send(this.messageBuilder.buildDocInsert(getTestDatabase(), collName, stringDocs, false), 0);
        this.protocol.readQueryResult(new StatementExecuteOkBuilder());
        FilterParams filterParams = new DocFilterParams(getTestDatabase(), collName);
        filterParams.setOrder("_id");
        this.protocol.send(this.messageBuilder.buildFind(filterParams), 0);
        ColumnDefinition metadata = this.protocol.readMetadata();
        Iterator<Row> ris = new XProtocolRowInputStream(metadata, this.protocol, null);
        Row r = ris.next();
        assertEquals(stringDocs.get(0), r.getValue(0, new StringValueFactory(this.protocol.getPropertySet())));
        r = ris.next();
        assertEquals(stringDocs.get(1), r.getValue(0, new StringValueFactory(this.protocol.getPropertySet())));
        r = ris.next();
        assertEquals(stringDocs.get(2), r.getValue(0, new StringValueFactory(this.protocol.getPropertySet())));
        this.protocol.readQueryResult(new StatementExecuteOkBuilder());
    } finally {
        dropTempTestCollection(this.protocol);
    }
}
Also used : XProtocolRowInputStream(com.mysql.cj.protocol.x.XProtocolRowInputStream) StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) StringValueFactory(com.mysql.cj.result.StringValueFactory) DocFilterParams(com.mysql.cj.xdevapi.DocFilterParams) TableFilterParams(com.mysql.cj.xdevapi.TableFilterParams) FilterParams(com.mysql.cj.xdevapi.FilterParams) ArrayList(java.util.ArrayList) Row(com.mysql.cj.result.Row) DocFilterParams(com.mysql.cj.xdevapi.DocFilterParams) ColumnDefinition(com.mysql.cj.protocol.ColumnDefinition) Test(org.junit.jupiter.api.Test)

Example 5 with DocFilterParams

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

the class XProtocolTest method testBasicCrudInsertFind.

@Test
public void testBasicCrudInsertFind() {
    assumeTrue(this.isSetForXTests, PropertyDefinitions.SYSP_testsuite_url_mysqlx + " must be set to run this test.");
    try {
        String collName = createTempTestCollection(this.protocol);
        String json = "{'_id': '85983efc2a9a11e5b345feff819cdc9f', 'testVal': 1, 'insertedBy': 'Jess'}".replaceAll("'", "\"");
        this.protocol.send(this.messageBuilder.buildDocInsert(getTestDatabase(), collName, Arrays.asList(new String[] { json }), false), 0);
        this.protocol.readQueryResult(new StatementExecuteOkBuilder());
        FilterParams filterParams = new DocFilterParams(getTestDatabase(), collName);
        filterParams.setCriteria("$.testVal = 2-1");
        this.protocol.send(this.messageBuilder.buildFind(filterParams), 0);
        ColumnDefinition metadata = this.protocol.readMetadata();
        Iterator<Row> ris = new XProtocolRowInputStream(metadata, this.protocol, null);
        Row r = ris.next();
        assertEquals(json, r.getValue(0, new StringValueFactory(this.protocol.getPropertySet())));
        this.protocol.readQueryResult(new StatementExecuteOkBuilder());
    } finally {
        dropTempTestCollection(this.protocol);
    }
}
Also used : XProtocolRowInputStream(com.mysql.cj.protocol.x.XProtocolRowInputStream) StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) StringValueFactory(com.mysql.cj.result.StringValueFactory) DocFilterParams(com.mysql.cj.xdevapi.DocFilterParams) TableFilterParams(com.mysql.cj.xdevapi.TableFilterParams) FilterParams(com.mysql.cj.xdevapi.FilterParams) Row(com.mysql.cj.result.Row) DocFilterParams(com.mysql.cj.xdevapi.DocFilterParams) ColumnDefinition(com.mysql.cj.protocol.ColumnDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

DocFilterParams (com.mysql.cj.xdevapi.DocFilterParams)5 Test (org.junit.jupiter.api.Test)5 ColumnDefinition (com.mysql.cj.protocol.ColumnDefinition)4 StatementExecuteOkBuilder (com.mysql.cj.protocol.x.StatementExecuteOkBuilder)4 Row (com.mysql.cj.result.Row)4 ArrayList (java.util.ArrayList)4 XProtocolRowInputStream (com.mysql.cj.protocol.x.XProtocolRowInputStream)3 StringValueFactory (com.mysql.cj.result.StringValueFactory)3 FilterParams (com.mysql.cj.xdevapi.FilterParams)3 TableFilterParams (com.mysql.cj.xdevapi.TableFilterParams)2 ProtocolEntity (com.mysql.cj.protocol.ProtocolEntity)1 StatementExecuteOk (com.mysql.cj.protocol.x.StatementExecuteOk)1 XMessage (com.mysql.cj.protocol.x.XMessage)1 XMessageBuilder (com.mysql.cj.protocol.x.XMessageBuilder)1 XProtocolError (com.mysql.cj.protocol.x.XProtocolError)1 DefaultColumnDefinition (com.mysql.cj.result.DefaultColumnDefinition)1 Field (com.mysql.cj.result.Field)1 DocResult (com.mysql.cj.xdevapi.DocResult)1 RowResult (com.mysql.cj.xdevapi.RowResult)1 StreamingDocResultBuilder (com.mysql.cj.xdevapi.StreamingDocResultBuilder)1