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));
}
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));
}
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;
}
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);
}
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);
}
}
Aggregations