|
14 | 14 |
|
15 | 15 | """Connections to gcloud datastore API servers.""" |
16 | 16 |
|
17 | | -from gcloud import connection |
| 17 | +from gcloud import connection as base_connection |
18 | 18 | from gcloud.datastore import datastore_v1_pb2 as datastore_pb |
19 | 19 | from gcloud.datastore import helpers |
20 | 20 | from gcloud.datastore.dataset import Dataset |
21 | 21 |
|
22 | 22 |
|
23 | | -class Connection(connection.Connection): |
| 23 | +class Connection(base_connection.Connection): |
24 | 24 | """A connection to the Google Cloud Datastore via the Protobuf API. |
25 | 25 |
|
26 | 26 | This class should understand only the basic types (and protobufs) |
@@ -125,7 +125,7 @@ def build_api_url(cls, dataset_id, method, base_url=None, |
125 | 125 | api_version=(api_version or cls.API_VERSION), |
126 | 126 | dataset_id=dataset_id, method=method) |
127 | 127 |
|
128 | | - def transaction(self, transaction=connection.Connection._EMPTY): |
| 128 | + def transaction(self, transaction=base_connection.Connection._EMPTY): |
129 | 129 | """Getter/setter for the connection's transaction object. |
130 | 130 |
|
131 | 131 | :type transaction: :class:`gcloud.datastore.transaction.Transaction`, |
@@ -575,3 +575,35 @@ def _copy_deferred_keys(lookup_request, lookup_response): |
575 | 575 | lookup_request.key.remove(old_key) |
576 | 576 | for def_key in lookup_response.deferred: |
577 | 577 | lookup_request.key.add().CopyFrom(def_key) |
| 578 | + |
| 579 | + |
| 580 | +def allocate_ids(incomplete_key, num_ids, connection, dataset_id): |
| 581 | + """Allocates a list of IDs from a partial key. |
| 582 | +
|
| 583 | + :type incomplete_key: A :class:`gcloud.datastore.key.Key` |
| 584 | + :param incomplete_key: Partial key to use as base for allocated IDs. |
| 585 | +
|
| 586 | + :type num_ids: A :class:`int`. |
| 587 | + :param num_ids: The number of IDs to allocate. |
| 588 | +
|
| 589 | + :type connection: :class:`gcloud.datastore.connection.Connection` |
| 590 | + :param connection: The connection used to allocate IDs. |
| 591 | +
|
| 592 | + :type dataset_id: :class:`str`. |
| 593 | + :param dataset_id: The ID of the dataset used to allocate. |
| 594 | +
|
| 595 | + :rtype: list of :class:`gcloud.datastore.key.Key` |
| 596 | + :returns: The (complete) keys allocated with `incomplete_key` as root. |
| 597 | + :raises: `ValueError` if `incomplete_key` is not a partial key. |
| 598 | + """ |
| 599 | + if not incomplete_key.is_partial: |
| 600 | + raise ValueError(('Key is not partial.', incomplete_key)) |
| 601 | + |
| 602 | + incomplete_key_pb = incomplete_key.to_protobuf() |
| 603 | + incomplete_key_pbs = [incomplete_key_pb] * num_ids |
| 604 | + |
| 605 | + allocated_key_pbs = connection.allocate_ids(dataset_id, incomplete_key_pbs) |
| 606 | + allocated_ids = [allocated_key_pb.path_element[-1].id |
| 607 | + for allocated_key_pb in allocated_key_pbs] |
| 608 | + return [incomplete_key.completed_key(allocated_id) |
| 609 | + for allocated_id in allocated_ids] |
0 commit comments