mongous 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.adoc +101 -23
- data/README.ja.adoc +100 -22
- data/lib/mongous/base.rb +5 -2
- data/lib/mongous/extention.rb +24 -18
- data/lib/mongous/filter.rb +273 -230
- data/lib/mongous/version.rb +1 -1
- data/mongous.gemspec +1 -1
- data/sample/connect_auto_0.rb +9 -0
- data/sample/{query_projection_1.rb → query_select_1.rb} +14 -4
- data/sample/zbenchmark_search_3.rb +90 -0
- metadata +6 -4
data/lib/mongous/base.rb
CHANGED
@@ -81,11 +81,14 @@ module Mongous
|
|
81
81
|
end
|
82
82
|
|
83
83
|
def client
|
84
|
-
self.
|
84
|
+
if not self.class_variable_defined?( :@@client )
|
85
|
+
connect!
|
86
|
+
end
|
87
|
+
self.class_variable_get( :@@client )
|
85
88
|
end
|
86
89
|
|
87
90
|
def client=( _client )
|
88
|
-
if
|
91
|
+
if not _client.is_a?( ::Mongo::Client )
|
89
92
|
raise Mongous::Error, "type invalid. : #{ _client }"
|
90
93
|
end
|
91
94
|
self.class_variable_set( :@@client, _client )
|
data/lib/mongous/extention.rb
CHANGED
@@ -21,11 +21,11 @@ module Mongous
|
|
21
21
|
|
22
22
|
def collection_name
|
23
23
|
if self.class_variable_defined?( :@@collection_name )
|
24
|
-
self.class_variable_get( :@@collection_name )
|
25
|
-
|
26
|
-
_client_name = self.name
|
27
|
-
self.class_variable_set( :@@collection_name, _client_name )
|
24
|
+
value = self.class_variable_get( :@@collection_name )
|
25
|
+
return value if value
|
28
26
|
end
|
27
|
+
|
28
|
+
self.class_variable_set( :@@collection_name, self.name )
|
29
29
|
end
|
30
30
|
|
31
31
|
def collection_name=( _collection_name )
|
@@ -35,14 +35,17 @@ module Mongous
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
def collection
|
39
|
-
if
|
40
|
-
if
|
41
|
-
|
38
|
+
def collection( temp_collection_name = nil )
|
39
|
+
if temp_collection_name.nil?
|
40
|
+
if self.class_variable_defined?( :@@collection )
|
41
|
+
if _collection = self.class_variable_get( :@@collection )
|
42
|
+
return _collection
|
43
|
+
end
|
42
44
|
end
|
45
|
+
_collection_name = collection_name
|
46
|
+
else
|
47
|
+
_collection_name = temp_collection_name
|
43
48
|
end
|
44
|
-
|
45
|
-
_collection_name = collection_name
|
46
49
|
_client = client
|
47
50
|
|
48
51
|
if _client.database.collection_names.include?( _collection_name )
|
@@ -56,36 +59,39 @@ module Mongous
|
|
56
59
|
_collection.indexes.create_one( keys, opts ) rescue nil
|
57
60
|
end
|
58
61
|
|
59
|
-
self.class_variable_set( :@@collection, _collection )
|
62
|
+
self.class_variable_set( :@@collection, _collection ) if temp_collection_name.nil?
|
63
|
+
_collection
|
60
64
|
end
|
61
65
|
|
62
66
|
def fields
|
63
|
-
|
67
|
+
setup_class_variable( :@@fields, {} )
|
64
68
|
end
|
65
69
|
|
66
70
|
def symbols
|
67
|
-
|
71
|
+
setup_class_variable( :@@symbols, {} )
|
68
72
|
end
|
69
73
|
|
70
74
|
def blocks
|
71
|
-
|
75
|
+
setup_class_variable( :@@blocks, {} )
|
72
76
|
end
|
73
77
|
|
74
78
|
def indexes
|
75
|
-
|
79
|
+
setup_class_variable( :@@indexes, [] )
|
76
80
|
end
|
77
81
|
|
78
82
|
def filters
|
79
|
-
|
83
|
+
setup_class_variable( :@@filters, {} )
|
80
84
|
end
|
81
85
|
|
82
86
|
def defaults
|
83
|
-
|
87
|
+
setup_class_variable( :@@defaults, {} )
|
84
88
|
end
|
85
89
|
|
86
|
-
def
|
90
|
+
def setup_class_variable( symbol, default = {}, &block )
|
87
91
|
if self.class_variable_defined?( symbol )
|
88
92
|
self.class_variable_get( symbol )
|
93
|
+
elsif block_given?
|
94
|
+
self.class_variable_set( symbol, block.call )
|
89
95
|
else
|
90
96
|
self.class_variable_set( symbol, default )
|
91
97
|
end
|
data/lib/mongous/filter.rb
CHANGED
@@ -1,230 +1,273 @@
|
|
1
|
-
|
2
|
-
module Mongous
|
3
|
-
module Extention
|
4
|
-
def count
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
1
|
+
|
2
|
+
module Mongous
|
3
|
+
module Extention
|
4
|
+
def count
|
5
|
+
self.collection.estimated_document_count
|
6
|
+
end
|
7
|
+
|
8
|
+
def first
|
9
|
+
doc = self.collection.find.first
|
10
|
+
self.new( **doc ) if doc
|
11
|
+
end
|
12
|
+
|
13
|
+
def all
|
14
|
+
self.collection.find.map do |doc|
|
15
|
+
self.new( **doc )
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def each( &block )
|
20
|
+
all.each( &block )
|
21
|
+
end
|
22
|
+
|
23
|
+
def map( &block )
|
24
|
+
all.map( &block )
|
25
|
+
end
|
26
|
+
|
27
|
+
def delete
|
28
|
+
self.collection.delete_many({})
|
29
|
+
end
|
30
|
+
|
31
|
+
def attach( collection_name )
|
32
|
+
Filter.new( self ).attach( collection_name )
|
33
|
+
end
|
34
|
+
|
35
|
+
def select( *keys, **hash )
|
36
|
+
Filter.new( self ).select( *keys, **hash )
|
37
|
+
end
|
38
|
+
|
39
|
+
def where( filter = nil, **conditions )
|
40
|
+
condition = normalize( filter, conditions )
|
41
|
+
Filter.new( self ).where( condition )
|
42
|
+
end
|
43
|
+
|
44
|
+
def not( filter = nil, **conditions )
|
45
|
+
raise Mongous::Error, "Unset args for #{self}.not." if filter.nil? && conditions.empty?
|
46
|
+
|
47
|
+
condition = normalize( filter, conditions )
|
48
|
+
Filter.new( self ).where({"$nor" => [condition]})
|
49
|
+
end
|
50
|
+
|
51
|
+
def and( *filters )
|
52
|
+
raise Mongous::Error, "Unset args for #{self}.and." if filters.empty?
|
53
|
+
|
54
|
+
conditions = filters.map do |filter|
|
55
|
+
normalize( filter, {} )
|
56
|
+
end
|
57
|
+
Filter.new( self ).where({"$and" => conditions})
|
58
|
+
end
|
59
|
+
|
60
|
+
def or( *filters )
|
61
|
+
raise Mongous::Error, "Unset args for #{self}.or." if filters.empty?
|
62
|
+
|
63
|
+
conditions = filters.map do |filter|
|
64
|
+
normalize( filter, {} )
|
65
|
+
end
|
66
|
+
Filter.new( self ).where({"$or" => conditions})
|
67
|
+
end
|
68
|
+
|
69
|
+
def normalize( filter, conditions )
|
70
|
+
condition = case filter
|
71
|
+
when Filter
|
72
|
+
filter.to_condition
|
73
|
+
when Symbol
|
74
|
+
case _filter = filters[filter]
|
75
|
+
when Filter
|
76
|
+
_filter.to_condition
|
77
|
+
when Hash
|
78
|
+
_filter
|
79
|
+
end
|
80
|
+
when NilClass
|
81
|
+
Filter.new( self ).where( **conditions ).to_condition
|
82
|
+
else
|
83
|
+
caller_method = /`(.*?)'/.match( caller()[0] )[1]
|
84
|
+
raise Mongous::Error, "Invalid args for #{self}.#{ caller_method }. : #{filter}, #{conditions}"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
module Mongous
|
91
|
+
class Filter
|
92
|
+
def initialize( klass )
|
93
|
+
@klass = klass
|
94
|
+
@filter = {}
|
95
|
+
@option = {}
|
96
|
+
end
|
97
|
+
|
98
|
+
def attach( collection_name )
|
99
|
+
w = self.dup
|
100
|
+
w.instance_variable_set( :@collection_name, collection_name.to_s )
|
101
|
+
w
|
102
|
+
end
|
103
|
+
|
104
|
+
def where( conditions )
|
105
|
+
hash = {}
|
106
|
+
conditions.each do |key, item|
|
107
|
+
case key
|
108
|
+
when /\$(and|or|nor)/
|
109
|
+
hash[key] = item
|
110
|
+
|
111
|
+
else
|
112
|
+
case item
|
113
|
+
when Array
|
114
|
+
hash[key] = {"$in"=>item}
|
115
|
+
|
116
|
+
when Range
|
117
|
+
_begin_oper = "$gte"
|
118
|
+
_end_oper = item.exclude_end? ? "$lt" : "$lte"
|
119
|
+
|
120
|
+
if item.begin && item.end
|
121
|
+
hash[key] = { _begin_oper => item.begin, _end_oper => item.end }
|
122
|
+
|
123
|
+
elsif !item.begin && item.end
|
124
|
+
hash[key] = { _end_oper => item.end }
|
125
|
+
|
126
|
+
elsif item.begin && !item.end
|
127
|
+
hash[key] = { _begin_oper => item.begin }
|
128
|
+
|
129
|
+
else
|
130
|
+
raise Mongous::Error, "invalid range. : #{ item }"
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
else
|
135
|
+
hash[key] = item
|
136
|
+
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
w = self.dup
|
141
|
+
w.instance_variable_set( :@filter, @filter.merge( hash ) )
|
142
|
+
w
|
143
|
+
end
|
144
|
+
|
145
|
+
def to_condition
|
146
|
+
@filter.dup
|
147
|
+
end
|
148
|
+
|
149
|
+
def option( _option )
|
150
|
+
w = self.dup
|
151
|
+
w.instance_variable_set( :@option, @option.merge( _option ) )
|
152
|
+
w
|
153
|
+
end
|
154
|
+
|
155
|
+
def projection( *keys, **hash )
|
156
|
+
if not keys.empty?
|
157
|
+
_projection = Hash[ keys.zip( Array.new(keys.length, 1) ) ]
|
158
|
+
elsif not hash.empty?
|
159
|
+
_projection = hash
|
160
|
+
else
|
161
|
+
_projection = nil
|
162
|
+
end
|
163
|
+
w = self.dup
|
164
|
+
w.instance_variable_set( :@projection, _projection )
|
165
|
+
w
|
166
|
+
end
|
167
|
+
alias :select :projection
|
168
|
+
|
169
|
+
def sort( *keys, **hash )
|
170
|
+
if not keys.empty?
|
171
|
+
_sort = Hash[ keys.zip( Array.new( keys.length, 1 ) ) ]
|
172
|
+
elsif not hash.empty?
|
173
|
+
_sort = hash
|
174
|
+
else
|
175
|
+
_sort = nil
|
176
|
+
end
|
177
|
+
w = self.dup
|
178
|
+
w.instance_variable_set( :@sort, _sort )
|
179
|
+
w
|
180
|
+
end
|
181
|
+
alias :order :sort
|
182
|
+
|
183
|
+
def []( nth_or_range, len = nil )
|
184
|
+
case nth_or_range
|
185
|
+
when Integer
|
186
|
+
_skip = nth_or_range
|
187
|
+
_limit = nil
|
188
|
+
|
189
|
+
if len
|
190
|
+
raise Mongous::Error, "invalid len. : #{ len }" if !len.is_a? Integer || len <= 0
|
191
|
+
_limit = len
|
192
|
+
end
|
193
|
+
|
194
|
+
when Range
|
195
|
+
from = nth_or_range.begin
|
196
|
+
raise Mongous::Error, "invalid range. : #{ nth_or_range }" unless from.is_a? Integer
|
197
|
+
|
198
|
+
to = nth_or_range.end
|
199
|
+
raise Mongous::Error, "invalid range. : #{ nth_or_range }" unless to.is_a? Integer
|
200
|
+
|
201
|
+
to -= 1 if nth_or_range.exclude_end?
|
202
|
+
_skip = from
|
203
|
+
_limit = to - from + 1
|
204
|
+
|
205
|
+
else
|
206
|
+
raise Mongous::Error, "invalid class. : #{ nth_or_range }"
|
207
|
+
|
208
|
+
end
|
209
|
+
|
210
|
+
w = self.dup
|
211
|
+
w.instance_variable_set( :@skip, _skip )
|
212
|
+
w.instance_variable_set( :@limit, _limit )
|
213
|
+
w
|
214
|
+
end
|
215
|
+
|
216
|
+
def exec_query
|
217
|
+
_filter = @filter
|
218
|
+
_option = @option.dup
|
219
|
+
_option[:projection] = @projection if @projection
|
220
|
+
found = @klass.collection( @collection_name ).find( _filter, _option )
|
221
|
+
found = found.sort( @sort ) if @sort
|
222
|
+
found = found.skip( @skip ) if @skip
|
223
|
+
found = found.limit( @limit ) if @limit
|
224
|
+
found
|
225
|
+
end
|
226
|
+
|
227
|
+
def count
|
228
|
+
found = @klass.collection.find( @filter )
|
229
|
+
found = found.skip( @skip ) if @skip
|
230
|
+
found = found.limit( @limit ) if @limit
|
231
|
+
_count = found.count_documents
|
232
|
+
if @skip
|
233
|
+
if @skip > _count
|
234
|
+
0
|
235
|
+
elsif @limit
|
236
|
+
[_count - @skip, @limit].min
|
237
|
+
else
|
238
|
+
_count - @skip
|
239
|
+
end
|
240
|
+
else
|
241
|
+
if @limit
|
242
|
+
[_count, @limit].min
|
243
|
+
else
|
244
|
+
_count
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
def first
|
250
|
+
doc = exec_query.first
|
251
|
+
@klass.new( **doc ) if doc
|
252
|
+
end
|
253
|
+
|
254
|
+
def all
|
255
|
+
exec_query.map do |doc|
|
256
|
+
@klass.new( **doc )
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
def each( &block )
|
261
|
+
all.each( &block )
|
262
|
+
end
|
263
|
+
|
264
|
+
def map( &block )
|
265
|
+
all.map( &block )
|
266
|
+
end
|
267
|
+
|
268
|
+
def delete
|
269
|
+
@klass.collection.delete_many( @filter )
|
270
|
+
end
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|