Class: Aqua::Store::CouchDB::Database
- Inherits:
-
Object
- Object
- Aqua::Store::CouchDB::Database
- Defined in:
- lib/aqua/store/couch_db/database.rb
Instance Attribute Summary collapse
-
#bulk_cache ⇒ Object
Returns the value of attribute bulk_cache.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Class Method Summary collapse
-
.create(name = nil, opts = {}) ⇒ Database, false
Creates a database representation and PUTs it on the CouchDB server.
-
.create!(name = nil, opts = {}) ⇒ Database
Creates a database representation and PUTs it on the CouchDB server.
Instance Method Summary collapse
-
#add_to_bulk_cache(doc) ⇒ Object
BULK ACTIVITIES ——————————————.
- #bulk_save ⇒ Object
-
#delete ⇒ Object
Deletes a database; use with caution as this isn’t reversible.
-
#delete! ⇒ Object
Deletes a database; use with caution as this isn’t reversible.
-
#delete_all ⇒ Object
Deletes all the documents in a given database.
-
#documents(params = {}) ⇒ Object
Query the
documents
view. -
#exists? ⇒ true, false
Checks to see if the database exists on the couchdb server.
-
#info ⇒ Object
GET the database info from CouchDB.
-
#initialize(name = nil, opts = {}) ⇒ Database
constructor
Create a CouchDB database representation from a name.
-
#initialize_server(server_option) ⇒ Server
private
Initializes the database server with the option provided.
-
#namespaced(name) ⇒ String
private
Namespaces the database path for the given server.
Constructor Details
#initialize(name = nil, opts = {}) ⇒ Database
Create a CouchDB database representation from a name. Does not actually create a database on couchdb. It does not ensure that the database actually exists either. Just creates a ruby representation of a ruby database interface.
19 20 21 22 23 24 25 26 |
# File 'lib/aqua/store/couch_db/database.rb', line 19 def initialize( name=nil, opts={}) name = nil if name && name.empty? opts = Mash.new( opts ) unless opts.empty? @name = name if name initialize_server( opts[:server] ) @uri = "#{server.uri}/#{namespaced( name )}" self.bulk_cache = [] end |
Instance Attribute Details
#bulk_cache ⇒ Object
Returns the value of attribute bulk_cache.
8 9 10 |
# File 'lib/aqua/store/couch_db/database.rb', line 8 def bulk_cache @bulk_cache end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/aqua/store/couch_db/database.rb', line 7 def name @name end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
7 8 9 |
# File 'lib/aqua/store/couch_db/database.rb', line 7 def server @server end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
7 8 9 |
# File 'lib/aqua/store/couch_db/database.rb', line 7 def uri @uri end |
Class Method Details
.create(name = nil, opts = {}) ⇒ Database, false
Creates a database representation and PUTs it on the CouchDB server. If successfull returns a database object. If not successful in creating the database on the CouchDB server then, false will be returned.
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/aqua/store/couch_db/database.rb', line 78 def self.create( name=nil, opts={} ) db = new(name, opts) begin CouchDB.put( db.uri ) rescue Exception => e # catch database already exists errors ... unless e..match(/412/) db = false end end db end |
.create!(name = nil, opts = {}) ⇒ Database
Creates a database representation and PUTs it on the CouchDB server. This version of the #create method raises an error if the PUT request fails. The exception on this, is if the database already exists then the 412 HTTP code will be ignored.
99 100 101 102 103 104 105 106 107 |
# File 'lib/aqua/store/couch_db/database.rb', line 99 def self.create!( name=nil, opts={} ) db = new( name, opts ) begin CouchDB.put( db.uri ) rescue Exception => e # catch database already exists errors ... raise e unless e.class == RequestFailed && e..match(/412/) end db end |
Instance Method Details
#add_to_bulk_cache(doc) ⇒ Object
BULK ACTIVITIES ——————————————
168 169 170 171 172 173 174 175 |
# File 'lib/aqua/store/couch_db/database.rb', line 168 def add_to_bulk_cache( doc ) if server.uuid_count/2.0 > bulk_cache.size self.bulk_cache << doc else bulk_save self.bulk_cache << doc end end |
#bulk_save ⇒ Object
177 178 179 180 181 |
# File 'lib/aqua/store/couch_db/database.rb', line 177 def bulk_save docs = bulk_cache self.bulk_cache = [] CouchDB.post( "#{uri}/_bulk_docs", {:docs => docs} ) end |
#delete ⇒ Object
Deletes a database; use with caution as this isn’t reversible.
132 133 134 135 136 137 138 |
# File 'lib/aqua/store/couch_db/database.rb', line 132 def delete begin CouchDB.delete( uri ) rescue CouchDB::ResourceNotFound nil end end |
#delete! ⇒ Object
Deletes a database; use with caution as this isn’t reversible. Similar to #delete, except that it will raise an error on failure to find the database.
145 146 147 |
# File 'lib/aqua/store/couch_db/database.rb', line 145 def delete! CouchDB.delete( uri ) end |
#delete_all ⇒ Object
Deletes all the documents in a given database
161 162 163 164 165 |
# File 'lib/aqua/store/couch_db/database.rb', line 161 def delete_all documents['rows'].each do |doc| CouchDB.delete( "#{uri}/#{CGI.escape( doc['id'])}?rev=#{doc['value']['rev']}" ) #rescue nil end end |
#documents(params = {}) ⇒ Object
Query the documents
view. Accepts all the same arguments as view.
150 151 152 153 154 155 156 157 158 |
# File 'lib/aqua/store/couch_db/database.rb', line 150 def documents(params = {}) keys = params.delete(:keys) url = CouchDB.paramify_url( "#{uri}/_all_docs", params ) if keys CouchDB.post(url, {:keys => keys}) else CouchDB.get url end end |
#exists? ⇒ true, false
Checks to see if the database exists on the couchdb server.
114 115 116 117 118 119 120 121 |
# File 'lib/aqua/store/couch_db/database.rb', line 114 def exists? begin info true rescue CouchDB::ResourceNotFound false end end |
#info ⇒ Object
GET the database info from CouchDB
124 125 126 |
# File 'lib/aqua/store/couch_db/database.rb', line 124 def info CouchDB.get( uri ) end |
#initialize_server(server_option) ⇒ Server
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes the database server with the option provided. If not option is provided the default CouchDB server is used instead.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/aqua/store/couch_db/database.rb', line 40 def initialize_server( server_option ) if server_option if server_option.class == Symbol @server = CouchDB.server( server_option ) elsif server_option.class == Aqua::Store::CouchDB::Server @server = server_option # WARNING: this won't get stashed in CouchDB for use with other database. else raise ArgumentError, ":server option must be a symbol identifying a CouchDB server, or a Server object" end else @server = CouchDB.server end @server end |
#namespaced(name) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Namespaces the database path for the given server. If no name is provided, then the database name is just the Server’s namespace.
62 63 64 65 66 67 68 |
# File 'lib/aqua/store/couch_db/database.rb', line 62 def namespaced( name ) if name "#{server.namespace}_#{CouchDB.escape(@name)}" else server.namespace end end |