Search in sources :

Example 1 with StreamingDocResultBuilder

use of com.mysql.cj.xdevapi.StreamingDocResultBuilder 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)

Aggregations

XMessage (com.mysql.cj.protocol.x.XMessage)1 XMessageBuilder (com.mysql.cj.protocol.x.XMessageBuilder)1 XProtocolError (com.mysql.cj.protocol.x.XProtocolError)1 DocFilterParams (com.mysql.cj.xdevapi.DocFilterParams)1 DocResult (com.mysql.cj.xdevapi.DocResult)1 FilterParams (com.mysql.cj.xdevapi.FilterParams)1 StreamingDocResultBuilder (com.mysql.cj.xdevapi.StreamingDocResultBuilder)1 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1