Class: Dakwak::MongoAdapter
Instance Attribute Summary collapse
Attributes inherited from Configurable
#cfg
Instance Method Summary
collapse
-
#connect ⇒ Object
-
#create(collection, args) ⇒ Object
-
#create!(collection, args) ⇒ Object
-
#disconnect ⇒ Object
-
#find(collection, cnd) ⇒ Object
-
#find_one(collection, cnd) ⇒ Object
-
#find_or_create(collection, args, creation_args = {}) ⇒ Object
-
#initialize(id = "MongoDB") ⇒ MongoAdapter
constructor
A new instance of MongoAdapter.
-
#remove(collection, args) ⇒ Object
-
#rescue_connection_failure(max_retries = 10) ⇒ Object
-
#update(collection, record, args) ⇒ Object
-
#update_all(collection, find_args, update_args) ⇒ Object
-
#update_where(collection, conds, args) ⇒ Object
Methods included from Logger
included, #log, #log_indent, #log_indent_dec, #log_indent_inc, #logging_context
Methods included from Silencable
#silence, #silent?
#set_option
Constructor Details
#initialize(id = "MongoDB") ⇒ MongoAdapter
Returns a new instance of MongoAdapter.
11
12
13
14
15
|
# File 'lib/dakwak/mongo_adapter.rb', line 11
def initialize(id = "MongoDB")
logging_context(id)
@cfg = { host: "localhost", port: 27017, db: "test" }
Configurator.subscribe(id, self)
end
|
Instance Attribute Details
#dba ⇒ Object
Returns the value of attribute dba.
9
10
11
|
# File 'lib/dakwak/mongo_adapter.rb', line 9
def dba
@dba
end
|
#dbh ⇒ Object
Returns the value of attribute dbh.
9
10
11
|
# File 'lib/dakwak/mongo_adapter.rb', line 9
def dbh
@dbh
end
|
Instance Method Details
#connect ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/dakwak/mongo_adapter.rb', line 17
def connect()
rescue_connection_failure do
if @cfg[:mode] == "single" then
conn_info = "#{@cfg[:single][:host]}:#{@cfg[:single][:port]}"
log_info "Connecting to MongoDB in Single mode to #{conn_info}"
@dba = Mongo::Client.new([@cfg[:single][:host]+":"+@cfg[:single][:port].to_s], ssl: @cfg[:ssl][:enable], ssl_cert: @cfg[:ssl][:ssl_cert], ssl_key: @cfg[:ssl][:ssl_key], ssl_verify: @cfg[:ssl][:ssl_verify], connect: :direct, :database => @cfg[:database])
else
conn_info = "#{@cfg[:rs][:seeds].inspect}, set: #{@cfg[:rs][:set]}"
log_info "Connecting to MongoDB in RS mode to #{conn_info}"
@dba = Mongo::Client.new([@cfg[:rs][:seeds]], ssl: @cfg[:ssl][:enable], ssl_cert: @cfg[:ssl][:ssl_cert], ssl_key: @cfg[:ssl][:ssl_key], ssl_verify: @cfg[:ssl][:ssl_verify], connect: :replica_set, :database => @cfg[:database] || "dakwak_production")
end
@dbh = @dba
end
true
end
|
#create(collection, args) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/dakwak/mongo_adapter.rb', line 72
def create(collection, args)
rescue_connection_failure do
args.delete("_id")
args.merge!({:created_at => Time.now.utc, :updated_at => Time.now.utc })
id = @dbh[collection].insert_one(args)
doc = find_one(collection,:_id => id.inserted_ids[0])
return doc
end
return false
end
|
#create!(collection, args) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/dakwak/mongo_adapter.rb', line 84
def create!(collection, args)
rescue_connection_failure do
args.delete("_id")
args.merge!({:created_at => Time.now.utc, :updated_at => Time.now.utc })
id = @dbh[collection].insert_one(args)
doc = find_one(collection,:_id => id.inserted_ids[0])
return doc
end
return false
end
|
#disconnect ⇒ Object
35
36
37
38
39
40
|
# File 'lib/dakwak/mongo_adapter.rb', line 35
def disconnect()
@dba.close
@dba = nil
log_info "disconnected"
end
|
#find(collection, cnd) ⇒ Object
43
44
45
46
47
48
49
50
|
# File 'lib/dakwak/mongo_adapter.rb', line 43
def find(collection, cnd)
rescue_connection_failure do
entries = @dbh[collection].find(cnd).sort(:created_at => Mongo::Index::DESCENDING).entries
return entries if entries.length > 0
end
return []
end
|
#find_one(collection, cnd) ⇒ Object
52
53
54
55
56
57
58
|
# File 'lib/dakwak/mongo_adapter.rb', line 52
def find_one(collection, cnd)
rescue_connection_failure do
return @dbh[collection].find(cnd).sort(:created_at => Mongo::Index::DESCENDING).first
end
return false
end
|
#find_or_create(collection, args, creation_args = {}) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/dakwak/mongo_adapter.rb', line 60
def find_or_create(collection, args, creation_args = {})
rescue_connection_failure do
doc = find_one(collection, args)
if !doc
doc = create!(collection, args.merge(creation_args))
end
return doc
end
return false
end
|
#remove(collection, args) ⇒ Object
122
123
124
|
# File 'lib/dakwak/mongo_adapter.rb', line 122
def remove(collection, args)
@dbh[collection].remove(args)
end
|
#rescue_connection_failure(max_retries = 10) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/dakwak/mongo_adapter.rb', line 128
def rescue_connection_failure(max_retries=10)
retries = 0
begin
yield
rescue Mongo::Error::NoServerAvailable => ex
log_warn "connection to MongoDB was lost, retrying for the #{retries} time"
retries += 1
raise ex if retries > max_retries
sleep(0.5)
retry
end
end
|
#update(collection, record, args) ⇒ Object
96
97
98
99
100
101
102
103
104
|
# File 'lib/dakwak/mongo_adapter.rb', line 96
def update(collection, record, args)
rescue_connection_failure do
id = @dbh[collection].update_many({ "_id" => record['_id'] }, args)
doc = find(collection, "_id" => record['_id'])
end
return false
end
|
#update_all(collection, find_args, update_args) ⇒ Object
114
115
116
117
118
119
120
|
# File 'lib/dakwak/mongo_adapter.rb', line 114
def update_all(collection, find_args, update_args)
rescue_connection_failure do
return @dbh[collection].update(find_args, update_args)
end
return false
end
|
#update_where(collection, conds, args) ⇒ Object
106
107
108
109
110
111
|
# File 'lib/dakwak/mongo_adapter.rb', line 106
def update_where(collection, conds, args)
rescue_connection_failure do
return update(collection, conds ,args)
end
return false
end
|