lib/mongo3/connection.rb in mongo3-0.0.4 vs lib/mongo3/connection.rb in mongo3-0.0.5
- old
+ new
@@ -3,19 +3,66 @@
def initialize( config_file )
@config_file = config_file
end
- def drop_db( path_names )
+ # drop a database using context and db name
+ def drop_database( path_names, db_name )
path_name_tokens = path_names.split( "|" )
env = path_name_tokens[1]
connect_for( env ) do |con|
+ con.drop_database( db_name )
+ end
+ end
+
+ def drop_db( path_names )
+ path_name_tokens = path_names.split( "|" )
+ env = path_name_tokens[1]
+ connect_for( env ) do |con|
db_name = path_name_tokens.pop
con.drop_database( db_name )
end
end
+ def indexes_for( path_names )
+ path_name_tokens = path_names.split( "|" )
+ env = path_name_tokens[1]
+ indexes = {}
+ connect_for( env ) do |con|
+ cltn_name = path_name_tokens.pop
+ db_name = path_name_tokens.pop
+ db = con.db( db_name )
+ cltn = db[cltn_name]
+ indexes = cltn.index_information
+ end
+ indexes
+ end
+
+ def drop_index( path_names, index )
+ path_name_tokens = path_names.split( "|" )
+ env = path_name_tokens[1]
+ connect_for( env ) do |con|
+ cltn_name = path_name_tokens.pop
+ db_name = path_name_tokens.pop
+ db = con.db( db_name )
+ cltn = db[cltn_name]
+ cltn.drop_index( index )
+ end
+ end
+
+ def create_index( path_names, index, constraints )
+ path_name_tokens = path_names.split( "|" )
+ env = path_name_tokens[1]
+ connect_for( env ) do |con|
+ cltn_name = path_name_tokens.pop
+ db_name = path_name_tokens.pop
+ db = con.db( db_name )
+ cltn = db[cltn_name]
+ cltn.create_index( index, constraints ? constraints['unique'] == 1 : false )
+ end
+ end
+
def drop_cltn( path_names )
path_name_tokens = path_names.split( "|" )
env = path_name_tokens[1]
connect_for( env ) do |con|
cltn_name = path_name_tokens.pop
@@ -73,11 +120,11 @@
db = con.db( db_name )
info[:links][:manage] = "/databases/1"
# info[:links][:drop] = "/databases/drop/"
info[:size] = to_mb( con.database_info[db_name] )
info[:node] = db.nodes
- info[:collections] = db.collection_names.size
+ info[:collections] = collection_names( db ).size
info[:error] = db.error
info[:last_status] = db.last_status
end
elsif path_name_tokens.size == 4
info[:links] = OrderedHash.new
@@ -102,11 +149,11 @@
env = path_name_tokens[1]
list = nil
connect_for( env ) do |con|
db_name = path_name_tokens.pop
db = con.db( db_name )
- cltn = db.collection_names.sort
+ cltn = collection_names(db).sort
list = WillPaginate::Collection.create( page, per_page, cltn.size ) do |pager|
offset = (page-1)*per_page
names = cltn[offset..(offset+per_page)]
cltns = []
@@ -129,19 +176,21 @@
list = nil
connect_for( env ) do |con|
cltn_name = path_name_tokens.pop
db_name = path_name_tokens.pop
db = con.db( db_name )
- cltn = db[cltn_name]
+ cltn = db[cltn_name]
+ count = cltn.find( query_params.first ).count
- list = WillPaginate::Collection.create( page, per_page, cltn.count ) do |pager|
+ list = WillPaginate::Collection.create( page, per_page, count ) do |pager|
offset = (page-1)*per_page
- sort = query_params.last.empty? ? [ ['_id', Mongo::DESCENDING] ] : query_params.last
- pager.replace( cltn.find( query_params.first,
+ sort = query_params.last.empty? ? [ ['_id', Mongo::DESCENDING] ] : query_params.last
+ results = cltn.find( query_params.first,
:sort => sort,
:skip => offset,
- :limit => per_page ).to_a)
+ :limit => per_page ).to_a
+ pager.replace( results )
end
end
list
end
@@ -175,30 +224,31 @@
# iterate thru envs
config.each_pair do |env, info|
node = Node.new( env, env, :dyna => true )
root << node
- if node.name == bm_env
- connect_for( env ) do |con|
- count = 0
- data = { :dyna => true }
- con.database_names.each do |db_name|
- db = con.db( db_name, :strict => true )
- cltns = db.collection_names.size
- db_node = Node.new( "#{env}_#{count}", "#{db_name}(#{cltns})", data.clone )
- node << db_node
- count += 1
- if bm_db and db_node.name =~ /^#{bm_db}/
- cltn_count = 0
- data = { :dyna => false }
- db.collection_names.each do |cltn_name|
- size = db[cltn_name].count
- cltn_node = Node.new( "#{db_name}_#{cltn_count}", "#{cltn_name}(#{size})", data.clone )
- db_node << cltn_node
- cltn_count += 1
- end
- end
+
+ next unless node.name == bm_env
+
+ connect_for( env ) do |con|
+ count = 0
+ data = { :dyna => true }
+ database_names( con ).each do |db_name|
+ db = con.db( db_name, :strict => true )
+ cltns = collection_names( db )
+ db_node = Node.new( "#{env}_#{count}", "#{db_name}(#{cltns.size})", data.clone )
+ node << db_node
+ count += 1
+ if bm_db and db_node.name =~ /^#{bm_db}/
+ cltn_count = 0
+ data = { :dyna => false }
+ cltns.each do |cltn_name|
+ size = db[cltn_name].count
+ cltn_node = Node.new( "#{db_name}_#{cltn_count}", "#{cltn_name}(#{size})", data.clone )
+ db_node << cltn_node
+ cltn_count += 1
+ end
end
end
end
end
root
@@ -207,17 +257,17 @@
# Build an appropriate subtree based on requested item
def build_sub_tree( parent_id, path_names )
path_name_tokens = path_names.split( "|" )
env = path_name_tokens[1]
- if db_request?( path_name_tokens )
+ if db_request?( path_name_tokens )
sub_tree = build_db_tree( parent_id, env )
else
db_name = path_name_tokens.last
sub_tree = build_cltn_tree( parent_id, env, db_name )
end
- sub_tree.to_adjacencies
+ sub_tree
end
# Connects to host and spews out all available dbs
# BOZO !! Need to deal with Auth?
def build_db_tree( parent_id, env )
@@ -228,13 +278,13 @@
root << sub_root
count = 0
data = { :dyna => true }
- con.database_names.each do |db_name|
+ database_names( con ).each do |db_name|
db = con.db( db_name, :strict => true )
- cltns = db.collection_names.size
+ cltns = collection_names( db ).size
node = Node.new( "#{env}_#{count}", "#{db_name}(#{cltns})", data.clone )
sub_root << node
count += 1
end
end
@@ -252,11 +302,11 @@
root << env_node
env_node << sub_root
count = 0
data = { :dyna => false }
- db.collection_names.each do |cltn_name|
+ collection_names( db ).each do |cltn_name|
size = db[cltn_name].count
node = Node.new( "#{db_name}_#{count}", "#{cltn_name}(#{size})", data.clone )
sub_root << node
count += 1
end
@@ -265,15 +315,26 @@
end
# =========================================================================
private
+ def collection_names( db )
+ excludes = %w[system.indexes]
+ db.collection_names - excludes
+ end
+
+ # Filters out system dbs
+ def database_names( con )
+ excludes = %w[admin local slave]
+ con.database_names - excludes
+ end
+
# Connects to mongo given an environment
# BOZO !! Auth...
def connect_for( env, &block )
info = landscape[env]
puts ">>> Connecting for #{env} -- #{info['host']}-#{info['port']}"
- con = Mongo::Connection.new( info['host'], info['port'] )
+ con = Mongo::Connection.new( info['host'], info['port'], { :slave_ok => true } )
if info['user'] and info['password']
con.db( 'admin' ).authenticate( info['user'], info['password'] )
end
yield con
\ No newline at end of file