Class: Lutaml::Store::BasicStore

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/store/basic_store.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_or_adapter = {}) ⇒ BasicStore

Returns a new instance of BasicStore.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lutaml/store/basic_store.rb', line 8

def initialize(config_or_adapter = {})
  if config_or_adapter.is_a?(Adapter::Base)
    @adapter = config_or_adapter
    @config = Config.new
  elsif config_or_adapter.is_a?(Config)
    @config = config_or_adapter
    @adapter = create_adapter
  else
    @config = Config.new(**config_or_adapter.transform_keys(&:to_sym))
    @adapter = create_adapter
  end

  @config.validate!

  @cache = @config.cache_enabled? ? create_cache : nil
  @monitor = @config.monitoring_enabled? ? Monitor.new : nil
  @events = Events.new(async: @config.async_events?)
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



6
7
8
# File 'lib/lutaml/store/basic_store.rb', line 6

def adapter
  @adapter
end

#cacheObject (readonly)

Returns the value of attribute cache.



6
7
8
# File 'lib/lutaml/store/basic_store.rb', line 6

def cache
  @cache
end

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/lutaml/store/basic_store.rb', line 6

def config
  @config
end

#eventsObject (readonly)

Returns the value of attribute events.



6
7
8
# File 'lib/lutaml/store/basic_store.rb', line 6

def events
  @events
end

#monitorObject (readonly)

Returns the value of attribute monitor.



6
7
8
# File 'lib/lutaml/store/basic_store.rb', line 6

def monitor
  @monitor
end

Class Method Details

.from_file(file_path) ⇒ Object



27
28
29
# File 'lib/lutaml/store/basic_store.rb', line 27

def self.from_file(file_path)
  new(Config.from_file(file_path))
end

.from_yaml(yaml_string) ⇒ Object



31
32
33
# File 'lib/lutaml/store/basic_store.rb', line 31

def self.from_yaml(yaml_string)
  new(Config.from_yaml(yaml_string))
end

Instance Method Details

#allObject



77
78
79
80
81
# File 'lib/lutaml/store/basic_store.rb', line 77

def all
  with_monitoring(:all) do
    @adapter.all
  end
end

#bulk_delete(keys) ⇒ Object



110
111
112
113
114
# File 'lib/lutaml/store/basic_store.rb', line 110

def bulk_delete(keys)
  result = @adapter.bulk_delete(keys)
  keys.each { |key| @cache&.delete(key) }
  result
end

#bulk_get(keys) ⇒ Object



101
102
103
# File 'lib/lutaml/store/basic_store.rb', line 101

def bulk_get(keys)
  @adapter.bulk_get(keys)
end

#bulk_set(key_value_pairs) ⇒ Object



105
106
107
108
# File 'lib/lutaml/store/basic_store.rb', line 105

def bulk_set(key_value_pairs)
  @adapter.bulk_set(key_value_pairs)
  key_value_pairs.each { |key, value| @cache&.set(key, value) }
end

#cache_statsObject



143
144
145
# File 'lib/lutaml/store/basic_store.rb', line 143

def cache_stats
  @cache&.stats
end

#clearObject



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

def clear
  with_monitoring(:clear) do
    @adapter.clear
    @cache&.clear
    emit_event(:clear)
  end
end

#closeObject



147
148
149
150
# File 'lib/lutaml/store/basic_store.rb', line 147

def close
  @events.stop if @config.async_events?
  @adapter.close
end

#delete(key) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/lutaml/store/basic_store.rb', line 60

def delete(key)
  with_monitoring(:delete) do
    result = @adapter.delete(key)
    @cache&.delete(key) if result
    emit_event(:delete, key: key, deleted: result)
    result
  end
end

#emit_event(event, data = {}) ⇒ Object



124
125
126
# File 'lib/lutaml/store/basic_store.rb', line 124

def emit_event(event, data = {})
  @events.emit(event, data)
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
# File 'lib/lutaml/store/basic_store.rb', line 69

def exists?(key)
  with_monitoring(:exists) do
    next true if @cache&.exists?(key)

    @adapter.exists?(key)
  end
end

#get(key) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lutaml/store/basic_store.rb', line 35

def get(key)
  with_monitoring(:get) do
    if @cache
      cached_value = @cache.get(key)
      if cached_value
        emit_event(:get, key: key, value: cached_value, source: :cache)
        next cached_value
      end
    end

    value = @adapter.get(key)
    @cache&.set(key, value) if value
    emit_event(:get, key: key, value: value, source: :adapter)
    value
  end
end

#keysObject



97
98
99
# File 'lib/lutaml/store/basic_store.rb', line 97

def keys
  @adapter.keys
end

#off(event, listener) ⇒ Object



120
121
122
# File 'lib/lutaml/store/basic_store.rb', line 120

def off(event, listener)
  @events.off(event, listener)
end

#on(event, callable = nil, &block) ⇒ Object



116
117
118
# File 'lib/lutaml/store/basic_store.rb', line 116

def on(event, callable = nil, &block)
  @events.on(event, callable, &block)
end

#set(key, value) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/lutaml/store/basic_store.rb', line 52

def set(key, value)
  with_monitoring(:set) do
    @adapter.set(key, value)
    @cache&.set(key, value)
    emit_event(:set, key: key, value: value)
  end
end

#sizeObject



91
92
93
94
95
# File 'lib/lutaml/store/basic_store.rb', line 91

def size
  with_monitoring(:size) do
    @adapter.size
  end
end

#statsObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/lutaml/store/basic_store.rb', line 128

def stats
  base_stats = {
    adapter: @adapter.class.name,
    size: size,
    cache_enabled: !@cache.nil?,
    monitoring_enabled: !@monitor.nil?
  }

  base_stats[:adapter_stats] = @adapter.stats
  base_stats[:cache_stats] = @cache.stats if @cache
  base_stats[:monitor_stats] = @monitor.stats if @monitor

  base_stats
end