aboutsummaryrefslogtreecommitdiffstats
path: root/docprocs
diff options
context:
space:
mode:
authorTor Brede Vekterli <vekterli@oath.com>2018-06-06 13:02:19 +0200
committerTor Brede Vekterli <vekterli@oath.com>2018-06-06 15:26:01 +0200
commit613bbb29cc6f683d0116e967c939c65f404b7128 (patch)
treeba2b8a862540cd4f3a22f905de49469486339793 /docprocs
parent389801098797ab37c7bc4ac5a3888ef4d92214e7 (diff)
Pass field path updates verbatim through indexing scripts
Avoids having to do a (potentially lossy) round-trip of update -> document -> update again during processing.
Diffstat (limited to 'docprocs')
-rw-r--r--docprocs/src/test/java/com/yahoo/docprocs/indexing/DocumentScriptTestCase.java83
1 files changed, 74 insertions, 9 deletions
diff --git a/docprocs/src/test/java/com/yahoo/docprocs/indexing/DocumentScriptTestCase.java b/docprocs/src/test/java/com/yahoo/docprocs/indexing/DocumentScriptTestCase.java
index 5b1a4412b41..ed88667a14a 100644
--- a/docprocs/src/test/java/com/yahoo/docprocs/indexing/DocumentScriptTestCase.java
+++ b/docprocs/src/test/java/com/yahoo/docprocs/indexing/DocumentScriptTestCase.java
@@ -1,11 +1,13 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.docprocs.indexing;
+import com.yahoo.document.ArrayDataType;
import com.yahoo.document.DataType;
import com.yahoo.document.Document;
import com.yahoo.document.DocumentType;
import com.yahoo.document.DocumentUpdate;
import com.yahoo.document.Field;
+import com.yahoo.document.MapDataType;
import com.yahoo.document.StructDataType;
import com.yahoo.document.annotation.SpanTree;
import com.yahoo.document.annotation.SpanTrees;
@@ -16,6 +18,7 @@ import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.document.datatypes.Struct;
import com.yahoo.document.datatypes.WeightedSet;
import com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate;
+import com.yahoo.document.fieldpathupdate.FieldPathUpdate;
import com.yahoo.document.update.FieldUpdate;
import com.yahoo.document.update.MapValueUpdate;
import com.yahoo.document.update.ValueUpdate;
@@ -30,6 +33,7 @@ import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@@ -76,7 +80,7 @@ public class DocumentScriptTestCase {
out = (StringFieldValue)processFieldUpdate(in).getValue();
assertSpanTrees(out, "mySpanTree");
- out = (StringFieldValue)processPathUpdate(in).getValue();
+ out = (StringFieldValue)processPathUpdate(in);
assertSpanTrees(out, "mySpanTree");
}
@@ -93,7 +97,7 @@ public class DocumentScriptTestCase {
assertEquals(1, out.size());
assertSpanTrees(out.get(0), "mySpanTree");
- out = (Array<StringFieldValue>)processPathUpdate(in).getValue();
+ out = (Array<StringFieldValue>)processPathUpdate(in);
assertEquals(1, out.size());
assertSpanTrees(out.get(0), "mySpanTree");
}
@@ -111,7 +115,7 @@ public class DocumentScriptTestCase {
assertEquals(1, out.size());
assertSpanTrees(out.keySet().iterator().next(), "mySpanTree");
- out = (WeightedSet<StringFieldValue>)processPathUpdate(in).getValue();
+ out = (WeightedSet<StringFieldValue>)processPathUpdate(in);
assertEquals(1, out.size());
assertSpanTrees(out.keySet().iterator().next(), "mySpanTree");
}
@@ -134,7 +138,7 @@ public class DocumentScriptTestCase {
assertSpanTrees(out.keySet().iterator().next(), "myKeySpanTree");
assertSpanTrees(out.values().iterator().next(), "myValueSpanTree");
- out = (MapFieldValue<StringFieldValue, StringFieldValue>)processPathUpdate(in).getValue();
+ out = (MapFieldValue<StringFieldValue, StringFieldValue>)processPathUpdate(in);
assertEquals(1, out.size());
assertSpanTrees(out.keySet().iterator().next(), "myKeySpanTree");
assertSpanTrees(out.values().iterator().next(), "myValueSpanTree");
@@ -157,6 +161,62 @@ public class DocumentScriptTestCase {
assertSpanTrees(str, "mySpanTree");
}
+ private class FieldPathFixture {
+ final DocumentType type;
+ final StructDataType structType;
+ final DataType structMap;
+ final DataType structArray;
+
+ FieldPathFixture() {
+ type = newDocumentType();
+ structType = new StructDataType("mystruct");
+ structType.addField(new Field("title", DataType.STRING));
+ structType.addField(new Field("rating", DataType.INT));
+ structArray = new ArrayDataType(structType);
+ type.addField(new Field("structarray", structArray));
+ structMap = new MapDataType(DataType.STRING, structType);
+ type.addField(new Field("structmap", structMap));
+ }
+
+ FieldPathUpdate executeWithUpdate(String fieldName, FieldPathUpdate updateIn) {
+ DocumentUpdate update = new DocumentUpdate(type, "doc:scheme:");
+ update.addFieldPathUpdate(updateIn);
+ update = newScript(type, fieldName).execute(ADAPTER_FACTORY, update);
+ assertEquals(1, update.getFieldPathUpdates().size());
+ return update.getFieldPathUpdates().get(0);
+ }
+ }
+
+ @Test
+ public void array_field_path_updates_survive_indexing_scripts() {
+ FieldPathFixture f = new FieldPathFixture();
+
+ Struct newElemValue = new Struct(f.structType);
+ newElemValue.setFieldValue("title", "iron moose 2, the moosening");
+
+ FieldPathUpdate updated = f.executeWithUpdate("structarray", new AssignFieldPathUpdate(f.type, "structarray[10]", newElemValue));
+
+ assertTrue(updated instanceof AssignFieldPathUpdate);
+ AssignFieldPathUpdate assignUpdate = (AssignFieldPathUpdate)updated;
+ assertEquals("structarray[10]", assignUpdate.getOriginalFieldPath());
+ assertEquals(newElemValue, assignUpdate.getFieldValue());
+ }
+
+ @Test
+ public void map_field_path_updates_survive_indexing_scripts() {
+ FieldPathFixture f = new FieldPathFixture();
+
+ Struct newElemValue = new Struct(f.structType);
+ newElemValue.setFieldValue("title", "iron moose 3, moose in new york");
+
+ FieldPathUpdate updated = f.executeWithUpdate("structmap", new AssignFieldPathUpdate(f.type, "structmap{foo}", newElemValue));
+
+ assertTrue(updated instanceof AssignFieldPathUpdate);
+ AssignFieldPathUpdate assignUpdate = (AssignFieldPathUpdate)updated;
+ assertEquals("structmap{foo}", assignUpdate.getOriginalFieldPath());
+ assertEquals(newElemValue, assignUpdate.getFieldValue());
+ }
+
private static FieldValue processDocument(FieldValue fieldValue) {
DocumentType docType = new DocumentType("myDocumentType");
docType.addField("myField", fieldValue.getDataType());
@@ -175,20 +235,24 @@ public class DocumentScriptTestCase {
return update.getFieldUpdate("myField").getValueUpdate(0);
}
- private static ValueUpdate<?> processPathUpdate(FieldValue fieldValue) {
+ private static FieldValue processPathUpdate(FieldValue fieldValue) {
DocumentType docType = new DocumentType("myDocumentType");
docType.addField("myField", fieldValue.getDataType());
DocumentUpdate update = new DocumentUpdate(docType, "doc:scheme:");
update.addFieldPathUpdate(new AssignFieldPathUpdate(docType, "myField", fieldValue));
update = newScript(docType).execute(ADAPTER_FACTORY, update);
- return update.getFieldUpdate("myField").getValueUpdate(0);
+ return ((AssignFieldPathUpdate)update.getFieldPathUpdates().get(0)).getFieldValue();
+ }
+
+ private static DocumentScript newScript(DocumentType docType, String fieldName) {
+ return new DocumentScript(docType.getName(), Collections.singletonList(fieldName),
+ new StatementExpression(new InputExpression(fieldName),
+ new IndexExpression(fieldName)));
}
private static DocumentScript newScript(DocumentType docType) {
String fieldName = docType.getFields().iterator().next().getName();
- return new DocumentScript(docType.getName(), Arrays.asList(fieldName),
- new StatementExpression(new InputExpression(fieldName),
- new IndexExpression(fieldName)));
+ return newScript(docType, fieldName);
}
private static StringFieldValue newString(String... spanTrees) {
@@ -210,6 +274,7 @@ public class DocumentScriptTestCase {
DocumentType type = new DocumentType("documentType");
type.addField("documentField", DataType.STRING);
type.addField("extraField", DataType.STRING);
+
return type;
}