couch_potato 1.15.0 → 1.16.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +5 -0
- data/lib/couch_potato/database.rb +14 -7
- data/lib/couch_potato/persistence/ghost_attributes.rb +7 -3
- data/lib/couch_potato/version.rb +1 -1
- data/spec/unit/caching_spec.rb +53 -1
- data/spec/unit/database_spec.rb +45 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3fd11db6b5bb37ef9b0d12115ae4586f8f0ec8afff586e7a2b381e1839af0196
|
4
|
+
data.tar.gz: 7f03e40ec9297bb669f28559e251b72f5b8e849010b8861d992c75c5ed7737a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72bc1831a2b773d3f64682f1fbe20fb10acccad204fd057e92676c53fe03f5f56b462c02905b3f3ee52ddb6fa640f6dfe6a05a3f1e1aeb8e750eaecb76922a38
|
7
|
+
data.tar.gz: 4a49349375cabe65b465c99336e3c0b05564d0b720faa6ae4d956633809ea52d6a73119118c7bc923d0ffde0aefd5af93f3fbdc587fed89696942b05ce421025
|
data/CHANGES.md
CHANGED
@@ -150,7 +150,7 @@ module CouchPotato
|
|
150
150
|
cached = cache && cache[id]
|
151
151
|
if cache
|
152
152
|
if cached
|
153
|
-
ActiveSupport::Notifications.instrument('couch_potato.load.cached') do
|
153
|
+
ActiveSupport::Notifications.instrument('couch_potato.load.cached', id: id, doc: cached) do
|
154
154
|
cached
|
155
155
|
end
|
156
156
|
else
|
@@ -168,15 +168,18 @@ module CouchPotato
|
|
168
168
|
|
169
169
|
uncached_ids = ids - (cache&.keys || [])
|
170
170
|
uncached_docs_by_id = bulk_load(uncached_ids).index_by {|doc| doc.id if doc.respond_to?(:id) }
|
171
|
+
cached_docs_by_id = cache&.slice(*ids) || {}
|
172
|
+
if cached_docs_by_id.any?
|
173
|
+
ActiveSupport::Notifications.instrument('couch_potato.load.cached', ids: cached_docs_by_id.keys, docs: cached_docs_by_id.values) do
|
174
|
+
cached_docs_by_id
|
175
|
+
end
|
176
|
+
end
|
171
177
|
if cache
|
172
178
|
uncached_ids.each do |id|
|
173
179
|
doc = uncached_docs_by_id[id]
|
174
180
|
cache[id] = doc if doc
|
175
181
|
end
|
176
182
|
end
|
177
|
-
cached_docs_by_id = ActiveSupport::Notifications.instrument('couch_potato.load.cached') do
|
178
|
-
cache&.slice(*ids) || {}
|
179
|
-
end
|
180
183
|
ids.filter_map { |id| (cached_docs_by_id[id]) || uncached_docs_by_id[id] }
|
181
184
|
end
|
182
185
|
|
@@ -278,9 +281,11 @@ module CouchPotato
|
|
278
281
|
def load_document_without_caching(id)
|
279
282
|
raise "Can't load a document without an id (got nil)" if id.nil?
|
280
283
|
|
281
|
-
|
284
|
+
payload = {id: id}
|
285
|
+
ActiveSupport::Notifications.instrument('couch_potato.load', payload) do
|
282
286
|
instance = couchrest_database.get(id)
|
283
287
|
instance.database = self if instance
|
288
|
+
payload[:doc] = instance
|
284
289
|
instance
|
285
290
|
end
|
286
291
|
end
|
@@ -321,9 +326,11 @@ module CouchPotato
|
|
321
326
|
def bulk_load(ids)
|
322
327
|
return [] if ids.empty?
|
323
328
|
|
324
|
-
|
329
|
+
payload = {ids: ids}
|
330
|
+
ActiveSupport::Notifications.instrument('couch_potato.load', payload) do
|
325
331
|
response = couchrest_database.bulk_load ids
|
326
|
-
docs = response[
|
332
|
+
docs = response["rows"].map { |row| row["doc"] }.compact
|
333
|
+
payload[:docs] = docs
|
327
334
|
docs.each do |doc|
|
328
335
|
doc.database = self if doc.respond_to?(:database=)
|
329
336
|
doc.database_collection = docs if doc.respond_to?(:database_collection=)
|
@@ -1,12 +1,16 @@
|
|
1
1
|
module CouchPotato
|
2
|
-
module GhostAttributes
|
3
|
-
def method_missing(name, *
|
4
|
-
if(value = _document && _document[name.to_s])
|
2
|
+
module GhostAttributes # :nodoc:
|
3
|
+
def method_missing(name, *)
|
4
|
+
if (value = _document && _document[name.to_s])
|
5
5
|
value
|
6
6
|
else
|
7
7
|
super
|
8
8
|
end
|
9
9
|
end
|
10
|
+
|
11
|
+
def respond_to_missing?(name, *)
|
12
|
+
_document && _document[name.to_s]
|
13
|
+
end
|
10
14
|
end
|
11
15
|
end
|
12
16
|
|
data/lib/couch_potato/version.rb
CHANGED
data/spec/unit/caching_spec.rb
CHANGED
@@ -18,6 +18,10 @@ RSpec.describe 'database caching' do
|
|
18
18
|
{}
|
19
19
|
end
|
20
20
|
|
21
|
+
after(:each) do
|
22
|
+
ActiveSupport::Notifications.unsubscribe(@subscriber) if @subscriber
|
23
|
+
end
|
24
|
+
|
21
25
|
context 'for a single document' do
|
22
26
|
it 'gets an object from the cache the 2nd time via #load_documemt' do
|
23
27
|
expect(couchrest_db).to receive(:get).with('1').exactly(1).times
|
@@ -47,6 +51,29 @@ RSpec.describe 'database caching' do
|
|
47
51
|
db.load_document '1'
|
48
52
|
expect(db.load_document('1')).to eql(doc)
|
49
53
|
end
|
54
|
+
|
55
|
+
it 'instruments the load call' do
|
56
|
+
doc = double("doc").as_null_object
|
57
|
+
allow(couchrest_db).to receive(:get).and_return(doc)
|
58
|
+
events = []
|
59
|
+
@subscriber = ActiveSupport::Notifications.subscribe(
|
60
|
+
'couch_potato.load.cached'
|
61
|
+
) do |event|
|
62
|
+
events << event
|
63
|
+
end
|
64
|
+
|
65
|
+
db.load("1")
|
66
|
+
db.load("1")
|
67
|
+
|
68
|
+
expect(events.size).to eq(1)
|
69
|
+
expect(events.first.payload).to eq(
|
70
|
+
{
|
71
|
+
id: "1",
|
72
|
+
doc: doc
|
73
|
+
}
|
74
|
+
)
|
75
|
+
|
76
|
+
end
|
50
77
|
end
|
51
78
|
|
52
79
|
context 'for multiple documents' do
|
@@ -65,6 +92,31 @@ RSpec.describe 'database caching' do
|
|
65
92
|
expect(couchrest_db).to have_received(:bulk_load).with(['2']).exactly(1).times
|
66
93
|
end
|
67
94
|
|
95
|
+
it 'instruments the load call' do
|
96
|
+
allow(couchrest_db).to receive(:bulk_load).with(['1'])
|
97
|
+
.and_return('rows' => [{'doc' => doc1}])
|
98
|
+
allow(couchrest_db).to receive(:bulk_load).with(['2'])
|
99
|
+
.and_return('rows' => [{'doc' => doc2}])
|
100
|
+
events = []
|
101
|
+
@subscriber = ActiveSupport::Notifications.subscribe(
|
102
|
+
'couch_potato.load.cached'
|
103
|
+
) do |event|
|
104
|
+
events << event
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
db.load_document(['1'])
|
109
|
+
db.load_document(['1', '2'])
|
110
|
+
|
111
|
+
expect(events.size).to eq(1)
|
112
|
+
expect(events.first.payload).to eq(
|
113
|
+
{
|
114
|
+
ids: ["1"],
|
115
|
+
docs: [doc1]
|
116
|
+
}
|
117
|
+
)
|
118
|
+
end
|
119
|
+
|
68
120
|
it 'loads nothing if all documents are cached' do
|
69
121
|
allow(couchrest_db).to receive(:bulk_load).with(['1', '2'])
|
70
122
|
.and_return('rows' => [{'doc' => doc1}, {'doc' => doc2}])
|
@@ -94,7 +146,7 @@ RSpec.describe 'database caching' do
|
|
94
146
|
'id' => '2',
|
95
147
|
}
|
96
148
|
allow(couchrest_db).to receive(:bulk_load).with(['1', '2'])
|
97
|
-
.and_return('rows' => [{'doc' => doc1}, {'doc' =>
|
149
|
+
.and_return('rows' => [{'doc' => doc1}, {'doc' => doc2}])
|
98
150
|
|
99
151
|
db.load_document(['1', '2'])
|
100
152
|
db.load_document(['1', '2'])
|
data/spec/unit/database_spec.rb
CHANGED
@@ -42,6 +42,11 @@ describe CouchPotato::Database, 'load' do
|
|
42
42
|
let(:couchrest_db) { double('couchrest db', info: nil).as_null_object }
|
43
43
|
let(:db) { CouchPotato::Database.new couchrest_db }
|
44
44
|
|
45
|
+
|
46
|
+
after(:each) do
|
47
|
+
ActiveSupport::Notifications.unsubscribe(@subscriber) if @subscriber
|
48
|
+
end
|
49
|
+
|
45
50
|
it 'should raise an exception if nil given' do
|
46
51
|
expect do
|
47
52
|
db.load nil
|
@@ -130,6 +135,46 @@ describe CouchPotato::Database, 'load' do
|
|
130
135
|
it 'returns an empty array when passing an empty array' do
|
131
136
|
expect(db.load([])).to eq([])
|
132
137
|
end
|
138
|
+
|
139
|
+
it 'instruments the load call' do
|
140
|
+
events = []
|
141
|
+
@subscriber = ActiveSupport::Notifications.subscribe(
|
142
|
+
'couch_potato.load'
|
143
|
+
) do |event|
|
144
|
+
events << event
|
145
|
+
end
|
146
|
+
|
147
|
+
db.load(["1", "2"])
|
148
|
+
|
149
|
+
expect(events.size).to eq(1)
|
150
|
+
expect(events.first.payload).to eq(
|
151
|
+
{
|
152
|
+
ids: ["1", "2"],
|
153
|
+
docs: [doc1, doc2]
|
154
|
+
}
|
155
|
+
)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'instruments the load call' do
|
160
|
+
doc = double("doc").as_null_object
|
161
|
+
allow(couchrest_db).to receive(:get).and_return(doc)
|
162
|
+
events = []
|
163
|
+
@subscriber = ActiveSupport::Notifications.subscribe(
|
164
|
+
'couch_potato.load'
|
165
|
+
) do |event|
|
166
|
+
events << event
|
167
|
+
end
|
168
|
+
|
169
|
+
db.load("1")
|
170
|
+
|
171
|
+
expect(events.size).to eq(1)
|
172
|
+
expect(events.first.payload).to eq(
|
173
|
+
{
|
174
|
+
id: "1",
|
175
|
+
doc: doc
|
176
|
+
}
|
177
|
+
)
|
133
178
|
end
|
134
179
|
end
|
135
180
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: couch_potato
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.16.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Lang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-04-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|