From 2db3d8d517050af1e3f86be55b56ee2a1ea1093e Mon Sep 17 00:00:00 2001 From: David Noble Date: Sun, 8 Sep 2019 22:24:12 -0700 Subject: [PATCH] Progressed on port from dotnet to java --- .../serialization/hybridrow/io/RowReader.java | 4 +- .../hybridrow/io/RowReaderExtensions.java | 96 +++++++++---------- .../hybridrow/unit/SerializerUnitTest.java | 2 +- 3 files changed, 49 insertions(+), 53 deletions(-) diff --git a/java/src/main/java/com/azure/data/cosmos/serialization/hybridrow/io/RowReader.java b/java/src/main/java/com/azure/data/cosmos/serialization/hybridrow/io/RowReader.java index dcc9d5a..3c008c1 100644 --- a/java/src/main/java/com/azure/data/cosmos/serialization/hybridrow/io/RowReader.java +++ b/java/src/main/java/com/azure/data/cosmos/serialization/hybridrow/io/RowReader.java @@ -1041,8 +1041,8 @@ public final class RowReader { /** * Reads a generic schematized field value via the scope's layout * - * @param value On success, receives the value, undefined otherwise. - * @return Success if the read is successful, an error code otherwise. + * @param value On success, receives the value, undefined otherwise + * @return {@link Result#SUCCESS} if the read is successful; an error {@link Result} otherwise */ private Result readPrimitiveValue(Out value) { diff --git a/java/src/main/java/com/azure/data/cosmos/serialization/hybridrow/io/RowReaderExtensions.java b/java/src/main/java/com/azure/data/cosmos/serialization/hybridrow/io/RowReaderExtensions.java index caf001e..01ea6dc 100644 --- a/java/src/main/java/com/azure/data/cosmos/serialization/hybridrow/io/RowReaderExtensions.java +++ b/java/src/main/java/com/azure/data/cosmos/serialization/hybridrow/io/RowReaderExtensions.java @@ -4,10 +4,11 @@ package com.azure.data.cosmos.serialization.hybridrow.io; import com.azure.data.cosmos.core.Out; -import com.azure.data.cosmos.core.Reference; import com.azure.data.cosmos.serialization.hybridrow.Result; +import javax.annotation.Nonnull; import java.util.ArrayList; +import java.util.List; public final class RowReaderExtensions { /** @@ -20,80 +21,75 @@ public final class RowReaderExtensions { * @param list On success, the collection of materialized items. * @return The result. */ - public static Result ReadList(Reference reader, DeserializerFunc deserializer, - Out> list) { - // Pass the context as a struct by value to avoid allocations. - ListContext ctx = new ListContext(); - ctx.List = new ArrayList<>(); - ctx.Deserializer = - (Reference reader.argValue, Out item) -> deserializer.invoke(reader.get().clone(), item); + @Nonnull + public static Result readList(RowReader reader, DeserializerFunc deserializer, Out> list) { - // All lambda's here are static. - // TODO: C# TO JAVA CONVERTER: The following lambda contained an unresolved 'ref' keyword - these are not - // converted by C# to Java Converter: - Result r = reader.get().readScope(ctx.clone(), (RowReader RowReader arrayReader, ListContext ctx1) -> - { - while (arrayReader.Read()) { - Result r2 = arrayReader.ReadScope(ctx1.clone(), (ref RowReader itemReader, ListContext ctx2) -> - { - Reference tempReference_itemReader = new Reference(itemReader); - TItem op; - Out tempOut_op = new Out(); - Result r3 = ctx2.Deserializer.invoke(tempReference_itemReader, tempOut_op); - op = tempOut_op.get(); - itemReader = tempReference_itemReader.get(); - if (r3 != Result.SUCCESS) { - return r3; + // Pass the context as a struct by value to avoid allocations + + final ListContext context = new ListContext(deserializer, new ArrayList()); + final Out item = new Out<>(); + + Result result = reader.readScope(context, (arrayReader, arrayContext) -> { + while (arrayReader.read()) { + Result arrayResult = arrayReader.readScope(arrayContext, (itemReader, itemContext) -> { + Result itemResult = itemContext.deserializer().invoke(itemReader, item); + if (itemResult != Result.SUCCESS) { + return itemResult; } - - ctx2.List.add(op); + itemContext.items().add(item.get()); return Result.SUCCESS; }); - - if (r2 != Result.SUCCESS) { - return r2; + if (arrayResult != Result.SUCCESS) { + return arrayResult; } } - return Result.SUCCESS; }); - if (r != Result.SUCCESS) { - list.setAndGet(null); - return r; + if (result != Result.SUCCESS) { + list.set(null); + return result; } - list.setAndGet(ctx.List); + list.set(context.items()); return Result.SUCCESS; } /** - * A function to read content from a {@link RowReader}. - * The type of the item to read. + * A functional interface to read content from a {@link RowReader} + * + * @param The type of item to read * - * @param reader A forward-only cursor for reading the item. - * @param item On success, the item read. - * @return The result. */ @FunctionalInterface public interface DeserializerFunc { - Result invoke(Reference reader, Out item); + /** + * Read a row from a {@link RowReader} + * + * @param reader A forward-only cursor for reading the item + * @param item On success, the item read + * @return The result + */ + @Nonnull + Result invoke(@Nonnull RowReader reader, @Nonnull Out item); } - //C# TO JAVA CONVERTER WARNING: Java does not allow user-defined value types. The behavior of this class may - // differ from the original: - //ORIGINAL LINE: private struct ListContext private final static class ListContext { - public DeserializerFunc Deserializer; - public ArrayList List; - public ListContext clone() { - ListContext varCopy = new ListContext(); + private final DeserializerFunc deserializer; + private final List items; - varCopy.List = this.List; - varCopy.Deserializer = this.Deserializer; + ListContext(DeserializerFunc deserializer, List items) { + this.deserializer = deserializer; + this.items = items; + } - return varCopy; + public DeserializerFunc deserializer() { + return this.deserializer; + } + + public List items() { + return this.items; } } } \ No newline at end of file diff --git a/java/src/test/java/com/azure/data/cosmos/serialization/hybridrow/unit/SerializerUnitTest.java b/java/src/test/java/com/azure/data/cosmos/serialization/hybridrow/unit/SerializerUnitTest.java index e409d0d..eed49ce 100644 --- a/java/src/test/java/com/azure/data/cosmos/serialization/hybridrow/unit/SerializerUnitTest.java +++ b/java/src/test/java/com/azure/data/cosmos/serialization/hybridrow/unit/SerializerUnitTest.java @@ -278,7 +278,7 @@ public final class SerializerUnitTest { java.util.ArrayList operations; Out> tempOut_operations = new Out>(); - Result r = RowReaderExtensions.ReadList(reader.get().clone(), BatchOperationSerializer.Read, tempOut_operations); + Result r = RowReaderExtensions.readList(reader.get().clone(), BatchOperationSerializer.Read, tempOut_operations); operations = tempOut_operations.get(); if (r != Result.SUCCESS) { request.setAndGet(null);