modis 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -0
- data/lib/modis/finder.rb +5 -0
- data/lib/modis/persistence.rb +16 -4
- data/lib/modis/version.rb +1 -1
- data/spec/finder_spec.rb +10 -0
- data/spec/persistence_spec.rb +22 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb24ac739595677e1200448641dc4e7c35a26f1d
|
4
|
+
data.tar.gz: 428715902d2b7e335e16d7ee2b65aa58c3864b52
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f34d83dd16b16bd113a116d060523c8f832593f604e318077be24d279d8a9f06ce269f86a3935f5fbe8e1a70d815075a54f69c9ee3f4d45eaa0fafc211a461e
|
7
|
+
data.tar.gz: a3451db68ba6943d667fb11470c9635dfb0c74ac9449b330125ffe7be4d9b0c3e94df459d8a2acb8ab0069a6fa80fadf4c83a8630898ef26c023857f34cfa473
|
data/README.md
CHANGED
@@ -36,6 +36,19 @@ end
|
|
36
36
|
MyModel.create!(:name => 'Ian', :age => 28)
|
37
37
|
```
|
38
38
|
|
39
|
+
### all index
|
40
|
+
|
41
|
+
Modis, by default, creates an `all` index in redis in which it stores all the IDs for records created. As a result, a large amount of memory will be consumed if many ids are stored. The `all` index functionality can be turned off by using `enable_all_index`
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
class MyModel
|
45
|
+
include Modis::Model
|
46
|
+
enable_all_index false
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
By disabling the `all` index functionality, the IDs of each record created won't be saved. As a side effect, using `all` finder method will raise a `IndexError` exception as we would not have enough information to fetch all records. See https://github.com/ileitch/modis/pull/7 for more context.
|
51
|
+
|
39
52
|
## Supported Features
|
40
53
|
|
41
54
|
TODO.
|
data/lib/modis/finder.rb
CHANGED
@@ -13,6 +13,11 @@ module Modis
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def all
|
16
|
+
unless all_index_enabled?
|
17
|
+
raise IndexError, "Unable to retrieve all records of #{name}, "\
|
18
|
+
"because you disabled all index. See :enable_all_index for more."
|
19
|
+
end
|
20
|
+
|
16
21
|
records = Modis.with_connection do |redis|
|
17
22
|
ids = redis.smembers(key_for(:all))
|
18
23
|
redis.pipelined do
|
data/lib/modis/persistence.rb
CHANGED
@@ -61,6 +61,14 @@ module Modis
|
|
61
61
|
"#{sti_base_absolute_namespace}:#{id}"
|
62
62
|
end
|
63
63
|
|
64
|
+
def enable_all_index(bool)
|
65
|
+
@use_all_index = bool
|
66
|
+
end
|
67
|
+
|
68
|
+
def all_index_enabled?
|
69
|
+
@use_all_index == true || @use_all_index.nil?
|
70
|
+
end
|
71
|
+
|
64
72
|
def create(attrs)
|
65
73
|
model = new(attrs)
|
66
74
|
model.save
|
@@ -128,8 +136,10 @@ module Modis
|
|
128
136
|
run_callbacks :destroy do
|
129
137
|
redis.pipelined do
|
130
138
|
remove_from_indexes(redis)
|
131
|
-
|
132
|
-
|
139
|
+
if self.class.all_index_enabled?
|
140
|
+
redis.srem(self.class.key_for(:all), id)
|
141
|
+
redis.srem(self.class.sti_base_key_for(:all), id) if self.class.sti_child?
|
142
|
+
end
|
133
143
|
redis.del(key)
|
134
144
|
end
|
135
145
|
end
|
@@ -198,8 +208,10 @@ module Modis
|
|
198
208
|
future = attrs.any? ? redis.hmset(key, attrs) : :unchanged
|
199
209
|
|
200
210
|
if new_record?
|
201
|
-
|
202
|
-
|
211
|
+
if self.class.all_index_enabled?
|
212
|
+
redis.sadd(self.class.key_for(:all), id)
|
213
|
+
redis.sadd(self.class.sti_base_key_for(:all), id) if self.class.sti_child?
|
214
|
+
end
|
203
215
|
add_to_indexes(redis)
|
204
216
|
else
|
205
217
|
update_indexes(redis)
|
data/lib/modis/version.rb
CHANGED
data/spec/finder_spec.rb
CHANGED
@@ -22,6 +22,10 @@ module FindersSpec
|
|
22
22
|
|
23
23
|
class Worker < Producer
|
24
24
|
end
|
25
|
+
|
26
|
+
class UserNoAllIndex < User
|
27
|
+
enable_all_index false
|
28
|
+
end
|
25
29
|
end
|
26
30
|
|
27
31
|
describe Modis::Finder do
|
@@ -67,6 +71,12 @@ describe Modis::Finder do
|
|
67
71
|
model.destroy
|
68
72
|
expect(FindersSpec::User.all).to eq([])
|
69
73
|
end
|
74
|
+
|
75
|
+
it 'throws error when enable_all_index option is set to false' do
|
76
|
+
FindersSpec::UserNoAllIndex.create!(name: 'Yana')
|
77
|
+
expect { FindersSpec::UserNoAllIndex.all }
|
78
|
+
.to raise_error(Modis::IndexError)
|
79
|
+
end
|
70
80
|
end
|
71
81
|
|
72
82
|
it 'identifies a found record as not being new' do
|
data/spec/persistence_spec.rb
CHANGED
@@ -47,6 +47,10 @@ module PersistenceSpec
|
|
47
47
|
called_callbacks << :test_before_save
|
48
48
|
end
|
49
49
|
end
|
50
|
+
|
51
|
+
class MockModelNoAllIndex < MockModel
|
52
|
+
enable_all_index false
|
53
|
+
end
|
50
54
|
end
|
51
55
|
|
52
56
|
describe Modis::Persistence do
|
@@ -296,4 +300,22 @@ describe Modis::Persistence do
|
|
296
300
|
expect(model.update_attributes(name: nil)).to be false
|
297
301
|
end
|
298
302
|
end
|
303
|
+
|
304
|
+
describe 'key for all records' do
|
305
|
+
let(:all_key_name) { "#{PersistenceSpec::MockModel.absolute_namespace}:all" }
|
306
|
+
|
307
|
+
describe 'when :enable_all_index option is set to false' do
|
308
|
+
it 'does not save new record to the *:all key' do
|
309
|
+
model = PersistenceSpec::MockModel.create!(name: 'Sage')
|
310
|
+
expect(Redis.new.smembers(all_key_name).map(&:to_i)).to include(model.id)
|
311
|
+
end
|
312
|
+
end
|
313
|
+
|
314
|
+
describe 'when :enable_all_index option is set to false' do
|
315
|
+
it 'does not save new record to the *:all key' do
|
316
|
+
model = PersistenceSpec::MockModelNoAllIndex.create!(name: 'Alex')
|
317
|
+
expect(Redis.new.smembers(all_key_name).map(&:to_i)).to_not include(model.id)
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|
299
321
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: modis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ian Leitch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|