def get_config(name): if _CurrentTransactionKey(): config = GeneralCounterShardConfig.get_by_key_name(name) if not config: config = GeneralCounterShardConfig(name=name) config.put() return config else: return GeneralCounterShardConfig.get_or_insert(name, name=name)
def increase_shards(name, num): """Increase the number of shards for a given sharded counter. Will never decrease the number of shards. Parameters: name - The name of the counter num - How many shards to use """ config = get_config(name) def txn(): if config.num_shards < num: config.num_shards = num config.put() if _CurrentTransactionKey(): txn() else: db.run_in_transaction(txn)
def execute_hooks(kind, key, entity): put_type_id = put_type.UNKOWN func_list = post_save_hooks.get(kind, None) if func_list is not None: last_path = entity.key().path().element_list()[-1] has_name = last_path.has_name() if last_path.has_id(): if last_path.id() == 0: put_type_id = put_type.NEWLY_CREATED else: put_type_id = put_type.UPDATED entity.key_.CopyFrom(key) e = datastore.Entity._FromPb(entity) instance = db.class_for_kind(kind).from_entity(e) if has_name: created = get_created_datetime(instance) updated = get_updated_datetime(instance) if created: import datetime threshold = datetime.timedelta(0,0,1000) if updated: if abs(created - updated) < threshold: put_type_id = put_type.MAYBE_NEWLY_CREATED else: put_type_id = put_type.MAYBE_UPDATED else: if (datetime.datetime.now() - created) < threshold: put_type_id = put_type.MAYBE_NEWLY_CREATED else: put_type_id = put_type.MAYBE_UPDATED if datastore._CurrentTransactionKey(): # This operation is inside the transaction. So, we reserve the # func_list and parameters for later execution. tmp_list = getattr(local, '_reserved_hooks', None) if tmp_list is None: tmp_list = [] tmp_list.append((func_list, instance, put_type_id)) local._reserved_hooks = tmp_list else: for func in func_list: func(instance, put_type_id)
def execute_post_save_hooks(kind, key, entity): put_type_id = put_type.UNKNOWN func_list = post_save_hooks.get(kind, None) if func_list is not None: last_path = entity.key().path().element_list()[-1] has_name = last_path.has_name() if last_path.has_id(): if last_path.id() == 0: key_auto_generated = True put_type_id = put_type.NEWLY_CREATED else: key_auto_generated = False entity.key_.CopyFrom(key) e = datastore.Entity._FromPb(entity) instance = db.class_for_kind(kind).from_entity(e) if has_name or not key_auto_generated: created = get_created_datetime(instance) updated = get_updated_datetime(instance) if created: threshold = datetime.timedelta(0, 0, 1500) if updated: if abs(created - updated) < threshold: put_type_id = put_type.MAYBE_NEWLY_CREATED else: put_type_id = put_type.MAYBE_UPDATED else: if (datetime.datetime.now() - created) < threshold: put_type_id = put_type.MAYBE_NEWLY_CREATED else: put_type_id = put_type.MAYBE_UPDATED if datastore._CurrentTransactionKey(): # This operation is inside the transaction. So, we reserve the # func_list and parameters for later execution. tmp_list = getattr(local, '_reserved_hooks', []) tmp_list.append((func_list, instance, put_type_id)) local._reserved_hooks = tmp_list else: for func in func_list: func(instance, put_type_id)
def increment(name): """Increment the value for a given sharded counter. Parameters: name - The name of the counter """ config = get_config(name) def txn(): index = random.randint(0, config.num_shards - 1) shard_name = name + str(index) counter = GeneralCounterShard.get_by_key_name(shard_name) if counter is None: counter = GeneralCounterShard(key_name=shard_name, name=name) counter.count += 1 counter.put() if _CurrentTransactionKey(): txn() else: db.run_in_transaction(txn) memcache.incr(name)