Search in sources :

Example 21 with DocumentUpdate

use of com.yahoo.document.DocumentUpdate 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)

Example 22 with DocumentUpdate

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

the class JsonReaderTestCase method testArrayIndexing.

@SuppressWarnings("rawtypes")
@Test
public final void testArrayIndexing() throws IOException {
    DocumentUpdate doc = parseUpdate("{\"update\": \"id:unittest:testarray::whee\"," + " \"fields\": { " + "\"actualarray\": {" + " \"match\": {" + " \"element\": 3," + " \"assign\": \"nalle\"}}}}");
    Map<Number, String> matches = new HashMap<>();
    FieldUpdate x = doc.getFieldUpdate("actualarray");
    for (ValueUpdate v : x.getValueUpdates()) {
        MapValueUpdate adder = (MapValueUpdate) v;
        final Number key = ((IntegerFieldValue) adder.getValue()).getNumber();
        String op = ((StringFieldValue) ((AssignValueUpdate) adder.getUpdate()).getValue()).getString();
        matches.put(key, op);
    }
    assertEquals(1, matches.size());
    Number n = Integer.valueOf(3);
    assertEquals("nalle", matches.get(n));
}
Also used : AddValueUpdate(com.yahoo.document.update.AddValueUpdate) MapValueUpdate(com.yahoo.document.update.MapValueUpdate) ValueUpdate(com.yahoo.document.update.ValueUpdate) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate) ArithmeticValueUpdate(com.yahoo.document.update.ArithmeticValueUpdate) ClearValueUpdate(com.yahoo.document.update.ClearValueUpdate) MapValueUpdate(com.yahoo.document.update.MapValueUpdate) DocumentUpdate(com.yahoo.document.DocumentUpdate) HashMap(java.util.HashMap) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldUpdate(com.yahoo.document.update.FieldUpdate) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Test(org.junit.Test)

Example 23 with DocumentUpdate

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

the class VespaXmlUpdateReaderTestCase method readUpdateHelper.

private static DocumentUpdate readUpdateHelper(Field field, String documentXml) throws Exception {
    DocumentTypeManager docManager = new DocumentTypeManager();
    DocumentType docType = new DocumentType("my_type");
    if (field != null) {
        docType.addField(field);
    }
    docManager.register(docType);
    InputStream in = new ByteArrayInputStream(documentXml.getBytes(StandardCharsets.UTF_8));
    DocumentUpdate doc = new DocumentUpdate(docType, "doc:scheme:");
    VespaXMLUpdateReader reader = new VespaXMLUpdateReader(in, docManager);
    // initialize reader
    reader.reader.next();
    reader.read(doc);
    return doc;
}
Also used : DocumentUpdate(com.yahoo.document.DocumentUpdate) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DocumentTypeManager(com.yahoo.document.DocumentTypeManager) DocumentType(com.yahoo.document.DocumentType)

Example 24 with DocumentUpdate

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

the class DocumentUpdateJsonSerializerTest method deSerializeAndSerializeJsonAndMatch.

private static void deSerializeAndSerializeJsonAndMatch(String jsonDoc) {
    jsonDoc = jsonDoc.replaceFirst("DOCUMENT_ID", DEFAULT_DOCUMENT_ID);
    DocumentUpdate update = deSerializeDocumentUpdate(jsonDoc, DEFAULT_DOCUMENT_ID);
    assertJsonEquals(serializeDocumentUpdate(update), jsonDoc);
}
Also used : DocumentUpdate(com.yahoo.document.DocumentUpdate)

Example 25 with DocumentUpdate

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

the class VespaXMLFeedReader method read.

/* (non-Javadoc)
     * @see com.yahoo.vespaxmlparser.FeedReader#read(com.yahoo.vespaxmlparser.VespaXMLFeedReader.Operation)
     */
@Override
public void read(Operation operation) throws Exception {
    String startTag = null;
    operation.setInvalid();
    try {
        while (reader.hasNext()) {
            int type = reader.next();
            if (type == XMLStreamReader.START_ELEMENT) {
                startTag = reader.getName().toString();
                if ("document".equals(startTag)) {
                    VespaXMLDocumentReader documentReader = new VespaXMLDocumentReader(reader, docTypeManager);
                    Document document = new Document(documentReader);
                    operation.setDocument(document);
                    operation.setCondition(TestAndSetCondition.fromConditionString(documentReader.getCondition()));
                    return;
                } else if ("update".equals(startTag)) {
                    VespaXMLUpdateReader updateReader = new VespaXMLUpdateReader(reader, docTypeManager);
                    DocumentUpdate update = new DocumentUpdate(updateReader);
                    operation.setDocumentUpdate(update);
                    operation.setCondition(TestAndSetCondition.fromConditionString(updateReader.getCondition()));
                    return;
                } else if ("remove".equals(startTag)) {
                    boolean documentIdFound = false;
                    Optional<String> condition = Optional.empty();
                    for (int i = 0; i < reader.getAttributeCount(); i++) {
                        final String attributeName = reader.getAttributeName(i).toString();
                        if ("documentid".equals(attributeName) || "id".equals(attributeName)) {
                            operation.setRemove(new DocumentId(reader.getAttributeValue(i)));
                            documentIdFound = true;
                        } else if ("condition".equals(attributeName)) {
                            condition = Optional.of(reader.getAttributeValue(i));
                        }
                    }
                    if (!documentIdFound) {
                        throw newDeserializeException("Missing \"documentid\" attribute for remove operation");
                    }
                    operation.setCondition(TestAndSetCondition.fromConditionString(condition));
                    return;
                } else {
                    throw newDeserializeException("Element \"" + startTag + "\" not allowed in this context");
                }
            }
        }
    } catch (XMLStreamException e) {
        throw (e);
    // Skip to end of current tag with other exceptions.
    } catch (Exception e) {
        try {
            if (startTag != null) {
                skipToEnd(startTag);
            }
        } catch (Exception ignore) {
        }
        throw (e);
    }
}
Also used : DocumentUpdate(com.yahoo.document.DocumentUpdate) XMLStreamException(javax.xml.stream.XMLStreamException) DocumentId(com.yahoo.document.DocumentId) Document(com.yahoo.document.Document) XMLStreamException(javax.xml.stream.XMLStreamException)

Aggregations

DocumentUpdate (com.yahoo.document.DocumentUpdate)43 Test (org.junit.Test)24 FieldUpdate (com.yahoo.document.update.FieldUpdate)16 DocumentType (com.yahoo.document.DocumentType)15 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)12 AssignValueUpdate (com.yahoo.document.update.AssignValueUpdate)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 InputStream (java.io.InputStream)9 Document (com.yahoo.document.Document)8 DocumentOperation (com.yahoo.document.DocumentOperation)8 DocumentPut (com.yahoo.document.DocumentPut)7 DocumentRemove (com.yahoo.document.DocumentRemove)6 DocumentId (com.yahoo.document.DocumentId)5 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)5 AddValueUpdate (com.yahoo.document.update.AddValueUpdate)5 ArithmeticValueUpdate (com.yahoo.document.update.ArithmeticValueUpdate)5 ClearValueUpdate (com.yahoo.document.update.ClearValueUpdate)5 MapValueUpdate (com.yahoo.document.update.MapValueUpdate)5 ValueUpdate (com.yahoo.document.update.ValueUpdate)5 HashMap (java.util.HashMap)5