aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2023-01-14 18:41:49 +0100
committerJon Bratseth <bratseth@gmail.com>2023-01-14 18:41:49 +0100
commit416f596b150ec159717bfd2f9b2ef70e4d4cd3dd (patch)
treefd78cf0541670dd50e2dc3256c5b9755ced8f73e /document/src/main/java/com
parenta289581cbf94ff6997356110b54bd6993e956b9e (diff)
Support direct tensor rendering
Diffstat (limited to 'document/src/main/java/com')
-rw-r--r--document/src/main/java/com/yahoo/document/json/JsonSerializationHelper.java4
-rw-r--r--document/src/main/java/com/yahoo/document/json/JsonWriter.java63
-rw-r--r--document/src/main/java/com/yahoo/document/serialization/DocumentWriter.java11
3 files changed, 38 insertions, 40 deletions
diff --git a/document/src/main/java/com/yahoo/document/json/JsonSerializationHelper.java b/document/src/main/java/com/yahoo/document/json/JsonSerializationHelper.java
index a3cda4034ac..110564bea46 100644
--- a/document/src/main/java/com/yahoo/document/json/JsonSerializationHelper.java
+++ b/document/src/main/java/com/yahoo/document/json/JsonSerializationHelper.java
@@ -75,12 +75,12 @@ public class JsonSerializationHelper {
}
public static void serializeTensorField(JsonGenerator generator, FieldBase field, TensorFieldValue value,
- boolean shortForm, boolean valueOnly) {
+ boolean shortForm, boolean directValues) {
wrapIOException(() -> {
fieldNameIfNotNull(generator, field);
if (value.getTensor().isPresent()) {
Tensor tensor = value.getTensor().get();
- byte[] encoded = shortForm ? JsonFormat.encodeShortForm(tensor) : JsonFormat.encodeWithType(tensor);
+ byte[] encoded = JsonFormat.encode(tensor, shortForm, directValues);
generator.writeRawValue(new String(encoded, StandardCharsets.UTF_8));
}
else {
diff --git a/document/src/main/java/com/yahoo/document/json/JsonWriter.java b/document/src/main/java/com/yahoo/document/json/JsonWriter.java
index 27a1dd150f3..33243ab832c 100644
--- a/document/src/main/java/com/yahoo/document/json/JsonWriter.java
+++ b/document/src/main/java/com/yahoo/document/json/JsonWriter.java
@@ -78,30 +78,20 @@ public class JsonWriter implements DocumentWriter {
private final JsonGenerator generator;
private final boolean tensorShortForm;
+ private final boolean tensorDirectValues;
- // I really hate exception unsafe constructors, but the alternative
- // requires generator to not be a final
/**
+ * Creates a JsonWriter.
*
- * @param out
- * the target output stream
- * @throws RuntimeException
- * if unable to create the internal JSON generator
+ * @param out the target output stream
+ * @throws RuntimeException if unable to create the internal JSON generator
*/
public JsonWriter(OutputStream out) {
this(createPrivateGenerator(out));
}
- public JsonWriter(OutputStream out, boolean tensorShortForm) {
- this(createPrivateGenerator(out), tensorShortForm);
- }
-
- private static JsonGenerator createPrivateGenerator(OutputStream out) {
- try {
- return jsonFactory.createGenerator(out);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
+ public JsonWriter(OutputStream out, boolean tensorShortForm, boolean tensorDirectValues) {
+ this(createPrivateGenerator(out), tensorShortForm, tensorDirectValues);
}
/**
@@ -110,16 +100,26 @@ public class JsonWriter implements DocumentWriter {
* after having written a full Document instance. In other words, JsonWriter
* will <i>not</i> take ownership of the generator.
*
- * @param generator
- * the output JSON generator
+ * @param generator the output JSON generator
+ * @param tensorShortForm whether to use the short type-dependent form for tensor values
+ * @param tensorDirectValues whether to output tensor values directly or wrapped in a map also containing the type
*/
- public JsonWriter(JsonGenerator generator) {
- this(generator, false);
- }
-
- public JsonWriter(JsonGenerator generator, boolean tensorShortForm) {
+ public JsonWriter(JsonGenerator generator, boolean tensorShortForm, boolean tensorDirectValues) {
this.generator = generator;
this.tensorShortForm = tensorShortForm;
+ this.tensorDirectValues = tensorDirectValues;
+ }
+
+ private static JsonGenerator createPrivateGenerator(OutputStream out) {
+ try {
+ return jsonFactory.createGenerator(out);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public JsonWriter(JsonGenerator generator) {
+ this(generator, false, false);
}
/**
@@ -128,8 +128,7 @@ public class JsonWriter implements DocumentWriter {
* updating this class. This implementation throws an exception if it is
* reached.
*
- * @throws UnsupportedOperationException
- * if invoked
+ * @throws UnsupportedOperationException if invoked
*/
@Override
public void write(FieldBase field, FieldValue value) {
@@ -217,7 +216,7 @@ public class JsonWriter implements DocumentWriter {
@Override
public void write(FieldBase field, TensorFieldValue value) {
- serializeTensorField(generator, field, value, tensorShortForm, false);
+ serializeTensorField(generator, field, value, tensorShortForm, tensorDirectValues);
}
@Override
@@ -265,12 +264,14 @@ public class JsonWriter implements DocumentWriter {
* Utility method to easily serialize a single document.
*
* @param document the document to be serialized
- * @param tensorShortForm whether tensors should be serialized in short form
+ * @param tensorShortForm whether tensors should be serialized in a type-dependent short form
+ * @param tensorDirectValues whether tensors should be serialized as direct values or wrapped in a
+ * map also containing the type
* @return the input document serialised as UTF-8 encoded JSON
*/
- public static byte[] toByteArray(Document document, boolean tensorShortForm) {
+ public static byte[] toByteArray(Document document, boolean tensorShortForm, boolean tensorDirectValues) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
- JsonWriter writer = new JsonWriter(out, tensorShortForm);
+ JsonWriter writer = new JsonWriter(out, tensorShortForm, tensorDirectValues);
writer.write(document);
return out.toByteArray();
}
@@ -282,8 +283,8 @@ public class JsonWriter implements DocumentWriter {
* @return the input document serialised as UTF-8 encoded JSON
*/
public static byte[] toByteArray(Document document) {
- // TODO Vespa 9: change tensorShortForm default to true
- return toByteArray(document, false);
+ // TODO Vespa 9: change tensorShortForm and tensorDirectValues default to true
+ return toByteArray(document, false, false);
}
/**
diff --git a/document/src/main/java/com/yahoo/document/serialization/DocumentWriter.java b/document/src/main/java/com/yahoo/document/serialization/DocumentWriter.java
index b9e67a65a8d..2d31a6b6734 100644
--- a/document/src/main/java/com/yahoo/document/serialization/DocumentWriter.java
+++ b/document/src/main/java/com/yahoo/document/serialization/DocumentWriter.java
@@ -6,18 +6,15 @@ import com.yahoo.document.DocumentId;
import com.yahoo.document.DocumentType;
/**
- * @author <a href="mailto:ravishar@yahoo-inc.com">ravishar</a>
+ * @author ravishar
*/
public interface DocumentWriter extends FieldWriter {
- /**
- * write out a document
- *
- * @param document
- * document to be written
- */
+
+ /** Writes a document. */
void write(Document document);
void write(DocumentId id);
void write(DocumentType type);
+
}