ruby_mongo_x 0.0.1
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 +7 -0
- data/lib/helpers/loggable.rb +7 -0
- data/lib/ruby_mongo_x.rb +112 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 572e5ea8830b21f4383aea598af182771d30cb7825b89632fca870be5de69cee
|
4
|
+
data.tar.gz: ec51fbaa55bd7593ca2417da94127440930ce5c90c18cb1e07f6d3d82027166b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d9298a9965220c26672083d00407838932367c065f2be36c093e23871d4787c13b2b5eea25a610a1f9f286b784ac6828c96b0d97794327b64cceb357b57ea680
|
7
|
+
data.tar.gz: 4a719efef6c5eb616adbb38748304a72cbe17ac431784efc0bd06c7f34354f0eb3dca4108f7f652d8bb2dc25a3774439e0b3fb2d3909722c0c6aa35415518be4
|
data/lib/ruby_mongo_x.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'mongo'
|
2
|
+
require 'concurrent'
|
3
|
+
require 'pry'
|
4
|
+
require_relative './helpers/loggable'
|
5
|
+
|
6
|
+
class RubyMongoX
|
7
|
+
include ::Helpers::Loggable
|
8
|
+
|
9
|
+
def self.build(collection_name, shards, max_count_per_shard)
|
10
|
+
new(collection_name, shards, max_count_per_shard)
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(collection_name, shards, max_count_per_shard)
|
14
|
+
@max_count_per_shard = max_count_per_shard
|
15
|
+
@mongo_clients = {}
|
16
|
+
|
17
|
+
shards.each_with_index do |shard, idx|
|
18
|
+
log_debug "\t Connecting to shard #{shard} ..."
|
19
|
+
@mongo_clients[idx.to_s] = ::Mongo::Client.new(shard)
|
20
|
+
log_debug " OK\n"
|
21
|
+
end
|
22
|
+
|
23
|
+
@collection_name = collection_name
|
24
|
+
end
|
25
|
+
|
26
|
+
def get_collection(key)
|
27
|
+
shard_idx = get_shard_index(key)
|
28
|
+
unless @mongo_clients[shard_idx.to_s].nil?
|
29
|
+
return @mongo_clients[shard_idx.to_s][@collection_name.to_sym]
|
30
|
+
else
|
31
|
+
raise StandardError.new("No Mongo shard for shard index: #{shard_idx} !!!")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# def ensure_indices(collection_name, index_definitions)
|
36
|
+
# @mongo_clients.each do |e|
|
37
|
+
# mongo_client = e[1]
|
38
|
+
# collection = mongo_client[collection_name.to_sym]
|
39
|
+
# mongo_user = collection.client.cluster.servers.first.options["user"]
|
40
|
+
|
41
|
+
# log_debug "\tEnsuring index: #{index_definitions} on #{collection_name} (#{mongo_user})... "
|
42
|
+
# mongo_client[collection_name.to_sym].indexes.create_many(index_definitions)
|
43
|
+
# log_debug " OK\n"
|
44
|
+
# end
|
45
|
+
# end
|
46
|
+
|
47
|
+
def get_shard_index(key)
|
48
|
+
((key.to_i - 1).to_f / @max_count_per_shard).to_i
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
def find(query)
|
53
|
+
log_debug "\t Query: #{query}\n"
|
54
|
+
promises = @mongo_clients.map do |mongo_client_item|
|
55
|
+
mongo_client = mongo_client_item[1]
|
56
|
+
log_debug "\t\tfind() with mongo_client: #{mongo_client.to_s}\n"
|
57
|
+
Concurrent::Promise.execute do
|
58
|
+
fetch_data_from_shard(mongo_client, query)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Wait for all promises to complete and retrieve their results
|
63
|
+
results = promises.map { |promise| promise.value }
|
64
|
+
|
65
|
+
# Merge the results from all data sources
|
66
|
+
merged_results = results.flatten
|
67
|
+
|
68
|
+
# Now you have merged_results containing data from all sources
|
69
|
+
merged_results
|
70
|
+
end
|
71
|
+
|
72
|
+
def count(query)
|
73
|
+
log_debug "\t Count query: #{query}\n"
|
74
|
+
promises = @mongo_clients.map do |mongo_client_item|
|
75
|
+
mongo_client = mongo_client_item[1]
|
76
|
+
log_debug "\t\tcount() with mongo_client: #{mongo_client.to_s}\n"
|
77
|
+
Concurrent::Promise.execute do
|
78
|
+
count_data_from_shard(mongo_client, query)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# Wait for all promises to complete and retrieve their results
|
83
|
+
results = promises.map { |promise| promise.value }
|
84
|
+
|
85
|
+
merged_results = results.sum()
|
86
|
+
|
87
|
+
# Now you have merged_results containing data from all sources
|
88
|
+
merged_results
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
def update_one()
|
93
|
+
# TODO: implement
|
94
|
+
end
|
95
|
+
|
96
|
+
def update_many()
|
97
|
+
# TODO: implement
|
98
|
+
end
|
99
|
+
|
100
|
+
def fetch_data_from_shard(mongo_client, query)
|
101
|
+
collection = mongo_client[@collection_name.to_sym]
|
102
|
+
collection.find(query).to_a
|
103
|
+
end
|
104
|
+
|
105
|
+
def count_data_from_shard(mongo_client, query)
|
106
|
+
collection = mongo_client[@collection_name.to_sym]
|
107
|
+
count = collection.count_documents(query)
|
108
|
+
count = 0 if count.nil?
|
109
|
+
count
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby_mongo_x
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Grzegorz Błaszczyk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-09-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: config
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mongo
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.19.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.19.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry-byebug
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.10.1
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.10.1
|
55
|
+
description: MongoDB client in Ruby that connects to dozens of Mongo databases, performs
|
56
|
+
queries simultaneously and gets the results in a unified way.
|
57
|
+
email: grzegorz.blaszczyk@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- lib/helpers/loggable.rb
|
63
|
+
- lib/ruby_mongo_x.rb
|
64
|
+
homepage: https://rubygems.org/gems/ruby_mongo_x
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
metadata: {}
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubygems_version: 3.4.6
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: Multi-database Ruby MongoDB client
|
87
|
+
test_files: []
|