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
;
13 import java
.util
.SortedSet
;
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
);
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())) {
38 return unorderedProps
.isEmpty();
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
);
55 return matcherProperties
.size();
59 public String
toString() {
60 return "UnorderedIndexComponent: " + matcherProperties
;