What would be the purpose of putting all datastore entities in a single group?

前端 未结 2 1746
抹茶落季
抹茶落季 2020-12-12 06:05

I have started working on an existing project which uses Google Datastore where for some of the entity kinds every entity is assigned the same ancestor. Example:

<         


        
2条回答
  •  暖寄归人
    2020-12-12 06:37

    Grouping many entities inside a single entity group offers at least 2 advantages I can think of:

    • ability to perform (ancestor) queries inside transactions - non-ancestor (or cross-group) queries are not allowed inside transactions
    • ability to access many entities inside the same transaction - cross-group transactions are limited to max 25 entity groups

    The 1 write/second/group limit might not be a scalability issue at all for some applications (think write once read a lot kind of apps, for example, or apps for which 1 write per sec is more than enough).

    As for the mechanics, the (unique) parent "entity" key for the group is the ndb.Key('Group', "xxx_group") key (which has the "xxx_group" key ID). The corresponding "entity" or its model doesn't need to exist (unless the entity itself needs to be created, bu that doesn't appear to be the case). The parent key is used simply to establish the group's "namespace" in the datastore, if you want.

    You can see a somehow similar use in the examples from the Entity Keys documentation, check out the Message use (except Message is just a "parent" entity in the ancestor path, but not the root entity):

    class Revision(ndb.Model): message_text = ndb.StringProperty()

    ndb.Key('Account', 'sandy@foo.com', 'Message', 123, 'Revision', '1')
    ndb.Key('Account', 'sandy@foo.com', 'Message', 123, 'Revision', '2')
    ndb.Key('Account', 'larry@foo.com', 'Message', 456, 'Revision', '1')
    ndb.Key('Account', 'larry@foo.com', 'Message', 789, 'Revision', '2')
    

    ...

    Notice that Message is not a model class. This is because we are using Message purely as a way to group Revisions, not to store data.

提交回复
热议问题