mongoid 4.0.2 → 5.0.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +66 -1
- data/README.md +14 -13
- data/lib/config/locales/en.yml +28 -28
- data/lib/mongoid.rb +28 -21
- data/lib/mongoid/atomic.rb +2 -4
- data/lib/mongoid/attributes.rb +7 -7
- data/lib/mongoid/attributes/processing.rb +4 -1
- data/lib/mongoid/attributes/readonly.rb +2 -2
- data/lib/mongoid/changeable.rb +4 -6
- data/lib/mongoid/clients.rb +142 -0
- data/lib/mongoid/clients/factory.rb +78 -0
- data/lib/mongoid/{sessions → clients}/options.rb +30 -19
- data/lib/mongoid/{sessions → clients}/storage_options.rb +27 -13
- data/lib/mongoid/{sessions → clients}/thread_options.rb +6 -3
- data/lib/mongoid/clients/validators.rb +2 -0
- data/lib/mongoid/{sessions → clients}/validators/storage.rb +5 -2
- data/lib/mongoid/composable.rb +3 -3
- data/lib/mongoid/config.rb +39 -41
- data/lib/mongoid/config/environment.rb +1 -1
- data/lib/mongoid/config/validators.rb +1 -1
- data/lib/mongoid/config/validators/{session.rb → client.rb} +31 -28
- data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
- data/lib/mongoid/contextual/atomic.rb +11 -11
- data/lib/mongoid/contextual/command.rb +9 -6
- data/lib/mongoid/contextual/geo_near.rb +17 -1
- data/lib/mongoid/contextual/map_reduce.rb +12 -11
- data/lib/mongoid/contextual/memory.rb +2 -5
- data/lib/mongoid/contextual/mongo.rb +92 -82
- data/lib/mongoid/contextual/none.rb +13 -0
- data/lib/mongoid/copyable.rb +6 -1
- data/lib/mongoid/criteria.rb +36 -3
- data/lib/mongoid/document.rb +3 -4
- data/lib/mongoid/errors.rb +6 -6
- data/lib/mongoid/errors/{mixed_session_configuration.rb → mixed_client_configuration.rb} +5 -5
- data/lib/mongoid/errors/no_client_config.rb +22 -0
- data/lib/mongoid/errors/{no_session_database.rb → no_client_database.rb} +4 -4
- data/lib/mongoid/errors/{no_session_hosts.rb → no_client_hosts.rb} +4 -4
- data/lib/mongoid/errors/{no_sessions_config.rb → no_clients_config.rb} +4 -4
- data/lib/mongoid/errors/no_default_client.rb +23 -0
- data/lib/mongoid/extensions/hash.rb +5 -1
- data/lib/mongoid/extensions/object.rb +3 -2
- data/lib/mongoid/extensions/set.rb +5 -5
- data/lib/mongoid/factory.rb +4 -2
- data/lib/mongoid/fields.rb +7 -2
- data/lib/mongoid/findable.rb +4 -1
- data/lib/mongoid/indexable.rb +15 -9
- data/lib/mongoid/persistable.rb +1 -2
- data/lib/mongoid/persistable/creatable.rb +2 -2
- data/lib/mongoid/persistable/deletable.rb +3 -3
- data/lib/mongoid/persistable/incrementable.rb +1 -1
- data/lib/mongoid/persistable/logical.rb +1 -1
- data/lib/mongoid/persistable/poppable.rb +1 -1
- data/lib/mongoid/persistable/pullable.rb +2 -2
- data/lib/mongoid/persistable/pushable.rb +2 -2
- data/lib/mongoid/persistable/renamable.rb +1 -1
- data/lib/mongoid/persistable/settable.rb +1 -1
- data/lib/mongoid/persistable/unsettable.rb +1 -1
- data/lib/mongoid/persistable/updatable.rb +2 -2
- data/lib/mongoid/persistable/upsertable.rb +1 -1
- data/lib/mongoid/query_cache.rb +98 -104
- data/lib/mongoid/railtie.rb +1 -21
- data/lib/mongoid/railties/database.rake +1 -1
- data/lib/mongoid/relations/builders.rb +3 -1
- data/lib/mongoid/relations/counter_cache.rb +1 -1
- data/lib/mongoid/relations/embedded/batchable.rb +3 -10
- data/lib/mongoid/relations/embedded/many.rb +4 -2
- data/lib/mongoid/relations/many.rb +1 -0
- data/lib/mongoid/relations/proxy.rb +6 -6
- data/lib/mongoid/relations/referenced/many.rb +2 -1
- data/lib/mongoid/relations/targets/enumerable.rb +11 -11
- data/lib/mongoid/relations/touchable.rb +1 -1
- data/lib/mongoid/reloadable.rb +2 -2
- data/lib/mongoid/scopable.rb +6 -17
- data/lib/mongoid/selectable.rb +1 -36
- data/lib/mongoid/serializable.rb +2 -2
- data/lib/mongoid/stateful.rb +0 -1
- data/lib/mongoid/tasks/database.rake +2 -2
- data/lib/mongoid/tasks/database.rb +23 -16
- data/lib/mongoid/threaded.rb +54 -33
- data/lib/mongoid/threaded/lifecycle.rb +21 -16
- data/lib/mongoid/traversable.rb +16 -1
- data/lib/mongoid/validatable.rb +1 -1
- data/lib/mongoid/validatable/queryable.rb +1 -1
- data/lib/mongoid/validatable/uniqueness.rb +3 -20
- data/lib/mongoid/version.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +91 -57
- data/lib/rails/mongoid.rb +2 -2
- data/spec/app/models/audio.rb +1 -1
- data/spec/app/models/band.rb +1 -0
- data/spec/app/models/company.rb +5 -0
- data/spec/app/models/label.rb +7 -0
- data/spec/app/models/pub.rb +6 -0
- data/spec/app/models/staff.rb +7 -0
- data/spec/app/models/store_as_dup_test1.rb +5 -0
- data/spec/app/models/store_as_dup_test2.rb +5 -0
- data/spec/config/mongoid.yml +7 -25
- data/spec/mongoid/atomic/paths_spec.rb +3 -11
- data/spec/mongoid/attributes/nested_spec.rb +16 -16
- data/spec/mongoid/attributes/readonly_spec.rb +80 -18
- data/spec/mongoid/attributes_spec.rb +3 -3
- data/spec/mongoid/changeable_spec.rb +70 -0
- data/spec/mongoid/clients/factory_spec.rb +284 -0
- data/spec/mongoid/{sessions → clients}/options_spec.rb +4 -6
- data/spec/mongoid/clients_spec.rb +739 -0
- data/spec/mongoid/config/environment_spec.rb +14 -11
- data/spec/mongoid/config_spec.rb +33 -48
- data/spec/mongoid/contextual/atomic_spec.rb +1 -17
- data/spec/mongoid/contextual/geo_near_spec.rb +35 -0
- data/spec/mongoid/contextual/mongo_spec.rb +26 -83
- data/spec/mongoid/contextual/none_spec.rb +15 -0
- data/spec/mongoid/copyable_spec.rb +35 -1
- data/spec/mongoid/criteria/findable_spec.rb +197 -0
- data/spec/mongoid/criteria/modifiable_spec.rb +7 -29
- data/spec/mongoid/criteria_spec.rb +74 -91
- data/spec/mongoid/document_spec.rb +1 -1
- data/spec/mongoid/errors/{mixed_session_configuration_spec.rb → mixed_client_configuration_spec.rb} +1 -1
- data/spec/mongoid/errors/{no_session_config_spec.rb → no_client_config_spec.rb} +4 -4
- data/spec/mongoid/errors/{no_session_database_spec.rb → no_client_database_spec.rb} +4 -4
- data/spec/mongoid/errors/{no_session_hosts_spec.rb → no_client_hosts_spec.rb} +3 -3
- data/spec/mongoid/errors/{no_sessions_config_spec.rb → no_clients_config_spec.rb} +2 -2
- data/spec/mongoid/fields/localized_spec.rb +1 -0
- data/spec/mongoid/fields_spec.rb +1 -0
- data/spec/mongoid/findable_spec.rb +2 -23
- data/spec/mongoid/indexable_spec.rb +12 -8
- data/spec/mongoid/interceptable_spec.rb +15 -0
- data/spec/mongoid/persistable/settable_spec.rb +16 -0
- data/spec/mongoid/persistable/updatable_spec.rb +3 -2
- data/spec/mongoid/persistable_spec.rb +4 -4
- data/spec/mongoid/query_cache_spec.rb +13 -8
- data/spec/mongoid/relations/auto_save_spec.rb +1 -1
- data/spec/mongoid/relations/counter_cache_spec.rb +34 -0
- data/spec/mongoid/relations/eager/belongs_to_spec.rb +9 -0
- data/spec/mongoid/relations/eager/has_and_belongs_to_many_spec.rb +3 -3
- data/spec/mongoid/relations/embedded/many_spec.rb +123 -1
- data/spec/mongoid/relations/embedded/one_spec.rb +3 -3
- data/spec/mongoid/relations/proxy_spec.rb +28 -0
- data/spec/mongoid/relations/referenced/in_spec.rb +1 -1
- data/spec/mongoid/relations/referenced/many_spec.rb +47 -23
- data/spec/mongoid/relations/referenced/many_to_many_spec.rb +1 -1
- data/spec/mongoid/relations/referenced/one_spec.rb +1 -1
- data/spec/mongoid/relations/targets/enumerable_spec.rb +9 -2
- data/spec/mongoid/reloadable_spec.rb +6 -6
- data/spec/mongoid/scopable_spec.rb +41 -28
- data/spec/mongoid/selectable_spec.rb +6 -16
- data/spec/mongoid/tasks/database_rake_spec.rb +13 -13
- data/spec/mongoid/tasks/database_spec.rb +2 -2
- data/spec/mongoid/threaded_spec.rb +0 -7
- data/spec/mongoid/traversable_spec.rb +2 -2
- data/spec/mongoid/validatable/uniqueness_spec.rb +30 -1
- data/spec/mongoid_spec.rb +13 -15
- data/spec/rails/mongoid_spec.rb +13 -4
- data/spec/spec_helper.rb +44 -27
- data/spec/support/authorization.rb +12 -0
- data/spec/support/expectations.rb +14 -0
- metadata +52 -59
- data/lib/mongoid/contextual/find_and_modify.rb +0 -69
- data/lib/mongoid/contextual/text_search.rb +0 -178
- data/lib/mongoid/criteria/#findable.rb# +0 -141
- data/lib/mongoid/errors/no_default_session.rb +0 -23
- data/lib/mongoid/errors/no_session_config.rb +0 -22
- data/lib/mongoid/log_subscriber.rb +0 -55
- data/lib/mongoid/positional.rb +0 -71
- data/lib/mongoid/sessions.rb +0 -125
- data/lib/mongoid/sessions/factory.rb +0 -131
- data/lib/mongoid/sessions/mongo_uri.rb +0 -93
- data/lib/mongoid/sessions/validators.rb +0 -2
- data/lib/mongoid/support/query_counter.rb +0 -23
- data/spec/helpers.rb +0 -18
- data/spec/mongoid/#atomic_spec.rb# +0 -365
- data/spec/mongoid/contextual/find_and_modify_spec.rb +0 -220
- data/spec/mongoid/contextual/text_search_spec.rb +0 -209
- data/spec/mongoid/log_subscriber_spec.rb +0 -75
- data/spec/mongoid/positional_spec.rb +0 -222
- data/spec/mongoid/sessions/factory_spec.rb +0 -333
- data/spec/mongoid/sessions/mongo_uri_spec.rb +0 -103
- data/spec/mongoid/sessions_spec.rb +0 -1252
data/lib/mongoid/indexable.rb
CHANGED
@@ -31,10 +31,10 @@ module Mongoid
|
|
31
31
|
index_specifications.each do |spec|
|
32
32
|
key, options = spec.key, spec.options
|
33
33
|
if database = options[:database]
|
34
|
-
with(read: :primary, database: database).
|
35
|
-
collection.indexes.
|
34
|
+
with(read: { mode: :primary }, database: database).
|
35
|
+
collection.indexes.create_one(key, options.except(:database))
|
36
36
|
else
|
37
|
-
with(read: :primary).collection.indexes.
|
37
|
+
with(read: { mode: :primary }).collection.indexes.create_one(key, options)
|
38
38
|
end
|
39
39
|
end and true
|
40
40
|
end
|
@@ -50,12 +50,18 @@ module Mongoid
|
|
50
50
|
# @since 3.0.0
|
51
51
|
def remove_indexes
|
52
52
|
indexed_database_names.each do |database|
|
53
|
-
collection = with(read: :primary, database: database).collection
|
54
|
-
|
55
|
-
|
56
|
-
|
53
|
+
collection = with(read: { mode: :primary }, database: database).collection
|
54
|
+
begin
|
55
|
+
collection.indexes.each do |spec|
|
56
|
+
unless spec["name"] == "_id_"
|
57
|
+
collection.indexes.drop_one(spec["key"])
|
58
|
+
logger.info(
|
59
|
+
"MONGOID: Removed index '#{spec["name"]}' on collection " +
|
60
|
+
"'#{collection.name}' in database '#{database}'."
|
61
|
+
)
|
62
|
+
end
|
57
63
|
end
|
58
|
-
end
|
64
|
+
rescue Mongo::Error::OperationFailure; end
|
59
65
|
end and true
|
60
66
|
end
|
61
67
|
|
@@ -142,7 +148,7 @@ module Mongoid
|
|
142
148
|
#
|
143
149
|
# @since 4.0.0
|
144
150
|
def index_keys
|
145
|
-
index_specifications.map
|
151
|
+
index_specifications.map(&:key)
|
146
152
|
end
|
147
153
|
end
|
148
154
|
end
|
data/lib/mongoid/persistable.rb
CHANGED
@@ -27,7 +27,6 @@ module Mongoid
|
|
27
27
|
include Incrementable
|
28
28
|
include Logical
|
29
29
|
include Poppable
|
30
|
-
include Positional
|
31
30
|
include Pullable
|
32
31
|
include Pushable
|
33
32
|
include Renamable
|
@@ -209,7 +208,7 @@ module Mongoid
|
|
209
208
|
def persist_atomic_operations(operations)
|
210
209
|
if persisted?
|
211
210
|
selector = atomic_selector
|
212
|
-
_root.collection.find(selector).
|
211
|
+
_root.collection.find(selector).update_one(operations)
|
213
212
|
end
|
214
213
|
end
|
215
214
|
end
|
@@ -61,7 +61,7 @@ module Mongoid
|
|
61
61
|
_parent.insert
|
62
62
|
else
|
63
63
|
selector = _parent.atomic_selector
|
64
|
-
_root.collection.find(selector).
|
64
|
+
_root.collection.find(selector).update_one(atomic_inserts)
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
@@ -76,7 +76,7 @@ module Mongoid
|
|
76
76
|
#
|
77
77
|
# @since 4.0.0
|
78
78
|
def insert_as_root
|
79
|
-
collection.
|
79
|
+
collection.insert_one(as_document)
|
80
80
|
end
|
81
81
|
|
82
82
|
# Post process an insert, which sets the new record attribute to false
|
@@ -62,7 +62,7 @@ module Mongoid
|
|
62
62
|
_parent.remove_child(self) if notifying_parent?(options)
|
63
63
|
if _parent.persisted?
|
64
64
|
selector = _parent.atomic_selector
|
65
|
-
_root.collection.find(selector).
|
65
|
+
_root.collection.find(selector).update_one(atomic_deletes)
|
66
66
|
end
|
67
67
|
true
|
68
68
|
end
|
@@ -78,7 +78,7 @@ module Mongoid
|
|
78
78
|
#
|
79
79
|
# @since 4.0.0
|
80
80
|
def delete_as_root
|
81
|
-
collection.find(atomic_selector).
|
81
|
+
collection.find(atomic_selector).delete_one
|
82
82
|
true
|
83
83
|
end
|
84
84
|
|
@@ -140,7 +140,7 @@ module Mongoid
|
|
140
140
|
selector.merge!(_type: name) if hereditary?
|
141
141
|
coll = collection
|
142
142
|
deleted = coll.find(selector).count
|
143
|
-
coll.find(selector).
|
143
|
+
coll.find(selector).delete_many
|
144
144
|
deleted
|
145
145
|
end
|
146
146
|
end
|
@@ -17,7 +17,7 @@ module Mongoid
|
|
17
17
|
#
|
18
18
|
# @param [ Hash ] pulls The field/value pull pairs.
|
19
19
|
#
|
20
|
-
# @return [
|
20
|
+
# @return [ Document ] The document.
|
21
21
|
#
|
22
22
|
# @since 4.0.0
|
23
23
|
def pull(pulls)
|
@@ -37,7 +37,7 @@ module Mongoid
|
|
37
37
|
#
|
38
38
|
# @param [ Hash ] pulls The pull all operations.
|
39
39
|
#
|
40
|
-
# @return [
|
40
|
+
# @return [ Document ] The document.
|
41
41
|
#
|
42
42
|
# @since 4.0.0
|
43
43
|
def pull_all(pulls)
|
@@ -16,7 +16,7 @@ module Mongoid
|
|
16
16
|
#
|
17
17
|
# @param [ Hash ] adds The field/value pairs to add.
|
18
18
|
#
|
19
|
-
# @return [
|
19
|
+
# @return [ Document ] The document.
|
20
20
|
#
|
21
21
|
# @since 4.0.0
|
22
22
|
def add_to_set(adds)
|
@@ -43,7 +43,7 @@ module Mongoid
|
|
43
43
|
#
|
44
44
|
# @param [ Hash ] pushes The $push operations.
|
45
45
|
#
|
46
|
-
# @return [
|
46
|
+
# @return [ Document ] The document.
|
47
47
|
#
|
48
48
|
# @since 4.0.0
|
49
49
|
def push(pushes)
|
@@ -141,9 +141,9 @@ module Mongoid
|
|
141
141
|
unless updates.empty?
|
142
142
|
coll = _root.collection
|
143
143
|
selector = atomic_selector
|
144
|
-
coll.find(selector).
|
144
|
+
coll.find(selector).update_one(updates)
|
145
145
|
conflicts.each_pair do |key, value|
|
146
|
-
coll.find(selector).
|
146
|
+
coll.find(selector).update_one({ key => value })
|
147
147
|
end
|
148
148
|
end
|
149
149
|
end
|
data/lib/mongoid/query_cache.rb
CHANGED
@@ -107,149 +107,143 @@ module Mongoid
|
|
107
107
|
end
|
108
108
|
end
|
109
109
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
def #{method_name}_with_clear_cache(*args) # def upsert_with_clear_cache(*args)
|
116
|
-
QueryCache.clear_cache # QueryCache.clear_cache
|
117
|
-
#{method_name}_without_clear_cache(*args) # upsert_without_clear_cache(*args)
|
118
|
-
end # end
|
119
|
-
CODE
|
110
|
+
# A Cursor that attempts to load documents from memory first before hitting
|
111
|
+
# the database if the same query has already been executed.
|
112
|
+
#
|
113
|
+
# @since 5.0.0
|
114
|
+
class CachedCursor < Mongo::Cursor
|
120
115
|
|
121
|
-
|
116
|
+
# We iterate over the cached documents if they exist already in the
|
117
|
+
# cursor otherwise proceed as normal.
|
118
|
+
#
|
119
|
+
# @example Iterate over the documents.
|
120
|
+
# cursor.each do |doc|
|
121
|
+
# # ...
|
122
|
+
# end
|
123
|
+
#
|
124
|
+
# @since 5.0.0
|
125
|
+
def each
|
126
|
+
if @cached_documents
|
127
|
+
@cached_documents.each{ |doc| yield doc }
|
128
|
+
else
|
129
|
+
super
|
122
130
|
end
|
123
131
|
end
|
132
|
+
|
133
|
+
# Get a human-readable string representation of +Cursor+.
|
134
|
+
#
|
135
|
+
# @example Inspect the cursor.
|
136
|
+
# cursor.inspect
|
137
|
+
#
|
138
|
+
# @return [ String ] A string representation of a +Cursor+ instance.
|
139
|
+
#
|
140
|
+
# @since 2.0.0
|
141
|
+
def inspect
|
142
|
+
"#<Mongoid::QueryCache::CachedCursor:0x#{object_id} @view=#{@view.inspect}>"
|
143
|
+
end
|
144
|
+
|
145
|
+
private
|
146
|
+
|
147
|
+
def process(result)
|
148
|
+
@remaining -= result.returned_count if limited?
|
149
|
+
@cursor_id = result.cursor_id
|
150
|
+
@coll_name ||= result.namespace.sub("#{database.name}.", '') if result.namespace
|
151
|
+
documents = result.documents
|
152
|
+
(@cached_documents ||= []).concat(documents)
|
153
|
+
documents
|
154
|
+
end
|
124
155
|
end
|
125
156
|
|
126
|
-
#
|
127
|
-
#
|
157
|
+
# Included to add behaviour for clearing out the query cache on certain
|
158
|
+
# operations.
|
128
159
|
#
|
129
160
|
# @since 4.0.0
|
130
|
-
module
|
161
|
+
module Base
|
131
162
|
|
132
|
-
|
163
|
+
def alias_query_cache_clear(*method_names)
|
164
|
+
method_names.each do |method_name|
|
165
|
+
class_eval <<-CODE, __FILE__, __LINE__ + 1
|
166
|
+
def #{method_name}_with_clear_cache(*args)
|
167
|
+
QueryCache.clear_cache
|
168
|
+
#{method_name}_without_clear_cache(*args)
|
169
|
+
end
|
170
|
+
CODE
|
133
171
|
|
134
|
-
|
135
|
-
return yield unless QueryCache.enabled?
|
136
|
-
return yield if system_collection?
|
137
|
-
key = cache_key.push(context)
|
138
|
-
|
139
|
-
if more
|
140
|
-
docs = yield
|
141
|
-
QueryCache.cache_table[key].push(*docs)
|
142
|
-
docs
|
143
|
-
elsif QueryCache.cache_table.has_key?(key)
|
144
|
-
instrument(key) { QueryCache.cache_table[key] }
|
145
|
-
else
|
146
|
-
QueryCache.cache_table[key] = yield
|
172
|
+
alias_method_chain method_name, :clear_cache
|
147
173
|
end
|
148
174
|
end
|
149
|
-
|
150
|
-
def instrument(key, &block)
|
151
|
-
ActiveSupport::Notifications.instrument("query_cache.mongoid", key: key, &block)
|
152
|
-
end
|
153
175
|
end
|
154
176
|
|
155
|
-
#
|
177
|
+
# Contains enhancements to the Mongo::Collection::View in order to get a
|
178
|
+
# cached cursor or a regular cursor on iteration.
|
156
179
|
#
|
157
|
-
# @since
|
158
|
-
module
|
180
|
+
# @since 5.0.0
|
181
|
+
module View
|
159
182
|
extend ActiveSupport::Concern
|
160
|
-
include Cacheable
|
161
183
|
|
162
184
|
included do
|
163
185
|
extend QueryCache::Base
|
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
|
-
|
186
|
+
alias_query_cache_clear :delete_one,
|
187
|
+
:delete_many,
|
188
|
+
:update_one,
|
189
|
+
:update_many,
|
190
|
+
:replace_one,
|
191
|
+
:find_one_and_delete,
|
192
|
+
:find_one_and_replace,
|
193
|
+
:find_one_and_update
|
194
|
+
end
|
195
|
+
|
196
|
+
# Override the default enumeration to handle if the cursor can be cached
|
197
|
+
# or not.
|
198
|
+
#
|
199
|
+
# @example Iterate over the view.
|
200
|
+
# view.each do |doc|
|
201
|
+
# # ...
|
202
|
+
# end
|
203
|
+
#
|
204
|
+
# @since 5.0.0
|
205
|
+
def each
|
206
|
+
if system_collection? || !QueryCache.enabled?
|
207
|
+
super
|
208
|
+
else
|
209
|
+
key = cache_key
|
210
|
+
cursor = QueryCache.cache_table[key]
|
211
|
+
unless cursor
|
212
|
+
server = read.select_server(cluster)
|
213
|
+
cursor = CachedCursor.new(view, send_initial_query(server), server)
|
214
|
+
QueryCache.cache_table[key] = cursor
|
215
|
+
end
|
216
|
+
cursor.each do |doc|
|
217
|
+
yield doc
|
218
|
+
end if block_given?
|
219
|
+
cursor
|
192
220
|
end
|
193
221
|
end
|
194
222
|
|
195
223
|
private
|
196
224
|
|
197
225
|
def cache_key
|
198
|
-
[
|
226
|
+
[ collection.namespace, selector, limit, skip, projection ]
|
199
227
|
end
|
200
228
|
|
201
229
|
def system_collection?
|
202
|
-
|
230
|
+
collection.namespace =~ /^system./
|
203
231
|
end
|
204
232
|
end
|
205
233
|
|
206
234
|
# Adds behaviour to the query cache for collections.
|
207
235
|
#
|
208
|
-
# @since
|
236
|
+
# @since 5.0.0
|
209
237
|
module Collection
|
210
238
|
extend ActiveSupport::Concern
|
211
239
|
|
212
240
|
included do
|
213
241
|
extend QueryCache::Base
|
214
|
-
alias_query_cache_clear :
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|
218
|
-
# A Cursor that attempts to load documents from memory first before hitting
|
219
|
-
# the database if the same query has already been executed.
|
220
|
-
#
|
221
|
-
# @since 4.0.0
|
222
|
-
class CachedCursor < Moped::Cursor
|
223
|
-
include Cacheable
|
224
|
-
|
225
|
-
# Override the loading of docs to attempt to fetch from the cache.
|
226
|
-
#
|
227
|
-
# @example Load the documents.
|
228
|
-
# cursor.load_docs
|
229
|
-
#
|
230
|
-
# @return [ Array<Hash> ] The documents.
|
231
|
-
#
|
232
|
-
# @since 4.0.0
|
233
|
-
def load_docs
|
234
|
-
with_cache { super }
|
235
|
-
end
|
236
|
-
|
237
|
-
def get_more
|
238
|
-
with_cache(:cursor, true) { super }
|
239
|
-
end
|
240
|
-
|
241
|
-
private
|
242
|
-
|
243
|
-
def cache_key
|
244
|
-
[ @database, @collection, @selector, @options[:limit], @options[:skip], @options[:fields] ]
|
245
|
-
end
|
246
|
-
|
247
|
-
def system_collection?
|
248
|
-
@collection =~ /^system./
|
242
|
+
alias_query_cache_clear :insert_one, :insert_many
|
249
243
|
end
|
250
244
|
end
|
251
245
|
end
|
252
246
|
end
|
253
247
|
|
254
|
-
|
255
|
-
|
248
|
+
Mongo::Collection.__send__(:include, Mongoid::QueryCache::Collection)
|
249
|
+
Mongo::Collection::View.__send__(:include, Mongoid::QueryCache::View)
|