根据事件日期查找日期范围的算法
我正在编写一个 PHP 函数,该函数将使用一个表格来根据我拥有的日期戳查找应用程序应该转到哪个数据库分片。
分片配置是这样的(伪代码):第一列是我正在查找的事件的日期,第二列是事件所在的分片。
pre-2008 -> shard1
2008-2009 -> shard2
2009_01-2009_06 -> shard3
2009_07 -> shard4
2009_08 -> shard5
2009_09 and up -> shard6
如您所见,我想要的配置非常灵活- 它可以采用任何日期范围,无论大小,并映射到分片。
我正在寻找根据给定日期进行查找的最快方法。
例如,如果我的日期是2009-05-02,那么我所寻找的分片是shard3。如果日期是 2007-08-01,那么它就是 shard1。
实际 PHP 代码的奖励积分,因为应用程序是用 PHP 编写的。
谢谢。
I'm writing a PHP function that would use a table of sorts to look up which DB shard the application should go to, based on the datestamp I have.
The shard configuration is something like this (pseudo-code): the first column is the date of the event I'm looking for and the 2nd is the shard the event resides in.
pre-2008 -> shard1
2008-2009 -> shard2
2009_01-2009_06 -> shard3
2009_07 -> shard4
2009_08 -> shard5
2009_09 and up -> shard6
As you can see, the configuration I want is pretty flexible - it can take any date range, however small or big and map to a shard.
I am looking for the quickest way to do a lookup based on a given date.
For example, if my date is 2009-05-02, the shard I am after is shard3. If the date is 2007-08-01, then it's shard1.
Bonus points for actual PHP code, as the application is in PHP.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜您不想希望日期范围中有漏洞,所以我建议您
只需为每个分片指定一个结束日期,并明确命名一个默认分片
它包含所有太新而无法放入其他分片之一的内容。
这使得获得正确的日期变得容易,并且根据日期查找分片的代码很简单:
编辑:使用静态变量,以便排序仅完成一次。
I am guessing that you don't want to have holes in date ranges, so I propose that you
just need to specify an end date for each shard, and explicitly name one Default Shard
which holds everything that is too new to fit into one of the other shards.
This makes it easy to get the dates right, and the code for finding a shard based on date is simple:
Edit: Used static variables so that sorting is only done once.
输出:
Outputs:
由于您的分片可以严格排序,因此似乎将它们存储在二叉树中,然后简单地通过该树运行二分搜索就会给您最快的结果。
Since your shards can be strictly ordered, it seems like storing them in a binary tree and then simply running a binary search through that tree would give you the fastest results.