Class: Mongo::DB
Constant Summary
collapse
- ProfileLevel =
{:off => 0, :slow_only => 1, :all => 2, 0 => 'off', 1 => 'slow_only', 2 => 'all'}
JavaImpl::Utils::SortingHash
JavaImpl::Db_::SYSTEM_NAMESPACE_COLLECTION, JavaImpl::Db_::SYSTEM_PROFILE_COLLECTION
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_user(username, password) ⇒ Object
-
#authenticate(username, password, save_auth = true) ⇒ Object
-
#collection(name, options = {}) ⇒ Object
(also: #[])
-
#collection_names ⇒ Object
-
#collections ⇒ Object
-
#collections_info(coll_name = nil) ⇒ Object
-
#command(cmd, opts = {}) ⇒ Object
-
#create_collection(name, options = {}) ⇒ Object
-
#create_index(collection_name, field_or_spec, unique = false) ⇒ Object
-
#dereference(dbref) ⇒ Object
-
#drop_collection(name) ⇒ Object
-
#drop_index(collection_name, index_name) ⇒ Object
-
#error? ⇒ Boolean
Return true
if an error was caused by the most recently executed database operation.
-
#eval(code, *args) ⇒ Object
-
#full_collection_name(collection_name) ⇒ Object
-
#get_last_error ⇒ Object
(also: #last_status)
-
#index_information(collection_name) ⇒ Object
-
#initialize(db_name, connection, options = {}) ⇒ DB
constructor
-
#logout ⇒ Object
-
#ok?(doc) ⇒ Boolean
-
#pk_factory ⇒ Object, Nil
The primary key factory object (or nil
).
-
#pk_factory=(pk_factory) ⇒ Object
Specify a primary key factory if not already set.
-
#previous_error ⇒ Object
-
#profiling_info ⇒ Object
-
#profiling_level ⇒ Object
-
#profiling_level=(level) ⇒ Object
-
#query(collection, query, admin = false) ⇒ Object
-
#remove_user(username) ⇒ Object
-
#rename_collection(from, to) ⇒ Object
-
#reset_error_history ⇒ Object
-
#stats ⇒ Object
-
#strict? ⇒ Boolean
Returns the value of the strict
flag.
-
#validate_collection(name) ⇒ Object
#from_dbobject, #prep_fields, #prep_hint, #prep_id, #prep_sort, #raise_not_implemented, #sort_value, #system_name?, #to_dbobject, #trap_raise, #validate_name
#write_concern
Constructor Details
#initialize(db_name, connection, options = {}) ⇒ DB
Returns a new instance of DB.
39
40
41
42
43
44
45
46
|
# File 'lib/jmongo/db.rb', line 39
def initialize(db_name, connection, options={})
@name = db_name
@connection = connection
@j_db = @connection.connection.get_db db_name
@pk_factory = options[:pk]
@safe = options[:safe]
@strict = options.fetch(:strict, false)
end
|
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
21
22
23
|
# File 'lib/jmongo/db.rb', line 21
def connection
@connection
end
|
Returns the value of attribute j_db.
21
22
23
|
# File 'lib/jmongo/db.rb', line 21
def j_db
@j_db
end
|
Returns the value of attribute name.
21
22
23
|
# File 'lib/jmongo/db.rb', line 21
def name
@name
end
|
Returns the value of attribute safe.
21
22
23
|
# File 'lib/jmongo/db.rb', line 21
def safe
@safe
end
|
Returns the value of attribute strict.
23
24
25
|
# File 'lib/jmongo/db.rb', line 23
def strict
@strict
end
|
Class Method Details
.write_concern(safe_) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/jmongo/db.rb', line 27
def self.write_concern(safe_)
return safe_ if safe_.is_a?(JMongo::WriteConcern)
return JMongo::WriteConcern.new(0) if safe_.is_a?(FalseClass)
return JMongo::WriteConcern.new(true) if safe_.is_a?(TrueClass)
return JMongo::WriteConcern.new(safe.to_i) if safe_.is_a?(Numeric)
return JMongo::WriteConcern.new(-1) unless safe_.is_a?(Hash)
w = safe_.fetch(:w, 0)
t = safe_.fetch(:wtimeout, 0)
f = !!safe_.fetch(:fsync, false)
JMongo::WriteConcern.new(w, t, f) end
|
Instance Method Details
#add_user(username, password) ⇒ Object
60
61
62
|
# File 'lib/jmongo/db.rb', line 60
def add_user(username, password)
@j_db.add_user(username, password)
end
|
#authenticate(username, password, save_auth = true) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/jmongo/db.rb', line 48
def authenticate(username, password, save_auth=true)
begin
succeeded = @j_db.authenticate(username, password)
if save_auth && succeeded
@connection.add_auth(@name, username, password)
end
rescue => e
succeeded = false
end
succeeded
end
|
#collection(name, options = {}) ⇒ Object
Also known as:
[]
102
103
104
105
106
107
108
109
|
# File 'lib/jmongo/db.rb', line 102
def collection(name, options = {})
validate_name(name)
if strict? && !collection_exists?(name)
raise MongoDBError, "Collection #{name} does not exists. Currently in strict mode."
else
Collection.new self, name, options, @j_db.get_collection(name)
end
end
|
#collection_names ⇒ Object
72
73
74
|
# File 'lib/jmongo/db.rb', line 72
def collection_names
@j_db.get_collection_names
end
|
#collections ⇒ Object
76
77
78
79
80
|
# File 'lib/jmongo/db.rb', line 76
def collections
collection_names.map do |name|
collection(name)
end
end
|
#collections_info(coll_name = nil) ⇒ Object
82
83
84
|
# File 'lib/jmongo/db.rb', line 82
def collections_info(coll_name=nil)
_collections_info coll_name
end
|
#command(cmd, opts = {}) ⇒ Object
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/jmongo/db.rb', line 191
def command(cmd, opts={})
selector = cmd.respond_to?('merge') ? cmd : {cmd.to_s => 1}
check_response = opts.fetch(:check_response, true)
raise MongoArgumentError, "command must be given a selector" if selector.empty?
if selector.keys.length > 1 && RUBY_VERSION < '1.9' && selector.class != BSON::OrderedHash
raise MongoArgumentError, "DB#command requires an OrderedHash when hash contains multiple keys"
end
begin
result = exec_command(selector)
rescue => ex
raise OperationFailure, "Database command '#{selector.keys.first}' failed: #{ex.message}"
end
raise OperationFailure, "Database command '#{selector.keys.first}' failed: returned null." if result.nil?
if (check_response && !ok?(result))
message = "Database command '#{selector.keys.first}' failed: (" + result.map{|k, v| "#{k}: '#{v}'"}.join('; ') + ")."
raise OperationFailure.new message
else
result
end
end
|
#create_collection(name, options = {}) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/jmongo/db.rb', line 86
def create_collection(name, options={})
validate_name(name)
if collection_exists?(name)
if strict?
raise MongoDBError, "Collection #{name} already exists. Currently in strict mode."
else
return Collection.new self, name, options, @j_db.get_collection(name)
end
end
begin
Collection.new self, name, options
rescue => ex
raise MongoDBError, "Collection #{name} creation error: " + ex.message
end
end
|
#create_index(collection_name, field_or_spec, unique = false) ⇒ Object
176
177
178
|
# File 'lib/jmongo/db.rb', line 176
def create_index(collection_name, field_or_spec, unique=false)
collection(collection_name).create_indexes(field_or_spec,{:unique=>unique})
end
|
#dereference(dbref) ⇒ Object
140
141
142
143
144
|
# File 'lib/jmongo/db.rb', line 140
def dereference(dbref)
ns = dbref.namespace
raise MongoArgumentError, "No collection for dbref: #{dbref.inspect}" unless collection_exists?(ns)
collection(ns).find_one("_id" => dbref.object_id)
end
|
#drop_collection(name) ⇒ Object
112
113
114
115
116
117
|
# File 'lib/jmongo/db.rb', line 112
def drop_collection(name)
col_name = name.to_s
return true unless collection_exists?(col_name)
ok?(command(:drop => col_name))
end
|
#drop_index(collection_name, index_name) ⇒ Object
160
161
162
|
# File 'lib/jmongo/db.rb', line 160
def drop_index(collection_name, index_name)
collection(collection_name).drop_index(index_name)
end
|
#error? ⇒ Boolean
Return true
if an error was caused by the most recently executed database operation.
183
184
185
|
# File 'lib/jmongo/db.rb', line 183
def error?
!get_last_error['err'].nil?
end
|
#eval(code, *args) ⇒ Object
146
147
148
149
|
# File 'lib/jmongo/db.rb', line 146
def eval(code, *args)
doc = do_eval(code, *args)
(retval = doc['retval']).is_a?(Hash) && (value = retval['value']) ? value : retval
end
|
#full_collection_name(collection_name) ⇒ Object
215
216
217
|
# File 'lib/jmongo/db.rb', line 215
def full_collection_name(collection_name)
"#{@name}.#{collection_name}"
end
|
#get_last_error ⇒ Object
Also known as:
last_status
119
120
121
|
# File 'lib/jmongo/db.rb', line 119
def get_last_error
from_dbobject(@j_db.getLastError)
end
|
164
165
166
167
168
169
170
|
# File 'lib/jmongo/db.rb', line 164
def index_information(collection_name)
info = {}
from_dbobject(@j_db.getCollectionFromString(collection_name).getIndexInfo).each do |index|
info[index['name']] = index
end
info
end
|
68
69
70
|
# File 'lib/jmongo/db.rb', line 68
def logout
raise_not_implemented
end
|
#ok?(doc) ⇒ Boolean
187
188
189
|
# File 'lib/jmongo/db.rb', line 187
def ok?(doc)
doc['ok'] == 1.0 || doc['ok'] == true
end
|
#pk_factory ⇒ Object, Nil
The primary key factory object (or nil
).
225
226
227
|
# File 'lib/jmongo/db.rb', line 225
def pk_factory
@pk_factory
end
|
#pk_factory=(pk_factory) ⇒ Object
Specify a primary key factory if not already set.
232
233
234
235
236
237
238
|
# File 'lib/jmongo/db.rb', line 232
def pk_factory=(pk_factory)
if @pk_factory
raise MongoArgumentError, "Cannot change primary key factory once it's been set"
end
@pk_factory = pk_factory
end
|
#previous_error ⇒ Object
128
129
130
|
# File 'lib/jmongo/db.rb', line 128
def previous_error
exec_command :getpreverror
end
|
#profiling_level ⇒ Object
240
241
242
243
244
245
246
247
248
|
# File 'lib/jmongo/db.rb', line 240
def profiling_level
oh = BSON::OrderedHash.new
oh['profile'] = -1
doc = command(oh, :check_response => false)
raise "Error with profile command: #{doc.inspect}" unless ok?(doc) && doc['was'].kind_of?(Numeric)
was = ProfileLevel[doc['was'].to_i]
raise "Error: illegal profiling level value #{doc['was']}" if was.nil?
was.to_sym
end
|
#profiling_level=(level) ⇒ Object
250
251
252
253
254
255
256
257
|
# File 'lib/jmongo/db.rb', line 250
def profiling_level=(level)
oh = BSON::OrderedHash.new
int_lvl = ProfileLevel[level]
raise "Error: illegal profiling level value #{level}" if int_lvl.nil?
oh['profile'] = int_lvl
doc = command(oh, :check_response => false)
ok?(doc) || raise(MongoDBError, "Error with profile command: #{doc.inspect}")
end
|
#query(collection, query, admin = false) ⇒ Object
136
137
138
|
# File 'lib/jmongo/db.rb', line 136
def query(collection, query, admin=false)
raise_not_implemented
end
|
#remove_user(username) ⇒ Object
64
65
66
|
# File 'lib/jmongo/db.rb', line 64
def remove_user(username)
@j_db.remove_user(username)
end
|
#rename_collection(from, to) ⇒ Object
151
152
153
154
155
156
157
158
|
# File 'lib/jmongo/db.rb', line 151
def rename_collection(from, to)
begin
@j_db.get_collection(from).rename(to)
rescue => ex
raise(MongoDBError, "Error renaming collection from: #{from}, to: #{to}")
end
true
end
|
#reset_error_history ⇒ Object
132
133
134
|
# File 'lib/jmongo/db.rb', line 132
def reset_error_history
exec_command :reseterror
end
|
172
173
174
|
# File 'lib/jmongo/db.rb', line 172
def stats
from_dbobject exec_command(:dbstats)
end
|
#strict? ⇒ Boolean
Returns the value of the strict
flag.
220
|
# File 'lib/jmongo/db.rb', line 220
def strict?; @strict; end
|
#validate_collection(name) ⇒ Object
263
264
265
266
267
268
269
270
271
272
273
274
275
|
# File 'lib/jmongo/db.rb', line 263
def validate_collection(name)
cmd = BSON::OrderedHash.new
cmd['validate'] = name
cmd['full'] = true
doc = command(cmd, :check_response => false)
if !ok?(doc)
raise MongoDBError, "Error with validate command: #{doc.inspect}"
end
if (doc.has_key?('valid') && !doc['valid']) || (doc['result'] =~ /\b(exception|corrupt)\b/i)
raise MongoDBError, "Error: invalid collection #{name}: #{doc.inspect}"
end
doc
end
|