add demonstration of geosearch
[gae-samples.git] / sharded-counters / generalcounter.py
blob2da6aa67f7e54ad8493c4175a2d4acd939ec3424
1 # Copyright 2008 Google Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 from google.appengine.api import memcache
17 from google.appengine.ext import db
18 import random
20 class GeneralCounterShardConfig(db.Model):
21 """Tracks the number of shards for each named counter."""
22 name = db.StringProperty(required=True)
23 num_shards = db.IntegerProperty(required=True, default=20)
26 class GeneralCounterShard(db.Model):
27 """Shards for each named counter"""
28 name = db.StringProperty(required=True)
29 count = db.IntegerProperty(required=True, default=0)
32 def get_count(name):
33 """Retrieve the value for a given sharded counter.
35 Parameters:
36 name - The name of the counter
37 """
38 total = memcache.get(name)
39 if total is None:
40 total = 0
41 for counter in GeneralCounterShard.all().filter('name = ', name):
42 total += counter.count
43 memcache.add(name, total, 60)
44 return total
47 def increment(name):
48 """Increment the value for a given sharded counter.
50 Parameters:
51 name - The name of the counter
52 """
53 config = GeneralCounterShardConfig.get_or_insert(name, name=name)
54 def txn():
55 index = random.randint(0, config.num_shards - 1)
56 shard_name = name + str(index)
57 counter = GeneralCounterShard.get_by_key_name(shard_name)
58 if counter is None:
59 counter = GeneralCounterShard(key_name=shard_name, name=name)
60 counter.count += 1
61 counter.put()
62 db.run_in_transaction(txn)
63 # does nothing if the key does not exist
64 memcache.incr(name)
67 def increase_shards(name, num):
68 """Increase the number of shards for a given sharded counter.
69 Will never decrease the number of shards.
71 Parameters:
72 name - The name of the counter
73 num - How many shards to use
75 """
76 config = GeneralCounterShardConfig.get_or_insert(name, name=name)
77 def txn():
78 if config.num_shards < num:
79 config.num_shards = num
80 config.put()
81 db.run_in_transaction(txn)