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
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)
-
#has_collection?(name) ⇒ Boolean
additions to the ruby driver.
-
#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_sort, #raise_not_implemented, #sort_value, #to_dbobject, #trap_raise
#write_concern
Constructor Details
#initialize(db_name, connection, options = {}) ⇒ DB
Returns a new instance of DB.
29
30
31
32
33
34
35
|
# File 'lib/jmongo/db.rb', line 29
def initialize(db_name, connection, options={})
@name = db_name
@connection = connection
@j_db = @connection.connection.get_db db_name
@pk_factory = options[:pk]
@strict = options.fetch(:strict, false)
end
|
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
23
24
25
|
# File 'lib/jmongo/db.rb', line 23
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.
22
23
24
|
# File 'lib/jmongo/db.rb', line 22
def name
@name
end
|
#strict=(value) ⇒ Object
Sets the attribute strict
25
26
27
|
# File 'lib/jmongo/db.rb', line 25
def strict=(value)
@strict = value
end
|
Instance Method Details
#add_user(username, password) ⇒ Object
49
50
51
|
# File 'lib/jmongo/db.rb', line 49
def add_user(username, password)
@j_db.add_user(username, password)
end
|
#authenticate(username, password, save_auth = true) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/jmongo/db.rb', line 37
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:
[]
93
94
95
|
# File 'lib/jmongo/db.rb', line 93
def collection(name, options = {})
Collection.new self, name, options
end
|
#collection_names ⇒ Object
61
62
63
|
# File 'lib/jmongo/db.rb', line 61
def collection_names
@j_db.get_collection_names
end
|
#collections ⇒ Object
65
66
67
68
69
|
# File 'lib/jmongo/db.rb', line 65
def collections
collection_names.map do |name|
Collection.new(self, name)
end
end
|
#collections_info(coll_name = nil) ⇒ Object
71
72
73
74
75
76
|
# File 'lib/jmongo/db.rb', line 71
def collections_info(coll_name=nil)
selector = {}
selector[:name] = full_collection_name(coll_name) if coll_name
coll = self.collection(SYSTEM_NAMESPACE_COLLECTION)
coll.find selector
end
|
#command(cmd, opts = {}) ⇒ Object
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/jmongo/db.rb', line 176
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/jmongo/db.rb', line 78
def create_collection(name, options={})
if collection_names.include?(name)
raise MongoDBError, "Collection #{name} already exists. Currently in strict mode." if @strict
collection(name, options)
else
begin
jc = @j_db.create_collection(name, to_dbobject(options))
Collection.new self, name, options, jc
rescue NativeException => ex
raise MongoDBError, "Collection #{name} creation error: " +
ex.message
end
end
end
|
#create_index(collection_name, field_or_spec, unique = false) ⇒ Object
161
162
163
|
# File 'lib/jmongo/db.rb', line 161
def create_index(collection_name, field_or_spec, unique=false)
collection(collection_name).create_indexes(field_or_spec,{:unique=>unique})
end
|
#dereference(dbref) ⇒ Object
123
124
125
126
127
|
# File 'lib/jmongo/db.rb', line 123
def dereference(dbref)
ns = dbref.namespace
raise MongoArgumentError, "No namespace for dbref: #{dbref.inspect}"
collection(ns).find_one("_id" => dbref.object_id)
end
|
#drop_collection(name) ⇒ Object
98
99
100
|
# File 'lib/jmongo/db.rb', line 98
def drop_collection(name)
coll = collection(name).j_collection.drop
end
|
#drop_index(collection_name, index_name) ⇒ Object
145
146
147
|
# File 'lib/jmongo/db.rb', line 145
def drop_index(collection_name, index_name)
self[collection_name].drop_index(index_name)
end
|
#error? ⇒ Boolean
Return true
if an error was caused by the most recently executed database operation.
168
169
170
|
# File 'lib/jmongo/db.rb', line 168
def error?
!get_last_error['err'].nil?
end
|
#eval(code, *args) ⇒ Object
129
130
131
132
133
134
|
# File 'lib/jmongo/db.rb', line 129
def eval(code, *args)
doc = do_eval(code, *args)
return unless doc
return doc['retval']['value'] if doc['retval'] && doc['retval']['value']
doc['retval']
end
|
#full_collection_name(collection_name) ⇒ Object
200
201
202
|
# File 'lib/jmongo/db.rb', line 200
def full_collection_name(collection_name)
"#{@name}.#{collection_name}"
end
|
#get_last_error ⇒ Object
Also known as:
last_status
102
103
104
|
# File 'lib/jmongo/db.rb', line 102
def get_last_error
from_dbobject(@j_db.getLastError)
end
|
#has_collection?(name) ⇒ Boolean
additions to the ruby driver
263
264
265
|
# File 'lib/jmongo/db.rb', line 263
def has_collection?(name)
has_coll name
end
|
149
150
151
152
153
154
155
|
# File 'lib/jmongo/db.rb', line 149
def index_information(collection_name)
info = {}
from_dbobject(@j_db.get_collection(collection_name).get_index_info).each do |index|
info[index['name']] = index
end
info
end
|
57
58
59
|
# File 'lib/jmongo/db.rb', line 57
def logout
raise_not_implemented
end
|
#ok?(doc) ⇒ Boolean
172
173
174
|
# File 'lib/jmongo/db.rb', line 172
def ok?(doc)
doc['ok'] == 1.0 || doc['ok'] == true
end
|
#pk_factory ⇒ Object, Nil
The primary key factory object (or nil
).
210
211
212
|
# File 'lib/jmongo/db.rb', line 210
def pk_factory
@pk_factory
end
|
#pk_factory=(pk_factory) ⇒ Object
Specify a primary key factory if not already set.
217
218
219
220
221
222
223
|
# File 'lib/jmongo/db.rb', line 217
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
111
112
113
|
# File 'lib/jmongo/db.rb', line 111
def previous_error
exec_command :getpreverror
end
|
#profiling_level ⇒ Object
225
226
227
228
229
230
231
232
233
|
# File 'lib/jmongo/db.rb', line 225
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
235
236
237
238
239
240
241
242
|
# File 'lib/jmongo/db.rb', line 235
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
119
120
121
|
# File 'lib/jmongo/db.rb', line 119
def query(collection, query, admin=false)
raise_not_implemented
end
|
#remove_user(username) ⇒ Object
53
54
55
|
# File 'lib/jmongo/db.rb', line 53
def remove_user(username)
@j_db.remove_user(username)
end
|
#rename_collection(from, to) ⇒ Object
136
137
138
139
140
141
142
143
|
# File 'lib/jmongo/db.rb', line 136
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
115
116
117
|
# File 'lib/jmongo/db.rb', line 115
def reset_error_history
exec_command :reseterror
end
|
157
158
159
|
# File 'lib/jmongo/db.rb', line 157
def stats
from_dbobject exec_command(:dbstats)
end
|
#strict? ⇒ Boolean
Returns the value of the strict
flag.
205
|
# File 'lib/jmongo/db.rb', line 205
def strict?; @strict; end
|
#validate_collection(name) ⇒ Object
248
249
250
251
252
253
254
255
256
257
258
259
260
|
# File 'lib/jmongo/db.rb', line 248
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
|