Class: RelaxDB::CouchDB

Inherits:
Object
  • Object
show all
Defined in:
lib/relaxdb/server.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (CouchDB) initialize(config)



15
16
17
18
19
# File 'lib/relaxdb/server.rb', line 15

def initialize(config)
  @get_count, @post_count, @put_count = 0, 0, 0
  @server = RelaxDB::Server.new(config[:host], config[:port])
  @logger = config[:logger] ? config[:logger] : Logger.new(Tempfile.new('relaxdb.log'))
end

Instance Attribute Details

- (Object) get_count

Used for test instrumentation only i.e. to assert that an expected number of requests have been issued



13
14
15
# File 'lib/relaxdb/server.rb', line 13

def get_count
  @get_count
end

- (Object) logger (readonly)

Returns the value of attribute logger



9
10
11
# File 'lib/relaxdb/server.rb', line 9

def logger
  @logger
end

- (Object) post_count

Used for test instrumentation only i.e. to assert that an expected number of requests have been issued



13
14
15
# File 'lib/relaxdb/server.rb', line 13

def post_count
  @post_count
end

- (Object) put_count

Used for test instrumentation only i.e. to assert that an expected number of requests have been issued



13
14
15
# File 'lib/relaxdb/server.rb', line 13

def put_count
  @put_count
end

- (Object) server (readonly)

Returns the value of attribute server



9
10
11
# File 'lib/relaxdb/server.rb', line 9

def server
  @server
end

Instance Method Details

- (Boolean) db_exists?(name)



26
27
28
# File 'lib/relaxdb/server.rb', line 26

def db_exists?(name)
  @server.get("/#{name}") rescue false
end

- (Object) delete(path = nil)



53
54
55
56
# File 'lib/relaxdb/server.rb', line 53

def delete(path=nil)
  @logger.info("DELETE /#{@db}/#{unesc(path)}")
  @server.delete("/#{@db}/#{path}")
end

- (Object) delete_db(name)

URL encode slashes e.g. RelaxDB.delete_db "foo%2Fbar"



31
32
33
34
35
36
37
38
39
# File 'lib/relaxdb/server.rb', line 31

def delete_db(name)
  # Close the http connection as CouchDB will keep a file handle to the db open
  # if the http connection remains open - this will result in CouchDB throwing
  # emfile errors after a significant number of databases are deleted.
  @server.close_connection
  
  @logger.info("Deleting database #{name}")
  @server.delete("/#{name}")
end

- (Object) get(path = nil)



58
59
60
61
62
# File 'lib/relaxdb/server.rb', line 58

def get(path=nil)
  @get_count += 1
  @logger.info("GET /#{@db}/#{unesc(path)}")
  @server.get("/#{@db}/#{path}")
end

- (Object) list_dbs



41
42
43
# File 'lib/relaxdb/server.rb', line 41

def list_dbs
  JSON.parse(@server.get("/_all_dbs").body)
end

- (Object) name



92
93
94
# File 'lib/relaxdb/server.rb', line 92

def name
  @db
end

- (Object) name=(name)



96
97
98
# File 'lib/relaxdb/server.rb', line 96

def name=(name)
  @db = name
end

- (Object) post(path = nil, json = nil)



64
65
66
67
68
# File 'lib/relaxdb/server.rb', line 64

def post(path=nil, json=nil)
  @post_count += 1
  @logger.info("POST /#{@db}/#{unesc(path)} #{json}")
  @server.post("/#{@db}/#{path}", json)
end

- (Object) put(path = nil, json = nil)



70
71
72
73
74
# File 'lib/relaxdb/server.rb', line 70

def put(path=nil, json=nil)
  @put_count += 1
  @logger.info("PUT /#{@db}/#{unesc(path)} #{json}")
  @server.put("/#{@db}/#{path}", json)
end

- (Object) replicate_db(source, target)



45
46
47
48
49
50
51
# File 'lib/relaxdb/server.rb', line 45

def replicate_db(source, target)
  @logger.info("Replicating from #{source} to #{target}")
  create_db_if_non_existant target      
  # Manual JSON encoding to allow for dbs containing a '/'
  data = %Q({"source":"#{source}","target":"#{target}"})       
  @server.post("/_replicate", data)
end

- (Object) req_count



100
101
102
# File 'lib/relaxdb/server.rb', line 100

def req_count
  get_count + put_count + post_count
end

- (Object) reset_req_count



104
105
106
# File 'lib/relaxdb/server.rb', line 104

def reset_req_count
  @get_count = @put_count = @post_count = 0
end

- (Object) unesc(path)



83
84
85
86
# File 'lib/relaxdb/server.rb', line 83

def unesc(path)
  # path
  path ? ::CGI::unescape(path) : ""
end

- (Object) uri



88
89
90
# File 'lib/relaxdb/server.rb', line 88

def uri
  "#@server" / @db
end

- (Object) use_db(name)



21
22
23
24
# File 'lib/relaxdb/server.rb', line 21

def use_db(name)
  create_db_if_non_existant(name)
  @db = name
end

- (Object) uuids(count = 1)



76
77
78
79
80
81
# File 'lib/relaxdb/server.rb', line 76

def uuids(count=1)
  @get_count += 1
  uri = "/_uuids?count=#{count}"
  @logger.info "GET #{uri}"
  @server.get uri
end