Parent

Ruote::Couch::Database

A database corresponds to a Couch database (not a Couch server).

There is one database per ruote document type (msgs, workitems, expressions, …)

Constants

QUERY_OPTIONS

These options are known and passed to CouchDB.

DESIGN_DOC_REGEX

Used by # and # when filtering design documents out of ‘_all_docs’.

Attributes

type[R]
couch[R]

Public Class Methods

new(host, port, type, name, opts={}) click to toggle source
    # File lib/ruote/couch/database.rb, line 42
42:     def initialize (host, port, type, name, opts={})
43: 
44:       @couch = Rufus::Jig::Couch.new(host, port, name, opts)
45: 
46:       @couch.put('.') unless @couch.get('.')
47: 
48:       @type = type
49: 
50:       prepare
51:     end

Public Instance Methods

delete(doc) click to toggle source
    # File lib/ruote/couch/database.rb, line 69
69:     def delete (doc)
70: 
71:       r = @couch.delete(doc)
72: 
73:       #p [ :del, doc['_id'], Thread.current.object_id.to_s[-3..-1], r.nil? ]
74:       Thread.pass
75:         # without this, test/functional/ct_0 fails after 1 to 10 runs...
76: 
77:       r
78: 
79:     rescue Rufus::Jig::TimeoutError => te
80:       true
81:     end
dump() click to toggle source
     # File lib/ruote/couch/database.rb, line 123
123:     def dump
124: 
125:       s = "=== #{@type} ===\n"
126: 
127:       get_many(nil, {}).inject(s) do |s1, e|
128:         s1 << "\n"
129:         e.keys.sort.inject(s1) do |s2, k|
130:           s2 << "  #{k} => #{e[k].inspect}\n"
131:         end
132:       end
133:     end
get(key, opts={}) click to toggle source
    # File lib/ruote/couch/database.rb, line 64
64:     def get (key, opts={})
65: 
66:       @couch.get(key, opts)
67:     end
get_many(key, opts) click to toggle source

The get_many used by msgs, configurations and variables.

     # File lib/ruote/couch/database.rb, line 85
 85:     def get_many (key, opts)
 86: 
 87:       return query('_all_docs?include_docs=true') if ( ! key) && opts.size < 1
 88: 
 89:       is = ids
 90: 
 91:       return is.length if opts[:count]
 92: 
 93:       is = is.reverse if opts[:descending]
 94: 
 95:       if key
 96:         keys = Array(key).map { |k| k.is_a?(String) ? "!#{k}" : k }
 97:         is = is.select { |i| Ruote::StorageBase.key_match?(keys, i) }
 98:       end
 99: 
100:       skip = opts[:skip] || 0
101:       limit = opts[:limit] || is.length
102: 
103:       is = is[skip, limit]
104: 
105:       query_by_post('_all_docs?include_docs=true', is)
106: 
107:       # TODO
108:       # maybe _count come be of use here
109:       # http://wiki.apache.org/couchdb/Built-In_Reduce_Functions
110:     end
ids() click to toggle source

Returns a sorted list of the ids of all the docs in this database.

     # File lib/ruote/couch/database.rb, line 114
114:     def ids
115: 
116:       @couch.get('_all_docs')['rows'].collect { |r|
117:         r['id']
118:       }.reject { |i|
119:         DESIGN_DOC_REGEX.match(i)
120:       }
121:     end
purge!() click to toggle source

Deletes all the documents in this database.

     # File lib/ruote/couch/database.rb, line 144
144:     def purge!
145: 
146:       @couch.http.cache.clear
147:       @couch.get('_all_docs')['rows'].each do |row|
148:         next if row['id'].match(/^\_design\//)
149:         doc = { '_id' => row['id'], '_rev' => row['value']['rev'] }
150:         @couch.delete(doc)
151:       end
152:         #
153:         # which is faster than
154:         #
155:       #@couch.delete('.')
156:       #@couch.put('.')
157:       #@couch.http.cache.clear
158:     end
put(doc, opts) click to toggle source
    # File lib/ruote/couch/database.rb, line 53
53:     def put (doc, opts)
54: 
55:       doc['put_at'] = Ruote.now_to_utc_s
56: 
57:       @couch.put(doc, :update_rev => opts[:update_rev])
58:         #
59:         # :update_rev => true :
60:         # updating the current doc _rev, this trick allows
61:         # direct "create then apply" chaining
62:     end
shutdown() click to toggle source

Makes sure to close the HTTP connection down.

     # File lib/ruote/couch/database.rb, line 137
137:     def shutdown
138: 
139:       @couch.close
140:     end

Protected Instance Methods

filter_design_docs(docs) click to toggle source
     # File lib/ruote/couch/database.rb, line 187
187:     def filter_design_docs (docs)
188: 
189:       docs.reject { |d| DESIGN_DOC_REGEX.match(d['_id']) }
190:     end
prepare() click to toggle source
     # File lib/ruote/couch/database.rb, line 162
162:     def prepare
163: 
164:       # nothing to do for a index-less database
165:     end
query(uri) click to toggle source
     # File lib/ruote/couch/database.rb, line 192
192:     def query (uri)
193: 
194:       rs = @couch.get(uri)
195: 
196:       filter_design_docs(rs['rows'].collect { |e| e['doc'] })
197:     end
query_by_post(uri, keys) click to toggle source
     # File lib/ruote/couch/database.rb, line 199
199:     def query_by_post (uri, keys)
200: 
201:       keys = { 'keys' => keys }
202: 
203:       rs = @couch.post(uri, keys)
204: 
205:       filter_design_docs(rs['rows'].collect { |e| e['doc'] }.uniq)
206:     end
query_options(opts) click to toggle source

:limit and :skip support

     # File lib/ruote/couch/database.rb, line 173
173:     def query_options (opts)
174: 
175:       opts = opts.select { |k, v| QUERY_OPTIONS.include?(k) && v != nil }
176: 
177:       s = opts.collect { |k, v| "#{k}=#{v}" }.join('&')
178: 
179:       s.length > 0 ? "&#{s}" : ''
180:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.