From 416f596b150ec159717bfd2f9b2ef70e4d4cd3dd Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Sat, 14 Jan 2023 18:41:49 +0100 Subject: Support direct tensor rendering --- .../document/json/JsonSerializationHelper.java | 4 +- .../java/com/yahoo/document/json/JsonWriter.java | 63 +++++++++++----------- .../document/serialization/DocumentWriter.java | 11 ++-- 3 files changed, 38 insertions(+), 40 deletions(-) (limited to 'document/src/main/java/com') 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 not 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 ravishar + * @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); + } -- cgit v1.2.3