|
| 1 | +# Copyright 2018 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Translate NDB queries to Datastore calls.""" |
| 16 | + |
| 17 | +from google.cloud.datastore_v1.proto import datastore_pb2 |
| 18 | +from google.cloud.datastore_v1.proto import query_pb2 |
| 19 | + |
| 20 | +from google.cloud.ndb import context as context_module |
| 21 | +from google.cloud.ndb import _datastore_api |
| 22 | +from google.cloud.ndb import model |
| 23 | +from google.cloud.ndb import tasklets |
| 24 | + |
| 25 | +MoreResultsType = query_pb2.QueryResultBatch.MoreResultsType |
| 26 | +MORE_RESULTS_TYPE_NOT_FINISHED = MoreResultsType.Value("NOT_FINISHED") |
| 27 | +ResultType = query_pb2.EntityResult.ResultType |
| 28 | +RESULT_TYPE_FULL = ResultType.Value("FULL") |
| 29 | + |
| 30 | + |
| 31 | +@tasklets.tasklet |
| 32 | +def fetch(query): |
| 33 | + """Fetch query results. |
| 34 | +
|
| 35 | + Args: |
| 36 | + query (query.Query): The query. |
| 37 | +
|
| 38 | + Returns: |
| 39 | + tasklets.Future: Result is List[model.Model]: The query results. |
| 40 | + """ |
| 41 | + for name in ( |
| 42 | + "ancestor", |
| 43 | + "filters", |
| 44 | + "orders", |
| 45 | + "app", |
| 46 | + "namespace", |
| 47 | + "default_options", |
| 48 | + "projection", |
| 49 | + "group_by", |
| 50 | + ): |
| 51 | + if getattr(query, name, None): |
| 52 | + raise NotImplementedError( |
| 53 | + "{} is not yet implemented for queries.".format(name) |
| 54 | + ) |
| 55 | + |
| 56 | + query_pb = _query_to_protobuf(query) |
| 57 | + results = yield _run_query(query_pb) |
| 58 | + return [ |
| 59 | + _process_result(result_type, result) for result_type, result in results |
| 60 | + ] |
| 61 | + |
| 62 | + |
| 63 | +def _process_result(result_type, result): |
| 64 | + """Process a single entity result. |
| 65 | +
|
| 66 | + Args: |
| 67 | + result_type (query_pb2.EntityResult.ResultType): The type of the result |
| 68 | + (full entity, projection, or key only). |
| 69 | + result (query_pb2.EntityResult): The protocol buffer representation of |
| 70 | + the query result. |
| 71 | +
|
| 72 | + Returns: |
| 73 | + Union[model.Model, key.Key]: The processed result. |
| 74 | + """ |
| 75 | + if result_type == RESULT_TYPE_FULL: |
| 76 | + return model._entity_from_protobuf(result.entity) |
| 77 | + |
| 78 | + raise NotImplementedError( |
| 79 | + "Processing for projection and key only entity results is not yet " |
| 80 | + "implemented for queries." |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +def _query_to_protobuf(query): |
| 85 | + """Convert an NDB query to a Datastore protocol buffer. |
| 86 | +
|
| 87 | + Args: |
| 88 | + query (query.Query): The query. |
| 89 | +
|
| 90 | + Returns: |
| 91 | + query_pb2.Query: The protocol buffer representation of the query. |
| 92 | + """ |
| 93 | + query_args = {} |
| 94 | + if query.kind: |
| 95 | + query_args["kind"] = [query_pb2.KindExpression(name=query.kind)] |
| 96 | + |
| 97 | + return query_pb2.Query(**query_args) |
| 98 | + |
| 99 | + |
| 100 | +@tasklets.tasklet |
| 101 | +def _run_query(query_pb): |
| 102 | + """Run a query in Datastore. |
| 103 | +
|
| 104 | + Will potentially repeat the query to get all results. |
| 105 | +
|
| 106 | + Args: |
| 107 | + query_pb (query_pb2.Query): The query protocol buffer representation. |
| 108 | +
|
| 109 | + Returns: |
| 110 | + tasklets.Future: List[Tuple[query_pb2.EntityResult.ResultType, |
| 111 | + query_pb2.EntityResult]]: The raw query results. |
| 112 | + """ |
| 113 | + client = context_module.get_context().client |
| 114 | + results = [] |
| 115 | + |
| 116 | + while True: |
| 117 | + # See what results we get from the backend |
| 118 | + request = datastore_pb2.RunQueryRequest( |
| 119 | + project_id=client.project, query=query_pb |
| 120 | + ) |
| 121 | + response = yield _datastore_api.make_call("RunQuery", request) |
| 122 | + batch = response.batch |
| 123 | + results.extend( |
| 124 | + ( |
| 125 | + (batch.entity_result_type, result) |
| 126 | + for result in batch.entity_results |
| 127 | + ) |
| 128 | + ) |
| 129 | + |
| 130 | + # Did we get all of them? |
| 131 | + if batch.more_results != MORE_RESULTS_TYPE_NOT_FINISHED: |
| 132 | + break |
| 133 | + |
| 134 | + # Still some results left to fetch. Update cursors and try again. |
| 135 | + query_pb.start_cursor = batch.end_cursor |
| 136 | + |
| 137 | + return results |
0 commit comments