Class: CouchRest::Pager
- Inherits:
-
Object
- Object
- CouchRest::Pager
- Defined in:
- lib/couchrest/helper/pager.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
Returns the value of attribute db.
Instance Method Summary collapse
- #all_docs(limit = 100, &block) ⇒ Object
-
#initialize(db) ⇒ Pager
constructor
A new instance of Pager.
- #key_reduce(view, limit = 2000, firstkey = nil, lastkey = nil, &block) ⇒ Object
Constructor Details
#initialize(db) ⇒ Pager
Returns a new instance of Pager.
4 5 6 |
# File 'lib/couchrest/helper/pager.rb', line 4 def initialize db @db = db end |
Instance Attribute Details
#db ⇒ Object
Returns the value of attribute db.
3 4 5 |
# File 'lib/couchrest/helper/pager.rb', line 3 def db @db end |
Instance Method Details
#all_docs(limit = 100, &block) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/couchrest/helper/pager.rb', line 8 def all_docs(limit=100, &block) startkey = nil oldend = nil while docrows = request_all_docs(limit+1, startkey) startkey = docrows.last['key'] docrows.pop if docrows.length > limit if oldend == startkey break end yield(docrows) oldend = startkey end end |
#key_reduce(view, limit = 2000, firstkey = nil, lastkey = nil, &block) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/couchrest/helper/pager.rb', line 23 def key_reduce(view, limit=2000, firstkey = nil, lastkey = nil, &block) # start with no keys startkey = firstkey # lastprocessedkey = nil keepgoing = true while keepgoing && viewrows = request_view(view, limit, startkey) startkey = viewrows.first['key'] endkey = viewrows.last['key'] if (startkey == endkey) # we need to rerequest to get a bigger page # so we know we have all the rows for that key viewrows = @db.view(view, :key => startkey)['rows'] # we need to do an offset thing to find the next startkey # otherwise we just get stuck lastdocid = viewrows.last['id'] fornextloop = @db.view(view, :startkey => startkey, :startkey_docid => lastdocid, :limit => 2)['rows'] newendkey = fornextloop.last['key'] if (newendkey == endkey) keepgoing = false else startkey = newendkey end rows = viewrows else rows = [] for r in viewrows if (lastkey && r['key'] == lastkey) keepgoing = false break end break if (r['key'] == endkey) rows << r end startkey = endkey end key = :begin values = [] rows.each do |r| if key != r['key'] # we're on a new key, yield the old first and then reset yield(key, values) if key != :begin key = r['key'] values = [] end # keep accumulating values << r['value'] end yield(key, values) end end |