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 SearchServiceConfig config
;
23 /** Only our classes may create instances of this class. */
24 SearchServiceImpl(SearchApiHelper apiHelper
, SearchServiceConfig config
) {
25 this.apiHelper
= apiHelper
;
26 this.config
= config
.toBuilder().setNamespace(getAppNamespace(config
.getNamespace())).build();
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(config
.getNamespace());
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 config
.getDeadline());
56 return new FutureWrapper
<SearchServicePb
.ListIndexesResponse
.Builder
,
57 GetResponse
<Index
>>(future
) {
59 protected Throwable
convertException(Throwable cause
) {
60 OperationResult result
= OperationResult
.convertToOperationResult(cause
);
61 return (result
== null) ? cause
: new GetException(result
);
65 protected GetResponse
<Index
> wrap(
66 SearchServicePb
.ListIndexesResponse
.Builder key
) throws Exception
{
67 SearchServicePb
.ListIndexesResponse response
= key
.build();
68 OperationResult operationResult
= new OperationResult(response
.getStatus());
69 if (operationResult
.getCode() != StatusCode
.OK
) {
70 throw new GetException(operationResult
);
72 ArrayList
<Index
> indexes
= new ArrayList
<Index
>(response
.getIndexMetadataCount());
73 for (SearchServicePb
.IndexMetadata metadata
: response
.getIndexMetadataList()) {
74 SearchServicePb
.IndexSpec indexSpec
= metadata
.getIndexSpec();
75 IndexSpec
.Builder builder
= IndexSpec
.newBuilder().setName(indexSpec
.getName());
76 if (indexSpec
.hasNamespace()) {
77 Preconditions
.checkArgument(indexSpec
.getNamespace().equals(config
.getNamespace()),
78 String
.format("Index with incorrect namespace received '%s' != '%s'",
79 indexSpec
.getNamespace(), config
.getNamespace()));
80 } else if (!config
.getNamespace().isEmpty()) {
81 Preconditions
.checkArgument(indexSpec
.getNamespace().equals(config
.getNamespace()),
82 String
.format("Index with incorrect namespace received '' != '%s'",
83 config
.getNamespace()));
85 indexes
.add(new IndexImpl(apiHelper
, config
,
86 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
, config
, indexSpec
);
104 public String
getNamespace() {
105 return config
.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
;