@@ -196,6 +196,16 @@ def __init__(self, options):
196196 self .options = options
197197 self .todo = {}
198198
199+ def full (self ):
200+
201+ """Indicates whether more work can be added to this batch.
202+
203+ Returns:
204+ boolean: `True` if number of keys to be looked up has reached 1000,
205+ else `False`.
206+ """
207+ return len (self .todo ) >= 1000
208+
199209 def add (self , key ):
200210 """Add a key to the batch to look up.
201211
@@ -477,6 +487,15 @@ def __init__(self, options):
477487 self .mutations = []
478488 self .futures = []
479489
490+ def full (self ):
491+ """Indicates whether more work can be added to this batch.
492+
493+ Returns:
494+ boolean: `True` if number of mutations has reached 500, else
495+ `False`.
496+ """
497+ return len (self .mutations ) >= 500
498+
480499 def put (self , entity_pb ):
481500 """Add an entity to batch to be stored.
482501
@@ -854,8 +873,15 @@ def allocate(keys, options):
854873 Returns:
855874 tasklets.Future: A future for the key completed with the allocated id.
856875 """
857- batch = _batch .get_batch (_AllocateIdsBatch , options )
858- return batch .add (keys )
876+ futures = []
877+ while keys :
878+ batch = _batch .get_batch (_AllocateIdsBatch , options )
879+ room_left = batch .room_left ()
880+ batch_keys = keys [:room_left ]
881+ futures .extend (batch .add (batch_keys ))
882+ keys = keys [room_left :]
883+
884+ return tasklets ._MultiFuture (futures )
859885
860886
861887class _AllocateIdsBatch (object ):
@@ -875,6 +901,22 @@ def __init__(self, options):
875901 self .keys = []
876902 self .futures = []
877903
904+ def full (self ):
905+ """Indicates whether more work can be added to this batch.
906+
907+ Returns:
908+ boolean: `True` if number of keys has reached 500, else `False`.
909+ """
910+ return len (self .keys ) >= 500
911+
912+ def room_left (self ):
913+ """Get how many more keys can be added to this batch.
914+
915+ Returns:
916+ int: 500 - number of keys already in batch
917+ """
918+ return 500 - len (self .keys )
919+
878920 def add (self , keys ):
879921 """Add incomplete keys to batch to allocate.
880922
@@ -892,7 +934,7 @@ def add(self, keys):
892934 self .keys .append (key )
893935
894936 self .futures .extend (futures )
895- return tasklets . _MultiFuture ( futures )
937+ return futures
896938
897939 def idle_callback (self ):
898940 """Perform a Datastore AllocateIds request on all batched keys."""
0 commit comments