CouchDB 中的上一个和下一个文档

发布于 2024-12-10 03:57:56 字数 364 浏览 0 评论 0原文

我有一个包含带有浮点时间戳的项目的数据库。当用户访问 url /item/id/ 的页面时,应显示具有相应标识符的文档(这很简单)以及指向上一个和下一个的链接按时间顺序排列下一个文档(如果存在)。

这些链接必须是持久的,并且项目往往会根据外部条件在不同的时间戳出现和消失,因此我无法选择在 URL 中缓存这些标识符之一或两者。

现在,我正在使用按时间戳排序的视图来进行两个查询(一个降序,一个升序),以便获取上一个和下一个文档。

  • 我可以用更少的请求来做到这一点吗?
  • 如果对象的时间戳非常接近,我担心浮点精度会造成混乱。我怎样才能避免这种情况?

I have a database containing items with a floating-point timestamp. When the user visits a page with url /item/id/<the-id-here>, the document with the corresponding identifier should be displayed (this is easy) along with links to the previous and next documents in chronological order, if they exist.

These links must be persistent, and items tend to appear and disappear at various timestamps based on external conditions, so I have no option to cache one or both of these identifiers in URLs.

Right now, I'm using a view sorted by timestamp to make two queries (one descending, one ascending) in order to fetch the previous and next documents.

  • Can I do this with fewer requests?
  • I'm afraid that floating-point precision will cause mayhem if objects have very close timestamps. How can I avoid this?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

关于从前 2024-12-17 03:57:56

是否有可能以某种方式在数据库中维护每个文档的上一个/下一个文档?
这取决于您拥有的读/写比率,但在每次插入/删除时执行这两个查询可能比在每次页面视图时执行更快。

Isn't it possible to somehow maintains in the database the previous/next document for each document ?
It depends of the ratio of reads/writes that you have but it might be faster to do these two queries at each insert/delete than at each page view.

丶情人眼里出诗心の 2024-12-17 03:57:56

不确定您是否仍在努力解决此问题,但如果是,您可以尝试以下操作:

  1. 将时间戳输出为日期数组:[2012, 04, 30, 03, 20, 35]
  2. 使用 ?startkey=[2012, 04, 30, 03, 20, 0]&endkey=[2012, 04, 30, 03, 20, 60]
    获取索引中最后一分钟(或小时、天等)的键/值范围。
    取决于您发出的内容的粒度)。
  3. 在客户端代码中,使用您要查找同级文档的时间戳,然后从数组/哈希表/字典/向量/其他内容中获取其同级文档。

这将使您减少到单个 GET 请求,但代价是更大的响应大小。但是,如果您使用 HTTP If-Not-Match/ETag 标头和一些客户端缓存,则可能可以通过单个请求来完成更大范围的文档 - 缓存一天或一周范围的列表,并使用缓存的数组/任何内容进行以后的查找,对服务器进行“较轻”的点击(因为它只会检查 ETag 值并返回“仅” 304 Not Modified 响应代码。FWIW

(因为它基于 CouchDB 的 View 系统/API)。

,这也适用于 Couchbase Server 2.0 API

Not sure if you're still working on solving this, but if so, you might try the follow:

  1. Output the timestamp as a date array: [2012, 04, 30, 03, 20, 35]
  2. Use ?startkey=[2012, 04, 30, 03, 20, 0]&endkey=[2012, 04, 30, 03, 20, 60] to
    get the range of key/values in the index for the last minute (or hour, day, etc.
    depending on the granularity of what you're emitting).
  3. In the client code use the timestamp of the document you're looking for siblings of, then get its siblings from the array/hash table/dictionary/vector/whatever.

That would get you down to a single GET request with the trade off of a larger response size. However, if you're using the HTTP If-Not-Match/ETag headers and some client-side caching, you may be able to get by with a single request for a larger range of documents--caching a list for the day or week range and using that cached array/whatever for later look-ups with much "lighter" hits to the server (as it would only check the ETag values and return "just" the 304 Not Modified response code.

FWIW, this works with the Couchbase Server 2.0 API as well (as it's based on CouchDB's View's system/API).

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文