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
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)
33 """Retrieve the value for a given sharded counter.
36 name - The name of the counter
38 total
= memcache
.get(name
)
41 for counter
in GeneralCounterShard
.all().filter('name = ', name
):
42 total
+= counter
.count
43 memcache
.add(name
, total
, 60)
48 """Increment the value for a given sharded counter.
51 name - The name of the counter
53 config
= GeneralCounterShardConfig
.get_or_insert(name
, name
=name
)
55 index
= random
.randint(0, config
.num_shards
- 1)
56 shard_name
= name
+ str(index
)
57 counter
= GeneralCounterShard
.get_by_key_name(shard_name
)
59 counter
= GeneralCounterShard(key_name
=shard_name
, name
=name
)
62 db
.run_in_transaction(txn
)
63 # does nothing if the key does not exist
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.
72 name - The name of the counter
73 num - How many shards to use
76 config
= GeneralCounterShardConfig
.get_or_insert(name
, name
=name
)
78 if config
.num_shards
< num
:
79 config
.num_shards
= num
81 db
.run_in_transaction(txn
)