3
# Copyright 2007 Google Inc.
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
18
"""The Python datastore admin API for managing indices and schemas.
23
from google.appengine.api import api_base_pb
24
from google.appengine.api import apiproxy_stub_map
25
from google.appengine.api import datastore
26
from google.appengine.api import datastore_errors
27
from google.appengine.api import datastore_types
28
from google.appengine.datastore import datastore_index
29
from google.appengine.datastore import datastore_pb
30
from google.appengine.runtime import apiproxy_errors
31
from google.appengine.datastore import entity_pb
35
'asc': entity_pb.Index_Property.ASCENDING,
36
'ascending': entity_pb.Index_Property.ASCENDING,
37
'desc': entity_pb.Index_Property.DESCENDING,
38
'descending': entity_pb.Index_Property.DESCENDING,
42
def GetSchema(_app=None):
43
"""Infers an app's schema from the entities in the datastore.
45
Note that the PropertyValue PBs in the returned EntityProtos are empty
46
placeholders, so they may cause problems if you try to convert them to
47
python values with e.g. datastore_types. In particular, user values will
48
throw UserNotFoundError because their email and auth domain fields will be
52
list of entity_pb.EntityProto, with kind and property names and types
54
req = api_base_pb.StringProto()
55
req.set_value(datastore_types.ResolveAppId(_app))
56
resp = datastore_pb.Schema()
58
_Call('GetSchema', req, resp)
59
return resp.kind_list()
62
def GetIndices(_app=None):
63
"""Fetches all composite indices in the datastore for this app.
66
list of entity_pb.CompositeIndex
68
req = api_base_pb.StringProto()
69
req.set_value(datastore_types.ResolveAppId(_app))
70
resp = datastore_pb.CompositeIndices()
72
apiproxy_stub_map.MakeSyncCall('datastore_v3', 'GetIndices', req, resp)
73
except apiproxy_errors.ApplicationError, err:
74
raise datastore._ToDatastoreError(err)
76
return resp.index_list()
79
def CreateIndex(index):
80
"""Creates a new composite index in the datastore for this app.
83
index: entity_pb.CompositeIndex
86
int, the id allocated to the index
88
resp = api_base_pb.Integer64Proto()
89
_Call('CreateIndex', index, resp)
93
def UpdateIndex(index):
94
"""Updates an index's status. The entire index definition must be present.
97
index: entity_pb.CompositeIndex
99
_Call('UpdateIndex', index, api_base_pb.VoidProto())
102
def DeleteIndex(index):
103
"""Deletes an index. The entire index definition must be present.
106
index: entity_pb.CompositeIndex
108
_Call('DeleteIndex', index, api_base_pb.VoidProto())
111
def _Call(call, req, resp):
112
"""Generic method for making a datastore API call.
115
call: string, the name of the RPC call
116
req: the request PB. if the app_id field is not set, it defaults to the
118
resp: the response PB
120
if hasattr(req, 'app_id'):
121
req.set_app_id(datastore_types.ResolveAppId(req.app_id(), 'req.app_id()'))
124
apiproxy_stub_map.MakeSyncCall('datastore_v3', call, req, resp)
125
except apiproxy_errors.ApplicationError, err:
126
raise datastore._ToDatastoreError(err)
129
def IndexDefinitionToProto(app_id, index_definition):
130
"""Transform individual Index definition to protocol buffer.
133
app_id: Application id for new protocol buffer CompositeIndex.
134
index_definition: datastore_index.Index object to transform.
137
New entity_pb.CompositeIndex with default values set and index
138
information filled in.
140
proto = entity_pb.CompositeIndex()
142
proto.set_app_id(app_id)
144
proto.set_state(entity_pb.CompositeIndex.WRITE_ONLY)
146
definition_proto = proto.mutable_definition()
147
definition_proto.set_entity_type(index_definition.kind)
148
definition_proto.set_ancestor(index_definition.ancestor)
150
if index_definition.properties is not None:
151
for prop in index_definition.properties:
152
prop_proto = definition_proto.add_property()
153
prop_proto.set_name(prop.name)
154
prop_proto.set_direction(_DIRECTION_MAP[prop.direction])
159
def IndexDefinitionsToProtos(app_id, index_definitions):
160
"""Transform multiple index definitions to composite index records
163
app_id: Application id for new protocol buffer CompositeIndex.
164
index_definition: A list of datastore_index.Index objects to transform.
167
A list of tranformed entity_pb.Compositeindex entities with default values
168
set and index information filled in.
170
return [IndexDefinitionToProto(app_id, index)
171
for index in index_definitions]
174
def ProtoToIndexDefinition(proto):
175
"""Transform individual index protocol buffer to index definition.
178
proto: An instance of entity_pb.CompositeIndex to transform.
181
A new instance of datastore_index.Index.
184
proto_index = proto.definition()
185
for prop_proto in proto_index.property_list():
186
prop_definition = datastore_index.Property(name=prop_proto.name())
187
if prop_proto.direction() == entity_pb.Index_Property.DESCENDING:
188
prop_definition.direction = 'descending'
189
properties.append(prop_definition)
191
index = datastore_index.Index(kind=proto_index.entity_type(),
192
properties=properties)
193
if proto_index.ancestor():
194
index.ancestor = True
197
def ProtosToIndexDefinitions(protos):
198
"""Transform multiple index protocol buffers to index definitions.
201
A list of entity_pb.Index records.
203
return [ProtoToIndexDefinition(definition) for definition in protos]