Class: Aqua::Store::CouchDB::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Server

Returns a new instance of Server.



11
12
13
14
15
16
# File 'lib/aqua/store/couch_db/server.rb', line 11

def initialize(opts={})
  opts = Mash.new(opts) unless opts.empty?
  self.uri =              opts[:server] || 'http://127.0.0.1:5984'
  self.uuid_batch_count = opts[:uuid_batch_count] || 1000 
  self.namespace =        opts[:namespace].to_s 
end

Instance Attribute Details

#namespaceObject

Returns the value of attribute namespace.



9
10
11
# File 'lib/aqua/store/couch_db/server.rb', line 9

def namespace
  @namespace
end

#uriObject

Returns the value of attribute uri.



8
9
10
# File 'lib/aqua/store/couch_db/server.rb', line 8

def uri
  @uri
end

#uuid_batch_countObject

Returns the value of attribute uuid_batch_count.



8
9
10
# File 'lib/aqua/store/couch_db/server.rb', line 8

def uuid_batch_count
  @uuid_batch_count
end

#uuidsObject

Returns the value of attribute uuids.



8
9
10
# File 'lib/aqua/store/couch_db/server.rb', line 8

def uuids
  @uuids
end

Instance Method Details

#database(name) ⇒ Object

Returns a CouchRest::Database for the given name



56
57
58
59
# File 'lib/aqua/store/couch_db/server.rb', line 56

def database(name)
  db = Database.new( name, :server => self )
  db.exists? ? db : nil
end

#database!(name) ⇒ Object

Creates the database if it doesn’t exist



62
63
64
# File 'lib/aqua/store/couch_db/server.rb', line 62

def database!(name)
  Database.create( name, :server => self )  
end

#database_namesObject

Lists all database names on the server



29
30
31
32
# File 'lib/aqua/store/couch_db/server.rb', line 29

def database_names
  dbs = CouchDB.get( "#{@uri}/_all_dbs" )
  dbs.select{|name| name.match(/\A#{namespace}_?/)}
end

#databasesObject



34
35
36
37
38
39
40
# File 'lib/aqua/store/couch_db/server.rb', line 34

def databases
  dbs = [] 
  database_names.each do |db_name|
    dbs << Database.new( db_name.gsub(/\A#{namespace}_|\A#{namespace}\z/, '') , :server => self )
  end
  dbs  
end

#delete_allObject

Deletes all database with the less exection raising method: database.delete. This will only raise errors related to request problems, and not errors related to the database not being found for deletion.



51
52
53
# File 'lib/aqua/store/couch_db/server.rb', line 51

def delete_all 
  databases.each{ |db| db.delete }
end

#delete_all!Object

Deletes all databases named for this namespace (i.e. this server) Use with caution … it is a permanent and undoable change



44
45
46
# File 'lib/aqua/store/couch_db/server.rb', line 44

def delete_all! 
  databases.each{ |db| db.delete! }
end

#infoObject

GET the welcome message



67
68
69
# File 'lib/aqua/store/couch_db/server.rb', line 67

def info
  CouchDB.get "#{uri}/"
end

#load_uuids(count = @uuid_batch_count) ⇒ Object



95
96
97
# File 'lib/aqua/store/couch_db/server.rb', line 95

def load_uuids( count=@uuid_batch_count ) 
  @uuids = CouchDB.get("#{@uri}/_uuids?count=#{count}")["uuids"]
end

#next_uuid(count = @uuid_batch_count) ⇒ Object

Retrive an unused UUID from CouchDB. Server instances manage caching a list of unused UUIDs.



87
88
89
90
91
92
93
# File 'lib/aqua/store/couch_db/server.rb', line 87

def next_uuid(count = @uuid_batch_count)
  @uuids ||= []
  if uuids.empty?
    load_uuids(count)
  end
  uuids.pop
end

#restart!Object

Restart the CouchDB instance



72
73
74
# File 'lib/aqua/store/couch_db/server.rb', line 72

def restart!
  CouchDB.post "#{uri}/_restart"
end

#uuid_countObject

counts the number of uuids available, used by Database to limit bulk save



77
78
79
80
81
82
83
84
# File 'lib/aqua/store/couch_db/server.rb', line 77

def uuid_count
  if uuids 
    uuids.size 
  else
    load_uuids  
    uuid_batch_count
  end  
end