Module: Aqua::Store::CouchDB::StorageMethods::InstanceMethods
InstanceMethods
Attributes
Instance Attributes
database | [W] | public |
sets the database. |
---|
Public Visibility
Public Instance Method Summary
#commit(defer = false) #save! |
Saves an Aqua::Storage instance to CouchDB as a document. Returns: Aqua::Storage |
---|---|
#database |
retrieves the previously set database or sets the new one with a default value. Returns: Aqua::Store::CouchDB::Database |
#delete(defer = false) |
Deletes an document from CouchDB. Returns: String |
#delete!(defer = false) |
Deletes an document from CouchDB. Returns: String |
#delete_logic(defer = false, mask_exceptions = true) |
Internal logic used by delete and delete! to delete a resource. Returns: String |
#delete_now |
Internal logic used by delete_logic delete a resource immediately. Returns: String |
#determine_database |
Looks to CouchDB. |
#ensure_id |
gets a uuid from the server if one doesn’t exist, otherwise escapes existing id. |
#escape_doc_id |
Escapes document id. |
#escape_for_id(str) |
Escapes a string for id usage. |
#exists? |
Returns true if a document exists at the CouchDB uri for this document. Returns: true |
#id |
setters and getters couchdb document specifics ----------------- Gets the document id. Returns: String |
#id=(str) |
Allows the id to be set. Returns: String |
#new? #new_document? |
Returns true if the document has never been saved or false if it has been saved. Returns: true |
#retrieve |
retrieves self from CouchDB database. Returns: Hash |
#rev |
Returns CouchDB document revision identifier. Returns: String |
#rev=(str) | |
#revisions |
Gets revision history, which is needed by Delete to remove all versions of a document. Returns: Array |
#save(defer = false) |
Saves an Aqua::Storage instance to CouchDB as a document. Returns: Aqua::Storage |
#save_logic(defer = false, mask_exception = true) |
Internal logic used by save, save! and commit to save an object. Returns: Aqua::Storage |
#save_now(mask_exception = true) |
Internal logic used by save_logic to save an object immediately instead of deferring for bulk save. Returns: Aqua::Storage |
#update_version(result) |
Updates the id and rev after a document is successfully saved. |
#uri |
couchdb database url for this document. Returns: String |
Public Instance Method Details
commit
Also known as: save!
Saves an Aqua::Storage instance to CouchDB as a document. Save can be deferred for bulk saving from the database. Unlike #save, this method will raise an error if the document is not saved.
86 87 88 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 86 def commit( defer=false ) save_logic( defer, false ) end |
database
retrieves the previously set database or sets the new one with a default value
241 242 243 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 241 def database @database ||= determine_database end |
delete
Deletes an document from CouchDB. Delete can be deferred for bulk saving/deletion.
161 162 163 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 161 def delete(defer = false) delete_logic( defer ) end |
delete!
Deletes an document from CouchDB. Delete can be deferred for bulk saving/deletion. This version raises an exception if an error other that resource not found is raised.
172 173 174 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 172 def delete!(defer = false) delete_logic( defer, false ) end |
delete_logic
Internal logic used by delete and delete! to delete a resource.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 184 def delete_logic( defer = false, mask_exceptions = true ) if defer database.add_to_bulk_cache( { '_id' => self['_id'], '_rev' => rev, '_deleted' => true } ) else begin delete_now rescue Exception => e if mask_exceptions || e.class == CouchDB::ResourceNotFound false else raise e end end end end |
delete_now
Internal logic used by delete_logic delete a resource immediately.
206 207 208 209 210 211 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 206 def delete_now revisions.each do |rev_id| CouchDB.delete( "#{uri}?rev=#{rev_id}" ) end true end |
determine_database
Looks to CouchDB.database_strategy for information about how the CouchDB store has generally been configured to store its data across databases and/or servers. In some cases the class for the parent object has configuration details about the database and server to use.
250 251 252 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 250 def determine_database Database.create # defaults to database 'aqua' using default server :aqua end |
ensure_id
gets a uuid from the server if one doesn’t exist, otherwise escapes existing id.
332 333 334 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 332 def ensure_id self[:_id] = ( id ? escape_doc_id : database.server.next_uuid ) end |
escape_doc_id
Escapes document id. Different strategies for design documents and normal documents.
338 339 340 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 338 def escape_doc_id escape_for_id( id ) end |
escape_for_id
Escapes a string for id usage
344 345 346 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 344 def escape_for_id( str ) str.match(/^_design\/(.*)/) ? "_design/#{CGI.escape($1)}" : CGI.escape(str) end |
exists?
Returns true if a document exists at the CouchDB uri for this document. Otherwise returns false
321 322 323 324 325 326 327 328 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 321 def exists? begin CouchDB.get uri true rescue false end end |
id
setters and getters couchdb document specifics ----------------- Gets the document id. In this engine id and _id are different data. The main reason for this is that CouchDB needs a relatively clean string as the key, where as the user can assign a messy string to the id. The user can continue to use the messy string since the engine also has access to the _id.
263 264 265 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 263 def id self[:id] end |
id=
Allows the id to be set. If the id is changed after creation, then the CouchDB document for the old id is deleted, and the _rev is set to nil, making it a new document. The id can only be a string (right now).
274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 274 def id=( str ) if str.respond_to?(:match) escaped = escape_for_id( str ) # CLEANUP: do a bulk delete request on the old id, now that it has changed delete(true) if !new? && escaped != self[:_id] self[:id] = str self[:_id] = escaped str end end |
new?
Also known as: new_document?
Returns true if the document has never been saved or false if it has been saved.
313 314 315 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 313 def new? !rev end |
retrieve
retrieves self from CouchDB database
151 152 153 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 151 def retrieve self.class.new( CouchDB.get( uri ) ) end |
rev
Returns CouchDB document revision identifier.
292 293 294 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 292 def rev self[:_rev] end |
rev=
297 298 299 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 297 def rev=( str ) self[:_rev] = str end |
revisions
Gets revision history, which is needed by Delete to remove all versions of a document
218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 218 def revisions active_revisions = [] begin hash = CouchDB.get( "#{uri}?revs_info=true" ) rescue return active_revisions end hash['_revs_info'].each do |rev_hash| active_revisions << rev_hash['rev'] if ['disk', 'available'].include?( rev_hash['status'] ) end active_revisions end |
save
Saves an Aqua::Storage instance to CouchDB as a document. Save can be deferred for bulk saving.
75 76 77 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 75 def save( defer=false ) save_logic( defer ) end |
save_logic
Internal logic used by save, save! and commit to save an object.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 99 def save_logic( defer=false, mask_exception = true ) if self[:_attachment] ensure_id if defer database.add_to_bulk_cache( self ) else # clear any bulk saving left over ... database.bulk_save if database.bulk_cache.size > 0 if mask_exception save_now else save_now( false ) end end end |
save_now
Internal logic used by save_logic to save an object immediately instead of deferring for bulk save.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 122 def save_now( mask_exception = true ) begin result = CouchDB.put( uri, self ) rescue Exception => e if mask_exception result = false else raise e end end if result && result['ok'] update_version( result ) self else result end end |
update_version
Updates the id and rev after a document is successfully saved.
305 306 307 308 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 305 def update_version( result ) self.id = result['id'] self.rev = result['rev'] end |
uri
couchdb database url for this document
144 145 146 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 144 def uri database.uri + '/' + ensure_id end |