Class: Orchestrate::Application::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/orchestrate_application/record.rb

Overview

Class for accessing collections belonging to an Orchestrate.io application.

  • public class methods for collection requests

  • public instance methods for key/value requests

PUT and DELETE requests return Orchestrate::API::Response[../API/Response.html].

GET requests return Result[Result.html] or SearchResult[SearchResult.html]. The result data is returned as Document[Document.html] objects.

Direct Known Subclasses

RefTable, Rails::Model

Constant Summary collapse

@@_collection =

:startdoc:

{}
@@_cache =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Record

Performs application-level initialization functions.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/orchestrate_application/record.rb', line 35

def initialize(params={})
  name = init_collection_name params[:define_collection_name]
  @@_collection[self.class.name] ||= Schema.instance.get_collection(name)
  @id = params[:id] if params[:id]
  @__ref_value__ = '"*"'

  # @event_types = params[:event_types] ? params[:event_types] : []
  # @relation_kinds = params[:graphs] ? params[:graphs] : []

  @@_cache[self.class.name] ||= SimpleCacheRequest.new(ocollection)
  @@cache_store ||= SimpleCacheStore.instance
end

Instance Attribute Details

#__ref_value__Object (readonly)

:stopdoc: The ref value for the current instance. Convenience method #orchestrate_ref_value is provided as the recommended way for an application to read this attribute.



28
29
30
# File 'lib/orchestrate_application/record.rb', line 28

def __ref_value__
  @__ref_value__
end

#idObject (readonly)

The unique primary_key that identifies a set of key/value data stored in an orchestrate.io collection.



20
21
22
# File 'lib/orchestrate_application/record.rb', line 20

def id
  @id
end

Class Method Details

.orchio_delete(collection) ⇒ Object

Delete the specified collection.



97
98
99
100
101
102
103
# File 'lib/orchestrate_application/record.rb', line 97

def self.orchio_delete(collection)
  response = client.send_request(
    :delete, { collection: collection, path: "?force=true" }
  )
  # SchemaCollection.delete(collection) if response.header.code == 204
  orchio_status response, 204
end

.orchio_delete_key(collection, key) ⇒ Object

Delete key from collection.



106
107
108
# File 'lib/orchestrate_application/record.rb', line 106

def self.orchio_delete_key(collection, key)
  new(collection: collection, id: key).orchio_delete
end

.orchio_list(collection, path = nil) ⇒ Object

Lists the collection contents. The results are based the options. Returns Result object.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/orchestrate_application/record.rb', line 78

def self.orchio_list(collection, path=nil)
  response = client.send_request :get, { collection: collection, path: path }
  Result.new(
    status:   orchio_status(response, 200),
    response: sanitize(response),
    count:    response.body.count,
    :next =>  response.body.next,
    results:  response.body.results.map { |result|
                Document.new(
                  result['value'].merge(id: result['path']['key']),
                  Metadata.new(
                    collection: result['path']['collection'],
                    key:        result['path']['key'],
                    ref:        result['path']['ref'],
                  )
  )})
end

.orchio_search(collection, query_str) ⇒ Object

Searches the collection; encodes any whitespace contained in the query string. Returns SearchResult object where the results attribute contains an array of Document objects.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/orchestrate_application/record.rb', line 54

def self.orchio_search(collection, query_str)
  response = client.send_request( :get, {
    collection: collection,
    path: "?query=#{query_str.gsub(/\s/, '%20')}"
  })

  SearchResult.new(
    status:      orchio_status(response, 200),
    response:    sanitize(response),
    count:       response.body.count,
    total_count: response.body.total_count,
    results:     response.body.results.map { |result|
                   Document.new(
                     result['value'].merge(id: result['path']['key']),
                     Metadata.new(
                        collection: result['path']['collection'],
                        key:        result['path']['key'],
                        ref:        result['path']['ref'],
                        score:      result['score']
  ))})
end

Instance Method Details

#orchestrate_clientObject

Returns the client handle.



334
335
336
# File 'lib/orchestrate_application/record.rb', line 334

def orchestrate_client
  client
end

#orchestrate_collection_nameObject

Returns the collection name for the current instance.



317
318
319
# File 'lib/orchestrate_application/record.rb', line 317

def orchestrate_collection_name
  ocollection
end

#orchestrate_primary_keyObject

Returns the primary_key for the current instance.



329
330
331
# File 'lib/orchestrate_application/record.rb', line 329

def orchestrate_primary_key
  id
end

#orchestrate_ref_valueObject

Returns the ref value for the current instance. The ref value is an immutable value assigned to each version of primary_key data in an orchestrate.io collection.



324
325
326
# File 'lib/orchestrate_application/record.rb', line 324

def orchestrate_ref_value
  __ref_value__
end

#orchio_deleteObject

Deletes the primary_key and data associated with this instance from the collection.



155
156
157
158
# File 'lib/orchestrate_application/record.rb', line 155

def orchio_delete
  response = client.send_request :delete, inst_args
  orchio_status response, 204
end

#orchio_delete_graph(kind, to_collection, to_key) ⇒ Object

Delete a graph/relation from the collection.



301
302
303
304
305
306
307
308
309
310
311
# File 'lib/orchestrate_application/record.rb', line 301

def orchio_delete_graph(kind, to_collection, to_key)
  response = client.send_request(
    :delete,
    inst_args(
      kind:          kind,
      to_collection: to_collection,
      to_key:        to_key,
      path:          "?purge=true"
  ))
  orchio_status response, 204
end

#orchio_getObject

Fetches the data associated with this id from the collection.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/orchestrate_application/record.rb', line 114

def orchio_get
  if cache.enabled? and response = cache_request.get(id)
    if response.header.status == :cache
      doc = response.body.document
    end
  else
    response = client.send_request :get, inst_args if response.nil?
    doc = Document.new(
            response.body.to_hash,
            Metadata.new(
              :collection => ocollection,
              :key => @id,
              :ref => response.header.etag
          ))
  end
  Result.new(
    status:   orchio_status(response, 200),
    response: response,
    results:  [ doc ]
  )
end

#orchio_get_by_ref(ref) ⇒ Object

Gets the key/value pair by its ‘ref’ value.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/orchestrate_application/record.rb', line 171

def orchio_get_by_ref(ref)
  response = client.send_request :get, inst_args(ref: ref)
  Result.new(
    status:   orchio_status(response, 200),
    response: response,
    results:  [ Document.new(
                  response.body.to_hash,
                  Metadata.new(
                    :collection => ocollection,
                    :key => @id,
                    :ref => response.header.etag
              ))]
  )
end

#orchio_get_events(event_type, timestamp = {}) ⇒ Object

Gets all events of specified type, within the timestamp parameters, where timestamp = { :start => start, :end => end }.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/orchestrate_application/record.rb', line 204

def orchio_get_events(event_type, timestamp={})
  # add_event_type event_type

  if cache.enabled? and response = cache_request.get_events(id, event_type)
    if response.header.status == :cache
      docs = response.body.documents
      count = docs.length
    end
  else
    response = client.send_request(
      :get, inst_args(event_type: event_type, timestamp: timestamp)
    )
    docs =  response.body.results.map { |result|
              Document.new result['value'].merge(
                  key: @id,
                  etype: event_type,
                  timestamp: result['timestamp']
                ),
                Metadata.new(
                  collection: ocollection,
                  key: @id,
                  ref: funkify("#{event_type}#{result['timestamp']}"),
                  etype: event_type,
                )
            }
    cache.save_event(id, event_type) if cache.enabled? and count == 0
  end
  Result.new(
    status:   orchio_status(response, 200),
    response: response,
    count:    response.body.count,
    results:  docs
  )
end

#orchio_get_graph(kind) ⇒ Object

Gets the graph for the specified kind of relation.



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/orchestrate_application/record.rb', line 251

def orchio_get_graph(kind)
  # add_relation_kind kind
  if cache.enabled? and response = cache_request.get_graph(id, kind)
    if response.header.status == :cache
      docs = response.body.documents
      count = docs.length
    end
  else
    response = client.send_request :get, inst_args(kind: kind)
    docs =  response.body.results.map { |result|
              Document.new(
                result['value'].merge(id: result['path']['key']),
                Metadata.new(
                  :collection => result['path']['collection'],
                  :key => result['path']['key'],
                  :ref => result['path']['ref'],
                  :kind => kind,
                  :from_collection => ocollection,
                  :from_key => @id,
            ))}
    cache.save_graph(id, kind) if cache.enabled? and count == 0
  end
  Result.new(
    status:   orchio_status(response, 200),
    response: response,
    count:    response.body.count,
    results:  docs
  )
end

#orchio_purgeObject

Deletes the primary_key and purges all of its immutable data from the collection.



162
163
164
165
# File 'lib/orchestrate_application/record.rb', line 162

def orchio_purge
  response = client.send_request :delete, inst_args(path: "?purge=true")
  orchio_status response, 204
end

#orchio_put(jdoc) ⇒ Object

Updates the collection with the data associated with this instance.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/orchestrate_application/record.rb', line 137

def orchio_put(jdoc)
  response = client.send_request :put, inst_args(json: jdoc)
  if cache.enabled?
    simple_cache.save(
      Document.new(
        response.body.to_hash,
        Metadata.new(
          :collection => ocollection,
          :key => @id,
          :ref => response.header.etag
    )))
  end
  set_ref_value response
  orchio_status response, 201
end

#orchio_put_event(event_type, timestamp = nil, document) ⇒ Object



239
240
241
242
243
244
245
# File 'lib/orchestrate_application/record.rb', line 239

def orchio_put_event(event_type, timestamp=nil, document)
  # add_event_type event_type
  response = client.send_request(:put, inst_args(
    event_type: event_type, timestamp: timestamp, json: document
  ))
  orchio_status response, 204
end

#orchio_put_graph(kind, to_collection, to_key) ⇒ Object

Add a graph/relation to the collection. Store the to_key’s ‘ref’ value if caching is enabled.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/orchestrate_application/record.rb', line 283

def orchio_put_graph(kind, to_collection, to_key)
  # add_relation_kind kind

  if cache.enabled?
    ref = simple_cache.get_ref to_collection, to_key
    simple_cache.get_cc(ocollection).save_graph Metadata.new(
      { from_key: @id, kind: kind, ref: ref }
    )
  end

  response = client.send_request(
    :put,
    inst_args(kind: kind, to_collection: to_collection, to_key: to_key)
  )
  orchio_status response, 204
end

#orchio_put_if_match(document, ref) ⇒ Object

Updates the key/value if the send’ref’ value matches the ‘ref’ value for the latest version in the collection.



188
189
190
191
192
# File 'lib/orchestrate_application/record.rb', line 188

def orchio_put_if_match(document, ref)
  response = client.send_request :put, inst_args(json: document, ref: ref)
  set_ref_value response
  orchio_status response, 201
end

#orchio_put_if_none_match(document) ⇒ Object

Updates the key/value if the key does not already exist in the collection.



195
196
197
# File 'lib/orchestrate_application/record.rb', line 195

def orchio_put_if_none_match(document)
  orchio_put_if_match(document, '"*"')
end