GAE Implications of NDB hierarchy and entity groups

蓝咒 提交于 2019-12-03 20:17:26

Let's see

rev_key = ndb.Key('Account', 'Sandy', 'Message', 'greeting', 'Revision', '2')

Means that every entity has strong consistency following the entity path. So you are correct.

Lets see it in action: Create the entities

account_sandy = Account.get_or_insert('Sandy')
sandy_message = Message.get_or_insert('greeting', parent=account_sandy.key)
sandy_message_rev = Revision.get_or_insert('2', parent=sandy_message.key)

That will give you strong consistency and grant you the ability to query all the above entities inside transactions as well.

I am using the get_or_insert which does what it says inside a transaction efficiently creating an entity if it does not exist with the key provided. This requires the key or id to be unique. So this way you cannot have 2 messages with Greeting and Sandy as parent.

The way keys work is like a binary tree.

S = Sandy, M=Message, R=Revision

    Sandy
   / |   \
  M1 M2   M3 
 / |  \   | \
R1 R2  R1 R1 R2

Each path to the end or shorter can be run in transaction and provide strong consistency*.

Reply in comment:

As this example is not sufficient to show the efficiency of GAE and NDB maybe the below will.

Imagine that you have a jukebox with queues per room let's say. And people are queueing songs to each queue of each jukebox.

J=Jukebox, Q=queue, S=Song

   Jukebox       
   / |   \        
  Q1 Q2   Q3     
 / |  \   | \
S1 S2  S3 S4 S5

In this example it is convenient to use paths, so each operation by a user, knowing wich jukebox, queue can CUD the song entity with consistency to jukebox, queue and song.

*Btw you can also lock paths not starting from root

Also keep in mind that Queries inside transactions must include ancestor filters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!