Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return results from getPartitions() in order #653

Merged
merged 6 commits into from Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -33,7 +33,9 @@
import io.opencensus.common.Scope;
import io.opencensus.trace.Span;
import io.opencensus.trace.Status;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -63,6 +65,8 @@ public class CollectionGroup extends Query {
* parallel. The returned partition cursors are split points that can be used as starting/end
* points for the query results.
*
* @deprecated This method is deprecated as the SDK has to post-process all results streamed from
schmidt-sebastian marked this conversation as resolved.
Show resolved Hide resolved
* the server. Use {@link #getPartitions(long)} to get the same behavior.
* @param desiredPartitionCount The desired maximum number of partition points. The number must be
* strictly positive. The actual number of partitions returned may be fewer.
* @param observer a stream observer that receives the result of the Partition request.
Expand Down Expand Up @@ -159,8 +163,23 @@ private PartitionQueryRequest buildRequest(long desiredPartitionCount) {

private void consumePartitions(
PartitionQueryPagedResponse response, Function<QueryPartition, Void> consumer) {
@Nullable Object[] lastCursor = null;
List<Cursor> cursors = new ArrayList<>();
for (Cursor cursor : response.iterateAll()) {
cursors.add(cursor);
}

// Sort the partitions as they may not be ordered if responses are paged.
Collections.sort(
cursors,
new Comparator<Cursor>() {
@Override
public int compare(Cursor left, Cursor right) {
return Order.INSTANCE.compareArrays(left.getValuesList(), right.getValuesList());
}
});

@Nullable Object[] lastCursor = null;
for (Cursor cursor : cursors) {
Object[] decodedCursorValue = new Object[cursor.getValuesCount()];
for (int i = 0; i < cursor.getValuesCount(); ++i) {
decodedCursorValue[i] = UserDataConverter.decodeValue(rpcContext, cursor.getValues(i));
Expand Down
Expand Up @@ -109,7 +109,8 @@ public int compare(@Nonnull Value left, @Nonnull Value right) {
case GEO_POINT:
return compareGeoPoints(left, right);
case ARRAY:
return compareArrays(left, right);
return compareArrays(
left.getArrayValue().getValuesList(), right.getArrayValue().getValuesList());
case OBJECT:
return compareObjects(left, right);
default:
Expand Down Expand Up @@ -171,27 +172,22 @@ private int compareResourcePaths(Value left, Value right) {
return leftPath.compareTo(rightPath);
}

private int compareArrays(Value left, Value right) {
List<Value> leftValue = left.getArrayValue().getValuesList();
List<Value> rightValue = right.getArrayValue().getValuesList();

int minLength = Math.min(leftValue.size(), rightValue.size());
public int compareArrays(List<Value> left, List<Value> right) {
int minLength = Math.min(left.size(), right.size());
for (int i = 0; i < minLength; i++) {
int cmp = compare(leftValue.get(i), rightValue.get(i));
int cmp = compare(left.get(i), right.get(i));
if (cmp != 0) {
return cmp;
}
}
return Integer.compare(leftValue.size(), rightValue.size());
return Integer.compare(left.size(), right.size());
}

private int compareObjects(Value left, Value right) {
// This requires iterating over the keys in the object in order and doing a
// deep comparison.
SortedMap<String, Value> leftMap = new TreeMap<>();
leftMap.putAll(left.getMapValue().getFieldsMap());
SortedMap<String, Value> rightMap = new TreeMap<>();
rightMap.putAll(right.getMapValue().getFieldsMap());
SortedMap<String, Value> leftMap = new TreeMap<>(left.getMapValue().getFieldsMap());
SortedMap<String, Value> rightMap = new TreeMap<>(right.getMapValue().getFieldsMap());

Iterator<Entry<String, Value>> leftIterator = leftMap.entrySet().iterator();
Iterator<Entry<String, Value>> rightIterator = rightMap.entrySet().iterator();
Expand Down