Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
srinjoyray committed Oct 28, 2024
1 parent dc4c3f1 commit b58db82
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ public Index compositeIndexForQuery(Query query) {
}

public Set<Index> compositeIndexesForQuery(Query query) {
List<DatastoreV3Pb.Query> pbQueries =
List<DatastoreV3Pb.Query.Builder> pbQueries =
convertQueryToPbs(query, FetchOptions.Builder.withDefaults());
Set<Index> resultSet = new HashSet<Index>();
for (DatastoreV3Pb.Query queryProto : pbQueries) {
for (DatastoreV3Pb.Query.Builder queryProto : pbQueries) {
IndexComponentsOnlyQuery indexQuery = new IndexComponentsOnlyQuery(queryProto);

OnestoreEntity.Index.Builder index =
Expand All @@ -281,7 +281,7 @@ public Index minimumCompositeIndexForQuery(Query query, Collection<Index> indexe
}

public Set<Index> minimumCompositeIndexesForQuery(Query query, Collection<Index> indexes) {
List<DatastoreV3Pb.Query> pbQueries =
List<DatastoreV3Pb.Query.Builder> pbQueries =
convertQueryToPbs(query, FetchOptions.Builder.withDefaults());

List<OnestoreEntity.Index> indexPbs = Lists.newArrayListWithCapacity(indexes.size());
Expand All @@ -290,7 +290,7 @@ public Set<Index> minimumCompositeIndexesForQuery(Query query, Collection<Index>
}

Set<Index> resultSet = new HashSet<Index>();
for (DatastoreV3Pb.Query queryProto : pbQueries) {
for (DatastoreV3Pb.Query.Builder queryProto : pbQueries) {
IndexComponentsOnlyQuery indexQuery = new IndexComponentsOnlyQuery(queryProto);

OnestoreEntity.Index.Builder index =
Expand All @@ -304,19 +304,19 @@ public Set<Index> minimumCompositeIndexesForQuery(Query query, Collection<Index>

/** Convert a query to a list of ProtocolBuffer Queries. */
@SuppressWarnings("deprecation")
private static List<DatastoreV3Pb.Query> convertQueryToPbs(
private static List<DatastoreV3Pb.Query.Builder> convertQueryToPbs(
Query query, FetchOptions fetchOptions) {
List<MultiQueryBuilder> queriesToRun = QuerySplitHelper.splitQuery(query);
// All Filters should be in queriesToRun
query.setFilter(null);
query.getFilterPredicates().clear();
List<DatastoreV3Pb.Query> resultQueries = new ArrayList<DatastoreV3Pb.Query>();
List<DatastoreV3Pb.Query.Builder> resultQueries = new ArrayList<DatastoreV3Pb.Query.Builder>();
for (MultiQueryBuilder multiQuery : queriesToRun) {
for (List<List<FilterPredicate>> parallelQueries : multiQuery) {
for (List<FilterPredicate> singleQuery : parallelQueries) {
Query newQuery = new Query(query);
newQuery.getFilterPredicates().addAll(singleQuery);
DatastoreV3Pb.Query queryProto = QueryTranslator.convertToPb(newQuery, fetchOptions);
DatastoreV3Pb.Query.Builder queryProto = QueryTranslator.convertToPb(newQuery, fetchOptions);
resultQueries.add(queryProto);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ protected TransactionImpl.InternalTransaction doBeginTransaction(TransactionOpti

Future<DatastoreV3Pb.Transaction> future =
DatastoreApiHelper.makeAsyncCall(
apiConfig, DatastoreService_3.Method.BeginTransaction, request.build(), remoteTxn.build());
apiConfig, DatastoreService_3.Method.BeginTransaction, request.build(), remoteTxn.buildPartial());

return new InternalTransactionV3(apiConfig, request.getApp(), future);
}
Expand Down Expand Up @@ -527,7 +527,7 @@ public Future<KeyRange> allocateIds(final Key parent, final String kind, long nu
req.setModelKey(allocateIdsRef);
AllocateIdsResponse.Builder resp = AllocateIdsResponse.newBuilder();
Future<AllocateIdsResponse> future =
makeAsyncCall(apiConfig, DatastoreService_3.Method.AllocateIds, req.build(), resp.build());
makeAsyncCall(apiConfig, DatastoreService_3.Method.AllocateIds, req.build(), resp.buildPartial());
return new FutureWrapper<AllocateIdsResponse, KeyRange>(future) {
@Override
protected KeyRange wrap(AllocateIdsResponse resp) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ class EqPropsAndAncestorConstraint {
*/
protected static class IndexComponentsOnlyQuery
extends com.google.appengine.api.datastore.IndexComponentsOnlyQuery {
public IndexComponentsOnlyQuery(DatastoreV3Pb.Query query) {
public IndexComponentsOnlyQuery(DatastoreV3Pb.Query.Builder query) {
super(query);
}
}
Expand All @@ -372,7 +372,7 @@ public IndexComponentsOnlyQuery(DatastoreV3Pb.Query query) {
* publicly exposing it in the api.
*/
protected static class ValidatedQuery extends com.google.appengine.api.datastore.ValidatedQuery {
public ValidatedQuery(DatastoreV3Pb.Query query) {
public ValidatedQuery(DatastoreV3Pb.Query.Builder query) {
super(query);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ private static void addListPropertyToPb(
// If the value is indexed it appears in queries, but distinction between
// null and empty list is lost.
}
property.setValue(PropertyValue.getDefaultInstance());
property.getValue(); // Indicate to the proto that we have set this field
if (indexed) {
proto.addProperty(property);
Expand Down Expand Up @@ -395,6 +396,7 @@ private static Property buildImplicitKeyProperty(EntityProto proto) {
PropertyValue.Builder propVal = PropertyValue.newBuilder();
propVal.setReferenceValue(KeyType.toReferenceValue(proto.getKey()));
keyProp.setValue(propVal.build());
keyProp.setMultiple(false);
return keyProp.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class IndexComponentsOnlyQuery extends ValidatedQuery {

private boolean hasKeyProperty = false;

public IndexComponentsOnlyQuery(DatastoreV3Pb.Query query) {
public IndexComponentsOnlyQuery(DatastoreV3Pb.Query.Builder query) {
super(query);
removeNativelySupportedComponents();
categorizeQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.apphosting.api.AppEngineInternal;
import com.google.storage.onestore.v3.proto2api.OnestoreEntity;
import com.google.storage.onestore.v3.proto2api.OnestoreEntity.CompositeIndex.State;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -73,6 +74,7 @@ public static Index.Property convertFromPb(OnestoreEntity.Index.Property propert
}

public static Index convertFromPb(OnestoreEntity.Index index) {
return convertFromPb(OnestoreEntity.CompositeIndex.newBuilder().setId(0).setDefinition(index).build());
return convertFromPb(OnestoreEntity.CompositeIndex.newBuilder().setId(0).setDefinition(index).setAppId("").setState(
State.WRITE_ONLY).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class NormalizedQuery {

protected final Query.Builder query;

public NormalizedQuery(Query query) {
this.query = query.toBuilder().clone();
public NormalizedQuery(Query.Builder query) {
this.query = query.clone();
normalizeQuery();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public NextRequest buildNextCallPrototype(QueryResult initialResult) {
req.setCompile(true);
}
// This used to call .freeze() but that method has been deleted, see go/javaproto1freezeremoval
return req.build();
return req.buildPartial();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected DatastoreV3Pb.QueryResult wrap(DatastoreV3Pb.QueryResult result)
}

private void addMissingIndexData(DatastoreV3Pb.Query queryProto, DatastoreNeedIndexException e) {
IndexComponentsOnlyQuery indexQuery = new IndexComponentsOnlyQuery(queryProto);
IndexComponentsOnlyQuery indexQuery = new IndexComponentsOnlyQuery(queryProto.toBuilder());
CompositeIndexManager mgr = new CompositeIndexManager();
OnestoreEntity.Index.Builder index = mgr.compositeIndexForQuery(indexQuery);
if (index != null) {
Expand All @@ -94,7 +94,7 @@ private void addMissingIndexData(DatastoreV3Pb.Query queryProto, DatastoreNeedIn
}

private DatastoreV3Pb.Query.Builder convertToPb(Query q, Transaction txn, FetchOptions fetchOptions) {
DatastoreV3Pb.Query.Builder queryProto = QueryTranslator.convertToPb(q, fetchOptions).toBuilder();
DatastoreV3Pb.Query.Builder queryProto = QueryTranslator.convertToPb(q, fetchOptions);
if (txn != null) {
TransactionImpl.ensureTxnActive(txn);
queryProto.setTransaction(InternalTransactionV3.toProto(txn));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.apphosting.datastore.proto2api.DatastoreV3Pb.Query.Order;
import com.google.apphosting.datastore.proto2api.DatastoreV3Pb.Query.Order.Direction;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.storage.onestore.v3.proto2api.OnestoreEntity.Property;
import com.google.storage.onestore.v3.proto2api.OnestoreEntity.PropertyValue;
import com.google.storage.onestore.v3.proto2api.OnestoreEntity.Reference;
import java.util.List;
Expand All @@ -35,7 +36,7 @@
final class QueryTranslator {

@SuppressWarnings("deprecation")
public static DatastoreV3Pb.Query convertToPb(Query query, FetchOptions fetchOptions) {
public static DatastoreV3Pb.Query.Builder convertToPb(Query query, FetchOptions fetchOptions) {
Key ancestor = query.getAncestor();
List<Query.SortPredicate> sortPredicates = query.getSortPredicates();

Expand Down Expand Up @@ -133,7 +134,7 @@ public static DatastoreV3Pb.Query convertToPb(Query query, FetchOptions fetchOpt
proto.addPropertyName(projection.getPropertyName());
}

return proto.build();
return proto;
}

static Order convertSortPredicateToPb(Query.SortPredicate predicate) {
Expand Down Expand Up @@ -204,13 +205,15 @@ private static Filter convertFilterPredicateToPb(Query.FilterPredicate predicate
filterPb
.addPropertyBuilder()
.setName(predicate.getPropertyName())
.setValue(DataTypeTranslator.toV3Value(value));
.setValue(DataTypeTranslator.toV3Value(value))
.setMultiple(false);
}
} else {
filterPb
.addPropertyBuilder()
.setName(predicate.getPropertyName())
.setValue(DataTypeTranslator.toV3Value(predicate.getValue()));
.setValue(DataTypeTranslator.toV3Value(predicate.getValue()))
.setMultiple(false);
}

return filterPb.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ValidatedQuery extends NormalizedQuery {
private boolean isGeo;

/** @throws IllegalQueryException If the provided query fails validation. */
ValidatedQuery(Query query) {
ValidatedQuery(Query.Builder query) {
super(query);
validateQuery();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ public static synchronized LocalCompositeIndexManager getInstance() {
* index file.
*/
public void processQuery(DatastoreV3Pb.Query query) {
IndexComponentsOnlyQuery indexOnlyQuery = new IndexComponentsOnlyQuery(query);
IndexComponentsOnlyQuery indexOnlyQuery = new IndexComponentsOnlyQuery(query.toBuilder());
boolean isNewQuery = updateQueryHistory(indexOnlyQuery);
if (isNewQuery) {
maybeUpdateIndexFile(indexOnlyQuery);
Expand Down Expand Up @@ -956,7 +956,7 @@ Map<Index, Integer> buildIndexMapFromQueryHistory() {

/** Get the single composite index used by this query, if any, as a list. */
public List<Index> queryIndexList(DatastoreV3Pb.Query query) {
IndexComponentsOnlyQuery indexOnlyQuery = new IndexComponentsOnlyQuery(query);
IndexComponentsOnlyQuery indexOnlyQuery = new IndexComponentsOnlyQuery(query.toBuilder());
Index.Builder index = compositeIndexForQuery(indexOnlyQuery);
List<Index> indexList;
if (index != null) {
Expand Down Expand Up @@ -993,7 +993,7 @@ protected Index.Builder minimumCompositeIndexForQuery(

/** Aliasing to make the class available in the package. */
protected static class ValidatedQuery extends CompositeIndexManager.ValidatedQuery {
protected ValidatedQuery(DatastoreV3Pb.Query query) {
protected ValidatedQuery(DatastoreV3Pb.Query.Builder query) {
super(query);
}

Expand All @@ -1010,7 +1010,7 @@ private KeyTranslator() {}
/** Aliasing to make the class available in the package. */
protected static class IndexComponentsOnlyQuery
extends CompositeIndexManager.IndexComponentsOnlyQuery {
protected IndexComponentsOnlyQuery(DatastoreV3Pb.Query query) {
protected IndexComponentsOnlyQuery(DatastoreV3Pb.Query.Builder query) {
super(query);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ public QueryResult runQuery(Status status, Query query) {
// Construct a validated query right away so we can fail fast
// if something is wrong.
final LocalCompositeIndexManager.ValidatedQuery validatedQuery =
new LocalCompositeIndexManager.ValidatedQuery(query);
new LocalCompositeIndexManager.ValidatedQuery(query.toBuilder());
query = validatedQuery.getV3Query();

// Modernize the query's cursors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.common.util.concurrent.Futures.immediateFuture;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.notNull;
Expand Down Expand Up @@ -202,7 +203,7 @@ public void testNewEntityBuilder() {
public void testCompositeIndexForQuery() {
Query query = new Query("kind1");
FetchOptions fo = FetchOptions.Builder.withDefaults();
DatastoreV3Pb.Query queryPb = QueryTranslator.convertToPb(query, fo);
DatastoreV3Pb.Query.Builder queryPb = QueryTranslator.convertToPb(query, fo);
CompositeIndexManager.IndexComponentsOnlyQuery ic =
new CompositeIndexManager.IndexComponentsOnlyQuery(queryPb);
Index index =
Expand All @@ -212,15 +213,19 @@ public void testCompositeIndexForQuery() {
true,
ImmutableList.of(new Index.Property("p1", Query.SortDirection.DESCENDING)));
OnestoreEntity.Index indexPb = IndexTranslator.convertToPb(index);
when(indexManager.compositeIndexForQuery(ic)).thenReturn(indexPb.toBuilder());
when(indexManager.compositeIndexForQuery(any(CompositeIndexManager.IndexComponentsOnlyQuery.class)))
.thenReturn(indexPb.toBuilder());
assertThat(adminDsWithMockDelegate.compositeIndexForQuery(query)).isEqualTo(index);
}

// indexManager = compositeIndexManager@4659
// ic = compositeIndexManager$IndexComponentOnlyQuery@4655
// queryPb = DatastoreV3Pb$Query$Builder@3648
// query = Query@3645
@Test
public void testMinimumCompositeIndexForQuery() {
Query query = new Query("kind1");
FetchOptions fo = FetchOptions.Builder.withDefaults();
DatastoreV3Pb.Query queryPb = QueryTranslator.convertToPb(query, fo);
DatastoreV3Pb.Query.Builder queryPb = QueryTranslator.convertToPb(query, fo);
CompositeIndexManager.IndexComponentsOnlyQuery ic =
new CompositeIndexManager.IndexComponentsOnlyQuery(queryPb);
Index index =
Expand Down
Loading

0 comments on commit b58db82

Please sign in to comment.