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.



9
10
11
# File 'lib/mongo3/connection.rb', line 9

def initialize( config_file )
  @config_file = config_file
end

Instance Method Details

#build_cltn_tree(parent_id, zone, db_name) ⇒ Object

Show collections



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/mongo3/connection.rb', line 372

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

#build_db_tree(parent_id, zone) ⇒ Object

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



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/mongo3/connection.rb', line 350

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

#build_partial_tree(path_names) ⇒ Object

Build zone tree



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/mongo3/connection.rb', line 269

def build_partial_tree( path_names )
  path_name_tokens = path_names.split( "|" )      
  bm_zone          = 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 zones
  adjacencies = {}      
  config.each_pair do |zone, info|
    node = Node.new( zone, zone, :dyna => true )
    root << node
    
    adjs = adjacencies[zone]
    if adjs
      node.mark_master!
      adjs.each { |n| node << n }
    end
    masters = slave?( zone )
    
    unless masters.empty?
      node.mark_slave!     
      masters.each do |master|
        host, port  = master.split( ":" )
        master_zone = zone_for( host, port )
        next unless master_zone
        master_node = root.find( master_zone )
        if master_node
          master_node.mark_master!
          master_node << node
        else
          adjacencies[master_zone] = [] unless adjacencies[master_zone]
          adjacencies[master_zone] << node
        end
      end
    end
            
    next unless node.name == bm_zone
            
    connect_for( zone ) 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( "#{zone}_#{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
end

#build_sub_tree(parent_id, path_names) ⇒ Object

Build an appropriate subtree based on requested item



335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/mongo3/connection.rb', line 335

def build_sub_tree( parent_id, path_names )
  path_name_tokens = path_names.split( "|" )
  zone              = path_name_tokens[1]      
        
  if db_request?( path_name_tokens )        
    sub_tree = build_db_tree( parent_id, zone )
  else
    db_name  = path_name_tokens.last        
    sub_tree = build_cltn_tree( parent_id, zone, db_name )
  end
  sub_tree
end

#build_treeObject

Build zone tree



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/mongo3/connection.rb', line 218

def build_tree
  root = Node.make_node( "home" )
  
  # iterate thru zones
  adjacencies = {}
  config.each_pair do |zone, info|
    node = Node.new( zone, zone, :dyna => true )
    root << node
    adjs = adjacencies[zone]
    if adjs
      node.mark_master!
      adjs.each { |n| node << n }
    end
    masters = slave?( zone )
    next if masters.empty?

    node.mark_slave!     
    masters.each do |master|
      host, port = master.split( ":" )
      master_zone = zone_for( host, port )
      next unless master_zone
      master_node = root.find( master_zone )
      if master_node
        master_node.mark_master!
        master_node << node
      else
        adjacencies[master_zone] = [] unless adjacencies[master_zone]
        adjacencies[master_zone] << node
      end
    end
  end
  root
end

#clear_cltn(path_names) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mongo3/connection.rb', line 73

def clear_cltn( path_names )
  path_name_tokens = path_names.split( "|" )
  zone              = path_name_tokens[1]      
  connect_for( zone ) 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

#create_index(path_names, index, constraints) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mongo3/connection.rb', line 49

def create_index( path_names, index, constraints )
  path_name_tokens = path_names.split( "|" )
  zone              = path_name_tokens[1]      
  connect_for( zone ) 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 )
  end      
end

#delete_row(path_names, id) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/mongo3/connection.rb', line 85

def delete_row( path_names, id )
  path_name_tokens = path_names.split( "|" )
  zone              = path_name_tokens[1]      
  connect_for( zone ) do |con|
    cltn_name = path_name_tokens.pop
    db_name   = path_name_tokens.pop
    db        = con.db( db_name )
    cltn      = db[cltn_name]
    res = cltn.remove( {:_id => BSON::ObjectID.from_string(id) } )
  end
end

#drop_cltn(path_names) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mongo3/connection.rb', line 61

def drop_cltn( path_names )
  path_name_tokens = path_names.split( "|" )
  zone              = path_name_tokens[1]      
  connect_for( zone ) 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

drop a db using a db path



14
15
16
17
18
19
20
21
# File 'lib/mongo3/connection.rb', line 14

def drop_db( path_names )
  path_name_tokens = path_names.split( "|" )
  zone              = path_name_tokens[1]
  connect_for( zone ) do |con|
    db_name   = path_name_tokens.pop
    con.drop_database( db_name )
  end
end

#drop_index(path_names, index) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mongo3/connection.rb', line 37

def drop_index( path_names, index )
  path_name_tokens = path_names.split( "|" )
  zone              = path_name_tokens[1]
  connect_for( zone ) 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

#indexes_for(path_names) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mongo3/connection.rb', line 23

def indexes_for( path_names )
  path_name_tokens = path_names.split( "|" )
  zone              = path_name_tokens[1]
  indexes          = {}
  connect_for( zone ) 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

#landscapeObject

Fetch the cluster landscape from the config file



213
214
215
# File 'lib/mongo3/connection.rb', line 213

def landscape
  config
end

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



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
206
207
208
209
210
# File 'lib/mongo3/connection.rb', line 179

def paginate_cltn( path_names, query_params=[{},[]], page=1, per_page=10 )
  path_name_tokens = path_names.split( "|" )
  zone              = path_name_tokens[1]
  list             = nil
  connect_for( zone ) do |con|
    cltn_name = path_name_tokens.pop
    db_name   = path_name_tokens.pop
    db        = con.db( db_name )
    cltn      = db[cltn_name]          
    count     = cltn.find( query_params.first ).count
puts "Count #{count} -- #{query_params.first}"        
    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
      query  = query_params.first
      
      # Scan for regexes...
      query.each_pair do |k,v|
        if v.is_a?( String ) and v.index( /^\// )
          query[k] = Regexp.new( v.gsub( "/", '' ) )
        end
      end
      results = cltn.find( query, 
        :sort  => sort,
        :skip  => offset, 
        :limit => per_page ).to_a   
puts "RESULTS #{results.count}"                   
      pager.replace( results )
    end        
  end
  list
end

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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/mongo3/connection.rb', line 153

def paginate_db( path_names, page=1, per_page=10 )
  path_name_tokens = path_names.split( "|" )      
  zone              = path_name_tokens[1]
  list             = nil
  connect_for( zone ) do |con|
    db_name = path_name_tokens.pop
    db      = con.db( db_name )
    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 = []
      names.each do |name|
        list = db[name]
        row  = BSON::OrderedHash.new
        row[:name]  = name
        row[:count] = list.count
        cltns << row
      end          
      pager.replace( cltns ) 
    end        
  end
  list
end

#show(path_names) ⇒ Object



97
98
99
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/mongo3/connection.rb', line 97

def show( path_names )
  path_name_tokens = path_names.split( "|" )
  info             = BSON::OrderedHash.new
  zone             = path_name_tokens[1]

  info[:links] = BSON::OrderedHash.new      
  info[:title] = path_name_tokens.last
        
  # If detect slave only show reg info
  slave = slave_zone?( path_name_tokens )
  if path_name_tokens.size == 2 or slave
    connect_for( zone ) do |con|
      info[:links][:users]  = "/users/1" unless slave
      info[:name]           = zone
      info[:host]           = con.host
      info[:users]          = con.db('admin')[Mongo::DB::SYSTEM_USER_COLLECTION].count rescue 0
      info[:port]           = con.port
      info[:databases]      = BSON::OrderedHash.new
      begin
        con.database_info.sort { |a,b| b[1] <=> a[1] }.each { |e| info[:databases][e[0]] = to_mb( e[1] ) }
      rescue 
        # some instances won't allow to snif. Hence no info
      end
      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
    connect_for( zone ) do |con|          
      db = con.db( db_name )
      info[:links][:manage] = "/databases/1"
      info.merge!(db.stats)
      info['dataSize']     = to_mb( info['dataSize'] )
      info['storageSize']  = to_mb( info['storageSize'] )
      info['avgObjSize']   = to_mb( info['avgObjSize'] )
      info['fileSize']     = to_mb( info['fileSize'] )
      info['indexSize']    = to_mb( info['indexSize'] )
      info.delete('ok')     
      info[:errors]        = db.error?
    end
  elsif path_name_tokens.size == 4        
    cltn_name = path_name_tokens.pop
    db_name   = path_name_tokens.pop
    connect_for( zone ) do |con|
      db      = con.db( db_name )
      cltn    = db[cltn_name]
      indexes = db.index_information( cltn_name )
                
      info[:links][:manage] = "/collections/1"
      info[:size]           = cltn.count
      info[:indexes]        = format_indexes( indexes ) if indexes and !indexes.empty?
    end
  end      
  info
end

#slave?(zone) ⇒ Boolean

Returns:

  • (Boolean)


252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/mongo3/connection.rb', line 252

def slave?( zone )
  masters = []
  connect_for( zone ) do |con|
    local = con.db( "local", :strict => true )
    return masters unless local
    begin
      sources = local['sources']
      srcs    = sources.find( {}, :fields => [:host] )
      srcs.each{ |src| masters << src['host'] }          
    rescue => boom
      ;
    end
  end
  masters
end