1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.search
;
5 import static com
.google
.appengine
.api
.search
.FutureHelper
.quietGet
;
7 import com
.google
.appengine
.api
.NamespaceManager
;
8 import com
.google
.appengine
.api
.search
.checkers
.Preconditions
;
9 import com
.google
.appengine
.api
.utils
.FutureWrapper
;
11 import java
.util
.ArrayList
;
12 import java
.util
.concurrent
.Future
;
15 * A concrete implementation of {@link SearchService}.
18 class SearchServiceImpl
implements SearchService
{
20 private final SearchApiHelper apiHelper
;
21 private final String namespace
;
23 /** Only our classes may create instances of this class. */
24 SearchServiceImpl(SearchApiHelper apiHelper
, String namespace
) {
25 this.apiHelper
= apiHelper
;
26 this.namespace
= getAppNamespace(namespace
);
30 public GetResponse
<Index
> getIndexes(GetIndexesRequest request
) {
31 return quietGet(getIndexesAsync(request
));
35 public GetResponse
<Index
> getIndexes(GetIndexesRequest
.Builder builder
) {
36 return getIndexes(builder
.build());
40 public Future
<GetResponse
<Index
>> getIndexesAsync(GetIndexesRequest
.Builder builder
) {
41 return getIndexesAsync(builder
.build());
45 public Future
<GetResponse
<Index
>> getIndexesAsync(GetIndexesRequest request
) {
46 SearchServicePb
.ListIndexesParams
.Builder paramsBuilder
= request
47 .copyToProtocolBuffer().setNamespace(namespace
);
48 SearchServicePb
.ListIndexesRequest pbRequest
= SearchServicePb
.ListIndexesRequest
.newBuilder()
49 .setParams(paramsBuilder
)
51 SearchServicePb
.ListIndexesResponse
.Builder responseBuilder
=
52 SearchServicePb
.ListIndexesResponse
.newBuilder();
53 Future
<SearchServicePb
.ListIndexesResponse
.Builder
> future
=
54 apiHelper
.makeAsyncCall("ListIndexes", pbRequest
, responseBuilder
);
55 return new FutureWrapper
<SearchServicePb
.ListIndexesResponse
.Builder
,
56 GetResponse
<Index
>>(future
) {
58 protected Throwable
convertException(Throwable cause
) {
59 OperationResult result
= OperationResult
.convertToOperationResult(cause
);
60 return (result
== null) ? cause
: new GetException(result
);
64 protected GetResponse
<Index
> wrap(
65 SearchServicePb
.ListIndexesResponse
.Builder key
) throws Exception
{
66 SearchServicePb
.ListIndexesResponse response
= key
.build();
67 OperationResult operationResult
= new OperationResult(response
.getStatus());
68 if (operationResult
.getCode() != StatusCode
.OK
) {
69 throw new GetException(operationResult
);
71 ArrayList
<Index
> indexes
= new ArrayList
<Index
>(response
.getIndexMetadataCount());
72 for (SearchServicePb
.IndexMetadata metadata
: response
.getIndexMetadataList()) {
73 SearchServicePb
.IndexSpec indexSpec
= metadata
.getIndexSpec();
74 IndexSpec
.Builder builder
= IndexSpec
.newBuilder()
75 .setName(indexSpec
.getName())
76 .setConsistency(Consistency
.fromProto(indexSpec
.getConsistency()));
77 if (indexSpec
.hasNamespace()) {
78 Preconditions
.checkArgument(indexSpec
.getNamespace().equals(namespace
),
79 String
.format("Index with incorrect namespace received '%s' != '%s'",
80 indexSpec
.getNamespace(), namespace
));
81 } else if (!namespace
.isEmpty()) {
82 Preconditions
.checkArgument(indexSpec
.getNamespace().equals(namespace
),
83 String
.format("Index with incorrect namespace received '' != '%s'", namespace
));
85 indexes
.add(new IndexImpl(
86 apiHelper
, indexSpec
.getNamespace(), builder
.build(), Schema
.createSchema(metadata
)));
88 return new GetResponse
<Index
>(indexes
);
94 public Index
getIndex(IndexSpec
.Builder builder
) {
95 return getIndex(builder
.build());
99 public Index
getIndex(IndexSpec indexSpec
) {
100 return new IndexImpl(apiHelper
, namespace
, indexSpec
);
104 public String
getNamespace() {
109 * Returns a namespace, preferring one passed via {@code namespaceGiven}
110 * parameter. If {@code null} is passed, it attempts to use namespace set
111 * in the {@link NamespaceManager}. If that one is not set, it returns
112 * an empty namespace.
114 * @param namespaceGiven the externally provided namespace
115 * @return a namespace which will not be null
117 private static String
getAppNamespace(String namespaceGiven
) {
118 if (namespaceGiven
!= null) {
119 return namespaceGiven
;
121 String currentNamespace
= NamespaceManager
.get();
122 return (currentNamespace
== null) ?
"" : currentNamespace
;