frozen_record 0.18.0 → 0.19.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +16 -0
- data/lib/frozen_record.rb +1 -0
- data/lib/frozen_record/base.rb +11 -5
- data/lib/frozen_record/compact.rb +3 -1
- data/lib/frozen_record/index.rb +77 -0
- data/lib/frozen_record/scope.rb +15 -4
- data/lib/frozen_record/version.rb +1 -1
- data/spec/frozen_record_spec.rb +1 -1
- data/spec/scope_spec.rb +14 -2
- data/spec/support/country.rb +3 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2fe4a43299f20ff72c35ee4093ef5f6c8d49214aabc82cf67108da083980b28e
|
4
|
+
data.tar.gz: 7023b4b75d3eb733ef534ae85f1b25e0efb7aac3ba8548dd9e1a25bdbe1ec804
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4031cf6cd49bec9dc97c246a0999736ed178d9f6b1e623ef6e60070da454e1141fb7ca25056d2d55f2f808d2812f0f1c14e856cecaf73b0dd403a2a047debf62
|
7
|
+
data.tar.gz: 823f999ec6e21c55b4b0cad6b39008c55377052d194a29af96edb0e85cc9a33227635246a9187fdc7207eac9ec515c352f54ac8c568a7ec7bd3589ef74dea0dd
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -140,6 +140,22 @@ Country.european.republics.part_of_nato.order(id: :desc)
|
|
140
140
|
- average
|
141
141
|
|
142
142
|
|
143
|
+
## Indexing
|
144
|
+
|
145
|
+
Querying is implemented as a simple linear search (`O(n)`). However if you are using Frozen Record with larger datasets, or are querying
|
146
|
+
a collection repetedly, you can define indices for faster access.
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
class Country < FrozenRecord::Base
|
150
|
+
add_index :name, unique: true
|
151
|
+
add_index :continent
|
152
|
+
end
|
153
|
+
```
|
154
|
+
|
155
|
+
Composite index keys are not supported.
|
156
|
+
|
157
|
+
The primary key isn't indexed by default.
|
158
|
+
|
143
159
|
## Configuration
|
144
160
|
|
145
161
|
### Reloading
|
data/lib/frozen_record.rb
CHANGED
data/lib/frozen_record/base.rb
CHANGED
@@ -21,6 +21,8 @@ module FrozenRecord
|
|
21
21
|
FALSY_VALUES = [false, nil, 0, -''].to_set
|
22
22
|
|
23
23
|
class_attribute :base_path, :primary_key, :backend, :auto_reloading, :default_attributes, instance_accessor: false
|
24
|
+
class_attribute :index_definitions, instance_accessor: false
|
25
|
+
self.index_definitions = {}.freeze
|
24
26
|
|
25
27
|
self.primary_key = 'id'
|
26
28
|
|
@@ -103,6 +105,11 @@ module FrozenRecord
|
|
103
105
|
end
|
104
106
|
end
|
105
107
|
|
108
|
+
def add_index(attribute, unique: false)
|
109
|
+
index = unique ? UniqueIndex.new(self, attribute) : Index.new(self, attribute)
|
110
|
+
self.index_definitions = index_definitions.merge(index.attribute => index).freeze
|
111
|
+
end
|
112
|
+
|
106
113
|
def memsize(object = self, seen = Set.new.compare_by_identity)
|
107
114
|
return 0 unless seen.add?(object)
|
108
115
|
|
@@ -143,15 +150,14 @@ module FrozenRecord
|
|
143
150
|
records = Deduplication.deep_deduplicate!(records)
|
144
151
|
@attributes = list_attributes(records).freeze
|
145
152
|
define_attribute_methods(@attributes.to_a)
|
146
|
-
records.map { |r| load(r) }.freeze
|
153
|
+
records = records.map { |r| load(r) }.freeze
|
154
|
+
index_definitions.values.each { |index| index.build(records) }
|
155
|
+
records
|
147
156
|
end
|
148
157
|
end
|
149
158
|
|
150
159
|
def scope(name, body)
|
151
|
-
|
152
|
-
raise ArgumentError, "The scope body needs to be callable."
|
153
|
-
end
|
154
|
-
singleton_class.send(:define_method, name) { |*args| body.call(*args) }
|
160
|
+
singleton_class.send(:define_method, name, &body)
|
155
161
|
end
|
156
162
|
|
157
163
|
alias_method :load, :new
|
@@ -18,7 +18,9 @@ module FrozenRecord
|
|
18
18
|
@attributes = list_attributes(records).freeze
|
19
19
|
build_attributes_cache
|
20
20
|
define_attribute_methods(@attributes.to_a)
|
21
|
-
records.map { |r| load(r) }.freeze
|
21
|
+
records = records.map { |r| load(r) }.freeze
|
22
|
+
index_definitions.values.each { |index| index.build(records) }
|
23
|
+
records
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FrozenRecord
|
4
|
+
class Index
|
5
|
+
EMPTY_ARRAY = [].freeze
|
6
|
+
private_constant :EMPTY_ARRAY
|
7
|
+
|
8
|
+
AttributeNonUnique = Class.new(StandardError)
|
9
|
+
|
10
|
+
attr_reader :attribute, :model
|
11
|
+
|
12
|
+
def initialize(model, attribute, unique: false)
|
13
|
+
@model = model
|
14
|
+
@attribute = -attribute.to_s
|
15
|
+
@index = nil
|
16
|
+
end
|
17
|
+
|
18
|
+
def unique?
|
19
|
+
false
|
20
|
+
end
|
21
|
+
|
22
|
+
def query(value)
|
23
|
+
case value
|
24
|
+
when Array, Range
|
25
|
+
lookup_multi(value)
|
26
|
+
else
|
27
|
+
lookup(value)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def lookup_multi(values)
|
32
|
+
values.flat_map { |v| lookup(v) }
|
33
|
+
end
|
34
|
+
|
35
|
+
def lookup(value)
|
36
|
+
@index.fetch(value, EMPTY_ARRAY)
|
37
|
+
end
|
38
|
+
|
39
|
+
def reset
|
40
|
+
@index = nil
|
41
|
+
end
|
42
|
+
|
43
|
+
def build(records)
|
44
|
+
@index = records.each_with_object({}) do |record, index|
|
45
|
+
entry = (index[record[attribute]] ||= [])
|
46
|
+
entry << record
|
47
|
+
end
|
48
|
+
@index.values.each(&:freeze)
|
49
|
+
@index.freeze
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class UniqueIndex < Index
|
54
|
+
def unique?
|
55
|
+
true
|
56
|
+
end
|
57
|
+
|
58
|
+
def lookup_multi(values)
|
59
|
+
results = @index.values_at(*values)
|
60
|
+
results.compact!
|
61
|
+
results
|
62
|
+
end
|
63
|
+
|
64
|
+
def lookup(value)
|
65
|
+
record = @index[value]
|
66
|
+
record ? [record] : EMPTY_ARRAY
|
67
|
+
end
|
68
|
+
|
69
|
+
def build(records)
|
70
|
+
@index = records.each_with_object({}) { |r, index| index[r[attribute]] = r }
|
71
|
+
if @index.size != records.size
|
72
|
+
raise AttributeNonUnique, "#{model}##{attribute.inspect} is not unique."
|
73
|
+
end
|
74
|
+
@index.freeze
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/lib/frozen_record/scope.rb
CHANGED
@@ -175,9 +175,20 @@ module FrozenRecord
|
|
175
175
|
def select_records(records)
|
176
176
|
return records if @where_values.empty? && @where_not_values.empty?
|
177
177
|
|
178
|
+
indices = @klass.index_definitions
|
179
|
+
indexed_where_values, unindexed_where_values = @where_values.partition { |criteria| indices.key?(criteria.first) }
|
180
|
+
|
181
|
+
unless indexed_where_values.empty?
|
182
|
+
attribute, value = indexed_where_values.shift
|
183
|
+
records = indices[attribute].query(value)
|
184
|
+
indexed_where_values.each do |(attribute, value)|
|
185
|
+
records &= indices[attribute].query(value)
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
178
189
|
records.select do |record|
|
179
|
-
|
180
|
-
|
190
|
+
unindexed_where_values.all? { |attr, value| compare_value(record[attr], value) } &&
|
191
|
+
!@where_not_values.any? { |attr, value| compare_value(record[attr], value) }
|
181
192
|
end
|
182
193
|
end
|
183
194
|
|
@@ -226,12 +237,12 @@ module FrozenRecord
|
|
226
237
|
end
|
227
238
|
|
228
239
|
def where!(criterias)
|
229
|
-
@where_values += criterias.
|
240
|
+
@where_values += criterias.map { |k, v| [k.to_s, v] }
|
230
241
|
self
|
231
242
|
end
|
232
243
|
|
233
244
|
def where_not!(criterias)
|
234
|
-
@where_not_values += criterias.
|
245
|
+
@where_not_values += criterias.map { |k, v| [k.to_s, v] }
|
235
246
|
self
|
236
247
|
end
|
237
248
|
|
data/spec/frozen_record_spec.rb
CHANGED
@@ -83,7 +83,7 @@ RSpec.shared_examples 'main' do
|
|
83
83
|
describe '.scope' do
|
84
84
|
|
85
85
|
it 'defines a scope method' do
|
86
|
-
country_model.scope :north_american, -> {
|
86
|
+
country_model.scope :north_american, -> { where(continent: 'North America') }
|
87
87
|
expect(country_model).to respond_to(:north_american)
|
88
88
|
expect(country_model.north_american.first.name).to be == 'Canada'
|
89
89
|
end
|
data/spec/scope_spec.rb
CHANGED
@@ -203,6 +203,18 @@ describe 'querying' do
|
|
203
203
|
expect(countries.length).to be == 2
|
204
204
|
end
|
205
205
|
|
206
|
+
it 'can combine indices' do
|
207
|
+
countries = Country.where(name: 'France', continent: 'Europe')
|
208
|
+
expect(countries.length).to be == 1
|
209
|
+
end
|
210
|
+
|
211
|
+
it 'can use indices with inclusion query' do
|
212
|
+
countries = Country.where(continent: ['Europe', 'North America'])
|
213
|
+
expect(countries.length).to be == 3
|
214
|
+
|
215
|
+
countries = Country.where(name: ['France', 'Canada'])
|
216
|
+
expect(countries.length).to be == 2
|
217
|
+
end
|
206
218
|
end
|
207
219
|
|
208
220
|
describe '.where.not' do
|
@@ -428,8 +440,8 @@ describe 'querying' do
|
|
428
440
|
it 'returns true when the same scope has be rechained' do
|
429
441
|
scope_a = Country.nato.republics.nato.republics
|
430
442
|
scope_b = Country.republics.nato
|
431
|
-
expect(scope_a.instance_variable_get(:@where_values)).to be == [[
|
432
|
-
expect(scope_b.instance_variable_get(:@where_values)).to be == [[
|
443
|
+
expect(scope_a.instance_variable_get(:@where_values)).to be == [['nato', true], ['king', nil], ['nato', true], ['king', nil]]
|
444
|
+
expect(scope_b.instance_variable_get(:@where_values)).to be == [['king', nil], ['nato', true]]
|
433
445
|
expect(scope_a).to be == scope_b
|
434
446
|
end
|
435
447
|
end
|
data/spec/support/country.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: frozen_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.19.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jean Boussier
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-08-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -119,6 +119,7 @@ files:
|
|
119
119
|
- lib/frozen_record/base.rb
|
120
120
|
- lib/frozen_record/compact.rb
|
121
121
|
- lib/frozen_record/deduplication.rb
|
122
|
+
- lib/frozen_record/index.rb
|
122
123
|
- lib/frozen_record/railtie.rb
|
123
124
|
- lib/frozen_record/scope.rb
|
124
125
|
- lib/frozen_record/test_helper.rb
|