Module: CPEE::Persistence

Defined in:
lib/cpee/persistence.rb

Constant Summary collapse

@@obj =
'instance'

Class Method Summary collapse

Class Method Details

.each_object(opts) ⇒ Object

}}}



123
124
125
126
127
# File 'lib/cpee/persistence.rb', line 123

def self::each_object(opts)
  opts[:redis].zrevrange(@@obj + 's',0,-1).each do |instance|
    yield instance
  end
end

.exists?(id, opts) ⇒ Boolean

}}}

Returns:

  • (Boolean)


116
117
118
# File 'lib/cpee/persistence.rb', line 116

def self::exists?(id,opts) #{{{
  opts[:redis].exists?(@@obj + ":#{id}/state")
end

.exists_handler?(id, opts, key) ⇒ Boolean

}}}

Returns:

  • (Boolean)


248
249
250
# File 'lib/cpee/persistence.rb', line 248

def self::exists_handler?(id,opts,key) #{{{
  opts[:redis].exists?(@@obj + ":#{id}/handlers/#{key}")
end

.extract_handler(id, opts, key) ⇒ Object

}}}



245
246
247
# File 'lib/cpee/persistence.rb', line 245

def self::extract_handler(id,opts,key) #{{{
  opts[:redis].smembers(@@obj + ":#{id}/handlers/#{key}")
end

.extract_handlers(id, opts) ⇒ Object

}}}



251
252
253
254
255
# File 'lib/cpee/persistence.rb', line 251

def self::extract_handlers(id,opts) #{{{
  opts[:redis].smembers(@@obj + ":#{id}/handlers").map do |e|
    [e, opts[:redis].get(@@obj + ":#{id}/handlers/#{e}/url")]
  end
end

.extract_item(id, opts, item) ⇒ Object

}}}



112
113
114
# File 'lib/cpee/persistence.rb', line 112

def self::extract_item(id,opts,item) #{{{
  opts[:redis].get(@@obj + ":#{id}/#{item}")
end

.extract_list(id, opts, item) ⇒ Object

}}}



94
95
96
97
98
# File 'lib/cpee/persistence.rb', line 94

def self::extract_list(id,opts,item) #{{{
  opts[:redis].zrange(@@obj + ":#{id}/#{item}",0,-1).map do |e|
    [e,opts[:redis].get(@@obj + ":#{id}/#{item}/#{e}")]
  end
end

.extract_set(id, opts, item) ⇒ Object

}}}



89
90
91
92
93
# File 'lib/cpee/persistence.rb', line 89

def self::extract_set(id,opts,item) #{{{
  opts[:redis].smembers(@@obj + ":#{id}/#{item}").map do |e|
    [e,opts[:redis].get(@@obj + ":#{id}/#{item}/#{e}")]
  end
end

.is_member?(id, opts, item, value) ⇒ Boolean

}}}

Returns:

  • (Boolean)


119
120
121
# File 'lib/cpee/persistence.rb', line 119

def self::is_member?(id,opts,item,value) #{{{
  opts[:redis].sismember(@@obj + ":#{id}/#{item}",value)
end

.keys(id, opts) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/cpee/persistence.rb', line 165

def self::keys(id,opts)
  res = []
  res += Persistence::keys_extract_zset(opts,id,'dataelements')
  res += Persistence::keys_extract_name(opts,id,'dataelements')
  res += Persistence::keys_extract_zset(opts,id,'documents')
  res += Persistence::keys_extract_name(opts,id,'documents')
  res += Persistence::keys_extract_zset(opts,id,'attributes')
  res += Persistence::keys_extract_name(opts,id,'attributes')
  res += Persistence::keys_extract_zset(opts,id,'endpoints')
  res += Persistence::keys_extract_name(opts,id,'endpoints')
  pos = Persistence::keys_extract_set(opts,id,'positions')
  res += pos
  pos.each do |p|
    res << File.join(p,'@passthrough')
  end
  res += Persistence::keys_extract_name(opts,id,'positions')
  hnd = Persistence::keys_extract_set(opts,id,'handlers')
  res += hnd
  res += Persistence::keys_extract_name(opts,id,'handlers')
  hnd.each do |h|
    res << File.join(h,'url')
    res += Persistence::keys_extract_set_raw(opts,h)
  end
  cbs = Persistence::keys_extract_set(opts,id,'callbacks')
  res += cbs
  res += Persistence::keys_extract_name(opts,id,'callbacks')
  cbs.each do |c|
    res << "#{c}/position"
    res << "#{c}/label"
    res << "#{c}/uuid"
    res << "#{c}/type"
    res << "#{c}/subscription"
  end
  res += Persistence::keys_extract_name(opts,id,'dsl')
  res += Persistence::keys_extract_name(opts,id,'dslx')
  res += Persistence::keys_extract_name(opts,id,'status','message')
  res += Persistence::keys_extract_name(opts,id,'status','id')
  res += Persistence::keys_extract_name(opts,id,'executionhandler')
  res += Persistence::keys_extract_name(opts,id,'description')
  res += Persistence::keys_extract_name(opts,id,'state')
  res += Persistence::keys_extract_name(opts,id,'state','@changed')
  res += Persistence::keys_extract_name(opts,id,'transformation','endpoints')
  res += Persistence::keys_extract_name(opts,id,'transformation','endpoints','@type')
  res += Persistence::keys_extract_name(opts,id,'transformation','description')
  res += Persistence::keys_extract_name(opts,id,'transformation','description','@type')
  res += Persistence::keys_extract_name(opts,id,'transformation','dataelements')
  res += Persistence::keys_extract_name(opts,id,'transformation','dataelements','@type')
  res
end

.keys_extract_name(opts, id, *item) ⇒ Object



161
162
163
# File 'lib/cpee/persistence.rb', line 161

def self::keys_extract_name(opts,id,*item)
  [@@obj + ":#{id}/#{File.join(*item)}"]
end

.keys_extract_set(opts, id, item) ⇒ Object



150
151
152
153
154
155
# File 'lib/cpee/persistence.rb', line 150

def self::keys_extract_set(opts,id,item)
  path = @@obj + ":#{id}/#{item}"
  opts[:redis].smembers(path).map do |e|
    File.join(path,e)
  end
end

.keys_extract_set_raw(opts, path) ⇒ Object



156
157
158
159
160
# File 'lib/cpee/persistence.rb', line 156

def self::keys_extract_set_raw(opts,path)
  opts[:redis].smembers(path).map do |e|
    File.join(File.dirname(path),e)
  end
end

.keys_extract_zset(opts, id, item) ⇒ Object



144
145
146
147
148
149
# File 'lib/cpee/persistence.rb', line 144

def self::keys_extract_zset(opts,id,item)
  path = @@obj + ":#{id}/#{item}"
  opts[:redis].zrange(path,0,-1).map do |e|
    File.join(path,e)
  end
end

.new_object(opts) ⇒ Object



129
130
131
132
133
# File 'lib/cpee/persistence.rb', line 129

def self::new_object(opts)
  id = opts[:redis].zrevrange(@@obj + 's', 0, 0).first.to_i + 1
  opts[:redis].zadd(@@obj + 's',id,id)
  id
end

.new_static_object(id, opts) ⇒ Object



134
135
136
137
# File 'lib/cpee/persistence.rb', line 134

def self::new_static_object(id,opts)
  opts[:redis].set(File.join(@@obj + ":#{id}",'state'),'')
  nil
end

.objObject

{{{



22
23
24
# File 'lib/cpee/persistence.rb', line 22

def self::obj #{{{
  @@obj
end

.obj=(it) ⇒ Object

}}}



25
26
27
# File 'lib/cpee/persistence.rb', line 25

def self::obj=(it) #{{{
  @@obj = it
end

.set_handler(id, opts, key, url, values, update = false) ⇒ Object

{{{



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
242
243
244
# File 'lib/cpee/persistence.rb', line 215

def self::set_handler(id,opts,key,url,values,update=false) #{{{
  exis = opts[:redis].smembers(@@obj + ":#{id}/handlers/#{key}")

  if update == false && exis.length > 0
    return 405
  end

  attributes = Persistence::extract_list(id,opts,'attributes').to_h

  deleted = exis - values

  CPEE::Message::send(
    :event,
    'handler/change',
    opts[:url],
    id,
    Persistence::extract_item(id,opts,'attributes/uuid'),
    Persistence::extract_item(id,opts,'attributes/info'),
    {
      :key => key,
      :url => url,
      :changed => values,
      :deleted => deleted,
      :attributes => attributes
    },
    opts[:redis]
  )

  200
end

.set_item(id, opts, item, value) ⇒ Object

}}}



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cpee/persistence.rb', line 100

def self::set_item(id,opts,item,value) #{{{
  CPEE::Message::send(
    :event,
    File.join(item,'change'),
    opts[:url],
    id,
    Persistence::extract_item(id,opts,'attributes/uuid'),
    Persistence::extract_item(id,opts,'attributes/info'),
    value,
    opts[:redis]
  )
end

.set_list(id, opts, item, values, diff = false, delete = false, deletions = []) ⇒ Object

{{{



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
# File 'lib/cpee/persistence.rb', line 33

def self::set_list(id,opts,item,values,diff=false,delete=false,deletions=[]) #{{{
  attributes = Persistence::extract_list(id,opts,'attributes').to_h
  dataelements = Persistence::extract_list(id,opts,'dataelements').to_h
  documents = Persistence::extract_list(id,opts,'documents').to_h
  endpoints = Persistence::extract_list(id,opts,'endpoints').to_h
  oldvalues = case item
    when 'attributes' then attributes
    when 'endpoints' then endpoints
    when 'dataelements' then dataelements
    when 'documents' then documents
  end
  sort = []
  if diff
    deletions = oldvalues.keys - values.keys
    if oldvalues.length > 1 && oldvalues.length == values.length && deletions.length == 0 && oldvalues.keys != values.keys
      sort = values.keys
    end
    values.delete_if do |k,v|
      oldvalues[k] && oldvalues[k] == v
    end
  end
  payload = {
    :values => oldvalues.merge(values).transform_values{|val| JSON::parse(val) rescue val },
    :attributes => attributes
  }
  CPEE::Message::send(
    :event,
    File.join(item,'modify'),
    opts[:url],
    id,
    Persistence::extract_item(id,opts,'attributes/uuid'),
    Persistence::extract_item(id,opts,'attributes/info'),
    payload,
    opts[:redis]
  )
  payload[:values] = values.transform_values{|val| JSON::parse(val) rescue val }
  payload[:changed] = values.keys
  if delete
    payload[:deleted] = deletions
  end
  if sort.any?
    payload[:sort] = sort
  end
  if values.any? || deletions.any? || sort.any?
    CPEE::Message::send(
      :event,
      File.join(item,'change'),
      opts[:url],
      id,
      Persistence::extract_item(id,opts,'attributes/uuid'),
      Persistence::extract_item(id,opts,'attributes/info'),
      payload,
      opts[:redis]
    )
  end
end

.wait(opts) ⇒ Object

}}}



29
30
31
# File 'lib/cpee/persistence.rb', line 29

def self::wait(opts)
  CPEE::Message::wait(opts[:redis],opts[:redis_dyn].call('Temporary Storage Transaction Subscriber'))
end