Class: Aqua::Store::CouchDB::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/aqua/store/couch_db/database.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • Name (optional String)

    of database. If not provided server namespace will be used as database name.

  • Options (optional Hash)

    for initialization. Currently the only option is :server which must be either a CouchDB server object or a symbol representing a server stored in the CouchDB module.



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_cacheObject

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

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/aqua/store/couch_db/database.rb', line 7

def name
  @name
end

#serverObject (readonly)

Returns the value of attribute server.



7
8
9
# File 'lib/aqua/store/couch_db/database.rb', line 7

def server
  @server
end

#uriObject (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.

Returns:

  • (Database, false)

    Will return the database on success, and false if it did not succeed.

See Also:

  • for option details


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.message.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.

Returns:

  • (Database)

    Will return the database on success.

Raises:

  • HttpAdapter Exceptions depending on the reason for failure.

See Also:

  • for option details


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.message.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_saveObject



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

#deleteObject

Deletes a database; use with caution as this isn’t reversible.

Returns:

  • A JSON response on success. nil if the resource is not found. And raises an error if another exception was raised

Raises:

  • Exception related to request failure that is not a ResourceNotFound error.



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.

Returns:

  • A JSON response on success.

Raises:

  • Exception related to request failure or ResourceNotFound.



145
146
147
# File 'lib/aqua/store/couch_db/database.rb', line 145

def delete!
  CouchDB.delete( uri )
end

#delete_allObject

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.

Returns:

  • (true, false)

    depending on whether the database already exists in CouchDB land



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

#infoObject

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.

Parameters:

  • If (Symbol, Server)

    server_option argument is a Symbol then the CouchDB server stash will be queried for a matching server. CouchDB manages the creation of that server in the stash, if not found. If server_option argument is a Server object then then it is added directly to the database object. No management of the server will be done with CouchDB’s server stash.

Returns:

Raises:

  • (ArgumentError)

    Raised if a server_option is passed in and if that option is neither a Hash nor a Symbol.



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.

Parameters:

  • Name (String)

    that the database is initialized with, if any.

Returns:

  • (String)

    Namespaced database for use as a http path



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