Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / datastore / UnorderedIndexComponent.java
blob05eb24ab28f05d8725ef2256365d4c18a6ef3459
1 // Copyright 2012 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.datastore;
5 import com.google.common.collect.Lists;
6 import com.google.common.collect.Sets;
7 import com.google.storage.onestore.v3.OnestoreEntity.Index.Property;
8 import com.google.storage.onestore.v3.OnestoreEntity.Index.Property.Direction;
10 import java.util.List;
11 import java.util.ListIterator;
12 import java.util.Set;
13 import java.util.SortedSet;
15 /**
16 * Implements an {@link IndexComponent} that is unordered.
18 * This is currently used for exists and group by filters.
21 class UnorderedIndexComponent implements IndexComponent {
23 private final SortedSet<String> matcherProperties;
25 public UnorderedIndexComponent(Set<String> unorderedGroup) {
26 this.matcherProperties = Sets.newTreeSet(unorderedGroup);
29 @Override
30 public boolean matches(List<Property> indexProperties) {
31 Set<String> unorderedProps = Sets.newHashSet(matcherProperties);
32 ListIterator<Property> indexItr = indexProperties.listIterator(indexProperties.size());
33 while (!unorderedProps.isEmpty() && indexItr.hasPrevious()) {
34 if (!unorderedProps.remove(indexItr.previous().getName())) {
35 return false;
38 return unorderedProps.isEmpty();
41 @Override
42 public List<Property> preferredIndexProperties() {
43 List<Property> indexProps = Lists.newArrayListWithExpectedSize(matcherProperties.size());
44 for (String name : matcherProperties) {
45 Property indexProperty = new Property();
46 indexProperty.setName(name);
47 indexProperty.setDirection(Direction.ASCENDING);
48 indexProps.add(indexProperty);
50 return indexProps;
53 @Override
54 public int size() {
55 return matcherProperties.size();
58 @Override
59 public String toString() {
60 return "UnorderedIndexComponent: " + matcherProperties;