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().setName(indexSpec
.getName());
75 if (indexSpec
.hasNamespace()) {
76 Preconditions
.checkArgument(indexSpec
.getNamespace().equals(namespace
),
77 String
.format("Index with incorrect namespace received '%s' != '%s'",
78 indexSpec
.getNamespace(), namespace
));
79 } else if (!namespace
.isEmpty()) {
80 Preconditions
.checkArgument(indexSpec
.getNamespace().equals(namespace
),
81 String
.format("Index with incorrect namespace received '' != '%s'", namespace
));
83 indexes
.add(new IndexImpl(
84 apiHelper
, indexSpec
.getNamespace(), builder
.build(), Schema
.createSchema(metadata
)));
86 return new GetResponse
<Index
>(indexes
);
92 public Index
getIndex(IndexSpec
.Builder builder
) {
93 return getIndex(builder
.build());
97 public Index
getIndex(IndexSpec indexSpec
) {
98 return new IndexImpl(apiHelper
, namespace
, indexSpec
);
102 public String
getNamespace() {
107 * Returns a namespace, preferring one passed via {@code namespaceGiven}
108 * parameter. If {@code null} is passed, it attempts to use namespace set
109 * in the {@link NamespaceManager}. If that one is not set, it returns
110 * an empty namespace.
112 * @param namespaceGiven the externally provided namespace
113 * @return a namespace which will not be null
115 private static String
getAppNamespace(String namespaceGiven
) {
116 if (namespaceGiven
!= null) {
117 return namespaceGiven
;
119 String currentNamespace
= NamespaceManager
.get();
120 return (currentNamespace
== null) ?
"" : currentNamespace
;