Class: Ruote::Couch::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/ruote/couch/database.rb

Overview

A database corresponds to a Couch database (not a Couch server).

There is one database per ruote document type (msgs, workitems, expressions, …)

Direct Known Subclasses

WfidIndexedDatabase

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, type, name, opts = {}) ⇒ Database

Returns a new instance of Database.



39
40
41
42
43
44
45
46
47
48
# File 'lib/ruote/couch/database.rb', line 39

def initialize (host, port, type, name, opts={})

  @couch = Rufus::Jig::Couch.new(host, port, name, opts)

  @couch.put('.') unless @couch.get('.')

  @type = type

  prepare
end

Instance Attribute Details

#couchObject (readonly)

Returns the value of attribute couch.



37
38
39
# File 'lib/ruote/couch/database.rb', line 37

def couch
  @couch
end

#typeObject (readonly)

Returns the value of attribute type.



36
37
38
# File 'lib/ruote/couch/database.rb', line 36

def type
  @type
end

Instance Method Details

#delete(doc) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ruote/couch/database.rb', line 66

def delete (doc)

  r = @couch.delete(doc)

  #p [ :del, doc['_id'], Thread.current.object_id.to_s[-3..-1], r.nil? ]
  Thread.pass
    # without this, test/functional/ct_0 fails after 1 to 10 runs...

  r

rescue Rufus::Jig::TimeoutError => te
  true
end

#dumpObject



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ruote/couch/database.rb', line 118

def dump

  s = "=== #{@type} ===\n"

  get_many(nil, {}).inject(s) do |s1, e|
    s1 << "\n"
    e.keys.sort.inject(s1) do |s2, k|
      s2 << "  #{k} => #{e[k].inspect}\n"
    end
  end
end

#get(key, opts = {}) ⇒ Object



61
62
63
64
# File 'lib/ruote/couch/database.rb', line 61

def get (key, opts={})

  @couch.get(key, opts)
end

#get_many(key, opts) ⇒ Object

The get_many used by msgs, configurations and variables.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ruote/couch/database.rb', line 82

def get_many (key, opts)

  if key.nil? && opts.empty?
    return @couch.all(:include_design_docs => false)
  end

  is = ids

  return is.length if opts[:count]
  return [] if is.empty?

  is = is.reverse if opts[:descending]

  if key
    keys = Array(key).map { |k| k.is_a?(String) ? "!#{k}" : k }
    is = is.select { |i| Ruote::StorageBase.key_match?(keys, i) }
  end

  skip = opts[:skip] || 0
  limit = opts[:limit] || is.length

  is = is[skip, limit]

  #return [] if is.empty?
    # not necessary at this point, jig provides already this optimization.

  @couch.all(:keys => is)
end

#idsObject

Returns a sorted list of the ids of all the docs in this database.



113
114
115
116
# File 'lib/ruote/couch/database.rb', line 113

def ids

  @couch.ids(:include_design_docs => false)
end

#purge!Object

Deletes all the documents in this database.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/ruote/couch/database.rb', line 139

def purge!

  #@couch.http.cache.clear
  #@couch.all(
  #  :include_docs => false, :include_design_docs => false
  #).each do |doc|
  #  @couch.delete(doc)
  #end
  docs = @couch.all(:include_docs => false, :include_design_docs => false)
  @couch.bulk_delete(docs)
    #
    # which is faster than
    #
  #@couch.delete('.')
  #@couch.put('.')
  #@couch.http.cache.clear
end

#put(doc, opts) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/ruote/couch/database.rb', line 50

def put (doc, opts)

  doc['put_at'] = Ruote.now_to_utc_s

  @couch.put(doc, :update_rev => opts[:update_rev])
    #
    # :update_rev => true :
    # updating the current doc _rev, this trick allows
    # direct "create then apply" chaining
end

#shutdownObject

Makes sure to close the HTTP connection down.



132
133
134
135
# File 'lib/ruote/couch/database.rb', line 132

def shutdown

  @couch.close
end