Class: Sova::Database
- Inherits:
-
Object
- Object
- Sova::Database
- Defined in:
- lib/sova/database.rb
Instance Attribute Summary collapse
-
#bulk_save_cache_limit ⇒ Object
Returns the value of attribute bulk_save_cache_limit.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#batch_save_doc(doc) ⇒ Object
Save a document to CouchDB in batch mode.
-
#bulk_save(docs = nil, use_uuids = true) ⇒ Object
(also: #bulk_delete)
POST an array of documents to CouchDB.
-
#bulk_save_doc(doc) ⇒ Object
Save a document to CouchDB in bulk mode.
-
#compact! ⇒ Object
Compact the database, removing old document revisions and optimizing space use.
-
#copy_doc(doc, dest) ⇒ Object
COPY an existing document to a new id.
-
#create! ⇒ Object
Create the database.
-
#delete! ⇒ Object
DELETE the database itself.
-
#delete_attachment(doc, name, force = false) ⇒ Object
DELETE an attachment directly from CouchDB.
-
#delete_doc(doc, bulk = false) ⇒ Object
DELETE the document from CouchDB that has the given
_id
and_rev
. -
#documents(params = {}) ⇒ Object
Query the
_all_docs
view. -
#fetch_attachment(doc, name) ⇒ Object
GET an attachment directly from CouchDB.
-
#get(id, params = {}) ⇒ Object
GET a document from CouchDB, by id.
-
#get_bulk(ids) ⇒ Object
(also: #bulk_load)
load a set of documents by passing an array of ids.
-
#info ⇒ Object
GET the database info from CouchDB.
-
#initialize(server, name) ⇒ Database
constructor
Create a Sova::Database adapter for the supplied Sova::Server and database name.
-
#put_attachment(doc, name, file, options = {}) ⇒ Object
PUT an attachment directly to CouchDB.
-
#recreate! ⇒ Object
Delete and re create the database.
-
#replicate_from(other_db, continuous = false, create_target = false) ⇒ Object
Replicates via “pulling” from another database to this database.
-
#replicate_to(other_db, continuous = false, create_target = false) ⇒ Object
Replicates via “pushing” to another database.
-
#save_doc(doc, bulk = false, batch = false) ⇒ Object
Save a document to CouchDB.
-
#slow_view(funcs, params = {}) ⇒ Object
(also: #temp_view)
POST a temporary view function to CouchDB for querying.
-
#to_s ⇒ Object
returns the database’s uri.
-
#update_doc(doc_id, params = {}, update_limit = 10) ⇒ Object
Updates the given doc by yielding the current state of the doc and trying to update update_limit times.
-
#view(name, params = {}, &block) ⇒ Object
Query a CouchDB view as defined by a
_design
document.
Constructor Details
#initialize(server, name) ⇒ Database
Create a Sova::Database adapter for the supplied Sova::Server and database name.
Parameters
- server<Sova::Server>
-
database host
- name<String>
-
database name
16 17 18 19 20 21 22 23 24 |
# File 'lib/sova/database.rb', line 16 def initialize(server, name) @name = name @server = server @host = server.uri @uri = "/#{name.gsub('/','%2F')}" @root = host + uri @bulk_save_cache = [] @bulk_save_cache_limit = 500 # must be smaller than the uuid count end |
Instance Attribute Details
#bulk_save_cache_limit ⇒ Object
Returns the value of attribute bulk_save_cache_limit.
7 8 9 |
# File 'lib/sova/database.rb', line 7 def bulk_save_cache_limit @bulk_save_cache_limit end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
6 7 8 |
# File 'lib/sova/database.rb', line 6 def host @host end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/sova/database.rb', line 6 def name @name end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
6 7 8 |
# File 'lib/sova/database.rb', line 6 def root @root end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
6 7 8 |
# File 'lib/sova/database.rb', line 6 def server @server end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
6 7 8 |
# File 'lib/sova/database.rb', line 6 def uri @uri end |
Instance Method Details
#batch_save_doc(doc) ⇒ Object
Save a document to CouchDB in batch mode. See #save_doc’s batch
argument.
185 186 187 |
# File 'lib/sova/database.rb', line 185 def batch_save_doc(doc) save_doc(doc, false, true) end |
#bulk_save(docs = nil, use_uuids = true) ⇒ Object Also known as: bulk_delete
POST an array of documents to CouchDB. If any of the documents are missing ids, supply one from the uuid cache.
If called with no arguments, bulk saves the cache of documents to be bulk saved.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/sova/database.rb', line 193 def bulk_save(docs = nil, use_uuids = true) if docs.nil? docs = @bulk_save_cache @bulk_save_cache = [] end if (use_uuids) ids, noids = docs.partition{|d|d['_id']} uuid_count = [noids.length, @server.uuid_batch_count].max noids.each do |doc| nextid = @server.next_uuid(uuid_count) rescue nil doc['_id'] = nextid if nextid end end HTTP.post "#{@root}/_bulk_docs", {:docs => docs} end |
#bulk_save_doc(doc) ⇒ Object
Save a document to CouchDB in bulk mode. See #save_doc’s bulk
argument.
180 181 182 |
# File 'lib/sova/database.rb', line 180 def bulk_save_doc(doc) save_doc(doc, true) end |
#compact! ⇒ Object
Compact the database, removing old document revisions and optimizing space use.
264 265 266 |
# File 'lib/sova/database.rb', line 264 def compact! HTTP.post "#{@root}/_compact" end |
#copy_doc(doc, dest) ⇒ Object
COPY an existing document to a new id. If the destination id currently exists, a rev must be provided. dest
can take one of two forms if overwriting: “id_to_overwrite?rev=revision” or the actual doc hash with a ‘_rev’ key
229 230 231 232 233 234 235 236 237 238 |
# File 'lib/sova/database.rb', line 229 def copy_doc(doc, dest) raise ArgumentError, "_id is required for copying" unless doc['_id'] slug = escape_docid(doc['_id']) destination = if dest.respond_to?(:has_key?) && dest['_id'] && dest['_rev'] "#{dest['_id']}?rev=#{dest['_rev']}" else dest end HTTP.copy "#{@root}/#{slug}", destination end |
#create! ⇒ Object
Create the database
269 270 271 272 |
# File 'lib/sova/database.rb', line 269 def create! bool = server.create_db(@name) rescue false bool && true end |
#delete! ⇒ Object
DELETE the database itself. This is not undoable and could be rather catastrophic. Use with care!
295 296 297 |
# File 'lib/sova/database.rb', line 295 def delete! HTTP.delete @root end |
#delete_attachment(doc, name, force = false) ⇒ Object
DELETE an attachment directly from CouchDB
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/sova/database.rb', line 103 def (doc, name, force=false) uri = (doc, name) # this needs a rev begin HTTP.delete(uri) rescue Exception => error if force # get over a 409 doc = get(doc['_id']) uri = (doc, name) HTTP.delete(uri) else raise error end end end |
#delete_doc(doc, bulk = false) ⇒ Object
DELETE the document from CouchDB that has the given _id
and _rev
.
If bulk
is true (false by default) the deletion is recorded for bulk-saving (bulk-deletion :) later. Bulk saving happens automatically when #bulk_save_cache limit is exceded, or on the next non bulk save.
215 216 217 218 219 220 221 222 223 224 |
# File 'lib/sova/database.rb', line 215 def delete_doc(doc, bulk = false) raise ArgumentError, "_id and _rev required for deleting" unless doc['_id'] && doc['_rev'] if bulk @bulk_save_cache << { '_id' => doc['_id'], '_rev' => doc['_rev'], '_deleted' => true } return bulk_save if @bulk_save_cache.length >= @bulk_save_cache_limit return { "ok" => true } # Mimic the non-deferred version end slug = escape_docid(doc['_id']) HTTP.delete "#{@root}/#{slug}?rev=#{doc['_rev']}" end |
#documents(params = {}) ⇒ Object
Query the _all_docs
view. Accepts all the same arguments as view.
37 38 39 40 41 42 43 44 45 |
# File 'lib/sova/database.rb', line 37 def documents(params = {}) keys = params.delete(:keys) url = Sova.paramify_url "#{@root}/_all_docs", params if keys HTTP.post(url, {:keys => keys}) else HTTP.get url end end |
#fetch_attachment(doc, name) ⇒ Object
GET an attachment directly from CouchDB
89 90 91 92 |
# File 'lib/sova/database.rb', line 89 def (doc, name) uri = (doc, name) HTTP.request(:get, uri).body end |
#get(id, params = {}) ⇒ Object
GET a document from CouchDB, by id. Returns a Ruby Hash.
82 83 84 85 86 |
# File 'lib/sova/database.rb', line 82 def get(id, params = {}) slug = escape_docid(id) url = Sova.paramify_url("#{@root}/#{slug}", params) result = HTTP.get(url) end |
#get_bulk(ids) ⇒ Object Also known as: bulk_load
load a set of documents by passing an array of ids
48 49 50 |
# File 'lib/sova/database.rb', line 48 def get_bulk(ids) documents(:keys => ids, :include_docs => true) end |
#info ⇒ Object
GET the database info from CouchDB
32 33 34 |
# File 'lib/sova/database.rb', line 32 def info HTTP.get @root end |
#put_attachment(doc, name, file, options = {}) ⇒ Object
PUT an attachment directly to CouchDB
95 96 97 98 99 100 |
# File 'lib/sova/database.rb', line 95 def (doc, name, file, = {}) docid = escape_docid(doc['_id']) uri = (doc, name) response = HTTP.request(:put, uri, file, ) JSON.parse(response.body) end |
#recreate! ⇒ Object
Delete and re create the database
275 276 277 278 279 280 281 |
# File 'lib/sova/database.rb', line 275 def recreate! delete! create! rescue Sova::NotFound ensure create! end |
#replicate_from(other_db, continuous = false, create_target = false) ⇒ Object
Replicates via “pulling” from another database to this database. Makes no attempt to deal with conflicts.
284 285 286 |
# File 'lib/sova/database.rb', line 284 def replicate_from(other_db, continuous = false, create_target = false) replicate(other_db, continuous, :target => name, :create_target => create_target) end |
#replicate_to(other_db, continuous = false, create_target = false) ⇒ Object
Replicates via “pushing” to another database. Makes no attempt to deal with conflicts.
289 290 291 |
# File 'lib/sova/database.rb', line 289 def replicate_to(other_db, continuous = false, create_target = false) replicate(other_db, continuous, :source => name, :create_target => create_target) end |
#save_doc(doc, bulk = false, batch = false) ⇒ Object
Save a document to CouchDB. This will use the _id
field from the document as the id for PUT, or request a new UUID from CouchDB, if no _id
is present on the document. IDs are attached to documents on the client side because POST has the curious property of being automatically retried by proxies in the event of network segmentation and lost responses.
If bulk
is true (false by default) the document is cached for bulk-saving later. Bulk saving happens automatically when #bulk_save_cache limit is exceded, or on the next non bulk save.
If batch
is true (false by default) the document is saved in batch mode, “used to achieve higher throughput at the cost of lower guarantees. When […] sent using this option, it is not immediately written to disk. Instead it is stored in memory on a per-user basis for a second or so (or the number of docs in memory reaches a certain point). After the threshold has passed, the docs are committed to disk. Instead of waiting for the doc to be written to disk before responding, CouchDB sends an HTTP 202 Accepted response immediately. batch=ok is not suitable for crucial data, but it ideal for applications like logging which can accept the risk that a small proportion of updates could be lost due to a crash.”
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/sova/database.rb', line 141 def save_doc(doc, bulk = false, batch = false) if doc['_attachments'] doc['_attachments'] = (doc['_attachments']) end if bulk @bulk_save_cache << doc bulk_save if @bulk_save_cache.length >= @bulk_save_cache_limit return {"ok" => true} # Compatibility with Document#save elsif !bulk && @bulk_save_cache.length > 0 bulk_save end result = if doc['_id'] slug = escape_docid(doc['_id']) begin uri = "#{@root}/#{slug}" uri << "?batch=ok" if batch HTTP.put uri, doc rescue Sova::NotFound p "resource not found when saving even tho an id was passed" slug = doc['_id'] = @server.next_uuid HTTP.put "#{@root}/#{slug}", doc end else begin slug = doc['_id'] = @server.next_uuid HTTP.put "#{@root}/#{slug}", doc rescue #old version of couchdb HTTP.post @root, doc end end if result['ok'] doc['_id'] = result['id'] doc['_rev'] = result['rev'] doc.database = self if doc.respond_to?(:database=) end result end |
#slow_view(funcs, params = {}) ⇒ Object Also known as: temp_view
POST a temporary view function to CouchDB for querying. This is not recommended, as you don’t get any performance benefit from CouchDB’s materialized views. Can be quite slow on large databases.
56 57 58 59 60 61 |
# File 'lib/sova/database.rb', line 56 def slow_view(funcs, params = {}) keys = params.delete(:keys) funcs = funcs.merge({:keys => keys}) if keys url = Sova.paramify_url "#{@root}/_temp_view", params HTTP.post(url, funcs) end |
#to_s ⇒ Object
returns the database’s uri
27 28 29 |
# File 'lib/sova/database.rb', line 27 def to_s @root end |
#update_doc(doc_id, params = {}, update_limit = 10) ⇒ Object
Updates the given doc by yielding the current state of the doc and trying to update update_limit times. Returns the new doc if the doc was successfully updated without hitting the limit
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/sova/database.rb', line 243 def update_doc(doc_id, params = {}, update_limit=10) resp = {'ok' => false} new_doc = nil last_fail = nil until resp['ok'] or update_limit <= 0 doc = self.get(doc_id, params) # grab the doc new_doc = yield doc # give it to the caller to be updated begin resp = self.save_doc new_doc # try to PUT the updated doc into the db rescue Sova::Conflict => e update_limit -= 1 last_fail = e end end raise last_fail unless resp['ok'] new_doc end |
#view(name, params = {}, &block) ⇒ Object
Query a CouchDB view as defined by a _design
document. Accepts paramaters as described in wiki.apache.org/couchdb/HttpViewApi
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/sova/database.rb', line 68 def view(name, params = {}, &block) keys = params.delete(:keys) name = name.split('/') # I think this will always be length == 2, but maybe not... dname = name.shift vname = name.join('/') url = Sova.paramify_url "#{@root}/_design/#{dname}/_view/#{vname}", params if keys HTTP.post(url, {:keys => keys}) else HTTP.get url end end |