Search in sources :

Example 11 with ToroDocument

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

the class R2DTranslatorTest method readSimpleDocument.

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

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

the class R2DTranslatorImpl method readResult.

private void readResult(MetaDocPart metaDocPart, TableRef tableRef, DocPartResult docPartResult, Map<Integer, Map<String, List<KvValue<?>>>> currentFieldDocPartRow, Map<Integer, Map<String, List<KvValue<?>>>> childFieldDocPartRow, ImmutableList.Builder<ToroDocument> readedDocuments) {
    while (docPartResult.hasNext()) {
        KvDocument.Builder documentBuilder = new KvDocument.Builder();
        DocPartResultRow row = docPartResult.next();
        Integer did = row.getDid();
        Integer rid = row.getRid();
        Integer pid = row.getPid();
        Integer seq = row.getSeq();
        Map<String, List<KvValue<?>>> childFieldDocPartCell = childFieldDocPartRow.get(rid);
        //TODO: ensure MetaField order using ResultSet meta data
        Iterator<? extends MetaScalar> metaScalarIterator = metaDocPart.streamScalars().iterator();
        boolean wasScalar = false;
        int fieldIndex = 0;
        while (metaScalarIterator.hasNext() && !wasScalar) {
            assert seq != null : "found scalar value outside of an array";
            MetaScalar metaScalar = metaScalarIterator.next();
            KvValue<?> value = row.getUserValue(fieldIndex, metaScalar.getType());
            fieldIndex++;
            if (value != null) {
                if (metaScalar.getType() == FieldType.CHILD) {
                    value = getChildValue(value, getDocPartCellName(tableRef), childFieldDocPartCell);
                }
                addValueToDocPartRow(currentFieldDocPartRow, tableRef, pid, seq, value);
                wasScalar = true;
            }
        }
        if (wasScalar) {
            continue;
        }
        Iterator<? extends MetaField> metaFieldIterator = metaDocPart.streamFields().iterator();
        while (metaFieldIterator.hasNext()) {
            MetaField metaField = metaFieldIterator.next();
            KvValue<?> value = row.getUserValue(fieldIndex, metaField.getType());
            fieldIndex++;
            if (value != null) {
                if (metaField.getType() == FieldType.CHILD) {
                    value = getChildValue(value, metaField.getName(), childFieldDocPartCell);
                }
                documentBuilder.putValue(metaField.getName(), value);
            }
        }
        if (tableRef.isRoot()) {
            readedDocuments.add(new ToroDocument(did, documentBuilder.build()));
        } else {
            addValueToDocPartRow(currentFieldDocPartRow, tableRef, pid, seq, documentBuilder.build());
        }
    }
}
Also used : KvDocument(com.torodb.kvdocument.values.KvDocument) MetaField(com.torodb.core.transaction.metainf.MetaField) MetaScalar(com.torodb.core.transaction.metainf.MetaScalar) DocPartResultRow(com.torodb.core.d2r.DocPartResultRow) ToroDocument(com.torodb.core.document.ToroDocument) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

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