Class: RegistersClient::InMemoryDataStore

Inherits:
Object
  • Object
show all
Includes:
DataStore
Defined in:
lib/in_memory_data_store.rb

Constant Summary collapse

EMPTY_ROOT_HASH =
'sha-256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'

Instance Method Summary collapse

Constructor Details

#initialize(config_options) ⇒ InMemoryDataStore

Returns a new instance of InMemoryDataStore.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/in_memory_data_store.rb', line 17

def initialize(config_options)
  @config_options = config_options

  @data = {
    records: { user: {}, system: {} },
    entries: { user: [], system: [] },
    items: {},
    root_hash: EMPTY_ROOT_HASH
  }

end

Instance Method Details

#add_item(item) ⇒ Object



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

def add_item(item)
  @data[:items][item.hash.to_s] = item
end

#after_loadObject



89
90
# File 'lib/in_memory_data_store.rb', line 89

def after_load
end

#append_entry(entry) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/in_memory_data_store.rb', line 33

def append_entry(entry)
  entry_type = entry.entry_type == 'user' ? :user : :system

  @data[:entries][entry_type] << entry

  if !@data[:records][entry_type].key?(entry.key)
    @data[:records][entry_type][entry.key] = []
  end

  @data[:records][entry_type][entry.key] << entry.entry_number
end

#get_entries(entry_type) ⇒ Object



70
71
72
# File 'lib/in_memory_data_store.rb', line 70

def get_entries(entry_type)
  EntryCollection.new(get_data[:entries][entry_type], @config_options.fetch(:page_size))
end

#get_entry(entry_type, entry_number) ⇒ Object



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

def get_entry(entry_type, entry_number)
  if (entry_number < 1 || entry_number > @data[:entries][entry_type].count)
    nil
  else
    @data[:entries][entry_type][entry_number - 1]
  end
end

#get_item(item_hash) ⇒ Object



45
46
47
# File 'lib/in_memory_data_store.rb', line 45

def get_item(item_hash)
  @data[:items].has_key?(item_hash) ? @data[:items][item_hash] : nil
end

#get_itemsObject



49
50
51
# File 'lib/in_memory_data_store.rb', line 49

def get_items
  ItemCollection.new(@data[:items].values)
end

#get_latest_entry_number(entry_type) ⇒ Object



83
84
85
86
87
# File 'lib/in_memory_data_store.rb', line 83

def get_latest_entry_number(entry_type)
  entries = @data[:entries][entry_type]
  entry = entries.any? ? entries.last : nil
  entry.nil? ? 0 : entry.entry_number
end

#get_record(entry_type, key) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/in_memory_data_store.rb', line 61

def get_record(entry_type, key)
  if (@data[:records][entry_type].has_key?(key))
    record_entry_numbers = @data[:records][entry_type][key]
    entry = get_data[:entries][entry_type][record_entry_numbers.last - 1]

    Record.new(entry, get_data[:items][entry.item_hash])
  end
end

#get_records(entry_type) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/in_memory_data_store.rb', line 74

def get_records(entry_type)
  RecordCollection.new(get_data[:records][entry_type].map do |_k, record_entry_numbers|
    entry = get_data[:entries][entry_type][record_entry_numbers.last - 1]
    item = get_data[:items][entry.item_hash]

    Record.new(entry, item)
  end, @config_options.fetch(:page_size))
end