1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.search
;
5 import com
.google
.appengine
.api
.search
.checkers
.IndexChecker
;
8 * Represents information about an index. This class is used to fully specify
9 * the index you want to retrieve from the {@link SearchService}.
10 * To build an instance use the {@link #newBuilder()} method and set
11 * all required parameters, plus optional values different than the defaults.
13 * SearchService searchService = SearchServiceFactory.getSearchService();
15 * IndexSpec spec = IndexSpec.newBuilder()
17 * .setConsistency(Consistency.PER_DOCUMENT)
20 * Index index = searchService.getIndex(spec);
24 public class IndexSpec
{
27 * A builder of IndexSpec.
29 public static final class Builder
{
31 private Consistency consistency
;
34 * Constructs a builder for an IndexSpec.
40 * Sets the unique name of the index.
42 * @param name the name of the index
43 * @return this Builder
44 * @throws IllegalArgumentException if the index name length is not between 1
45 * and {@literal IndexChecker#MAXIMUM_INDEX_NAME_LENGTH}
47 public Builder
setName(String name
) {
48 this.name
= IndexChecker
.checkName(name
);
53 * Sets the consistency mode in which the index operates.
55 * @param consistency the consistency mode in which the index operates
56 * @return this Builder
58 public Builder
setConsistency(Consistency consistency
) {
59 this.consistency
= consistency
;
64 * Builds a valid IndexSpec. The builder must have set a valid
67 * @return the IndexSpec built by this builder
68 * @throws IllegalArgumentException if the IndexSpec built is not valid
70 public IndexSpec
build() {
71 return new IndexSpec(this);
75 private final String name
;
76 private final Consistency consistency
;
79 * Creates new index specification.
81 * @param builder the IndexSpec builder to use to construct an instance
82 * @throws IllegalArgumentException if the index name is invalid
84 private IndexSpec(Builder builder
) {
85 consistency
= Util
.defaultIfNull(builder
.consistency
, Consistency
.PER_DOCUMENT
);
90 * @return the name of the index
92 public String
getName() {
97 * @return the consistency mode of this index
99 public Consistency
getConsistency() {
104 * Creates a new IndexSpec builder. You must use this method to obtain a new
105 * builder. The returned builder must be used to specify all properties of
106 * the IndexSpec. To obtain the IndexSpec call the {@link Builder#build()}
107 * method on the returned builder.
109 * @return a builder which constructs a IndexSpec object
111 public static Builder
newBuilder() {
112 return new Builder();
116 public int hashCode() {
117 final int prime
= 31;
119 result
= prime
* result
+ ((consistency
== null) ?
0 : consistency
.hashCode());
120 result
= prime
* result
+ ((name
== null) ?
0 : name
.hashCode());
125 public boolean equals(Object obj
) {
132 if (getClass() != obj
.getClass()) {
135 IndexSpec other
= (IndexSpec
) obj
;
136 if (consistency
== null) {
137 if (other
.consistency
!= null) {
140 } else if (!consistency
.equals(other
.consistency
)) {
144 if (other
.name
!= null) {
147 } else if (!name
.equals(other
.name
)) {
154 public String
toString() {
155 return String
.format("IndexSpec{name: %s, consistency: %s}", name
, consistency
.name());
159 * Creates an SearchServicePb.IndexSpec from the given Index.
161 * @param namespace the namespace for the index
162 * @return a valid SearchServicePb.IndexSpec.Builder
163 * @throws IllegalArgumentException if the consistency is not a known type
165 SearchServicePb
.IndexSpec
.Builder
copyToProtocolBuffer(String namespace
) {
166 SearchServicePb
.IndexSpec
.Builder builder
= SearchServicePb
.IndexSpec
.newBuilder()
168 .setNamespace(namespace
);
170 switch (getConsistency().getConsistency()) {
172 builder
.setConsistency(SearchServicePb
.IndexSpec
.Consistency
.GLOBAL
);
175 builder
.setConsistency(SearchServicePb
.IndexSpec
.Consistency
.PER_DOCUMENT
);
178 throw new IllegalArgumentException(String
.format("unknown consistency type %s",
179 getConsistency().getConsistency().name()));