Search in sources :

Example 1 with ToroDocument

use of com.torodb.core.document.ToroDocument in project torodb by torodb.

the class R2DTranslatorTest method readDocumentWithOutPresentFields.

/*
   * Document: { "name" : "jero" }
   */
@Test
public void readDocumentWithOutPresentFields() {
    MetaDocPartBuilder builder = new MetaDocPartBuilder(rootRef);
    builder.addMetaField("name", "name_s", FieldType.STRING);
    builder.addMetaField("address", "address_s", FieldType.STRING);
    builder.addRow(1, null, 1, null, "jero", null);
    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());
    assertNull(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 2 with ToroDocument

use of com.torodb.core.document.ToroDocument in project torodb by torodb.

the class R2DTranslatorTest method readInnerArray.

/*
   * Document: { "name" : "jero", "numbers" : [4, 8, 15, 16] }
   */
@Test
public void readInnerArray() {
    /*
     * Root Level
     */
    MetaDocPartBuilder builder = new MetaDocPartBuilder(rootRef);
    builder.addMetaField("name", "name_s", FieldType.STRING);
    builder.addMetaField("numbers", "numbers_e", FieldType.CHILD);
    builder.addRow(1, null, 1, null, "jero", IsArray);
    MockedDocPartResult root = builder.getResultSet();
    /*
     * Second Level
     */
    TableRef secondRef = fact.createChild(rootRef, "numbers");
    MetaDocPartBuilder secondBuilder = new MetaDocPartBuilder(secondRef);
    secondBuilder.addMetaScalar("v_i", FieldType.INTEGER);
    secondBuilder.addRow(1, 1, 20, 0, 4);
    secondBuilder.addRow(1, 1, 21, 1, 8);
    secondBuilder.addRow(1, 1, 23, 2, 15);
    secondBuilder.addRow(1, 1, 24, 3, 16);
    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("numbers");
    assertTrue(kvValue instanceof KvArray);
    KvArray array = (KvArray) kvValue;
    assertEquals(4, array.size());
    assertEquals(4, array.get(0).getValue());
    assertEquals(8, array.get(1).getValue());
    assertEquals(15, array.get(2).getValue());
    assertEquals(16, array.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)

Example 3 with ToroDocument

use of com.torodb.core.document.ToroDocument in project torodb by torodb.

the class R2DTranslatorTest method readDocumentInArray.

/*
   * Document: { "name" : "jero", "numbers" : [ 666, { "address": "myhome" } ] }
   */
@Test
public void readDocumentInArray() {
    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.addMetaField("address", "address_s", FieldType.STRING);
    secondBuilder.addRow(did, pid, 20, 0, 666, null);
    secondBuilder.addRow(did, pid, 21, 1, null, "myhome");
    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("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 KvDocument);
    KvDocument doc2 = (KvDocument) kvValueSecond;
    assertEquals("myhome", doc2.get("address").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)

Example 4 with ToroDocument

use of com.torodb.core.document.ToroDocument in project torodb by torodb.

the class R2DTranslatorTest method readDocumentWithMultipleFields.

/*
   * Document: { "name" : "jero", "address" : "my home", "age" : 25 }
   */
@Test
public void readDocumentWithMultipleFields() {
    MetaDocPartBuilder builder = new MetaDocPartBuilder(rootRef);
    builder.addMetaField("name", "name_s", FieldType.STRING);
    builder.addMetaField("address", "address_s", FieldType.STRING);
    builder.addMetaField("age", "age_i", FieldType.INTEGER);
    builder.addRow(1, null, 1, null, "jero", "my home", 25);
    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("my home", doc.get("address").getValue());
    assertEquals(25, doc.get("age").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 5 with ToroDocument

use of com.torodb.core.document.ToroDocument in project torodb by torodb.

the class R2DTranslatorImpl method translate.

@Override
public List<ToroDocument> translate(Iterator<DocPartResult> docPartResultIt) {
    ImmutableList.Builder<ToroDocument> readedDocuments = ImmutableList.builder();
    Table<TableRef, Integer, Map<String, List<KvValue<?>>>> currentFieldDocPartTable = HashBasedTable.<TableRef, Integer, Map<String, List<KvValue<?>>>>create();
    Table<TableRef, Integer, Map<String, List<KvValue<?>>>> childFieldDocPartTable = HashBasedTable.<TableRef, Integer, Map<String, List<KvValue<?>>>>create();
    int previousDepth = -1;
    while (docPartResultIt.hasNext()) {
        DocPartResult docPartResult = docPartResultIt.next();
        MetaDocPart metaDocPart = docPartResult.getMetaDocPart();
        TableRef tableRef = metaDocPart.getTableRef();
        if (previousDepth != -1 && previousDepth != tableRef.getDepth()) {
            Table<TableRef, Integer, Map<String, List<KvValue<?>>>> previousFieldChildDocPartTable = childFieldDocPartTable;
            childFieldDocPartTable = currentFieldDocPartTable;
            currentFieldDocPartTable = previousFieldChildDocPartTable;
            if (!tableRef.isRoot()) {
                currentFieldDocPartTable.clear();
            }
        }
        previousDepth = tableRef.getDepth();
        Map<Integer, Map<String, List<KvValue<?>>>> childFieldDocPartRow = childFieldDocPartTable.row(tableRef);
        Map<Integer, Map<String, List<KvValue<?>>>> currentFieldDocPartRow;
        if (tableRef.isRoot()) {
            currentFieldDocPartRow = null;
        } else {
            currentFieldDocPartRow = currentFieldDocPartTable.row(tableRef.getParent().get());
        }
        readResult(metaDocPart, tableRef, docPartResult, currentFieldDocPartRow, childFieldDocPartRow, readedDocuments);
    }
    return readedDocuments.build();
}
Also used : MetaDocPart(com.torodb.core.transaction.metainf.MetaDocPart) ImmutableList(com.google.common.collect.ImmutableList) ToroDocument(com.torodb.core.document.ToroDocument) DocPartResult(com.torodb.core.d2r.DocPartResult) HashMap(java.util.HashMap) Map(java.util.Map) TableRef(com.torodb.core.TableRef) KvValue(com.torodb.kvdocument.values.KvValue)

Aggregations

ToroDocument (com.torodb.core.document.ToroDocument)12 KvDocument (com.torodb.kvdocument.values.KvDocument)11 DocPartResult (com.torodb.core.d2r.DocPartResult)10 R2DTranslator (com.torodb.core.d2r.R2DTranslator)9 Test (org.junit.Test)9 TableRef (com.torodb.core.TableRef)5 ImmutableList (com.google.common.collect.ImmutableList)3 KvArray (com.torodb.kvdocument.values.KvArray)3 KvValue (com.torodb.kvdocument.values.KvValue)2 ErrorCode (com.eightkdata.mongowp.ErrorCode)1 Status (com.eightkdata.mongowp.Status)1 BsonDocument (com.eightkdata.mongowp.bson.BsonDocument)1 CommandFailed (com.eightkdata.mongowp.exceptions.CommandFailed)1 Command (com.eightkdata.mongowp.server.api.Command)1 Request (com.eightkdata.mongowp.server.api.Request)1 Cursor (com.torodb.core.cursors.Cursor)1 DocPartResultRow (com.torodb.core.d2r.DocPartResultRow)1 UserWrappedException (com.torodb.core.exceptions.UserWrappedException)1 UpdateException (com.torodb.core.exceptions.user.UpdateException)1 UserException (com.torodb.core.exceptions.user.UserException)1