1313# limitations under the License.
1414
1515# [START fs_counter_classes_async]
16+ # [START firestore_solution_sharded_counter_custom_type_async]
1617import random
1718
1819from google .cloud import firestore
@@ -42,9 +43,11 @@ class Counter(object):
4243 def __init__ (self , num_shards ):
4344 self ._num_shards = num_shards
4445
46+ # [END firestore_solution_sharded_counter_custom_type_async]
4547 # [END fs_counter_classes_async]
4648
4749 # [START fs_create_counter_async]
50+ # [START firestore_solution_sharded_counter_create_async]
4851 async def init_counter (self , doc_ref ):
4952 """
5053 Create a given number of shards as
@@ -57,19 +60,23 @@ async def init_counter(self, doc_ref):
5760 shard = Shard ()
5861 await col_ref .document (str (num )).set (shard .to_dict ())
5962
63+ # [END firestore_solution_sharded_counter_create_async]
6064 # [END fs_create_counter_async]
6165
6266 # [START fs_increment_counter_async]
67+ # [START firestore_solution_sharded_counter_increment_async]
6368 async def increment_counter (self , doc_ref ):
6469 """Increment a randomly picked shard."""
6570 doc_id = random .randint (0 , self ._num_shards - 1 )
6671
6772 shard_ref = doc_ref .collection ("shards" ).document (str (doc_id ))
6873 return await shard_ref .update ({"count" : firestore .Increment (1 )})
6974
75+ # [END firestore_solution_sharded_counter_increment_async]
7076 # [END fs_increment_counter_async]
7177
7278 # [START fs_get_count_async]
79+ # [START firestore_solution_sharded_counter_get_async]
7380 async def get_count (self , doc_ref ):
7481 """Return a total count across all shards."""
7582 total = 0
@@ -78,4 +85,5 @@ async def get_count(self, doc_ref):
7885 total += (await shard .get ()).to_dict ().get ("count" , 0 )
7986 return total
8087
88+ # [END firestore_solution_sharded_counter_get_async]
8189 # [END fs_get_count_async]
0 commit comments