ruby_mongo_x 0.0.3 → 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 +86 -5
- metadata +8 -8
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
|
@@ -22,6 +23,12 @@ class RubyMongoX
|
|
22
23
|
log_debug "\t Connecting to shard #{shard} ...\n"
|
23
24
|
begin
|
24
25
|
@mongo_clients[idx.to_s] = ::Mongo::Client.new(shard, options)
|
26
|
+
rescue Mongo::Error::SocketError => e
|
27
|
+
log_debug "\t SocketError: #{e.message}...\n"
|
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"
|
25
32
|
rescue Mongo::Error::NoSRVRecords => e
|
26
33
|
log_debug "\t NoSRVRecords: #{e.message}...\n"
|
27
34
|
log_debug "\t NoSRVRecords: Retrying connect to shard #{shard}...\n"
|
@@ -117,12 +124,52 @@ class RubyMongoX
|
|
117
124
|
end
|
118
125
|
|
119
126
|
|
120
|
-
def update_one()
|
121
|
-
#
|
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
|
122
149
|
end
|
123
150
|
|
124
|
-
def update_many()
|
125
|
-
#
|
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
|
126
173
|
end
|
127
174
|
|
128
175
|
def fetch_data_from_shard(mongo_client, query)
|
@@ -154,6 +201,40 @@ class RubyMongoX
|
|
154
201
|
}
|
155
202
|
end
|
156
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
|
+
|
157
238
|
def disconnect()
|
158
239
|
@mongo_clients.values.each do |client|
|
159
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:
|
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
|
@@ -56,16 +56,16 @@ dependencies:
|
|
56
56
|
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 13.2.1
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 13.2.1
|
69
69
|
description: MongoDB client in Ruby that connects to dozens of Mongo databases, performs
|
70
70
|
queries simultaneously and gets the results in a unified way.
|
71
71
|
email: grzegorz.blaszczyk@gmail.com
|