在GAE中强制唯一约束 [英] Enforcing Unique Constraint in GAE

查看:112
本文介绍了在GAE中强制唯一约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google App Engine Java,但是没有唯一的约束会让事情变得困难。
我一直通过这个帖子 这个博客建议一种方法实施类似的东西。我的背景是在MySQL中。移动到没有唯一约束的数据存储让我感到紧张,因为我从来不必担心重复值,并且在插入新值之前检查每个值仍然有误差空间。


不,在模式创建期间,您仍然无法指定唯一的

$ b - David Underhill 谈论GAE和独特的限制(

strong> 你们用什么来实现类似于独特或主键的功能? 我听说过一个抽象数据存储图层使用低级api创建,其工作方式类似于常规的RDB,但不是免费的(但我不记得软件的名称)。

m的示意图y问题
$ b $ pre $ $ $ $ $ $ s $
$ s $ $ $ $ $ $ $ $ $ s $ //检查点
用户添加与当前serial_number有关的数据
用serial_number为sNo


$ b的数据更新条目$ b

然而,在第3行(检查点),我感觉两个用户可能会添加相同的sNo。这就是阻止我使用appengine的原因。

解决方案

您无需执行即可为您的产品生成唯一的序列号唯一的ID或查询整组实体以找出当前最大的序列号。您可以使用交易和单一实体来生成下一个序列号。由于操作发生在交易中,因此您可以确定没有两个产品会获得相同的序列号。



然而,这种方法将成为潜在的性能瓶颈并限制应用程序的可伸缩性。如果创建新的序列号的情况不会经常发生以至于争用,它可能适用于您。

编辑:
为了澄清,保存当前或下一个要分配的序列号的单例完全独立于任何实体实际上有分配给他们的序列号。他们不需要全部成为实体组的一部分。您可以让多个模型中的实体使用相同的机制来获取新的唯一序列号。



我不记得Java足以提供示例代码,而我的Python例子对你来说可能毫无意义,但这里有伪代码来说明这个想法:


  1. 接收创建新库存项目的请求。 / li>
  2. 输入交易。
  3. 检索SerialNumber模型的单个实体的当前值。将其写入数据库
  4. 在退出事务时返回值。

现在,代码它能够完成创建库存项目以及将其与新序列号一起存储的所有工作,而不需要在事务中运行。



警告:如上所述,这可能是一个主要的性能瓶颈,因为任何时候只能创建一个序列号。但是,它确实为您提供了确定性,即您刚刚生成的序列号是唯一的,并且不在使用中。

I am trying out Google App Engine Java, however the absence of a unique constraint is making things difficult. I have been through this post and this blog suggests a method to implement something similar. My background is in MySQL.Moving to datastore without a unique constraint makes me jittery because I never had to worry about duplicate values before and checking each value before inserting a new value still has room for error.

"No, you still cannot specify unique during schema creation."

-- David Underhill talks about GAE and the unique constraint (post link)

What are you guys using to implement something similar to a unique or primary key?

I heard about a abstract datastore layer created using the low level api which worked like a regular RDB, which however was not free(however I do not remember the name of the software)

Schematic view of my problem

sNo = biggest serial_number in the db
sNo++
Insert new entry with sNo as serial_number value //checkpoint
User adds data pertaining to current serial_number 
Update entry with data where serial_number is sNo 

However at line number 3(checkpoint), I feel two users might add the same sNo. And that is what is preventing me from working with appengine.

解决方案

You can generate unique serial numbers for your products without needing to enforce unique IDs or querying the entire set of entities to find out what the largest serial number currently is. You can use transactions and a singleton entity to generate the 'next' serial number. Because the operation occurs inside a transaction, you can be sure that no two products will ever get the same serial number.

This approach will, however, be a potential performance chokepoint and limit your application's scalability. If it is the case that the creation of new serial numbers does not happen so often that you get contention, it may work for you.

EDIT: To clarify, the singleton that holds the current -- or next -- serial number that is to be assigned is completely independent of any entities that actually have serial numbers assigned to them. They do not need to be all be a part of an entity group. You could have entities from multiple models using the same mechanism to get a new, unique serial number.

I don't remember Java well enough to provide sample code, and my Python example might be meaningless to you, but here's pseudo-code to illustrate the idea:

  1. Receive request to create a new inventory item.
  2. Enter transaction.
  3. Retrieve current value of the single entity of the SerialNumber model.
  4. Increment value and write it to the database
  5. Return value as you exit transaction.

Now, the code that does all the work of actually creating the inventory item and storing it along with its new serial number DOES NOT need to run in a transaction.

Caveat: as I stated above, this could be a major performance bottleneck, as only one serial number can be created at any one time. However, it does provide you with the certainty that the serial number that you just generated is unique and not in-use.

这篇关于在GAE中强制唯一约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆