Doctrine ORM:使用票证服务器生成主 ID 的最佳方法?

发布于 2024-10-02 06:35:10 字数 1123 浏览 0 评论 0原文

我正在创建一个集中式票证服务器,用于分发唯一的 32 位 INT id。我有一个可以在我的应用程序中调用的函数,它接受实体/表的参数,例如 getPrimaryKey('user')。我通过这篇博客文章从 Flickr 得到了这个想法:

http://code.flickr.com/blog/2010/02/08/ticket-servers-distributed-unique-primary-keys-on-the-cheap/

我们最终将在多个 MySQL 数据库之间分割我们的数据,我希望启动并运行此代码作为概念证明。

我的问题是在 Doctrine 中使用此功能的最佳方法是什么?显然,使用上述票证服务器,我需要指定我的 Doctrine 模型都不包含自动递增主键。

我是否可以扩展 Doctrine_Model 方法,在将新记录插入数据库之前使用 getPrimaryKey 方法检索主键?如果不是,我应该从应用程序中的另一个地方解决这个问题吗?

理想情况下,我希望在使用 Doctrine 关系时使用上述方法生成 ids,例如:

$user = new User():  
$user->name = 'Bob';  
$user->Phonenumbers[]->number = '555-5555';  
$user->save();  

此外,此场景的另一面是从特定分片中选择数据。我当前的想法是维护每个分片上包含的键范围的主列表,如下所示(忽略每个分片的少量行):

Shard A user_id 1-1000
分片 B user_id 1001-2000
Shard C user_id 2001-3000

然后,我可以调用 getShard($table_name, $primary_key) 之类的方法来获取分片,并可能将我的 Doctrine 连接切换到该特定分片。特定用户/实体的相关数据很可能驻留在同一个分片上,因此我不太担心跨越多个分片的关系。不过,这可能是一种可能性,因此我们也将不胜感激有关此问题的任何指导。

I am in the process of creating a centralized ticket server for handing out unique 32bit INT ids. I have a function that can be called within my application that accepts a parameter for the entity/ table, e.g. getPrimaryKey('user'). I got the idea from Flickr via this blog post:

http://code.flickr.com/blog/2010/02/08/ticket-servers-distributed-unique-primary-keys-on-the-cheap/

We will eventually be sharding our data amongst multiple MySQL databases and I would like to get this code up and running as a proof of concept.

My question is what is the best method for using this functionality within Doctrine? Obviously, using the above ticket server, I will need to specify that none of my Doctrine models contain an auto- incrementing primary key.

Is there a Doctrine_Model method I can extend that will retrieve the primary key using my getPrimaryKey method prior to inserting a new record into the database? If not, should I be approaching this problem from another place within my application?

Ideally, I would like the ids to be generated using the above method when using Doctrine relations such as:

$user = new User():  
$user->name = 'Bob';  
$user->Phonenumbers[]->number = '555-5555';  
$user->save();  

Also, the other side of this scenario is in selecting data from a particular shard. My current thought is to maintain a master list of key ranges contained on each shard like this (ignore the small number of rows per shard):

Shard A user_id 1-1000
Shard B user_id 1001-2000
Shard C user_id 2001-3000

I can then call something like getShard($table_name, $primary_key) to get the shard and possibly switch my Doctrine connection to that particular shard. Related data for a particular user/entity will most likely reside on the same shard so I am not too worried relations spanning multiple shards. Though, it may be a possibility, so any guidance on this issue would be greatly appreciated as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文