Search in sources :

Example 6 with DocPartResult

use of com.torodb.core.d2r.DocPartResult in project torodb by torodb.

the class R2DTranslatorTest method readMultipleDocument.

/*
   * Document 1 { "name" : "jero" } Document 2 { "name" : "john" }
   */
@Test
public void readMultipleDocument() {
    MetaDocPartBuilder builder = new MetaDocPartBuilder(rootRef);
    builder.addMetaField("name", "name_s", FieldType.STRING);
    builder.addRow(1, null, 1, null, "jero");
    builder.addRow(2, null, 2, null, "john");
    MockedDocPartResult root = builder.getResultSet();
    List<DocPartResult> lst = Collections.singletonList(root);
    R2DTranslator r2dTranslator = new R2DTranslatorImpl();
    ToroDocument[] readedDocuments = r2dTranslator.translate(lst.iterator()).toArray(new ToroDocument[0]);
    assertEquals(2, readedDocuments.length);
    KvDocument doc1 = readedDocuments[0].getRoot();
    assertEquals("jero", doc1.get("name").getValue());
    KvDocument doc2 = readedDocuments[1].getRoot();
    assertEquals("john", doc2.get("name").getValue());
}
Also used : KvDocument(com.torodb.kvdocument.values.KvDocument) R2DTranslator(com.torodb.core.d2r.R2DTranslator) ToroDocument(com.torodb.core.document.ToroDocument) DocPartResult(com.torodb.core.d2r.DocPartResult) Test(org.junit.Test)

Example 7 with DocPartResult

use of com.torodb.core.d2r.DocPartResult in project torodb by torodb.

the class AbstractReadInterface method getCollectionResultSets.

@Override
@SuppressFBWarnings(value = { "OBL_UNSATISFIED_OBLIGATION", "ODR_OPEN_DATABASE_RESOURCE" }, justification = "ResultSet is wrapped in a DocPartResult. It's iterated and closed in caller code")
public List<DocPartResult> getCollectionResultSets(DSLContext dsl, MetaDatabase metaDatabase, MetaCollection metaCollection, Collection<Integer> dids) throws SQLException {
    ArrayList<DocPartResult> result = new ArrayList<>();
    Connection connection = dsl.configuration().connectionProvider().acquire();
    try {
        Iterator<? extends MetaDocPart> metaDocPartIterator = metaCollection.streamContainedMetaDocParts().sorted(TableRefComparator.MetaDocPart.DESC).iterator();
        while (metaDocPartIterator.hasNext()) {
            MetaDocPart metaDocPart = metaDocPartIterator.next();
            String statament = getDocPartStatament(metaDatabase, metaDocPart, dids);
            PreparedStatement preparedStatement = connection.prepareStatement(statament);
            result.add(new ResultSetDocPartResult(metaDataReadInterface, dataTypeProvider, errorHandler, metaDocPart, preparedStatement.executeQuery(), sqlHelper));
        }
    } finally {
        dsl.configuration().connectionProvider().release(connection);
    }
    return result;
}
Also used : ResultSetDocPartResult(com.torodb.backend.d2r.ResultSetDocPartResult) MetaDocPart(com.torodb.core.transaction.metainf.MetaDocPart) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSetDocPartResult(com.torodb.backend.d2r.ResultSetDocPartResult) DocPartResult(com.torodb.core.d2r.DocPartResult) PreparedStatement(java.sql.PreparedStatement) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 8 with DocPartResult

use of com.torodb.core.d2r.DocPartResult in project torodb by torodb.

the class R2DTranslatorTest method readTwoLevelDocument.

/*
   * Document: { "name" : "jero", "address" : { "street" : "myhouse" } }
   */
@Test
public void readTwoLevelDocument() {
    /*
     * Root Level
     */
    MetaDocPartBuilder builder = new MetaDocPartBuilder(rootRef);
    builder.addMetaField("name", "name_s", FieldType.STRING);
    builder.addMetaField("address", "address_e", FieldType.CHILD);
    builder.addRow(1, null, 1, null, "jero", IsDocument);
    MockedDocPartResult root = builder.getResultSet();
    /*
     * Second Level
     */
    TableRef secondRef = fact.createChild(rootRef, "address");
    MetaDocPartBuilder secondBuilder = new MetaDocPartBuilder(secondRef);
    secondBuilder.addMetaField("street", "street_s", FieldType.STRING);
    secondBuilder.addRow(1, 1, 20, null, "myhouse");
    MockedDocPartResult secondLevel = secondBuilder.getResultSet();
    List<DocPartResult> lst = Lists.newArrayList(secondLevel, root);
    R2DTranslator r2dTranslator = new R2DTranslatorImpl();
    Collection<ToroDocument> readedDocuments = r2dTranslator.translate(lst.iterator());
    assertEquals(1, readedDocuments.size());
    KvDocument doc = readedDocuments.iterator().next().getRoot();
    assertEquals("jero", doc.get("name").getValue());
    KvValue<?> kvValue = doc.get("address");
    assertTrue(kvValue instanceof KvDocument);
}
Also used : KvDocument(com.torodb.kvdocument.values.KvDocument) R2DTranslator(com.torodb.core.d2r.R2DTranslator) ToroDocument(com.torodb.core.document.ToroDocument) DocPartResult(com.torodb.core.d2r.DocPartResult) TableRef(com.torodb.core.TableRef) Test(org.junit.Test)

Example 9 with DocPartResult

use of com.torodb.core.d2r.DocPartResult in project torodb by torodb.

the class R2DTranslatorTest method readDocumentWithNullField.

/*
   * Document: { "name" : "jero", "address" : null }
   */
@Test
public void readDocumentWithNullField() {
    MetaDocPartBuilder builder = new MetaDocPartBuilder(rootRef);
    builder.addMetaField("name", "name_s", FieldType.STRING);
    builder.addMetaField("address", "address_s", FieldType.STRING);
    builder.addMetaField("address", "address_n", FieldType.NULL);
    builder.addRow(1, null, 1, null, "jero", null, true);
    MockedDocPartResult root = builder.getResultSet();
    List<DocPartResult> lst = Collections.singletonList(root);
    R2DTranslator r2dTranslator = new R2DTranslatorImpl();
    Collection<ToroDocument> readedDocuments = r2dTranslator.translate(lst.iterator());
    assertEquals(1, readedDocuments.size());
    KvDocument doc = readedDocuments.iterator().next().getRoot();
    assertEquals("jero", doc.get("name").getValue());
    assertEquals(KvNull.getInstance(), doc.get("address"));
}
Also used : KvDocument(com.torodb.kvdocument.values.KvDocument) R2DTranslator(com.torodb.core.d2r.R2DTranslator) ToroDocument(com.torodb.core.document.ToroDocument) DocPartResult(com.torodb.core.d2r.DocPartResult) Test(org.junit.Test)

Example 10 with DocPartResult

use of com.torodb.core.d2r.DocPartResult in project torodb by torodb.

the class R2DTranslatorTest method readTwoInnerArray.

/*
   * Document: { "name" : "jero", "numbers" : [666, [4,8, 15, 16]] }
   */
@Test
public void readTwoInnerArray() {
    int did = 1;
    /*
     * Root Level
     */
    MetaDocPartBuilder builder = new MetaDocPartBuilder(rootRef);
    builder.addMetaField("name", "name_s", FieldType.STRING);
    builder.addMetaField("numbers", "numbers_e", FieldType.CHILD);
    builder.addRow(did, null, did, null, "jero", IsArray);
    MockedDocPartResult root = builder.getResultSet();
    /*
     * Second Level
     */
    int pid = did;
    TableRef secondRef = fact.createChild(rootRef, "numbers");
    MetaDocPartBuilder secondBuilder = new MetaDocPartBuilder(secondRef);
    secondBuilder.addMetaScalar("v_i", FieldType.INTEGER);
    secondBuilder.addMetaScalar("v_e", FieldType.CHILD);
    secondBuilder.addRow(did, pid, 20, 0, 666, null);
    secondBuilder.addRow(did, pid, 21, 1, null, IsArray);
    MockedDocPartResult secondLevel = secondBuilder.getResultSet();
    /*
     * Third Level
     */
    int pid1 = 21;
    TableRef thirdRef = fact.createChild(secondRef, 2);
    MetaDocPartBuilder thirdBuilder = new MetaDocPartBuilder(thirdRef);
    thirdBuilder.addMetaScalar("v_i", FieldType.INTEGER);
    thirdBuilder.addRow(did, pid1, 30, 0, 4);
    thirdBuilder.addRow(did, pid1, 31, 1, 8);
    thirdBuilder.addRow(did, pid1, 33, 2, 15);
    thirdBuilder.addRow(did, pid1, 34, 3, 16);
    MockedDocPartResult thirdLevel = thirdBuilder.getResultSet();
    List<DocPartResult> lst = Lists.newArrayList(thirdLevel, secondLevel, root);
    R2DTranslator r2dTranslator = new R2DTranslatorImpl();
    Collection<ToroDocument> readedDocuments = r2dTranslator.translate(lst.iterator());
    assertEquals(1, readedDocuments.size());
    KvDocument doc = readedDocuments.iterator().next().getRoot();
    assertEquals("jero", doc.get("name").getValue());
    KvValue<?> kvValue = doc.get("numbers");
    assertTrue(kvValue instanceof KvArray);
    KvArray array = (KvArray) kvValue;
    assertEquals(2, array.size());
    assertEquals(666, array.get(0).getValue());
    KvValue<?> kvValueSecond = array.get(1);
    assertNotNull(kvValueSecond);
    assertTrue(kvValueSecond instanceof KvArray);
    KvArray array2 = (KvArray) kvValueSecond;
    assertEquals(4, array2.size());
    assertEquals(4, array2.get(0).getValue());
    assertEquals(8, array2.get(1).getValue());
    assertEquals(15, array2.get(2).getValue());
    assertEquals(16, array2.get(3).getValue());
}
Also used : KvDocument(com.torodb.kvdocument.values.KvDocument) R2DTranslator(com.torodb.core.d2r.R2DTranslator) KvArray(com.torodb.kvdocument.values.KvArray) ToroDocument(com.torodb.core.document.ToroDocument) DocPartResult(com.torodb.core.d2r.DocPartResult) TableRef(com.torodb.core.TableRef) Test(org.junit.Test)

Aggregations

DocPartResult (com.torodb.core.d2r.DocPartResult)11 ToroDocument (com.torodb.core.document.ToroDocument)10 R2DTranslator (com.torodb.core.d2r.R2DTranslator)9 KvDocument (com.torodb.kvdocument.values.KvDocument)9 Test (org.junit.Test)9 TableRef (com.torodb.core.TableRef)5 KvArray (com.torodb.kvdocument.values.KvArray)3 MetaDocPart (com.torodb.core.transaction.metainf.MetaDocPart)2 ImmutableList (com.google.common.collect.ImmutableList)1 ResultSetDocPartResult (com.torodb.backend.d2r.ResultSetDocPartResult)1 KvValue (com.torodb.kvdocument.values.KvValue)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1