Class: Mongo3::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo3/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(config_file) ⇒ Connection

Returns a new instance of Connection.



4
5
6
# File 'lib/mongo3/connection.rb', line 4

def initialize( config_file )
  @config_file = config_file
end

Instance Method Details

#build_cltn_tree(parent_id, env, db_name) ⇒ Object

Show collections



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/mongo3/connection.rb', line 245

def build_cltn_tree( parent_id, env, db_name ) 
  sub_root = nil
  connect_for( env ) do |con|
    db        = con.db( db_name )      
    root      = Node.make_node( "home" )
    env_node  = Node.make_node( env )
    sub_root  = Node.new( parent_id, db_name )
    root     << env_node
    env_node << sub_root
  
    count = 0
    data = { :dyna => false }
    db.collection_names.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
  end
  sub_root
end

#build_db_tree(parent_id, env) ⇒ Object

Connects to host and spews out all available dbs BOZO !! Need to deal with Auth?



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/mongo3/connection.rb', line 223

def build_db_tree( parent_id, env )    
  sub_root = nil
  connect_for( env ) do |con|      
    root = Node.make_node( "home" )
    sub_root = Node.new( parent_id, env )
  
    root << sub_root
  
    count = 0
    data  = { :dyna => true }
    con.database_names.each do |db_name|
      db    = con.db( db_name, :strict => true )
      cltns = db.collection_names.size  
      node  = Node.new( "#{env}_#{count}", "#{db_name}(#{cltns})", data.clone )
      sub_root << node
      count += 1
    end
  end
  sub_root
end

#build_partial_tree(path_names) ⇒ Object

Build environment tree



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/mongo3/connection.rb', line 168

def build_partial_tree( path_names )
  path_name_tokens = path_names.split( "|" )      
  bm_env           = path_name_tokens[1]
  bm_cltn          = path_name_tokens.pop if path_name_tokens.size == 4
  bm_db            = path_name_tokens.pop if path_name_tokens.size == 3
  
  root = Node.make_node( "home" )
  
  # 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
        end
      end
    end
  end
  root
end

#build_sub_tree(parent_id, path_names) ⇒ Object

Build an appropriate subtree based on requested item



208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/mongo3/connection.rb', line 208

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 )
    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
end

#build_treeObject

Build environment tree



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/mongo3/connection.rb', line 154

def build_tree
  root = Node.make_node( "home" )
  
  # iterate thru envs
  id = 1
  config.each_pair do |env, info|
    node = Node.new( env, env, :dyna => true )
    root << node
    id += 1
  end
  root
end

#clear_cltn(path_names) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mongo3/connection.rb', line 29

def clear_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
    db_name   = path_name_tokens.pop
    db        = con.db( db_name )
    cltn      = db[cltn_name]
    cltn.remove
  end
end

#delete_row(path_names, id) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mongo3/connection.rb', line 41

def delete_row( path_names, id )
  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.remove( {:_id => Mongo::ObjectID.from_string(id) } )
  end
end

#drop_cltn(path_names) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mongo3/connection.rb', line 17

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
    db_name   = path_name_tokens.pop
    db        = con.db( db_name )
    cltn      = db[cltn_name]
    cltn.drop
  end
end

#drop_db(path_names) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/mongo3/connection.rb', line 8

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

#landscapeObject

Fetch the environment landscape from the config file



149
150
151
# File 'lib/mongo3/connection.rb', line 149

def landscape
  config
end

#paginate_cltn(path_names, query_params = [{},[]], page = 1, per_page = 10) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/mongo3/connection.rb', line 126

def paginate_cltn( path_names, query_params=[{},[]], page=1, per_page=10 )
  path_name_tokens = path_names.split( "|" )
  env              = path_name_tokens[1]
  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]
    
    list = WillPaginate::Collection.create( page, per_page, cltn.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  => sort,
        :skip  => offset, 
        :limit => per_page ).to_a)
    end        
  end
  list
end

#paginate_db(path_names, page = 1, per_page = 10) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/mongo3/connection.rb', line 100

def paginate_db( path_names, page=1, per_page=10 )
  path_name_tokens = path_names.split( "|" )
  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
    
    list = WillPaginate::Collection.create( page, per_page, cltn.size ) do |pager|
      offset = (page-1)*per_page
      names = cltn[offset..(offset+per_page)]
      cltns = []
      names.each do |name|
        list = db[name]
        row  = OrderedHash.new
        row[:name]  = name
        row[:count] = list.count
        cltns << row
      end          
      pager.replace( cltns ) 
    end        
  end
  list
end

#show(path_names) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mongo3/connection.rb', line 53

def show( path_names )
  path_name_tokens = path_names.split( "|" )
  info             = OrderedHash.new
  env              = path_name_tokens[1]
  
  info[:title] = path_name_tokens.last
  if path_name_tokens.size == 2
    connect_for( env ) do |con|
      info[:name]        = env
      info[:host]        = con.host
      info[:port]        = con.port
      info[:databases]   = OrderedHash.new
      con.database_info.sort { |a,b| b[1] <=> a[1] }.each { |e| info[:databases][e[0]] = to_mb( e[1] ) }
      info[:server] = con.server_info
    end
  # BOZO !! Need to figure out links strategy!
  elsif path_name_tokens.size == 3
    db_name = path_name_tokens.pop
    info[:links] = OrderedHash.new
    connect_for( env ) do |con|          
      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[:error]       = db.error
      info[:last_status] = db.last_status
    end
  elsif path_name_tokens.size == 4
    info[:links] = OrderedHash.new        
    cltn_name = path_name_tokens.pop
    db_name   = path_name_tokens.pop
    connect_for( env ) do |con|
      db      = con.db( db_name )
      cltn    = db[cltn_name]
      indexes = db.index_information( cltn_name )
                
      info[:links][:manage] = "/collections/1"
      # info[:links][:drop]   = "/collections/drop/"          
      info[:size]           = cltn.count
      info[:indexes]        = format_indexes( indexes ) if indexes and !indexes.empty?
    end
  end      
  info
end