简体   繁体   English

重新生成CouchDB视图

[英]Regenerating CouchDB views

Contrived example: 举例:

{
  productName: 'Lost Series 67 DVD',
  availableFrom: '19/May/2011',
  availableTo: '19/Sep/2011'
}

View storeFront/currentlyAvailableProducts basically checks if current datetime is within availableFrom - availableTo and emits the doc. 查看storeFront / currentAvailableProducts基本上检查当前日期时间是否在availableFrom - availableTo内并发出doc。

I would like to force a view to regenerate at 1am every night, ie process/map all docs. 我想强制一个视图在每天凌晨1点重新生成,即处理/映射所有文档。

At first I had a simple python script scheduled via crontab that touched each document hence causing a new revision and the view to update,however since couchdb is append only this wasnt very efficient - ie loads of unnecessary IO and disk space usage followed by compaction, very resource wasteful on all fronts. 起初我有一个简单的python脚本通过crontab调度触及每个文档,因此导致新版本和视图更新,但是因为couchdb只是附加,这不是非常有效 - 即加载不必要的IO和磁盘空间使用后紧接着,在各方面都非常浪费资源。

Second solution was to push the view definition again via couchapp push however this meant the view was unavailable (or partially unavailable) for several minutes which was also unacceptable. 第二个解决方案是通过couchapp push再次推送视图定义,但是这意味着视图在几分钟内不可用(或部分不可用),这也是不可接受的。

Is there any other solutions? 还有其他解决方案吗?

Will's answer is great; 威尔的答案很棒; but just to get the consensus viewpoint represented here: 但只是为了得到这里所代表的共识观点:

Keep one view, and query it differently every day 保留一个视图,每天查询不同的视图

Determine your time-slice size, for example one day. 确定您的时间片大小,例如一天。

Next, for each document, you emit once for every time slice (day) that it is available. 接下来,对于每个文档,您为每个可用的时间片(日)发出一次。 So if a document is available from 19 May to 21 May (inclusive), your emit keys would be: 因此,如果文档在5月19日至5月21日(含)之间可用,您的发射键将是:

"2011-05-19"
"2011-05-20"
"2011-05-21"

Once that is computed for every document, to find docs available on a certain day, just query the view with (eg today) ?key="2011-05-18" . 一旦为每个文档计算,找到某一天可用的文档,只需查询视图(例如今天) ?key="2011-05-18"

You never have to update or re-run your views. 您永远不必更新或重新运行您的视图。

If you must never change your query URL for some reason, you might be able to use a _show function to 302 (temporary) redirect to today's correct query. 如果由于某种原因必须永远不会更改查询URL,则可以使用_show函数将302(临时)重定向到今天的正确查询。

So your view is not being updated automatically I take it? 所以你的视图没有自动更新我接受了吗?

New and changed documents are not being added on the fly? 新增和更改的文档没有动态添加?

Oh I see, you're cheating. 哦,我明白了,你是在作弊。 You're using "out of document" information (ie the current date) during view creation. 您在视图创建期间使用“缺少文档”信息(即当前日期)。

There's no view renaming, but if you were desperate you could use url rewriting. 没有视图重命名,但如果你绝望,你可以使用网址重写。

Simply create a design document "each day": /db/_design/today05172011 只需“每天”创建一个设计文档:/ db / _design / today05172011

Then use some url rewriting to change: GET /db/_design/today/_view/yourview 然后使用一些url重写来更改:GET / db / _design / today / _view / yourview

to: GET /db/_design/today051711/_view/yourview to:GET / db / _design / today051711 / _view / yourview

Create the view at 11pm server time (tweak it so that "now" is "tomorrow", or whatever). 在服务器时间晚上11点创建视图(调整它以使“now”是“明天”,或者其他)。

Then add some more clean up code to later delete the older views. 然后添加一些清理代码以便以后删除旧视图。

This way your view builds each night as you like. 这样您的视图就可以随心所欲地构建。

Obviously you'll need to front Couch with some other web server/proxy to pull this off. 显然你需要将Couch与其他一些Web服务器/代理连接起来才能解决这个问题。

It's elegant, and inelegant, at the same time. 它既优雅又不雅,同时也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM