14
14
"""Convenience wrapper for invoking APIs/factories w/ a project."""
15
15
16
16
import os
17
+ import warnings
17
18
18
19
import google .api_core .client_options
19
20
from google .auth .credentials import AnonymousCredentials
@@ -816,11 +817,18 @@ def do_something(entity):
816
817
kwargs ["namespace" ] = self .namespace
817
818
return Query (self , ** kwargs )
818
819
819
- def reserve_ids (self , complete_key , num_ids , retry = None , timeout = None ):
820
- """Reserve a list of IDs from a complete key.
820
+ def reserve_ids_sequential (self , complete_key , num_ids , retry = None , timeout = None ):
821
+ """Reserve a list of IDs sequentially from a complete key.
822
+
823
+ This will reserve the key passed as `complete_key` as well as
824
+ additional keys derived by incrementing the last ID in the path of
825
+ `complete_key` sequentially to obtain the number of keys specified in
826
+ `num_ids`.
821
827
822
828
:type complete_key: :class:`google.cloud.datastore.key.Key`
823
- :param complete_key: Complete key to use as base for reserved IDs.
829
+ :param complete_key:
830
+ Complete key to use as base for reserved IDs. Key must use a
831
+ numeric ID and not a string name.
824
832
825
833
:type num_ids: int
826
834
:param num_ids: The number of IDs to reserve.
@@ -844,16 +852,75 @@ def reserve_ids(self, complete_key, num_ids, retry=None, timeout=None):
844
852
if complete_key .is_partial :
845
853
raise ValueError (("Key is not Complete." , complete_key ))
846
854
855
+ if complete_key .id is None :
856
+ raise ValueError (("Key must use numeric id." , complete_key ))
857
+
847
858
if not isinstance (num_ids , int ):
848
859
raise ValueError (("num_ids is not a valid integer." , num_ids ))
849
860
861
+ key_class = type (complete_key )
862
+ namespace = complete_key ._namespace
863
+ project = complete_key ._project
864
+ flat_path = list (complete_key ._flat_path [:- 1 ])
865
+ start_id = complete_key ._flat_path [- 1 ]
866
+
867
+ key_pbs = []
868
+ for id in range (start_id , start_id + num_ids ):
869
+ path = flat_path + [id ]
870
+ key = key_class (* path , project = project , namespace = namespace )
871
+ key_pbs .append (key .to_protobuf ())
872
+
850
873
kwargs = _make_retry_timeout_kwargs (retry , timeout )
874
+ self ._datastore_api .reserve_ids (complete_key .project , key_pbs , ** kwargs )
875
+
876
+ return None
877
+
878
+ def reserve_ids (self , complete_key , num_ids , retry = None , timeout = None ):
879
+ """Reserve a list of IDs sequentially from a complete key.
851
880
852
- complete_key_pb = complete_key .to_protobuf ()
853
- complete_key_pbs = [complete_key_pb ] * num_ids
881
+ DEPRECATED. Alias for :meth:`reserve_ids_sequential`.
854
882
855
- self ._datastore_api .reserve_ids (
856
- complete_key .project , complete_key_pbs , ** kwargs
883
+ Please use either :meth:`reserve_ids_multi` (recommended) or
884
+ :meth:`reserve_ids_sequential`.
885
+ """
886
+ message = (
887
+ "Client.reserve_ids is deprecated. Please use "
888
+ "Client.reserve_ids_multi or Client.reserve_ids_sequential" ,
857
889
)
890
+ warnings .warn (message , DeprecationWarning )
891
+ return self .reserve_ids_sequential (
892
+ complete_key , num_ids , retry = retry , timeout = timeout
893
+ )
894
+
895
+ def reserve_ids_multi (self , complete_keys , retry = None , timeout = None ):
896
+ """Reserve IDs from a list of complete keys.
897
+
898
+ :type complete_keys: `list` of :class:`google.cloud.datastore.key.Key`
899
+ :param complete_keys:
900
+ Complete keys for which to reserve IDs.
901
+
902
+ :type retry: :class:`google.api_core.retry.Retry`
903
+ :param retry:
904
+ A retry object used to retry requests. If ``None`` is specified,
905
+ requests will be retried using a default configuration.
906
+
907
+ :type timeout: float
908
+ :param timeout:
909
+ Time, in seconds, to wait for the request to complete.
910
+ Note that if ``retry`` is specified, the timeout applies
911
+ to each individual attempt.
912
+
913
+ :rtype: class:`NoneType`
914
+ :returns: None
915
+ :raises: :class:`ValueError` if any of `complete_keys`` is not a
916
+ Complete key.
917
+ """
918
+ for complete_key in complete_keys :
919
+ if complete_key .is_partial :
920
+ raise ValueError (("Key is not Complete." , complete_key ))
921
+
922
+ kwargs = _make_retry_timeout_kwargs (retry , timeout )
923
+ key_pbs = [key .to_protobuf () for key in complete_keys ]
924
+ self ._datastore_api .reserve_ids (complete_keys [0 ].project , key_pbs , ** kwargs )
858
925
859
926
return None
0 commit comments