Class: Ruote::Dm::DmStorage

Inherits:
Object
  • Object
show all
Includes:
StorageBase
Defined in:
lib/ruote/dm/storage.rb

Overview

A datamapper-powered storage for ruote.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository = nil, options = {}) ⇒ DmStorage

Returns a new instance of DmStorage.



53
54
55
56
57
58
59
# File 'lib/ruote/dm/storage.rb', line 53

def initialize (repository=nil, options={})

  @options = options
  @repository = repository

  put_configuration
end

Instance Attribute Details

#repositoryObject (readonly)

Returns the value of attribute repository.



51
52
53
# File 'lib/ruote/dm/storage.rb', line 51

def repository
  @repository
end

Instance Method Details

#add_type(type) ⇒ Object

Mainly used by ruote’s test/unit/ut_17_storage.rb



171
172
173
174
# File 'lib/ruote/dm/storage.rb', line 171

def add_type (type)

  # does nothing, types are differentiated by the 'typ' column
end

#by_field(type, field, value = nil) ⇒ Object

Querying workitems by field (warning, goes deep into the JSON structure)

Raises:

  • (NotImplementedError)


201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/ruote/dm/storage.rb', line 201

def by_field (type, field, value=nil)

  raise NotImplementedError if type != 'workitems'

  like = [ '%"', field, '":' ]
  like.push(Rufus::Json.encode(value)) if value
  like.push('%')

  Document.all(:typ => type, :doc.like => like.join).collect { |d|
    Rufus::Json.decode(d.doc)
  }
end

#by_participant(type, participant_name) ⇒ Object

A provision made for workitems, allow to query them directly by participant name.

Raises:

  • (NotImplementedError)


188
189
190
191
192
193
194
195
196
197
# File 'lib/ruote/dm/storage.rb', line 188

def by_participant (type, participant_name)

  raise NotImplementedError if type != 'workitems'

  Document.all(
    :typ => type, :participant_name => participant_name
  ).collect { |d|
    Rufus::Json.decode(d.doc)
  }
end

#delete(doc) ⇒ Object

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ruote/dm/storage.rb', line 106

def delete (doc)

  raise ArgumentError.new('no _rev for doc') unless doc['_rev']

  DataMapper.repository(@repository) do

    d = Document.first(
      :typ => doc['type'], :ide => doc['_id'], :rev => doc['_rev'])

    return true unless d

    d.destroy!

    nil
  end
end

#get(type, key) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/ruote/dm/storage.rb', line 98

def get (type, key)

  DataMapper.repository(@repository) do
    d = Document.first(:typ => type, :ide => key)
    d ? Rufus::Json.decode(d.doc) : nil
  end
end

#get_many(type, key = nil, opts = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ruote/dm/storage.rb', line 123

def get_many (type, key=nil, opts={})

  q = { :typ => type }

  if l = opts[:limit]
    q[:limit] = l
  end

  if key
    q[:ide.like] = if m = key.source.match(/(.+)\$$/)
      "%#{m[1]}"
    elsif m = key.source.match(/^\^(.+)/)
      "#{m[1]}%"
    else
      "%#{key.source}%"
    end
  end

  DataMapper.repository(@repository) do
    Document.all(q).collect { |d| Rufus::Json.decode(d.doc) }
  end
end

#ids(type) ⇒ Object



146
147
148
149
150
151
# File 'lib/ruote/dm/storage.rb', line 146

def ids (type)

  DataMapper.repository(@repository) do
    Document.all(:typ => type).collect { |d| d.ide }
  end
end

#purge!Object



153
154
155
156
157
158
# File 'lib/ruote/dm/storage.rb', line 153

def purge!

  DataMapper.repository(@repository) do
    Document.all.destroy!
  end
end

#purge_type!(type) ⇒ Object

Nukes a db type and reputs it (losing all the documents that were in it).



178
179
180
181
182
183
# File 'lib/ruote/dm/storage.rb', line 178

def purge_type! (type)

  DataMapper.repository(@repository) do
    Document.all(:typ => type).destroy!
  end
end

#put(doc, opts = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ruote/dm/storage.rb', line 61

def put (doc, opts={})

  DataMapper.repository(@repository) do

    d = Document.first(:ide => doc['_id'], :typ => doc['type'])

    return Rufus::Json.decode(d.doc) if d && d.rev != doc['_rev']

    if doc['_rev'].nil?

      d = Document.new(
        :ide => doc['_id'],
        :rev => 0,
        :typ => doc['type'],
        :doc => Rufus::Json.encode(doc.merge(
          '_rev' => 0, 'put_at' => Ruote.now_to_utc_s)),
        :participant_name => doc['participant_name']
      ).save

      doc['_rev'] = 0 if opts[:update_rev]

    else

      return true unless d

      d.rev = d.rev + 1
      d.doc = Rufus::Json.encode(doc.merge(
        '_rev' => d.rev, 'put_at' => Ruote.now_to_utc_s))
      d.save

      doc['_rev'] = d.rev if opts[:update_rev]
    end

    nil
  end
end

#query_workitems(criteria) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/ruote/dm/storage.rb', line 214

def query_workitems (criteria)

  offset = criteria.delete('offset')
  limit = criteria.delete('limit')

  wfid =
    criteria.delete('wfid')
  pname =
    criteria.delete('participant_name') || criteria.delete('participant')

  cr = { :typ => 'workitems' }

  cr[:ide.like] = "%!#{wfid}" if wfid
  cr[:offset] = offset if offset
  cr[:limit] = limit if limit
  cr[:participant_name] = pname if pname

  likes = criteria.collect do |k, v|
    "%\"#{k}\":#{Rufus::Json.encode(v)}%"
  end
  cr[:conditions] = [
    ([ 'doc LIKE ?' ] * likes.size).join(' AND '), *likes
  ] unless likes.empty?

  Document.all(cr).collect { |d|
    Ruote::Workitem.new(Rufus::Json.decode(d.doc))
  }
end

#shutdownObject

def dump (type)

@dbs[type].dump

end



164
165
166
167
# File 'lib/ruote/dm/storage.rb', line 164

def shutdown

  #@dbs.values.each { |db| db.shutdown }
end