post_json 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/post_json/base.rb +20 -26
- data/lib/post_json/concerns/dynamic_index_methods.rb +7 -10
- data/lib/post_json/concerns/settings_methods.rb +19 -14
- data/lib/post_json/dynamic_index.rb +3 -13
- data/lib/post_json/query_translator.rb +1 -1
- data/lib/post_json/version.rb +1 -1
- 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: ba717a41c7ab9e983103b70305d09b301e8c1130
|
4
|
+
data.tar.gz: 564e3a2eee9880edde739632a7223f7d571f78a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96775c8330a18731ec8ba1b56c7c30a1287a8824c50d9deae8e85f877638ed17c107d26a88f2a22876bcf36e157d499ec9138dccc6f5f3acdc4ca54769bf9f29
|
7
|
+
data.tar.gz: af55ee745807c8c1246651a319ec9978e4dc266fd84460840f5c79a14d240afc04543c57da37ad97af00d55205e88094b317171d3dca077d2cac155971e34403
|
data/README.md
CHANGED
@@ -158,7 +158,7 @@ Also, __notice you don't have to define model attributes anywhere!__
|
|
158
158
|
We have created a feature we call `Dynamic Index`. It will automatically create indexes on slow queries, so queries
|
159
159
|
speed up considerably.
|
160
160
|
|
161
|
-
PostJson will measure the duration of each `SELECT` query and instruct PostgreSQL to create an
|
161
|
+
PostJson will measure the duration of each `SELECT` query and instruct PostgreSQL to create an Index,
|
162
162
|
if the query duration is above a specified threshold.
|
163
163
|
|
164
164
|
Each collection (like PostJson::Collection["people"]) have attribute `use_dynamic_index` (which is true by default) and
|
@@ -168,7 +168,7 @@ Lets say that you execute the following query and the duration is above the thre
|
|
168
168
|
|
169
169
|
`PostJson::Collection["people"].where(name: "Jacob").count`
|
170
170
|
|
171
|
-
PostJson will create (unless it already exists) an
|
171
|
+
PostJson will create (unless it already exists) an Index on `name` behind the scenes. The next time
|
172
172
|
you execute a query with `name` the performance will be much improved.
|
173
173
|
|
174
174
|
## Requirements
|
data/lib/post_json/base.rb
CHANGED
@@ -159,18 +159,11 @@ module PostJson
|
|
159
159
|
end
|
160
160
|
|
161
161
|
def default_scopes
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
query = original_all.joins("INNER JOIN #{model_settings} ON lower(#{model_settings}.collection_name) = '#{collection_name.downcase}'")
|
168
|
-
query = query.where("#{table_name}.__doc__model_settings_id = #{model_settings}.id")
|
169
|
-
|
170
|
-
[Proc.new { query }] + super
|
171
|
-
else
|
172
|
-
[Proc.new { none }] + super
|
173
|
-
end
|
162
|
+
# query = original_all.where("\"#{table_name}\".__doc__model_settings_id = ?", settings_id)
|
163
|
+
model_settings = ModelSettings.table_name
|
164
|
+
query = original_all.joins("INNER JOIN \"#{model_settings}\" ON lower(\"#{model_settings}\".collection_name) = '#{collection_name.downcase}'")
|
165
|
+
query = query.where("\"#{table_name}\".__doc__model_settings_id = \"#{model_settings}\".id")
|
166
|
+
super + [Proc.new { query }]
|
174
167
|
end
|
175
168
|
|
176
169
|
def collection_name
|
@@ -185,12 +178,13 @@ end"
|
|
185
178
|
def collection_name=(name)
|
186
179
|
raise ArgumentError, "Collection name must be present" unless name.present?
|
187
180
|
@collection_name = name.to_s.strip
|
181
|
+
reload_settings!
|
182
|
+
@collection_name
|
188
183
|
end
|
189
184
|
|
190
185
|
def rename_collection(new_name)
|
191
186
|
new_name = new_name.to_s.strip
|
192
|
-
settings
|
193
|
-
if settings
|
187
|
+
if settings.persisted?
|
194
188
|
settings.collection_name = new_name
|
195
189
|
settings.save!
|
196
190
|
end
|
@@ -238,18 +232,18 @@ end"
|
|
238
232
|
self.id = self.__doc__body['id'] = SecureRandom.uuid
|
239
233
|
end
|
240
234
|
|
241
|
-
self.__doc__model_settings_id =
|
235
|
+
self.__doc__model_settings_id = self.class.persisted_settings.id
|
242
236
|
self.__doc__version = 1
|
243
237
|
|
244
|
-
if
|
245
|
-
__doc__body_read_attribute(
|
246
|
-
__doc__body_write_attribute(
|
238
|
+
if self.class.persisted_settings.include_version_number == true &&
|
239
|
+
__doc__body_read_attribute(self.class.persisted_settings.version_attribute_name) == nil
|
240
|
+
__doc__body_write_attribute(self.class.persisted_settings.version_attribute_name, self.__doc__version)
|
247
241
|
end
|
248
242
|
|
249
|
-
if
|
243
|
+
if self.class.persisted_settings.use_timestamps
|
250
244
|
__local__current_time = Time.zone.now.strftime('%Y-%m-%dT%H:%M:%S.%LZ')
|
251
|
-
__doc__body_write_attribute(
|
252
|
-
__doc__body_write_attribute(
|
245
|
+
__doc__body_write_attribute(self.class.persisted_settings.created_at_attribute_name, __local__current_time)
|
246
|
+
__doc__body_write_attribute(self.class.persisted_settings.updated_at_attribute_name, __local__current_time)
|
253
247
|
end
|
254
248
|
super
|
255
249
|
end
|
@@ -263,14 +257,14 @@ end"
|
|
263
257
|
self.__doc__version = self.__doc__version + 1
|
264
258
|
end
|
265
259
|
|
266
|
-
if
|
267
|
-
__doc__body_attribute_changed?(
|
268
|
-
__doc__body_write_attribute(
|
260
|
+
if self.class.persisted_settings.include_version_number == true &&
|
261
|
+
__doc__body_attribute_changed?(self.class.persisted_settings.version_attribute_name) == false
|
262
|
+
__doc__body_write_attribute(self.class.persisted_settings.version_attribute_name, self.__doc__version)
|
269
263
|
end
|
270
264
|
|
271
|
-
if
|
265
|
+
if self.class.persisted_settings.use_timestamps && __doc__body_attribute_changed?(self.class.persisted_settings.updated_at_attribute_name)
|
272
266
|
__local__current_time = Time.zone.now.strftime('%Y-%m-%dT%H:%M:%S.%LZ')
|
273
|
-
__doc__body_write_attribute(
|
267
|
+
__doc__body_write_attribute(self.class.persisted_settings.updated_at_attribute_name, __local__current_time)
|
274
268
|
end
|
275
269
|
super
|
276
270
|
end
|
@@ -4,11 +4,10 @@ module PostJson
|
|
4
4
|
|
5
5
|
module ClassMethods
|
6
6
|
def dynamic_indexes
|
7
|
-
settings
|
8
|
-
if settings
|
9
|
-
DynamicIndex.indexed_selectors(settings.id)
|
10
|
-
else
|
7
|
+
if settings.new_record?
|
11
8
|
[]
|
9
|
+
else
|
10
|
+
DynamicIndex.indexed_selectors(settings.id)
|
12
11
|
end
|
13
12
|
end
|
14
13
|
|
@@ -17,16 +16,14 @@ module PostJson
|
|
17
16
|
end
|
18
17
|
|
19
18
|
def create_dynamic_indexes(*selectors)
|
20
|
-
|
21
|
-
DynamicIndex.ensure_index(settings.id, *selectors).count
|
19
|
+
DynamicIndex.ensure_index(persisted_settings.id, *selectors).count
|
22
20
|
end
|
23
21
|
|
24
22
|
def destroy_dynamic_index(selector)
|
25
|
-
settings
|
26
|
-
if settings
|
27
|
-
DynamicIndex.destroy_index(settings.id, selector)
|
28
|
-
else
|
23
|
+
if settings.new_record?
|
29
24
|
false
|
25
|
+
else
|
26
|
+
DynamicIndex.destroy_index(settings.id, selector)
|
30
27
|
end
|
31
28
|
end
|
32
29
|
end
|
@@ -2,32 +2,37 @@ module PostJson
|
|
2
2
|
module SettingsMethods
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
-
def __model__settings
|
6
|
-
@__model__settings ||= self.class.find_settings_or_create
|
7
|
-
end
|
8
|
-
|
9
5
|
module ClassMethods
|
10
|
-
def
|
11
|
-
ModelSettings.by_collection(collection_name).
|
12
|
-
end
|
13
|
-
|
14
|
-
def
|
15
|
-
|
6
|
+
def settings
|
7
|
+
@settings ||= ModelSettings.by_collection(collection_name).first_or_initialize(collection_name: collection_name)
|
8
|
+
end
|
9
|
+
|
10
|
+
def persisted_settings
|
11
|
+
if settings.new_record?
|
12
|
+
existing = ModelSettings.by_collection(collection_name).first
|
13
|
+
if existing
|
14
|
+
updates = settings.changes.inject({}) {|result, (k,v)| result[k] = v[1]; result}
|
15
|
+
existing.update_attributes(updates)
|
16
|
+
@settings = existing
|
17
|
+
else
|
18
|
+
settings.save!
|
19
|
+
end
|
20
|
+
end
|
21
|
+
settings
|
16
22
|
end
|
17
23
|
|
18
|
-
def
|
19
|
-
|
24
|
+
def reload_settings!
|
25
|
+
@settings = nil
|
26
|
+
settings
|
20
27
|
end
|
21
28
|
|
22
29
|
def read_settings_attribute(attribute_name)
|
23
30
|
attribute_name = attribute_name.to_s
|
24
|
-
settings = find_settings_or_initialize
|
25
31
|
settings[attribute_name]
|
26
32
|
end
|
27
33
|
|
28
34
|
def write_settings_attribute(attribute_name, value)
|
29
35
|
attribute_name = attribute_name.to_s
|
30
|
-
settings = find_settings_or_initialize
|
31
36
|
if settings[attribute_name] != value
|
32
37
|
settings[attribute_name] = value
|
33
38
|
settings.save!
|
@@ -35,18 +35,6 @@ module PostJson
|
|
35
35
|
validates :selector, presence: true
|
36
36
|
|
37
37
|
def index_name
|
38
|
-
# if defined?(@index_name)
|
39
|
-
# @index_name
|
40
|
-
# else
|
41
|
-
# prefix = "dyn_#{model_settings_id.gsub('-', '')}_"
|
42
|
-
# @index_name = if 63 < prefix.length + selector.length
|
43
|
-
# digest = Digest::MD5.hexdigest(selector)
|
44
|
-
# "#{prefix}#{digest}"[0..62]
|
45
|
-
# else
|
46
|
-
# "#{prefix}#{selector.gsub('.', '_')}"
|
47
|
-
# end
|
48
|
-
# end
|
49
|
-
|
50
38
|
@index_name ||= unless @index_name
|
51
39
|
prefix = "dyn_#{model_settings_id.gsub('-', '')}_"
|
52
40
|
if 63 < prefix.length + selector.length
|
@@ -90,10 +78,12 @@ IF NOT EXISTS (
|
|
90
78
|
AND n.nspname = '#{current_schema}' -- 'public' by default
|
91
79
|
) THEN
|
92
80
|
|
93
|
-
CREATE INDEX #{index_name} ON #{current_schema}.#{Base.table_name} (json_selector('#{selector}', __doc__body))
|
81
|
+
CREATE INDEX #{index_name} ON #{current_schema}.#{Base.table_name} (json_selector('#{selector}', __doc__body));
|
94
82
|
END IF;
|
95
83
|
|
96
84
|
END$$;"
|
97
85
|
end
|
98
86
|
end
|
99
87
|
end
|
88
|
+
|
89
|
+
# CREATE INDEX #{index_name} ON #{current_schema}.#{Base.table_name} (json_selector('#{selector}', __doc__body)) WHERE __doc__model_settings_id = '#{model_settings_id.gsub('-', '')}';
|
@@ -28,7 +28,7 @@ module PostJson
|
|
28
28
|
select_duration = ActiveRecord::Base.connection.last_select_query_duration * 1000
|
29
29
|
if model_class.use_dynamic_index == true &&
|
30
30
|
model_class.create_dynamic_index_milliseconds_threshold < select_duration
|
31
|
-
selectors = select_query.scan(/.*?json_selector\('(.*?)', __doc__body\)/).flatten.uniq
|
31
|
+
selectors = select_query.scan(/.*?json_selector\('(.*?)', \"post_json_documents\"\.__doc__body\)/).flatten.uniq
|
32
32
|
model_class.create_dynamic_indexes(selectors)
|
33
33
|
end
|
34
34
|
result
|
data/lib/post_json/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: post_json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jacob Madsen and Martin Thoegersen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|