How to implement “autoincrement” on Google AppEngine

前端 未结 9 819
面向向阳花
面向向阳花 2020-11-27 16:23

I have to label something in a \"strong monotone increasing\" fashion. Be it Invoice Numbers, shipping label numbers or the like.

  1. A number MUST NOT BE used twi
9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 17:03

    If you drop the requirement that IDs must be strictly sequential, you can use a hierarchical allocation scheme. The basic idea/limitation is that transactions must not affect multiple storage groups.

    For example, assuming you have the notion of "users", you can allocate a storage group for each user (creating some global object per user). Each user has a list of reserved IDs. When allocating an ID for a user, pick a reserved one (in a transaction). If no IDs are left, make a new transaction allocating 100 IDs (say) from the global pool, then make a new transaction to add them to the user and simultaneously withdraw one. Assuming each user interacts with the application only sequentially, there will be no concurrency on the user objects.

提交回复
热议问题