aboutsummaryrefslogtreecommitdiffstats
path: root/document
diff options
context:
space:
mode:
authorTor Egge <Tor.Egge@broadpark.no>2016-08-16 15:01:54 +0200
committerGitHub <noreply@github.com>2016-08-16 15:01:54 +0200
commitf79b7fc2200f1a4959ed36ea63365db8e41d5a47 (patch)
treedcfc7e7c0a10f0b104ebb6566cbceb933791a79e /document
parent504448751720a23df0d6d8b94ca838ed7c72977c (diff)
parent97d78392c671f48a8f7e49f11b131cd7ec83aab6 (diff)
Merge pull request #417 from yahoo/geirst/add-support-for-assign-and-clear-update-for-tensor-fields
Geirst/add support for assign and clear update for tensor fields
Diffstat (limited to 'document')
-rw-r--r--document/src/main/java/com/yahoo/document/json/JsonReader.java8
-rw-r--r--document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java56
-rw-r--r--document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java61
3 files changed, 95 insertions, 30 deletions
diff --git a/document/src/main/java/com/yahoo/document/json/JsonReader.java b/document/src/main/java/com/yahoo/document/json/JsonReader.java
index e5402d617bd..bdd5a4b1922 100644
--- a/document/src/main/java/com/yahoo/document/json/JsonReader.java
+++ b/document/src/main/java/com/yahoo/document/json/JsonReader.java
@@ -275,7 +275,6 @@ public class JsonReader {
}
private void addFieldUpdates(DocumentUpdate update, Field field) {
- validateFieldUpdates(update, field);
int localNesting = buffer.nesting();
FieldUpdate fieldUpdate = FieldUpdate.create(field);
@@ -300,13 +299,6 @@ public class JsonReader {
update.addFieldUpdate(fieldUpdate);
}
- private static void validateFieldUpdates(DocumentUpdate update, Field field) {
- if (field.getDataType() == DataType.TENSOR) {
- throw new IllegalArgumentException("Updates to fields of type TENSOR is not yet supported ("
- + "id='" + update.getId().toString() + "', field='" + field.getName() + "')");
- }
- }
-
@SuppressWarnings("rawtypes")
private ValueUpdate createMapUpdate(Field field) {
buffer.next();
diff --git a/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java b/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java
index 0e7313991f8..c16ffd2d54c 100644
--- a/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java
+++ b/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java
@@ -9,6 +9,7 @@ import com.yahoo.document.update.AssignValueUpdate;
import com.yahoo.document.update.FieldUpdate;
import com.yahoo.document.update.ValueUpdate;
import com.yahoo.io.GrowableByteBuffer;
+import com.yahoo.tensor.MapTensor;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -41,6 +42,13 @@ public class DocumentUpdateTestCase extends junit.framework.TestCase {
FieldUpdate addMultiList = null;
FieldUpdate addMultiWset = null;
+ private final String documentId = "doc:something:foooo";
+ private final String tensorField = "tensorfield";
+
+ private Document createDocument() {
+ return new Document(docMan.getDocumentType("foobar"), new DocumentId(documentId));
+ }
+
public void setUp() {
docMan = new DocumentTypeManager();
@@ -52,6 +60,8 @@ public class DocumentUpdateTestCase extends junit.framework.TestCase {
DataType stringwset = DataType.getWeightedSet(DataType.STRING);
docType.addField(new Field("strwset", stringwset));
+
+ docType.addField(new Field(tensorField, DataType.TENSOR));
docMan.register(docType);
docType2 = new DocumentType("otherdoctype");
@@ -101,7 +111,7 @@ public class DocumentUpdateTestCase extends junit.framework.TestCase {
}
public void testApplyRemoveSingle() {
- Document doc = new Document(docMan.getDocumentType("foobar"), new DocumentId("doc:something:foooo"));
+ Document doc = createDocument();
assertNull(doc.getFieldValue("strfoo"));
doc.setFieldValue("strfoo", new StringFieldValue("cocacola"));
assertEquals(new StringFieldValue("cocacola"), doc.getFieldValue("strfoo"));
@@ -111,7 +121,7 @@ public class DocumentUpdateTestCase extends junit.framework.TestCase {
}
public void testApplyRemoveMultiList() {
- Document doc = new Document(docMan.getDocumentType("foobar"), new DocumentId("doc:something:foooo"));
+ Document doc = createDocument();
assertNull(doc.getFieldValue("strarray"));
Array<StringFieldValue> strArray = new Array<>(DataType.getArray(DataType.STRING));
strArray.add(new StringFieldValue("hello hello"));
@@ -126,7 +136,7 @@ public class DocumentUpdateTestCase extends junit.framework.TestCase {
}
public void testApplyRemoveMultiWset() {
- Document doc = new Document(docMan.getDocumentType("foobar"), new DocumentId("doc:something:foooo"));
+ Document doc = createDocument();
assertNull(doc.getFieldValue("strwset"));
WeightedSet<StringFieldValue> strwset = new WeightedSet<>(doc.getDataType().getField("strwset").getDataType());
strwset.put(new StringFieldValue("hello hello"), 10);
@@ -141,7 +151,7 @@ public class DocumentUpdateTestCase extends junit.framework.TestCase {
}
public void testApplyAssignSingle() {
- Document doc = new Document(docMan.getDocumentType("foobar"), new DocumentId("doc:something:foooo"));
+ Document doc = createDocument();
assertNull(doc.getFieldValue("strfoo"));
docUp.addFieldUpdate(assignSingle);
docUp.applyTo(doc);
@@ -149,7 +159,7 @@ public class DocumentUpdateTestCase extends junit.framework.TestCase {
}
public void testApplyAssignMultiList() {
- Document doc = new Document(docMan.getDocumentType("foobar"), new DocumentId("doc:something:foooo"));
+ Document doc = createDocument();
assertNull(doc.getFieldValue("strarray"));
Array<StringFieldValue> strArray = new Array<>(DataType.getArray(DataType.STRING));
strArray.add(new StringFieldValue("hello hello"));
@@ -165,7 +175,7 @@ public class DocumentUpdateTestCase extends junit.framework.TestCase {
}
public void testApplyAssignMultiWlist() {
- Document doc = new Document(docMan.getDocumentType("foobar"), new DocumentId("doc:something:foooo"));
+ Document doc = createDocument();
assertNull(doc.getFieldValue("strwset"));
WeightedSet<StringFieldValue> strwset = new WeightedSet<>(doc.getDataType().getField("strwset").getDataType());
strwset.put(new StringFieldValue("hello hello"), 164);
@@ -185,7 +195,7 @@ public class DocumentUpdateTestCase extends junit.framework.TestCase {
}
public void testApplyAddMultiList() {
- Document doc = new Document(docMan.getDocumentType("foobar"), new DocumentId("doc:something:foooo"));
+ Document doc = createDocument();
assertNull(doc.getFieldValue("strarray"));
docUp.addFieldUpdate(addMultiList);
@@ -198,7 +208,7 @@ public class DocumentUpdateTestCase extends junit.framework.TestCase {
}
public void testApplyAddMultiWset() {
- Document doc = new Document(docMan.getDocumentType("foobar"), new DocumentId("doc:something:foooo"));
+ Document doc = createDocument();
assertNull(doc.getFieldValue("strwset"));
WeightedSet<StringFieldValue> wset = new WeightedSet<>(doc.getDataType().getField("strwset").getDataType());
@@ -574,4 +584,34 @@ public class DocumentUpdateTestCase extends junit.framework.TestCase {
assertEquals(docUp, deserialized);
assertTrue(deserialized.getCreateIfNonExistent());
}
+
+ private static TensorFieldValue createTensorFieldValue(String tensor) {
+ return new TensorFieldValue(MapTensor.from(tensor));
+ }
+
+ public void testThatAssignValueUpdateForTensorFieldCanBeApplied() {
+ Document doc = createDocument();
+ assertNull(doc.getFieldValue(tensorField));
+
+ DocumentUpdate update = new DocumentUpdate(docType, new DocumentId(documentId));
+ update.addFieldUpdate(FieldUpdate.createAssign(docType.getField(tensorField),
+ createTensorFieldValue("{{x:0}:2.0}")));
+ update.applyTo(doc);
+
+ TensorFieldValue tensor = (TensorFieldValue) doc.getFieldValue(tensorField);
+ assertEquals(createTensorFieldValue("{{x:0}:2.0}"), tensor);
+ }
+
+ public void testThatClearValueUpdateForTensorFieldCanBeApplied() {
+ Document doc = createDocument();
+ doc.setFieldValue(docType.getField(tensorField), createTensorFieldValue("{{x:0}:2.0}"));
+ assertNotNull(doc.getFieldValue(tensorField));
+
+ DocumentUpdate update = new DocumentUpdate(docType, new DocumentId(documentId));
+ update.addFieldUpdate(FieldUpdate.createClear(docType.getField(tensorField)));
+ update.applyTo(doc);
+
+ assertNull(doc.getFieldValue(tensorField));
+ }
+
}
diff --git a/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java b/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java
index 860b2c9fba8..8236a32e94c 100644
--- a/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java
+++ b/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java
@@ -1131,6 +1131,31 @@ public class JsonReaderTestCase {
}
@Test
+ public void testAssignUpdateOfEmptyTensor() {
+ assertTensorAssignUpdate("{}", createAssignUpdateWithTensor("{}"));
+ }
+
+ @Test
+ public void testAssignUpdateOfNullTensor() {
+ ClearValueUpdate clearUpdate = (ClearValueUpdate) getTensorField(createAssignUpdateWithTensor(null)).getValueUpdate(0);
+ assertTrue(clearUpdate != null);
+ assertTrue(clearUpdate.getValue() == null);
+ }
+
+ @Test
+ public void testAssignUpdateOfTensorWithCells() {
+ assertTensorAssignUpdate("{{x:a,y:b}:2.0,{x:c}:3.0}}",
+ createAssignUpdateWithTensor("{ "
+ + " \"cells\": [ "
+ + " { \"address\": { \"x\": \"a\", \"y\": \"b\" }, "
+ + " \"value\": 2.0 }, "
+ + " { \"address\": { \"x\": \"c\" }, "
+ + " \"value\": 3.0 } "
+ + " ]"
+ + "}"));
+ }
+
+ @Test
public void require_that_parser_propagates_datatype_parser_errors_predicate() {
assertParserErrorMatches(
"Error in document 'id:unittest:testpredicate::0' - could not parse field 'boolean' of type 'predicate': " +
@@ -1182,20 +1207,6 @@ public class JsonReaderTestCase {
}
@Test
- public void requireThatUpdatesForTensorFieldsAreNotSupported() {
- try {
- InputStream rawDoc = new ByteArrayInputStream(
- Utf8.toBytes("[ { \"update\": \"" + TENSOR_DOC_ID + "\", \"fields\": { \"tensorfield\": {"
- + "\"assign\": {} } } } ]"));
- new JsonReader(types, rawDoc, parserFactory).next();
- assertTrue("Exception not thrown", false);
- } catch (IllegalArgumentException e) {
- assertEquals("Updates to fields of type TENSOR is not yet supported (id='id:unittest:testtensor::0', field='tensorfield')",
- e.getMessage());
- }
- }
-
- @Test
public void requireThatUnknownDocTypeThrowsIllegalArgumentException() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage(new Contains("Document type walrus does not exist"));
@@ -1233,6 +1244,14 @@ public class JsonReaderTestCase {
return (DocumentPut) reader.next();
}
+ private DocumentUpdate createAssignUpdateWithTensor(String inputTensor) {
+ InputStream rawDoc = new ByteArrayInputStream(
+ Utf8.toBytes("[ { \"update\": \"" + TENSOR_DOC_ID + "\", \"fields\": { \"tensorfield\": {"
+ + "\"assign\": " + (inputTensor != null ? inputTensor : "null") + " } } } ]"));
+ JsonReader reader = new JsonReader(types, rawDoc, parserFactory);
+ return (DocumentUpdate) reader.next();
+ }
+
private static void assertTensorField(String expectedTensor, DocumentPut put) {
final Document doc = put.getDocument();
assertEquals("testtensor", doc.getId().getDocType());
@@ -1241,6 +1260,20 @@ public class JsonReaderTestCase {
assertEquals(MapTensor.from(expectedTensor), fieldValue.getTensor().get());
}
+ private static void assertTensorAssignUpdate(String expectedTensor, DocumentUpdate update) {
+ assertEquals("testtensor", update.getId().getDocType());
+ assertEquals(TENSOR_DOC_ID, update.getId().toString());
+ AssignValueUpdate assignUpdate = (AssignValueUpdate) getTensorField(update).getValueUpdate(0);
+ TensorFieldValue fieldValue = (TensorFieldValue) assignUpdate.getValue();
+ assertEquals(MapTensor.from(expectedTensor), fieldValue.getTensor().get());
+ }
+
+ private static FieldUpdate getTensorField(DocumentUpdate update) {
+ FieldUpdate fieldUpdate = update.getFieldUpdate("tensorfield");
+ assertEquals(1, fieldUpdate.size());
+ return fieldUpdate;
+ }
+
// NOTE: Do not call this method multiple times from a test method as it's using the ExpectedException rule
private void assertParserErrorMatches(String expectedError, String... json) {
exception.expect(JsonReaderException.class);