Class: Akasha::Storage::MemoryEventStore

Inherits:
Object
  • Object
show all
Defined in:
lib/akasha/storage/memory_event_store.rb,
lib/akasha/storage/memory_event_store/stream.rb

Overview

Memory-based event store.

Defined Under Namespace

Classes: Stream

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMemoryEventStore

Returns a new instance of MemoryEventStore.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/akasha/storage/memory_event_store.rb', line 12

def initialize
  @monitor = Monitor.new
  store = self
  @streams = Hash.new do |streams, name|
    @monitor.synchronize do
      # Double-checked-locking.
      if streams.key?(name)
        streams[name]
      else
        streams[name] = Stream.new do |new_events|
          store.update_projections(new_events)
          new_events
        end
      end
    end
  end
  @projections = []
end

Instance Attribute Details

#streamsObject (readonly)

Access to streams Example:

store.streams['some-stream-name']


10
11
12
# File 'lib/akasha/storage/memory_event_store.rb', line 10

def streams
  @streams
end

Instance Method Details

#merge_all_by_event(into:, only:, namespace: nil) ⇒ Object

Merges all streams into one, filtering the resulting stream so it only contains events with the specified names.

Arguments:

`new_stream_name` - name of the new stream
`only` - array of event names
`namespace` - optional namespace; if provided, the resulting stream will
              only contain events with the same metadata.namespace


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/akasha/storage/memory_event_store.rb', line 39

def merge_all_by_event(into:, only:, namespace: nil)
  new_stream = Stream.new do |new_events|
    new_events.select do |event|
      (namespace.nil? || namespace == event.[:namespace]) &&
        only.include?(event.name)
    end
  end
  @monitor.synchronize do
    @streams[into] = new_stream
    @projections << new_stream
  end
  new_stream
end