Progressed on port from dotnet to java

This commit is contained in:
David Noble
2019-09-08 22:24:12 -07:00
parent ff8d32a8b5
commit 2db3d8d517
3 changed files with 49 additions and 53 deletions

View File

@@ -1041,8 +1041,8 @@ public final class RowReader {
/** /**
* Reads a generic schematized field value via the scope's layout * Reads a generic schematized field value via the scope's layout
* *
* @param value On success, receives the value, undefined otherwise. * @param value On success, receives the value, undefined otherwise
* @return Success if the read is successful, an error code otherwise. * @return {@link Result#SUCCESS} if the read is successful; an error {@link Result} otherwise
*/ */
private Result readPrimitiveValue(Out<Utf8String> value) { private Result readPrimitiveValue(Out<Utf8String> value) {

View File

@@ -4,10 +4,11 @@
package com.azure.data.cosmos.serialization.hybridrow.io; package com.azure.data.cosmos.serialization.hybridrow.io;
import com.azure.data.cosmos.core.Out; import com.azure.data.cosmos.core.Out;
import com.azure.data.cosmos.core.Reference;
import com.azure.data.cosmos.serialization.hybridrow.Result; import com.azure.data.cosmos.serialization.hybridrow.Result;
import javax.annotation.Nonnull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
public final class RowReaderExtensions { public final class RowReaderExtensions {
/** /**
@@ -20,80 +21,75 @@ public final class RowReaderExtensions {
* @param list On success, the collection of materialized items. * @param list On success, the collection of materialized items.
* @return The result. * @return The result.
*/ */
public static <TItem> Result ReadList(Reference<RowReader> reader, DeserializerFunc<TItem> deserializer, @Nonnull
Out<ArrayList<TItem>> list) { public static <TItem> Result readList(RowReader reader, DeserializerFunc<TItem> deserializer, Out<List<TItem>> list) {
// Pass the context as a struct by value to avoid allocations.
ListContext<TItem> ctx = new ListContext<TItem>();
ctx.List = new ArrayList<>();
ctx.Deserializer =
(Reference<RowReader> reader.argValue, Out<TItem> item) -> deserializer.invoke(reader.get().clone(), item);
// All lambda's here are static. // Pass the context as a struct by value to avoid allocations
// TODO: C# TO JAVA CONVERTER: The following lambda contained an unresolved 'ref' keyword - these are not
// converted by C# to Java Converter: final ListContext<TItem> context = new ListContext<TItem>(deserializer, new ArrayList<TItem>());
Result r = reader.get().readScope(ctx.clone(), (RowReader RowReader arrayReader, ListContext<TItem> ctx1) -> final Out<TItem> item = new Out<>();
{
while (arrayReader.Read()) { Result result = reader.readScope(context, (arrayReader, arrayContext) -> {
Result r2 = arrayReader.ReadScope(ctx1.clone(), (ref RowReader itemReader, ListContext<TItem> ctx2) -> while (arrayReader.read()) {
{ Result arrayResult = arrayReader.readScope(arrayContext, (itemReader, itemContext) -> {
Reference<com.azure.data.cosmos.serialization.hybridrow.io.RowReader> tempReference_itemReader = new Reference<com.azure.data.cosmos.serialization.hybridrow.io.RowReader>(itemReader); Result itemResult = itemContext.deserializer().invoke(itemReader, item);
TItem op; if (itemResult != Result.SUCCESS) {
Out<TItem> tempOut_op = new Out<TItem>(); return itemResult;
Result r3 = ctx2.Deserializer.invoke(tempReference_itemReader, tempOut_op);
op = tempOut_op.get();
itemReader = tempReference_itemReader.get();
if (r3 != Result.SUCCESS) {
return r3;
} }
itemContext.items().add(item.get());
ctx2.List.add(op);
return Result.SUCCESS; return Result.SUCCESS;
}); });
if (arrayResult != Result.SUCCESS) {
if (r2 != Result.SUCCESS) { return arrayResult;
return r2;
} }
} }
return Result.SUCCESS; return Result.SUCCESS;
}); });
if (r != Result.SUCCESS) { if (result != Result.SUCCESS) {
list.setAndGet(null); list.set(null);
return r; return result;
} }
list.setAndGet(ctx.List); list.set(context.items());
return Result.SUCCESS; return Result.SUCCESS;
} }
/** /**
* A function to read content from a {@link RowReader}. * A functional interface to read content from a {@link RowReader}
* <typeparam name="TItem">The type of the item to read.</typeparam> *
* @param <TItem> 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 @FunctionalInterface
public interface DeserializerFunc<TItem> { public interface DeserializerFunc<TItem> {
Result invoke(Reference<RowReader> reader, Out<TItem> 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<TItem> 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<TItem>
private final static class ListContext<TItem> { private final static class ListContext<TItem> {
public DeserializerFunc<TItem> Deserializer;
public ArrayList<TItem> List;
public ListContext clone() { private final DeserializerFunc<TItem> deserializer;
ListContext varCopy = new ListContext(); private final List<TItem> items;
varCopy.List = this.List; ListContext(DeserializerFunc<TItem> deserializer, List<TItem> items) {
varCopy.Deserializer = this.Deserializer; this.deserializer = deserializer;
this.items = items;
}
return varCopy; public DeserializerFunc<TItem> deserializer() {
return this.deserializer;
}
public List<TItem> items() {
return this.items;
} }
} }
} }

View File

@@ -278,7 +278,7 @@ public final class SerializerUnitTest {
java.util.ArrayList<BatchOperation> operations; java.util.ArrayList<BatchOperation> operations;
Out<ArrayList<TItem>> tempOut_operations = new Out<ArrayList<TItem>>(); Out<ArrayList<TItem>> tempOut_operations = new Out<ArrayList<TItem>>();
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(); operations = tempOut_operations.get();
if (r != Result.SUCCESS) { if (r != Result.SUCCESS) {
request.setAndGet(null); request.setAndGet(null);