groovy 0.4.6 → 0.4.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/example/basic.rb +15 -1
- data/lib/groovy/model.rb +27 -16
- data/lib/groovy/query.rb +10 -5
- data/lib/groovy/schema.rb +14 -14
- data/lib/groovy/version.rb +1 -1
- data/spec/model_spec.rb +22 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 43812d3bd8ef9579ceed22b3d19e78378ceb3a924bfc8d6c0983e49199c26c88
|
4
|
+
data.tar.gz: 5960ca73a253d62f31301995c8f643bcaa084a99140b5f59eee0161c816d20e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 22cc09a73d9804f5d5e1cb432d3948ff24ef9a28dbd84b5a403b3709477d013089617ebf19b23f4d3450c3c7dc8e15c01ab527698c5efa5821c9d351056777ad
|
7
|
+
data.tar.gz: 809b9fa8e3158df7afeb6e80967d7156772094b21b69f6fe7d9f5e6db03626fe8a9b2ee123f89c7bf791ef6e195415524b1e967291d836becdc6173fbd0bfbfc
|
data/example/basic.rb
CHANGED
@@ -28,4 +28,18 @@ populate if Product.count == 0
|
|
28
28
|
|
29
29
|
# 50_000 products: 50M
|
30
30
|
# 100_000 products: 50M
|
31
|
-
# 500_000 products: 62M
|
31
|
+
# 500_000 products: 62M
|
32
|
+
|
33
|
+
module MemInfo
|
34
|
+
KERNEL_PAGE_SIZE = `getconf PAGESIZE`.chomp.to_i rescue 4096
|
35
|
+
STATM_PATH = "/proc/#{Process.pid}/statm"
|
36
|
+
STATM_FOUND = File.exist?(STATM_PATH)
|
37
|
+
def self.rss
|
38
|
+
STATM_FOUND ? (File.read(STATM_PATH).split(' ')[1].to_i * KERNEL_PAGE_SIZE) / 1024 : 0
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
puts MemInfo.rss
|
43
|
+
prices = Product.all.map { |x| x.price }
|
44
|
+
puts prices.inspect
|
45
|
+
puts MemInfo.rss
|
data/lib/groovy/model.rb
CHANGED
@@ -150,7 +150,6 @@ module Groovy
|
|
150
150
|
# Groonga["#{table_name}.#{name}"] # .search, .similar_search, etc
|
151
151
|
# end
|
152
152
|
|
153
|
-
|
154
153
|
def index_search(column, query, options = {}, &block)
|
155
154
|
results = table.select { |rec| rec[column].match(query) }
|
156
155
|
render_results(results, &block)
|
@@ -283,13 +282,13 @@ module Groovy
|
|
283
282
|
end
|
284
283
|
end
|
285
284
|
|
286
|
-
attr_reader :id, :
|
285
|
+
attr_reader :id, :record, :changes
|
287
286
|
|
288
287
|
def initialize(attrs = nil, record = nil, key = nil)
|
289
288
|
@attributes, @vectors, @_key = {}, {}, key # key is used on creation only
|
290
289
|
|
291
290
|
if set_record(record)
|
292
|
-
|
291
|
+
# load_attributes_from_record(record)
|
293
292
|
else
|
294
293
|
attrs ||= {}
|
295
294
|
unless attrs.is_a?(Hash)
|
@@ -307,8 +306,13 @@ module Groovy
|
|
307
306
|
|
308
307
|
# get reference to the actual record in the Groonga table,
|
309
308
|
# not the temporary one we get as part of a search result.
|
310
|
-
def load_record
|
311
|
-
|
309
|
+
# def load_record
|
310
|
+
# self.class.table[id]
|
311
|
+
# end
|
312
|
+
|
313
|
+
def attributes
|
314
|
+
load_attributes_from_record # populate missing
|
315
|
+
@attributes
|
312
316
|
end
|
313
317
|
|
314
318
|
def inspect
|
@@ -321,7 +325,9 @@ module Groovy
|
|
321
325
|
end
|
322
326
|
|
323
327
|
def [](key)
|
324
|
-
|
328
|
+
k = key.to_sym
|
329
|
+
@attributes[k] = get_record_attribute(k) unless @attributes.key?(k)
|
330
|
+
@attributes[k]
|
325
331
|
end
|
326
332
|
|
327
333
|
def []=(key, val)
|
@@ -382,11 +388,15 @@ module Groovy
|
|
382
388
|
end
|
383
389
|
|
384
390
|
def reload
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
391
|
+
unless new_record?
|
392
|
+
# raise RecordNotPersisted if id.nil?
|
393
|
+
# ensure_persisted!
|
394
|
+
rec = self.class.table[id] # _key
|
395
|
+
set_record(rec)
|
396
|
+
# load_attributes_from_record
|
397
|
+
end
|
398
|
+
|
399
|
+
@attributes = {}
|
390
400
|
@changes = {}
|
391
401
|
self
|
392
402
|
end
|
@@ -406,6 +416,7 @@ module Groovy
|
|
406
416
|
private
|
407
417
|
|
408
418
|
def get_record_attribute(key)
|
419
|
+
return if record.nil?
|
409
420
|
val = record[key]
|
410
421
|
if self.class.schema.time_columns.include?(key)
|
411
422
|
fix_time_value(val)
|
@@ -423,15 +434,15 @@ module Groovy
|
|
423
434
|
# record.respond_to?(:_key) ? record._key : id
|
424
435
|
# end
|
425
436
|
|
426
|
-
def
|
437
|
+
def load_attributes_from_record
|
427
438
|
self.class.attribute_names.each do |col|
|
428
|
-
public_send("#{col}=", get_record_attribute(col))
|
439
|
+
public_send("#{col}=", get_record_attribute(col)) unless @attributes.key?(col)
|
429
440
|
end
|
430
441
|
end
|
431
442
|
|
432
443
|
def set_attribute(key, val)
|
433
|
-
changes[key.to_sym] = [self[key], val] if changes # nil
|
434
|
-
attributes[key.to_sym] = val
|
444
|
+
changes[key.to_sym] = [self[key], val] if changes # nil before initializing
|
445
|
+
@attributes[key.to_sym] = val
|
435
446
|
end
|
436
447
|
|
437
448
|
def get_ref(name)
|
@@ -456,7 +467,7 @@ module Groovy
|
|
456
467
|
|
457
468
|
def create
|
458
469
|
fire_callbacks(:before_create)
|
459
|
-
set_record(self.class.insert(attributes, @_key))
|
470
|
+
set_record(self.class.insert(@attributes, @_key))
|
460
471
|
fire_callbacks(:after_create)
|
461
472
|
self
|
462
473
|
end
|
data/lib/groovy/query.rb
CHANGED
@@ -192,11 +192,16 @@ module Groovy
|
|
192
192
|
end
|
193
193
|
|
194
194
|
def [](index)
|
195
|
-
|
195
|
+
if r = results[index]
|
196
|
+
model.new_from_record(r)
|
197
|
+
end
|
196
198
|
end
|
197
199
|
|
198
200
|
def each(&block)
|
199
|
-
records.each { |r| block.call(r) }
|
201
|
+
# records.each { |r| block.call(r) }
|
202
|
+
results.each_with_index do |r, index|
|
203
|
+
yield model.new_from_record(r)
|
204
|
+
end
|
200
205
|
end
|
201
206
|
|
202
207
|
def update_all(attrs)
|
@@ -236,15 +241,15 @@ module Groovy
|
|
236
241
|
end
|
237
242
|
end
|
238
243
|
|
244
|
+
private
|
245
|
+
attr_reader :model, :table, :options, :select_block
|
246
|
+
|
239
247
|
def records
|
240
248
|
@records ||= results.map do |r|
|
241
249
|
model.new_from_record(r)
|
242
250
|
end
|
243
251
|
end
|
244
252
|
|
245
|
-
private
|
246
|
-
attr_reader :model, :table, :options, :select_block
|
247
|
-
|
248
253
|
def get_last(count = 1)
|
249
254
|
if count > 1
|
250
255
|
records[(size-count)..-1]
|
data/lib/groovy/schema.rb
CHANGED
@@ -26,6 +26,7 @@ module Groovy
|
|
26
26
|
def initialize(context, table_name, opts = {})
|
27
27
|
@context, @table_name, @opts = context, table_name, opts || {}
|
28
28
|
@spec, @index_columns = {}, []
|
29
|
+
@cache = {}
|
29
30
|
end
|
30
31
|
|
31
32
|
def table
|
@@ -33,47 +34,44 @@ module Groovy
|
|
33
34
|
end
|
34
35
|
|
35
36
|
def search_table
|
36
|
-
@search_table ||= context[SEARCH_TABLE_NAME]
|
37
|
+
@cache[:search_table] ||= context[SEARCH_TABLE_NAME]
|
37
38
|
end
|
38
39
|
|
39
40
|
def column_names
|
40
|
-
get_names
|
41
|
+
@cache[:column_names] ||= get_names(table.columns)
|
41
42
|
end
|
42
43
|
|
43
44
|
def singular_references
|
44
|
-
|
45
|
-
get_names(table.columns.select(&:reference_column?).reject(&:vector?))
|
45
|
+
@cache[:singular_references] ||= get_names(table.columns.select(&:reference_column?).reject(&:vector?))
|
46
46
|
end
|
47
47
|
|
48
48
|
def plural_references
|
49
|
-
|
50
|
-
get_names(table.columns.select(&:vector?))
|
49
|
+
@cache[:plural_references] ||= get_names(table.columns.select(&:vector?))
|
51
50
|
end
|
52
51
|
|
53
52
|
def attribute_columns
|
54
|
-
|
55
|
-
get_names(table.columns.select { |c| c.column? && !c.reference_column? && !c.vector? })
|
53
|
+
@cache[:attribute_columns] ||= get_names(table.columns.select { |c| c.column? && !c.reference_column? && !c.vector? })
|
56
54
|
end
|
57
55
|
|
58
56
|
def time_columns
|
59
|
-
columns_by_type('Time')
|
57
|
+
@cache[:time_columns] ||= columns_by_type('Time')
|
60
58
|
end
|
61
59
|
|
62
60
|
def integer_columns
|
63
|
-
columns_by_type('Int32')
|
61
|
+
@cache[:integer_columns] ||= columns_by_type('Int32')
|
64
62
|
end
|
65
63
|
|
66
64
|
def boolean_columns
|
67
|
-
columns_by_type('Bool')
|
65
|
+
@cache[:boolean_columns] ||= columns_by_type('Bool')
|
68
66
|
end
|
69
67
|
|
70
68
|
def columns_by_type(type)
|
71
69
|
get_names(table.columns.select { |c| c.column? && c.range.name == type })
|
72
70
|
end
|
73
71
|
|
74
|
-
|
75
|
-
|
76
|
-
|
72
|
+
def reload
|
73
|
+
@cache = {}
|
74
|
+
end
|
77
75
|
|
78
76
|
def rebuild!
|
79
77
|
log("Rebuilding!")
|
@@ -116,6 +114,8 @@ module Groovy
|
|
116
114
|
@index_columns.each do |col|
|
117
115
|
add_index_on(col)
|
118
116
|
end
|
117
|
+
|
118
|
+
reload
|
119
119
|
self
|
120
120
|
end
|
121
121
|
|
data/lib/groovy/version.rb
CHANGED
data/spec/model_spec.rb
CHANGED
@@ -86,6 +86,28 @@ describe Groovy::Model do
|
|
86
86
|
end
|
87
87
|
|
88
88
|
describe '#[]' do
|
89
|
+
it 'reads value from record' do
|
90
|
+
prod = TestProduct.create!(name: 'A product', price: 100)
|
91
|
+
expect(prod.name).to eq('A product')
|
92
|
+
expect(prod['name']).to eq('A product')
|
93
|
+
expect(prod[:name]).to eq('A product')
|
94
|
+
|
95
|
+
prod = TestProduct.find(prod.id)
|
96
|
+
expect(prod.name).to eq('A product')
|
97
|
+
expect(prod['name']).to eq('A product')
|
98
|
+
expect(prod[:name]).to eq('A product')
|
99
|
+
|
100
|
+
prod = TestProduct.new
|
101
|
+
expect(prod.name).to eq(nil)
|
102
|
+
prod.name = 'Another product'
|
103
|
+
expect(prod.name).to eq('Another product')
|
104
|
+
prod.reload
|
105
|
+
expect(prod.name).to eq(nil)
|
106
|
+
prod.name = 'Another product'
|
107
|
+
prod.save
|
108
|
+
prod.reload
|
109
|
+
expect(prod.name).to eq('Another product')
|
110
|
+
end
|
89
111
|
end
|
90
112
|
|
91
113
|
describe '#[]=' do
|