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



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/mongo3/connection.rb', line 364

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?



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/mongo3/connection.rb', line 342

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



261
262
263
264
265
266
267
268
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
# File 'lib/mongo3/connection.rb', line 261

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



327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/mongo3/connection.rb', line 327

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



210
211
212
213
214
215
216
217
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
# File 'lib/mongo3/connection.rb', line 210

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 ? constraints['unique'] == 1 : false )
  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 => Mongo::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



205
206
207
# File 'lib/mongo3/connection.rb', line 205

def landscape
  config
end

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



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
# File 'lib/mongo3/connection.rb', line 172

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
    
    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          
      pager.replace( results )
    end        
  end
  list
end

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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/mongo3/connection.rb', line 146

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  = 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
# File 'lib/mongo3/connection.rb', line 97

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

  info[:links] = 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]      = 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
    connect_for( zone ) do |con|          
      db = con.db( db_name )
      info[:links][:manage] = "/databases/1"
      info[:size]           = to_mb( con.database_info[db_name] )
      info[:node]           = db.nodes
      info[:collections]    = collection_names( db ).size
      info[:error]          = db.error
      info[:last_status]    = db.last_status
    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)


244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/mongo3/connection.rb', line 244

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