Class: CouchRest::Database
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
-
#bulk_save(docs = nil, use_uuids = true) ⇒ Object
(also: #bulk_delete)
POST an array of documents to CouchDB.
-
#compact! ⇒ Object
Compact the database, removing old document revisions and optimizing space use.
-
#copy(doc, dest) ⇒ Object
DEPRECATION NOTICE.
-
#copy_doc(doc, dest) ⇒ Object
COPY an existing document to a new id.
-
#create! ⇒ Object
Create the database.
-
#delete(doc, bulk = false) ⇒ Object
DEPRECATION NOTICE.
-
#delete! ⇒ Object
DELETE the database itself.
-
#delete_attachment(doc, name) ⇒ 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) ⇒ Object
GET a document from CouchDB, by id.
-
#info ⇒ Object
GET the database info from CouchDB.
-
#initialize(server, name) ⇒ Database
constructor
Create a CouchRest::Database adapter for the supplied CouchRest::Server and database name.
-
#move(doc, dest) ⇒ Object
DEPRECATION NOTICE.
-
#move_doc(doc, dest) ⇒ Object
MOVE an existing document to a new id.
-
#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) ⇒ Object
Replicates via “pulling” from another database to this database.
-
#replicate_to(other_db) ⇒ Object
Replicates via “pushing” to another database.
-
#save(doc, bulk = false) ⇒ Object
DEPRECATION NOTICE.
-
#save_doc(doc, bulk = 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.
-
#view(name, params = {}, &block) ⇒ Object
Query a CouchDB view as defined by a
_design
document.
Constructor Details
#initialize(server, name) ⇒ Database
Create a CouchRest::Database adapter for the supplied CouchRest::Server and database name.
Parameters
- server<CouchRest::Server>
-
database host
- name<String>
-
database name
16 17 18 19 20 21 22 23 24 |
# File 'lib/couchrest/core/database.rb', line 16 def initialize(server, name) @name = name @server = server @host = server.uri @uri = @root = "#{host}/#{CGI.escape(name)}" @streamer = Streamer.new(self) @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/couchrest/core/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/couchrest/core/database.rb', line 6 def host @host end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/couchrest/core/database.rb', line 6 def name @name end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
6 7 8 |
# File 'lib/couchrest/core/database.rb', line 6 def root @root end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
6 7 8 |
# File 'lib/couchrest/core/database.rb', line 6 def server @server end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
6 7 8 |
# File 'lib/couchrest/core/database.rb', line 6 def uri @uri end |
Instance Method Details
#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.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/couchrest/core/database.rb', line 166 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 CouchRest.post "#{@uri}/_bulk_docs", {:docs => docs} end |
#compact! ⇒ Object
Compact the database, removing old document revisions and optimizing space use.
246 247 248 |
# File 'lib/couchrest/core/database.rb', line 246 def compact! CouchRest.post "#{@uri}/_compact" end |
#copy(doc, dest) ⇒ Object
DEPRECATION NOTICE
220 221 222 223 |
# File 'lib/couchrest/core/database.rb', line 220 def copy(doc, dest) puts "CouchRest::Database's copy method is being deprecated, please use copy_doc instead" copy_doc(doc, dest) 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
208 209 210 211 212 213 214 215 216 217 |
# File 'lib/couchrest/core/database.rb', line 208 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 CouchRest.copy "#{@uri}/#{slug}", destination end |
#create! ⇒ Object
Create the database
251 252 253 254 |
# File 'lib/couchrest/core/database.rb', line 251 def create! bool = server.create_db(@name) rescue false bool && true end |
#delete(doc, bulk = false) ⇒ Object
DEPRECATION NOTICE
200 201 202 203 |
# File 'lib/couchrest/core/database.rb', line 200 def delete(doc, bulk=false) puts "CouchRest::Database's delete method is being deprecated, please use delete_doc instead" delete_doc(doc, bulk) end |
#delete! ⇒ Object
DELETE the database itself. This is not undoable and could be rather catastrophic. Use with care!
279 280 281 |
# File 'lib/couchrest/core/database.rb', line 279 def delete! CouchRest.delete @uri end |
#delete_attachment(doc, name) ⇒ Object
DELETE an attachment directly from CouchDB
110 111 112 113 114 |
# File 'lib/couchrest/core/database.rb', line 110 def doc, name uri = (doc, name) # this needs a rev JSON.parse(RestClient.delete(uri)) 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.
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/couchrest/core/database.rb', line 188 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']) CouchRest.delete "#{@uri}/#{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/couchrest/core/database.rb', line 37 def documents(params = {}) keys = params.delete(:keys) url = CouchRest.paramify_url "#{@uri}/_all_docs", params if keys CouchRest.post(url, {:keys => keys}) else CouchRest.get url end end |
#fetch_attachment(doc, name) ⇒ Object
GET an attachment directly from CouchDB
93 94 95 96 97 98 99 |
# File 'lib/couchrest/core/database.rb', line 93 def (doc, name) # slug = escape_docid(docid) # name = CGI.escape(name) uri = (doc, name) RestClient.get uri # "#{@uri}/#{slug}/#{name}" end |
#get(id) ⇒ Object
GET a document from CouchDB, by id. Returns a Ruby Hash.
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/couchrest/core/database.rb', line 80 def get(id) slug = escape_docid(id) hash = CouchRest.get("#{@uri}/#{slug}") doc = if /^_design/ =~ hash["_id"] Design.new(hash) else Document.new(hash) end doc.database = self doc end |
#info ⇒ Object
GET the database info from CouchDB
32 33 34 |
# File 'lib/couchrest/core/database.rb', line 32 def info CouchRest.get @uri end |
#move(doc, dest) ⇒ Object
DEPRECATION NOTICE
240 241 242 243 |
# File 'lib/couchrest/core/database.rb', line 240 def move(doc, dest) puts "CouchRest::Database's move method is being deprecated, please use move_doc instead" move_doc(doc, dest) end |
#move_doc(doc, dest) ⇒ Object
MOVE 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
228 229 230 231 232 233 234 235 236 237 |
# File 'lib/couchrest/core/database.rb', line 228 def move_doc(doc, dest) raise ArgumentError, "_id and _rev are required for moving" unless doc['_id'] && doc['_rev'] slug = escape_docid(doc['_id']) destination = if dest.respond_to?(:has_key?) && dest['_id'] && dest['_rev'] "#{dest['_id']}?rev=#{dest['_rev']}" else dest end CouchRest.move "#{@uri}/#{slug}?rev=#{doc['_rev']}", destination end |
#put_attachment(doc, name, file, options = {}) ⇒ Object
PUT an attachment directly to CouchDB
102 103 104 105 106 107 |
# File 'lib/couchrest/core/database.rb', line 102 def (doc, name, file, = {}) docid = escape_docid(doc['_id']) name = CGI.escape(name) uri = (doc, name) JSON.parse(RestClient.put(uri, file, )) end |
#recreate! ⇒ Object
Delete and re create the database
257 258 259 260 261 262 263 |
# File 'lib/couchrest/core/database.rb', line 257 def recreate! delete! create! rescue RestClient::ResourceNotFound ensure create! end |
#replicate_from(other_db) ⇒ Object
Replicates via “pulling” from another database to this database. Makes no attempt to deal with conflicts.
266 267 268 269 |
# File 'lib/couchrest/core/database.rb', line 266 def replicate_from other_db raise ArgumentError, "must provide a CouchReset::Database" unless other_db.kind_of?(CouchRest::Database) CouchRest.post "#{@host}/_replicate", :source => other_db.root, :target => name end |
#replicate_to(other_db) ⇒ Object
Replicates via “pushing” to another database. Makes no attempt to deal with conflicts.
272 273 274 275 |
# File 'lib/couchrest/core/database.rb', line 272 def replicate_to other_db raise ArgumentError, "must provide a CouchReset::Database" unless other_db.kind_of?(CouchRest::Database) CouchRest.post "#{@host}/_replicate", :target => other_db.root, :source => name end |
#save(doc, bulk = false) ⇒ Object
DEPRECATION NOTICE
156 157 158 159 |
# File 'lib/couchrest/core/database.rb', line 156 def save(doc, bulk=false) puts "CouchRest::Database's save method is being deprecated, please use save_doc instead" save_doc(doc, bulk) end |
#save_doc(doc, bulk = 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.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/couchrest/core/database.rb', line 125 def save_doc(doc, bulk = false) if doc['_attachments'] doc['_attachments'] = (doc['_attachments']) end if bulk @bulk_save_cache << doc return 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']) CouchRest.put "#{@uri}/#{slug}", doc else begin slug = doc['_id'] = @server.next_uuid CouchRest.put "#{@uri}/#{slug}", doc rescue #old version of couchdb CouchRest.post @uri, 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.
50 51 52 53 54 55 |
# File 'lib/couchrest/core/database.rb', line 50 def slow_view(funcs, params = {}) keys = params.delete(:keys) funcs = funcs.merge({:keys => keys}) if keys url = CouchRest.paramify_url "#{@uri}/_temp_view", params JSON.parse(RestClient.post(url, funcs.to_json, {"Content-Type" => 'application/json'})) end |
#to_s ⇒ Object
returns the database’s uri
27 28 29 |
# File 'lib/couchrest/core/database.rb', line 27 def to_s @uri 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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/couchrest/core/database.rb', line 62 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 = CouchRest.paramify_url "#{@uri}/_design/#{dname}/_view/#{vname}", params if keys CouchRest.post(url, {:keys => keys}) else if block_given? @streamer.view("_design/#{dname}/_view/#{vname}", params, &block) else CouchRest.get url end end end |