use of com.yahoo.document.update.FieldUpdate in project vespa by vespa-engine.
the class DocumentUpdateTestCase method requireThatArrayOfStructElementAddIsProcessedCorrectly.
@Test
public void requireThatArrayOfStructElementAddIsProcessedCorrectly() throws ParseException {
DocumentType docType = new DocumentType("my_input");
docType.addField(new Field("my_str", DataType.getArray(DataType.STRING)));
docType.addField(new Field("my_pos", DataType.getArray(PositionDataType.INSTANCE)));
DocumentUpdate docUpdate = new DocumentUpdate(docType, "doc:scheme:");
docUpdate.addFieldUpdate(FieldUpdate.createAdd(docType.getField("my_str"), new StringFieldValue("6;9")));
docUpdate = Expression.execute(Expression.fromString("input my_str | for_each { to_pos } | index my_pos"), docUpdate);
assertNotNull(docUpdate);
assertEquals(0, docUpdate.getFieldPathUpdates().size());
assertEquals(1, docUpdate.getFieldUpdates().size());
FieldUpdate fieldUpd = docUpdate.getFieldUpdate(0);
assertNotNull(fieldUpd);
assertEquals(docType.getField("my_pos"), fieldUpd.getField());
assertEquals(1, fieldUpd.getValueUpdates().size());
ValueUpdate valueUpd = fieldUpd.getValueUpdate(0);
assertNotNull(valueUpd);
assertTrue(valueUpd instanceof AddValueUpdate);
Object val = valueUpd.getValue();
assertNotNull(val);
assertTrue(val instanceof Struct);
Struct pos = (Struct) val;
assertEquals(PositionDataType.INSTANCE, pos.getDataType());
assertEquals(new IntegerFieldValue(6), PositionDataType.getXValue(pos));
assertEquals(new IntegerFieldValue(9), PositionDataType.getYValue(pos));
}
use of com.yahoo.document.update.FieldUpdate in project vespa by vespa-engine.
the class GuardTestCase method requireThatInputFieldsAreIncludedByUpdate.
@Test
public void requireThatInputFieldsAreIncludedByUpdate() throws ParseException {
DocumentType docType = new DocumentType("my_input");
docType.addField(new Field("my_lng", DataType.LONG));
docType.addField(new Field("my_str", DataType.STRING));
DocumentUpdate docUpdate = new DocumentUpdate(docType, "doc:scheme:");
docUpdate.addFieldUpdate(FieldUpdate.createAssign(docType.getField("my_str"), new StringFieldValue("69")));
assertNotNull(docUpdate = Expression.execute(Expression.fromString("guard { input my_str | to_int | attribute my_lng }"), docUpdate));
assertEquals(0, docUpdate.getFieldPathUpdates().size());
assertEquals(1, docUpdate.getFieldUpdates().size());
FieldUpdate fieldUpd = docUpdate.getFieldUpdate(0);
assertNotNull(fieldUpd);
assertEquals(docType.getField("my_lng"), fieldUpd.getField());
assertEquals(1, fieldUpd.getValueUpdates().size());
ValueUpdate valueUpd = fieldUpd.getValueUpdate(0);
assertNotNull(valueUpd);
assertTrue(valueUpd instanceof AssignValueUpdate);
assertEquals(new LongFieldValue(69), valueUpd.getValue());
}
use of com.yahoo.document.update.FieldUpdate in project vespa by vespa-engine.
the class SchemaMappingAndAccessesTest method testMappedDocUpdateAPI.
public void testMappedDocUpdateAPI() {
Document doc = getDoc();
DocumentType type = doc.getDataType();
DocumentUpdate dud = new DocumentUpdate(type, new DocumentId("doc:map:test:1"));
FieldUpdate assignSingle = FieldUpdate.createAssign(type.getField("title"), new StringFieldValue("something"));
Map<String, String> fieldMap = new HashMap<>();
fieldMap.put("t", "title");
fieldMap.put("a", "artist");
ProxyDocumentUpdate pup = new ProxyDocumentUpdate(dud, fieldMap);
pup.addFieldUpdate(assignSingle);
assertEquals(pup.getFieldUpdates(), dud.getFieldUpdates());
assertEquals(pup.getDocumentType(), dud.getDocumentType());
assertEquals(pup.getFieldUpdate(new com.yahoo.document.Field("title")).size(), 1);
assertEquals(pup.getFieldUpdate(0), dud.getFieldUpdate(0));
assertEquals(pup.getFieldUpdate("title"), dud.getFieldUpdate("title"));
assertEquals(pup.getId(), dud.getId());
assertEquals(pup.getType(), dud.getType());
assertEquals(pup.applyTo(doc), dud);
assertEquals(doc.getFieldValue("title").getWrappedValue(), "something");
assertEquals(pup, dud);
assertEquals(pup.hashCode(), dud.hashCode());
assertEquals(pup.toString(), dud.toString());
assertEquals(pup.size(), dud.size());
assertEquals(pup.getWrappedDocumentOperation().getId().toString(), "doc:map:test:1");
}
use of com.yahoo.document.update.FieldUpdate in project vespa by vespa-engine.
the class DocumentUpdate method addFieldUpdate.
/**
* Adds the given {@link FieldUpdate} to this DocumentUpdate. If this DocumentUpdate already contains a FieldUpdate
* for the named field, the content of the given FieldUpdate is added to the existing one.
*
* @param update The FieldUpdate to add to this DocumentUpdate.
* @return This, to allow chaining.
* @throws IllegalArgumentException If the {@link DocumentType} of this DocumentUpdate does not have a corresponding
* field.
*/
public DocumentUpdate addFieldUpdate(FieldUpdate update) {
String fieldName = update.getField().getName();
if (!documentType.hasField(fieldName)) {
throw new IllegalArgumentException("Document type '" + documentType.getName() + "' does not have field '" + fieldName + "'.");
}
FieldUpdate prevUpdate = getFieldUpdate(fieldName);
if (prevUpdate != update) {
if (prevUpdate != null) {
prevUpdate.addAll(update);
} else {
fieldUpdates.add(update);
}
}
return this;
}
use of com.yahoo.document.update.FieldUpdate in project vespa by vespa-engine.
the class SimpleAdapterFactory method newUpdateAdapterList.
@Override
public List<UpdateAdapter> newUpdateAdapterList(DocumentUpdate upd) {
List<UpdateAdapter> ret = new ArrayList<>();
DocumentType docType = upd.getDocumentType();
DocumentId docId = upd.getId();
Document complete = new Document(docType, upd.getId());
for (FieldPathUpdate fieldUpd : upd) {
if (FieldPathUpdateHelper.isComplete(fieldUpd)) {
FieldPathUpdateHelper.applyUpdate(fieldUpd, complete);
} else {
Document partial = FieldPathUpdateHelper.newPartialDocument(docId, fieldUpd);
ret.add(new FieldPathUpdateAdapter(newDocumentAdapter(partial, true), fieldUpd));
}
}
for (FieldUpdate fieldUpd : upd.getFieldUpdates()) {
Field field = fieldUpd.getField();
for (ValueUpdate valueUpd : fieldUpd.getValueUpdates()) {
if (FieldUpdateHelper.isComplete(field, valueUpd)) {
FieldUpdateHelper.applyUpdate(field, valueUpd, complete);
} else {
Document partial = FieldUpdateHelper.newPartialDocument(docType, docId, field, valueUpd);
ret.add(FieldUpdateAdapter.fromPartialUpdate(expressionSelector.selectExpression(docType, field.getName()), newDocumentAdapter(partial, true), valueUpd));
}
}
}
ret.add(FieldUpdateAdapter.fromCompleteUpdate(newDocumentAdapter(complete, true)));
return ret;
}
Aggregations