rethinkdb 什么时候会返回一个游标 [英] When will rethinkdb return a cursor
问题描述
我注意到 r.table('xxx')
并不总是返回一个 cursor
但有时也只是直接返回 docs
是
cursor
客户端的唯一实现还是server
做了一些特殊的事情来执行与cursor
相关的查询?如果它有与
server
相关的东西,它是什么,我什么时候会收到cursor
<块引用>
例如,我在查询中使用 skip
和 limit
指定结果偏移量和大小.服务器会返回 cursor
还是只返回结果 docs
?
当查询返回一个流时,驱动程序返回一个游标.
基本上当服务器产生一个流(一个延迟计算的序列)时,驱动程序将返回一个游标.当您从游标中获取行时,服务器将计算序列中的更多元素.
例如,当您运行 r.table('xxx')
时,您将返回一个游标.当您通过驱动程序请求文件时,服务器将从磁盘加载文件.
在 JavaScript 驱动程序中,当查询返回一个数组时,驱动程序将潜入一个模仿数组本身和 Array.prototype 之间的游标接口的对象.所以如果 query.run(...)
返回一个序列,你可以做
query.run(connection).then(function(result) {返回结果.toArray()}).then(函数(结果){//对结果做一些事情}).错误(函数(错误){//处理错误})
基本上,如果您不想考虑返回的是游标还是数组,您可以将其视为游标.
您可以在此处阅读有关流/游标的更多信息:
http://www.rethinkdb.com/docs/data-types/>
I notice that r.table('xxx')
not always return a cursor
but also sometimes just return docs
directly
Is the
cursor
client side implementation only or there are some special thingsserver
did to perform queries associate with acursor
?If it has somethings related to
server
, what is it and when will I receive acursor
For example I specify result offset and size with
skip
andlimit
in the query. Will server return acursor
or just resultdocs
?
A driver returns a cursor when the query returns a stream.
Basically when the server produces a stream (a sequence that is lazily computed), the driver will return a cursor. As you fetch rows from the cursor, the server will compute more elements in the sequence.
For example, when you run r.table('xxx')
, you will get back a cursor. The server will load the documents from disk as you request them with the driver.
In the JavaScript driver, when a query return an array, the driver will sneak an object that mimics the cursor interface between the arrray itself and Array.prototype.
So if query.run(...)
returns a sequence, you can just do
query.run(connection).then(function(result) {
return result.toArray()
}).then(function(result) {
// do something with result
}).error(function(err) {
// handle err
})
Basically if you don't want to think if you are getting back a cursor or an array, you can just consider that it's a cursor.
You can read more about stream/cursor here:
http://www.rethinkdb.com/docs/data-types/
这篇关于rethinkdb 什么时候会返回一个游标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!