Class: WCC::Contentful::Store::MemoryStore
- Inherits:
-
Base
- Object
- Base
- WCC::Contentful::Store::MemoryStore
show all
- Defined in:
- lib/wcc/contentful/store/memory_store.rb
Overview
The MemoryStore is the most naiive store implementation and a good reference point for more useful implementations. It only implements equality queries and does not support querying through an association.
Constant Summary
collapse
- SUPPORTED_OPS =
%i[eq ne in nin].freeze
Constants included
from Interface
Interface::INTERFACE_METHODS
Instance Method Summary
collapse
Methods inherited from Base
#ensure_hash, #find_all, #find_by, #index, #index?
Methods included from Interface
#find_all, #find_by, #index, #index?
Constructor Details
#initialize(configuration = nil) ⇒ MemoryStore
Returns a new instance of MemoryStore.
12
13
14
15
16
17
18
|
# File 'lib/wcc/contentful/store/memory_store.rb', line 12
def initialize(configuration = nil)
super
@configuration = configuration
@mutex = Concurrent::ReentrantReadWriteLock.new
@hash = {}
end
|
Instance Method Details
#delete(key) ⇒ Object
30
31
32
33
34
|
# File 'lib/wcc/contentful/store/memory_store.rb', line 30
def delete(key)
@mutex.with_write_lock do
@hash.delete(key)
end
end
|
#execute(query) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/wcc/contentful/store/memory_store.rb', line 48
def execute(query)
if bad_op = (query.conditions.map(&:op) - SUPPORTED_OPS).first
raise ArgumentError, "Operator :#{bad_op} not supported"
end
relation = @mutex.with_read_lock { @hash.values }
relation =
relation.lazy.reject do |v|
value_content_type = v.try(:dig, 'sys', 'contentType', 'sys', 'id')
if query.content_type == 'Asset'
!value_content_type.nil?
else
value_content_type != query.content_type
end
end
relation =
query.conditions.reduce(relation) do |memo, condition|
__send__("apply_#{condition.op}", memo, condition)
end
relation.map(&:deep_dup)
end
|
#find(key, **_options) ⇒ Object
40
41
42
43
44
|
# File 'lib/wcc/contentful/store/memory_store.rb', line 40
def find(key, **_options)
@mutex.with_read_lock do
@hash[key].deep_dup
end
end
|
#keys ⇒ Object
36
37
38
|
# File 'lib/wcc/contentful/store/memory_store.rb', line 36
def keys
@mutex.with_read_lock { @hash.keys }
end
|
#set(key, value) ⇒ Object
20
21
22
23
24
25
26
27
28
|
# File 'lib/wcc/contentful/store/memory_store.rb', line 20
def set(key, value)
value = value.deep_dup.freeze
ensure_hash value
@mutex.with_write_lock do
old = @hash[key]
@hash[key] = value
old
end
end
|