Class: EntityStore::Store

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/entity_store/store.rb

Instance Method Summary collapse

Methods included from Logging

#log_error

Instance Method Details

#add(entity) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/entity_store/store.rb', line 9

def add(entity)
  entity.id = storage_client.add_entity(entity)
  add_events(entity)

  # publish version increment signal event to the bus
  event_bus.publish(entity.type, entity.generate_version_incremented_event)

  entity
rescue => e
  logger.error { "Store#add error: #{e.inspect} - #{entity.inspect}" }
  raise e
end

#add_events(entity) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/entity_store/store.rb', line 64

def add_events(entity)
  items = entity.pending_events.map do |event|
    event.entity_id = entity.id.to_s
    event.entity_version = entity.version
    event
  end
  storage_client.add_events(items)

  yield if block_given?

  items.each {|e| event_bus.publish(entity.type, e) }

  entity.clear_pending_events
end

#clear_all(confirm) ⇒ Object

Public: USE AT YOUR PERIL this clears the ENTIRE data store

confirm - Symbol that must equal :i_am_sure

Returns nothing



141
142
143
144
145
146
147
# File 'lib/entity_store/store.rb', line 141

def clear_all(confirm)
  unless confirm == :i_am_sure
    raise "#clear_all call with :i_am_sure in order to do this"
  end
  storage_client.clear
  @_storage_client = nil
end

#clear_entity_events(id, excluded_types = []) ⇒ Object



60
61
62
# File 'lib/entity_store/store.rb', line 60

def clear_entity_events(id, excluded_types = [])
  storage_client.clear_entity_events(id, excluded_types)
end

#event_busObject



149
150
151
# File 'lib/entity_store/store.rb', line 149

def event_bus
  @_event_bus ||= EventBus.new
end

#get(id, raise_exception = false) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/entity_store/store.rb', line 83

def get(id, raise_exception=false)
  options = {
    raise_exception: raise_exception
  }

  get_with_ids([id], options).first
end

#get!(id) ⇒ Object



79
80
81
# File 'lib/entity_store/store.rb', line 79

def get!(id)
  get(id, true)
end

#get_audit(id, output = nil) ⇒ Object

Public: returns an array representing a full audit trail for the entity. After each event is applied the state of the entity is rendered. Optionally accepts a block which should return true or false to indicate whether to render the line. The block yields entity, event, lines collection



157
158
159
160
161
162
163
164
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
# File 'lib/entity_store/store.rb', line 157

def get_audit(id, output=nil)
  lines = []

  if entity = storage_client.get_entity(id, true)

    lines << "---"
    lines << entity.inspect
    lines << "---"

    storage_client.get_events(id, entity.version).each do |event|

      begin
        entity.apply_event(event)
        entity.version = event.entity_version

        render = true

        if block_given?
          render = yield(entity, event, lines)
        end

        if render
          lines << event.inspect
          lines << entity.inspect

          lines << "---"
        end

      rescue => e
        lines << "ERROR #{e.class.name} #{e.message}"
      end
    end

  else
    lines << "No entity for #{id}"
  end

  if output
    output.write lines.join("\n")
  else
    lines
  end
end

#get_with_ids(ids, options = {}) ⇒ Object

Public: get a series of entities

ids - Array of id strings options - Hash of options (default: {})

:raise_exception - Boolean (default true)

Returns an Array of entities



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/entity_store/store.rb', line 98

def get_with_ids(ids, options={})

  entities = Hash[ storage_client.get_entities(ids, options).map { |e| [ e.id, e] } ]

  if options.fetch(:raise_exception, true)
    ids.each do |id|
      raise NotFound.new(id) unless entities[id]
    end
  end

  criteria = entities.map do |id, entity|
    { id: id, since_version: entity.version }
  end

  events = storage_client.get_events(criteria)

  entities.each do |id, entity|

    next unless entity_events = events[id]

    entity_events.each do |event|
      begin
        entity.apply_event(event)
        log_debug { "Applied #{event.inspect} to #{id}" }
      rescue => e
        log_error "Failed to apply #{event.class.name} #{event.attributes} to #{id} with #{e.inspect}", e
        raise if options.fetch(:raise_exception, true)
      end
      entity.version = event.entity_version
    end

  end

  # ensure entities are returned in same order as requested
  ids.map { |id| entities[id] }

end

#remove_entity_snapshot(id) ⇒ Object



52
53
54
# File 'lib/entity_store/store.rb', line 52

def remove_entity_snapshot(id)
  storage_client.remove_entity_snapshot(id)
end

#remove_snapshots(type = nil) ⇒ Object



56
57
58
# File 'lib/entity_store/store.rb', line 56

def remove_snapshots type=nil
  storage_client.remove_snapshots type
end

#save(entity) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/entity_store/store.rb', line 22

def save(entity)
  # need to look at concurrency if we start storing version on client
  if entity.pending_events.empty?
    snapshot_entity(entity) if entity.snapshot_due?
  else
    entity.version += 1
    if entity.id
      storage_client.save_entity(entity)
    else
      entity.id = storage_client.add_entity(entity)
    end

    add_events(entity) do
      snapshot_entity(entity) if entity.snapshot_due?
    end

    # publish version increment signal event to the bus
    event_bus.publish(entity.type, entity.generate_version_incremented_event)
  end
  entity
rescue => e
  log_error "Store#save error: #{e.inspect} - #{entity.inspect}", e
  raise e
end

#snapshot_entity(entity) ⇒ Object



47
48
49
50
# File 'lib/entity_store/store.rb', line 47

def snapshot_entity(entity)
  log_info { "Store#snapshot_entity : Snapshotting #{entity.id}"}
  storage_client.snapshot_entity(entity)
end

#storage_clientObject



5
6
7
# File 'lib/entity_store/store.rb', line 5

def storage_client
  @_storage_client ||= EntityStore::Config.store
end