Search in sources :

Example 6 with Index

use of com.google.storage.onestore.v3.OnestoreEntity.Index in project appengine-java-standard by GoogleCloudPlatform.

the class AdminDatastoreService method minimumCompositeIndexesForQuery.

public Set<Index> minimumCompositeIndexesForQuery(Query query, Collection<Index> indexes) {
    List<DatastoreV3Pb.Query> pbQueries = convertQueryToPbs(query, FetchOptions.Builder.withDefaults());
    List<OnestoreEntity.Index> indexPbs = Lists.newArrayListWithCapacity(indexes.size());
    for (Index index : indexes) {
        indexPbs.add(IndexTranslator.convertToPb(index));
    }
    Set<Index> resultSet = new HashSet<Index>();
    for (DatastoreV3Pb.Query queryProto : pbQueries) {
        IndexComponentsOnlyQuery indexQuery = new IndexComponentsOnlyQuery(queryProto);
        OnestoreEntity.Index index = factory.getCompositeIndexManager().minimumCompositeIndexForQuery(indexQuery, indexPbs);
        if (index != null) {
            resultSet.add(IndexTranslator.convertFromPb(index));
        }
    }
    return resultSet;
}
Also used : IndexComponentsOnlyQuery(com.google.appengine.api.datastore.CompositeIndexManager.IndexComponentsOnlyQuery) IndexComponentsOnlyQuery(com.google.appengine.api.datastore.CompositeIndexManager.IndexComponentsOnlyQuery) OnestoreEntity(com.google.storage.onestore.v3.OnestoreEntity) DatastoreV3Pb(com.google.apphosting.datastore.DatastoreV3Pb) HashSet(java.util.HashSet)

Example 7 with Index

use of com.google.storage.onestore.v3.OnestoreEntity.Index in project appengine-java-standard by GoogleCloudPlatform.

the class AdminDatastoreService method compositeIndexesForQuery.

public Set<Index> compositeIndexesForQuery(Query query) {
    List<DatastoreV3Pb.Query> pbQueries = convertQueryToPbs(query, FetchOptions.Builder.withDefaults());
    Set<Index> resultSet = new HashSet<Index>();
    for (DatastoreV3Pb.Query queryProto : pbQueries) {
        IndexComponentsOnlyQuery indexQuery = new IndexComponentsOnlyQuery(queryProto);
        OnestoreEntity.Index index = factory.getCompositeIndexManager().compositeIndexForQuery(indexQuery);
        if (index != null) {
            resultSet.add(IndexTranslator.convertFromPb(index));
        }
    }
    return resultSet;
}
Also used : IndexComponentsOnlyQuery(com.google.appengine.api.datastore.CompositeIndexManager.IndexComponentsOnlyQuery) IndexComponentsOnlyQuery(com.google.appengine.api.datastore.CompositeIndexManager.IndexComponentsOnlyQuery) OnestoreEntity(com.google.storage.onestore.v3.OnestoreEntity) DatastoreV3Pb(com.google.apphosting.datastore.DatastoreV3Pb) HashSet(java.util.HashSet)

Example 8 with Index

use of com.google.storage.onestore.v3.OnestoreEntity.Index in project appengine-java-standard by GoogleCloudPlatform.

the class AdminDatastoreServiceTest method testMinimumCompositeIndexWithOr.

@SuppressWarnings("deprecation")
@Test
public void testMinimumCompositeIndexWithOr() {
    Query query = new Query("kind1");
    query.setFilter(CompositeFilterOperator.and(CompositeFilterOperator.or(new FilterPredicate("a", FilterOperator.EQUAL, 1), new FilterPredicate("b", FilterOperator.EQUAL, 1)), new FilterPredicate("c", FilterOperator.LESS_THAN, 2)));
    Query firstQuery = new Query("kind1");
    firstQuery.addFilter("a", FilterOperator.EQUAL, 1);
    firstQuery.addFilter("c", FilterOperator.LESS_THAN, 2);
    Query secondQuery = new Query("kind1");
    secondQuery.addFilter("b", FilterOperator.EQUAL, 1);
    secondQuery.addFilter("c", FilterOperator.LESS_THAN, 2);
    Collection<Index> indexList = ImmutableList.of();
    Collection<OnestoreEntity.Index> indexPbList = ImmutableList.of();
    Index expectedFirstIndex = new Index(0, "kind1", true, Lists.newArrayList(new Index.Property("a", Query.SortDirection.DESCENDING), new Index.Property("c", Query.SortDirection.DESCENDING)));
    OnestoreEntity.Index expectedFirstIndexPb = IndexTranslator.convertToPb(expectedFirstIndex);
    Index expectedSecondIndex = new Index(0, "kind1", true, Lists.newArrayList(new Index.Property("b", Query.SortDirection.DESCENDING), new Index.Property("c", Query.SortDirection.DESCENDING)));
    OnestoreEntity.Index expectedSecondIndexPb = IndexTranslator.convertToPb(expectedSecondIndex);
    // We cannot predict the order of predicates in the IndexComponentsOnlyQuery.
    when(indexManager.minimumCompositeIndexForQuery(notNull(), eq(indexPbList))).thenReturn(expectedFirstIndexPb).thenReturn(expectedSecondIndexPb);
    Set<Index> minimumIndexes = adminDsWithMockDelegate.minimumCompositeIndexesForQuery(query, indexList);
    assertThat(minimumIndexes).containsExactly(expectedFirstIndex, expectedSecondIndex);
}
Also used : FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) OnestoreEntity(com.google.storage.onestore.v3.OnestoreEntity) Test(org.junit.Test)

Example 9 with Index

use of com.google.storage.onestore.v3.OnestoreEntity.Index in project appengine-java-standard by GoogleCloudPlatform.

the class LocalDatastoreService method createIndexOnlyQueryResults.

/**
 * Converts a normal result set into the results seen in an index-only query (a projection).
 *
 * @param queryEntities the results to convert
 * @param entityComparator the comparator derived from the query
 * @return the converted results
 */
private List<EntityProto> createIndexOnlyQueryResults(List<EntityProto> queryEntities, EntityProtoComparator entityComparator) {
    Set<String> postfixProps = Sets.newHashSetWithExpectedSize(entityComparator.getAdjustedOrders().size());
    for (Query.Order order : entityComparator.getAdjustedOrders()) {
        postfixProps.add(order.getProperty());
    }
    List<EntityProto> results = Lists.newArrayListWithExpectedSize(queryEntities.size());
    for (EntityProto entity : queryEntities) {
        List<EntityProto> indexEntities = createIndexEntities(entity, postfixProps, entityComparator);
        results.addAll(indexEntities);
    }
    return results;
}
Also used : Query(com.google.apphosting.datastore.DatastoreV3Pb.Query) CompiledQuery(com.google.apphosting.datastore.DatastoreV3Pb.CompiledQuery) Order(com.google.apphosting.datastore.DatastoreV3Pb.Query.Order) ByteString(com.google.protobuf.ByteString) EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto)

Example 10 with Index

use of com.google.storage.onestore.v3.OnestoreEntity.Index in project appengine-java-standard by GoogleCloudPlatform.

the class LocalCompositeIndexManager method buildIndexMapFromQueryHistory.

// @VisibleForTesting
Map<Index, Integer> buildIndexMapFromQueryHistory() {
    // LinkedHashMap gives us repeatable results in our tests and
    // minimizes file churn.
    Map<Index, Integer> indexMap = Maps.newLinkedHashMap();
    synchronized (queryHistory) {
        for (Map.Entry<IndexComponentsOnlyQuery, AtomicInteger> entry : queryHistory.entrySet()) {
            Index index = compositeIndexForQuery(entry.getKey());
            if (index == null) {
                // not interested in queries that don't need an index
                continue;
            }
            Integer count = indexMap.get(index);
            if (count == null) {
                count = 0;
            }
            count += entry.getValue().intValue();
            indexMap.put(index, count);
        }
    }
    return indexMap;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Index(com.google.storage.onestore.v3.OnestoreEntity.Index) Map(java.util.Map) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Index (com.google.storage.onestore.v3.OnestoreEntity.Index)8 OnestoreEntity (com.google.storage.onestore.v3.OnestoreEntity)5 Property (com.google.storage.onestore.v3.OnestoreEntity.Index.Property)5 Order (com.google.apphosting.datastore.DatastoreV3Pb.Query.Order)4 Property (com.google.storage.onestore.v3.OnestoreEntity.Property)4 IndexComponentsOnlyQuery (com.google.appengine.api.datastore.CompositeIndexManager.IndexComponentsOnlyQuery)3 DatastoreV3Pb (com.google.apphosting.datastore.DatastoreV3Pb)3 ByteString (com.google.protobuf.ByteString)3 EntityProto (com.google.storage.onestore.v3.OnestoreEntity.EntityProto)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 Test (org.junit.Test)3 Filter (com.google.apphosting.datastore.DatastoreV3Pb.Query.Filter)2 ImmutableList (com.google.common.collect.ImmutableList)2 CompositeIndex (com.google.storage.onestore.v3.OnestoreEntity.CompositeIndex)2 PropertyValue (com.google.storage.onestore.v3.OnestoreEntity.PropertyValue)2 Reference (com.google.storage.onestore.v3.OnestoreEntity.Reference)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2