Search in sources :

Example 6 with Array

use of com.yahoo.document.datatypes.Array in project vespa by vespa-engine.

the class SplitterJoinerTestCase method testSplitJoin.

@Test
public void testSplitJoin() {
    ConfigGetter<SplitterJoinerDocumentProcessorConfig> getter = new ConfigGetter<>(SplitterJoinerDocumentProcessorConfig.class);
    ConfigGetter<DocumentmanagerConfig> docManGetter = new ConfigGetter<>(DocumentmanagerConfig.class);
    SplitterJoinerDocumentProcessorConfig cfg = getter.getConfig("file:src/test/java/com/yahoo/docproc/util/splitter-joiner-document-processor.cfg");
    DocumentmanagerConfig docManCfg = docManGetter.getConfig("file:src/test/java/com/yahoo/docproc/util/documentmanager.docindoc.cfg");
    SplitterDocumentProcessor splitter = new SplitterDocumentProcessor(cfg, docManCfg);
    DocumentTypeManager manager = splitter.manager;
    /**
     ** Create documents: ***
     */
    Document inner1 = new Document(manager.getDocumentType("docindoc"), "doc:inner:number:one");
    inner1.setFieldValue("name", new StringFieldValue("Donald Duck"));
    inner1.setFieldValue("content", new StringFieldValue("Lives in Duckburg"));
    Document inner2 = new Document(manager.getDocumentType("docindoc"), "doc:inner:number:two");
    inner2.setFieldValue("name", new StringFieldValue("Uncle Scrooge"));
    inner2.setFieldValue("content", new StringFieldValue("Lives in Duckburg, too."));
    Array<Document> innerArray = (Array<Document>) manager.getDocumentType("outerdoc").getField("innerdocuments").getDataType().createFieldValue();
    innerArray.add(inner1);
    innerArray.add(inner2);
    Document outer = new Document(manager.getDocumentType("outerdoc"), "doc:outer:the:only:one");
    outer.setFieldValue("innerdocuments", innerArray);
    /**
     ** End create documents ***
     */
    Processing p = Processing.of(new DocumentPut(outer));
    splitter.process(p);
    assertEquals(2, p.getDocumentOperations().size());
    assertThat(((DocumentPut) (p.getDocumentOperations().get(0))).getDocument(), sameInstance(inner1));
    assertThat(((DocumentPut) (p.getDocumentOperations().get(1))).getDocument(), sameInstance(inner2));
    assertThat(((DocumentPut) (p.getVariable(cfg.contextFieldName()))).getDocument(), sameInstance(outer));
    assertThat(outer.getFieldValue("innerdocuments"), sameInstance(innerArray));
    assertTrue(innerArray.isEmpty());
    JoinerDocumentProcessor joiner = new JoinerDocumentProcessor(cfg, docManCfg);
    joiner.process(p);
    assertThat(p.getDocumentOperations().size(), equalTo(1));
    assertThat(((DocumentPut) p.getDocumentOperations().get(0)).getDocument(), sameInstance(outer));
    assertThat(p.getVariable(cfg.contextFieldName()), nullValue());
    assertThat(outer.getFieldValue("innerdocuments"), sameInstance(innerArray));
    assertThat(innerArray.size(), equalTo(2));
    assertThat(innerArray.get(0), sameInstance(inner1));
    assertThat(innerArray.get(1), sameInstance(inner2));
}
Also used : ConfigGetter(com.yahoo.config.subscription.ConfigGetter) DocumentPut(com.yahoo.document.DocumentPut) SplitterJoinerDocumentProcessorConfig(com.yahoo.config.docproc.SplitterJoinerDocumentProcessorConfig) Document(com.yahoo.document.Document) Processing(com.yahoo.docproc.Processing) Array(com.yahoo.document.datatypes.Array) DocumentmanagerConfig(com.yahoo.document.config.DocumentmanagerConfig) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) DocumentTypeManager(com.yahoo.document.DocumentTypeManager) Test(org.junit.Test)

Example 7 with Array

use of com.yahoo.document.datatypes.Array in project vespa by vespa-engine.

the class SplitterDocumentProcessor method process.

@Override
public Progress process(Processing processing) {
    if (processing.getDocumentOperations().size() != 1) {
        // we were given more than one document, return
        log.log(LogLevel.DEBUG, "More than one document given, returning. (Was given " + processing.getDocumentOperations().size() + " documents).");
        return Progress.DONE;
    }
    if (!doProcessOuterDocument(processing.getDocumentOperations().get(0), documentTypeName)) {
        return Progress.DONE;
    }
    Document outerDoc = ((DocumentPut) processing.getDocumentOperations().get(0)).getDocument();
    ;
    @SuppressWarnings("unchecked") Array<Document> innerDocuments = (Array<Document>) outerDoc.getFieldValue(arrayFieldName);
    if (innerDocuments == null) {
        // the document does not have the field, return
        log.log(LogLevel.DEBUG, "The given Document does not have a field value for field " + arrayFieldName + ", returning. (Was given " + outerDoc + ").");
        return Progress.DONE;
    }
    if (innerDocuments.size() == 0) {
        // the array is empty, return
        log.log(LogLevel.DEBUG, "The given Document does not have any elements in array field " + arrayFieldName + ", returning. (Was given " + outerDoc + ").");
        return Progress.DONE;
    }
    split(processing, innerDocuments);
    return Progress.DONE;
}
Also used : Array(com.yahoo.document.datatypes.Array) DocumentPut(com.yahoo.document.DocumentPut) Document(com.yahoo.document.Document)

Example 8 with Array

use of com.yahoo.document.datatypes.Array in project vespa by vespa-engine.

the class JsonReaderTestCase method testMapStringToArrayOfInt.

@Test
public final void testMapStringToArrayOfInt() throws IOException {
    InputStream rawDoc = new ByteArrayInputStream(Utf8.toBytes("{\"put\": \"id:unittest:testMapStringToArrayOfInt::whee\"," + " \"fields\": { \"actualMapStringToArrayOfInt\": { \"bamse\": [1, 2, 3] }}}"));
    JsonReader r = new JsonReader(types, rawDoc, parserFactory);
    DocumentParseInfo parseInfo = r.parseDocument().get();
    DocumentType docType = r.readDocumentType(parseInfo.documentId);
    DocumentPut put = new DocumentPut(new Document(docType, parseInfo.documentId));
    new VespaJsonDocumentReader().readPut(parseInfo.fieldsBuffer, put);
    Document doc = put.getDocument();
    FieldValue f = doc.getFieldValue("actualMapStringToArrayOfInt");
    assertSame(MapFieldValue.class, f.getClass());
    MapFieldValue<?, ?> m = (MapFieldValue<?, ?>) f;
    Array<?> a = (Array<?>) m.get(new StringFieldValue("bamse"));
    assertEquals(3, a.size());
    assertEquals(new IntegerFieldValue(1), a.get(0));
    assertEquals(new IntegerFieldValue(2), a.get(1));
    assertEquals(new IntegerFieldValue(3), a.get(2));
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DocumentPut(com.yahoo.document.DocumentPut) DocumentType(com.yahoo.document.DocumentType) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Document(com.yahoo.document.Document) DocumentParseInfo(com.yahoo.document.json.readers.DocumentParseInfo) VespaJsonDocumentReader(com.yahoo.document.json.readers.VespaJsonDocumentReader) Array(com.yahoo.document.datatypes.Array) ByteArrayInputStream(java.io.ByteArrayInputStream) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) TensorFieldValue(com.yahoo.document.datatypes.TensorFieldValue) MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) Test(org.junit.Test)

Example 9 with Array

use of com.yahoo.document.datatypes.Array in project vespa by vespa-engine.

the class JsonReaderTestCase method testOldAssignToArray.

@Test
public final void testOldAssignToArray() throws IOException {
    DocumentUpdate doc = parseUpdate("{\"update\": \"id:unittest:testMapStringToArrayOfInt::whee\"," + " \"fields\": { \"actualMapStringToArrayOfInt\": {" + " \"assign\": [" + "{ \"key\": \"bamse\", \"value\": [1, 2, 3] }" + "]}}}");
    FieldUpdate f = doc.getFieldUpdate("actualMapStringToArrayOfInt");
    assertEquals(1, f.size());
    AssignValueUpdate assign = (AssignValueUpdate) f.getValueUpdate(0);
    MapFieldValue<?, ?> m = (MapFieldValue<?, ?>) assign.getValue();
    Array<?> a = (Array<?>) m.get(new StringFieldValue("bamse"));
    assertEquals(3, a.size());
    assertEquals(new IntegerFieldValue(1), a.get(0));
    assertEquals(new IntegerFieldValue(2), a.get(1));
    assertEquals(new IntegerFieldValue(3), a.get(2));
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) Array(com.yahoo.document.datatypes.Array) DocumentUpdate(com.yahoo.document.DocumentUpdate) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldUpdate(com.yahoo.document.update.FieldUpdate) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate) Test(org.junit.Test)

Example 10 with Array

use of com.yahoo.document.datatypes.Array in project vespa by vespa-engine.

the class JsonReaderTestCase method testAssignToArray.

@Test
public final void testAssignToArray() throws IOException {
    DocumentUpdate doc = parseUpdate("{\"update\": \"id:unittest:testMapStringToArrayOfInt::whee\"," + " \"fields\": { \"actualMapStringToArrayOfInt\": {" + " \"assign\": { \"bamse\": [1, 2, 3] }}}}");
    FieldUpdate f = doc.getFieldUpdate("actualMapStringToArrayOfInt");
    assertEquals(1, f.size());
    AssignValueUpdate assign = (AssignValueUpdate) f.getValueUpdate(0);
    MapFieldValue<?, ?> m = (MapFieldValue<?, ?>) assign.getValue();
    Array<?> a = (Array<?>) m.get(new StringFieldValue("bamse"));
    assertEquals(3, a.size());
    assertEquals(new IntegerFieldValue(1), a.get(0));
    assertEquals(new IntegerFieldValue(2), a.get(1));
    assertEquals(new IntegerFieldValue(3), a.get(2));
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) Array(com.yahoo.document.datatypes.Array) DocumentUpdate(com.yahoo.document.DocumentUpdate) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldUpdate(com.yahoo.document.update.FieldUpdate) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate) Test(org.junit.Test)

Aggregations

Array (com.yahoo.document.datatypes.Array)34 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)27 FieldValue (com.yahoo.document.datatypes.FieldValue)20 Test (org.junit.Test)17 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)16 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)9 Struct (com.yahoo.document.datatypes.Struct)9 WeightedSet (com.yahoo.document.datatypes.WeightedSet)9 Document (com.yahoo.document.Document)8 DocumentPut (com.yahoo.document.DocumentPut)7 DoubleFieldValue (com.yahoo.document.datatypes.DoubleFieldValue)7 StructDataType (com.yahoo.document.StructDataType)6 ByteFieldValue (com.yahoo.document.datatypes.ByteFieldValue)6 FloatFieldValue (com.yahoo.document.datatypes.FloatFieldValue)6 LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)6 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)5 DocumentType (com.yahoo.document.DocumentType)4 Field (com.yahoo.document.Field)4 TensorFieldValue (com.yahoo.document.datatypes.TensorFieldValue)4 SimpleTestAdapter (com.yahoo.vespa.indexinglanguage.SimpleTestAdapter)4