1 package com
.google
.appengine
.api
.search
;
3 import static com
.google
.appengine
.api
.search
.FutureHelper
.quietGet
;
5 import com
.google
.appengine
.api
.NamespaceManager
;
6 import com
.google
.appengine
.api
.search
.checkers
.Preconditions
;
7 import com
.google
.appengine
.api
.utils
.FutureWrapper
;
9 import java
.util
.ArrayList
;
10 import java
.util
.concurrent
.Future
;
13 * A concrete implementation of {@link SearchService}.
15 class SearchServiceImpl
implements SearchService
{
17 private final SearchApiHelper apiHelper
;
18 private final SearchServiceConfig config
;
20 /** Only our classes may create instances of this class. */
21 SearchServiceImpl(SearchApiHelper apiHelper
, SearchServiceConfig config
) {
22 this.apiHelper
= apiHelper
;
23 this.config
= config
.toBuilder().setNamespace(getAppNamespace(config
.getNamespace())).build();
27 public GetResponse
<Index
> getIndexes(GetIndexesRequest request
) {
28 return quietGet(getIndexesAsync(request
));
32 public GetResponse
<Index
> getIndexes(GetIndexesRequest
.Builder builder
) {
33 return getIndexes(builder
.build());
37 public Future
<GetResponse
<Index
>> getIndexesAsync(GetIndexesRequest
.Builder builder
) {
38 return getIndexesAsync(builder
.build());
42 public Future
<GetResponse
<Index
>> getIndexesAsync(GetIndexesRequest request
) {
43 SearchServicePb
.ListIndexesParams
.Builder paramsBuilder
= request
44 .copyToProtocolBuffer().setNamespace(config
.getNamespace());
45 Future
<SearchServicePb
.ListIndexesResponse
.Builder
> future
=
46 apiHelper
.makeAsyncListIndexesCall(paramsBuilder
.build(), config
.getDeadline());
47 return new FutureWrapper
<SearchServicePb
.ListIndexesResponse
.Builder
,
48 GetResponse
<Index
>>(future
) {
50 protected Throwable
convertException(Throwable cause
) {
51 OperationResult result
= OperationResult
.convertToOperationResult(cause
);
52 return (result
== null) ? cause
: new GetException(result
);
56 protected GetResponse
<Index
> wrap(
57 SearchServicePb
.ListIndexesResponse
.Builder key
) throws Exception
{
58 SearchServicePb
.ListIndexesResponse response
= key
.build();
59 OperationResult operationResult
= new OperationResult(response
.getStatus());
60 if (operationResult
.getCode() != StatusCode
.OK
) {
61 throw new GetException(operationResult
);
63 ArrayList
<Index
> indexes
= new ArrayList
<Index
>(response
.getIndexMetadataCount());
64 for (SearchServicePb
.IndexMetadata metadata
: response
.getIndexMetadataList()) {
65 SearchServicePb
.IndexSpec indexSpec
= metadata
.getIndexSpec();
66 IndexSpec
.Builder builder
= IndexSpec
.newBuilder().setName(indexSpec
.getName());
67 if (indexSpec
.hasNamespace()) {
68 Preconditions
.checkArgument(indexSpec
.getNamespace().equals(config
.getNamespace()),
69 String
.format("Index with incorrect namespace received '%s' != '%s'",
70 indexSpec
.getNamespace(), config
.getNamespace()));
71 } else if (!config
.getNamespace().isEmpty()) {
72 Preconditions
.checkArgument(indexSpec
.getNamespace().equals(config
.getNamespace()),
73 String
.format("Index with incorrect namespace received '' != '%s'",
74 config
.getNamespace()));
76 Long amountUsed
= null;
78 if (metadata
.hasStorage()) {
79 amountUsed
= metadata
.getStorage().getAmountUsed();
80 limit
= metadata
.getStorage().getLimit();
82 indexes
.add(new IndexImpl(apiHelper
, config
,
83 builder
.build(), Schema
.createSchema(metadata
), amountUsed
, limit
));
85 return new GetResponse
<Index
>(indexes
);
91 public Index
getIndex(IndexSpec
.Builder builder
) {
92 return getIndex(builder
.build());
96 public Index
getIndex(IndexSpec indexSpec
) {
97 return new IndexImpl(apiHelper
, config
, indexSpec
);
101 public String
getNamespace() {
102 return config
.getNamespace();
106 * Returns a namespace, preferring one passed via {@code namespaceGiven}
107 * parameter. If {@code null} is passed, it attempts to use namespace set
108 * in the {@link NamespaceManager}. If that one is not set, it returns
109 * an empty namespace.
111 * @param namespaceGiven the externally provided namespace
112 * @return a namespace which will not be null
114 private static String
getAppNamespace(String namespaceGiven
) {
115 if (namespaceGiven
!= null) {
116 return namespaceGiven
;
118 String currentNamespace
= NamespaceManager
.get();
119 return (currentNamespace
== null) ?
"" : currentNamespace
;