GrantsV,您可以通过为每个唯一约束创建代理实体并使用跨组事务通过正常写入提交约束来实现此目的。
class UniqueConstraint(db.Model):
# Consider adding a reference to the owner of the constraint.
@db.transactional(propagation=db.MANDATORY, xg=True)
@classmethod
def reserve(cls, kind, property, value):
key = cls.__get_key(kind, property, value)
if db.get(key):
raise Exception # Already exists
cls(key=key).put()
@db.transactional(propagation=db.MANDATORY, xg=True)
@classmethod
def release(cls, kind, property, value):
db.delete(cls.__get_key(kind, property, value))
@classmethod
def __get_key(cls, kind, property, value):
# Consider using a larger entity group.
return db.Key.from_path(cls.kind(), '%s:%s:%s' % (kind, property, value))
# To restrict to 1 insert per second per kind, use:
# return db.Key.from_path(cls.kind(), kind, cls.kind(), '%s:%s' % (property, value))