ruby_mongo_x 0.0.4 → 0.0.5
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/lib/ruby_mongo_x.rb +83 -5
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8f573ba827ea6150f64c72810b168a43c3bf5fe415f78778942f1ea37d00d12
|
4
|
+
data.tar.gz: 19ad8c68b5ee1d24369db71157271ee42c44c84faac150a105fcc165640fdb1e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d55880966d5792c766a03f8eaef4ceeb5b0bb403f088af7a9c34364dabd1d0c9cae5bc0bbe525639afabc6cc7343bc5ac67c8b3ec3757401d5e051230b20c5f
|
7
|
+
data.tar.gz: 5632252bb60d51f28a500548fafeebdf52d4e73785e5d085a0db534a6e6c17c719b263ccc6c54fb587349d57e9a39cfaa3f2c9ea57a8a586c27e83248f81a466
|
data/lib/ruby_mongo_x.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
require 'mongo'
|
2
1
|
require 'concurrent'
|
2
|
+
require 'mongo'
|
3
|
+
require 'openssl'
|
3
4
|
require_relative './helpers/loggable'
|
4
5
|
|
5
6
|
class RubyMongoX
|
@@ -25,6 +26,9 @@ class RubyMongoX
|
|
25
26
|
rescue Mongo::Error::SocketError => e
|
26
27
|
log_debug "\t SocketError: #{e.message}...\n"
|
27
28
|
log_debug "\t SocketError: Stopped trying to connect to shard #{shard}... FAIL!\n"
|
29
|
+
rescue OpenSSL::SSL::SSLError => e
|
30
|
+
log_debug "\t SSLError: #{e.message}...\n"
|
31
|
+
log_debug "\t SSLError: Stopped trying to connect to shard #{shard}... FAIL!\n"
|
28
32
|
rescue Mongo::Error::NoSRVRecords => e
|
29
33
|
log_debug "\t NoSRVRecords: #{e.message}...\n"
|
30
34
|
log_debug "\t NoSRVRecords: Retrying connect to shard #{shard}...\n"
|
@@ -120,12 +124,52 @@ class RubyMongoX
|
|
120
124
|
end
|
121
125
|
|
122
126
|
|
123
|
-
def update_one()
|
124
|
-
#
|
127
|
+
def update_one(query, update_query_or_doc, update_options={})
|
128
|
+
log_debug "\t Update one query: #{query.to_json}, update query or doc: #{update_query_or_doc.to_json}\n"
|
129
|
+
|
130
|
+
promises = @mongo_clients.map do |mongo_client_item|
|
131
|
+
mongo_client = mongo_client_item[1]
|
132
|
+
Concurrent::Promise.execute do
|
133
|
+
update_one_in_shard(mongo_client, query, update_query_or_doc, update_options)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
results = {
|
138
|
+
modified_count: 0,
|
139
|
+
shards: {}
|
140
|
+
}
|
141
|
+
|
142
|
+
promises.map { |promise|
|
143
|
+
results[:shards][promise.value[:shard]] = promise.value[:modified_count]
|
144
|
+
results[:modified_count] = results[:modified_count] + promise.value[:modified_count]
|
145
|
+
}
|
146
|
+
|
147
|
+
log_debug "Results: #{results}\n"
|
148
|
+
results
|
125
149
|
end
|
126
150
|
|
127
|
-
def update_many()
|
128
|
-
#
|
151
|
+
def update_many(query, update_query, update_options={})
|
152
|
+
log_debug "\t Update many query: #{query.to_json}, update query: #{update_query.to_json}\n"
|
153
|
+
|
154
|
+
promises = @mongo_clients.map do |mongo_client_item|
|
155
|
+
mongo_client = mongo_client_item[1]
|
156
|
+
Concurrent::Promise.execute do
|
157
|
+
update_many_in_shard(mongo_client, query, update_query, update_options)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
results = {
|
162
|
+
modified_count: 0,
|
163
|
+
shards: {}
|
164
|
+
}
|
165
|
+
|
166
|
+
promises.map { |promise|
|
167
|
+
results[:shards][promise.value[:shard]] = promise.value[:modified_count]
|
168
|
+
results[:modified_count] = results[:modified_count] + promise.value[:modified_count]
|
169
|
+
}
|
170
|
+
|
171
|
+
log_debug "Results: #{results}\n"
|
172
|
+
results
|
129
173
|
end
|
130
174
|
|
131
175
|
def fetch_data_from_shard(mongo_client, query)
|
@@ -157,6 +201,40 @@ class RubyMongoX
|
|
157
201
|
}
|
158
202
|
end
|
159
203
|
|
204
|
+
def update_one_in_shard(mongo_client, query, update_query_or_doc, update_options)
|
205
|
+
collection = mongo_client[@collection_name.to_sym]
|
206
|
+
result = collection.update_one(query, update_query_or_doc, update_options)
|
207
|
+
result_document = result.documents.first
|
208
|
+
|
209
|
+
mongo_server = mongo_client&.cluster&.servers&.first
|
210
|
+
mongo_user = mongo_server&.options["user"] || ""
|
211
|
+
mongo_db = mongo_server&.options["database"] || ""
|
212
|
+
log_debug "\t\tupdate_one() with mongo_client user: #{mongo_user}, database: #{mongo_db} returns #{result_document}\n"
|
213
|
+
|
214
|
+
{
|
215
|
+
shard: (mongo_user.nil? || mongo_user == "") ? mongo_db : mongo_user,
|
216
|
+
modified_count: result_document["nModified"],
|
217
|
+
ok: result_document["ok"]
|
218
|
+
}
|
219
|
+
end
|
220
|
+
|
221
|
+
def update_many_in_shard(mongo_client, query, update_query, update_options)
|
222
|
+
collection = mongo_client[@collection_name.to_sym]
|
223
|
+
result = collection.update_many(query, update_query, update_options)
|
224
|
+
result_document = result.documents.first
|
225
|
+
|
226
|
+
mongo_server = mongo_client&.cluster&.servers&.first
|
227
|
+
mongo_user = mongo_server&.options["user"] || ""
|
228
|
+
mongo_db = mongo_server&.options["database"] || ""
|
229
|
+
log_debug "\t\tupdate_many() with mongo_client user: #{mongo_user}, database: #{mongo_db} returns #{result_document}\n"
|
230
|
+
|
231
|
+
{
|
232
|
+
shard: (mongo_user.nil? || mongo_user == "") ? mongo_db : mongo_user,
|
233
|
+
modified_count: result_document["nModified"],
|
234
|
+
ok: result_document["ok"]
|
235
|
+
}
|
236
|
+
end
|
237
|
+
|
160
238
|
def disconnect()
|
161
239
|
@mongo_clients.values.each do |client|
|
162
240
|
log_debug "\t Disconnecting from client #{client.to_s} ..."
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_mongo_x
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Grzegorz Błaszczyk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-
|
11
|
+
date: 2025-05-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: config
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 2.
|
33
|
+
version: 2.21.1
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 2.
|
40
|
+
version: 2.21.1
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: pry-byebug
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|