class Oplogreplayer::Mongobridge

Public Class Methods

new(config) click to toggle source
# File lib/oplogreplayer/mongobridge.rb, line 11
def initialize(config)

  setupLogger

  @log.info("Mongo bridge being configured")

  connect_uri = "mongodb://"
  connect_uri += "#{config["username"]}:#{config["password"]}@" if config["username"] and config["password"]
  connect_uri += "#{config["host"]}"
  connect_uri += "?replicaSet=#{config["replicaSet"]}" if config["replicaSet"]

  if config["replicaSet"]
    @_mongoClient = Mongo::MongoReplicaSetClient.from_uri(connect_uri)
    @log.info("Target connection configured. Target is a replica set.")
  else
    @_mongoClient = Mongo::MongoClient.from_uri(connect_uri)
    @log.info("Target connection configured. Target is a single instance.")
  end

  if config["onlyDbs"]
    @filterDbs = config["onlyDbs"].split(",")
    @log.info("Filter Dbs has been configured: #{@filterDbs.to_s}")
  end
end

Public Instance Methods

create_collection(db_name, collection_name, options) click to toggle source
# File lib/oplogreplayer/mongobridge.rb, line 91
def create_collection(db_name, collection_name,  options)
  if @filterDbs and @filterDbs.include? db_name
    @log.debug("create_collection #{db_name} : #{collection_name} : #{options}")
    @_mongoClient.db(db_name).create_collection(collection_name,options)
  end
end
create_index(db_name, collection_name, index_key, options) click to toggle source
# File lib/oplogreplayer/mongobridge.rb, line 77
def create_index(db_name, collection_name, index_key, options)
  if @filterDbs and @filterDbs.include? db_name
    @log.debug("create_index #{db_name}.#{collection_name} : #{index_key} : #{options}")
    @_mongoClient.db(db_name).collection(collection_name).create_index(index_key,options)
  end
end
drop_collection(db_name, collection_name) click to toggle source
# File lib/oplogreplayer/mongobridge.rb, line 97
def drop_collection(db_name, collection_name)
  if @filterDbs and @filterDbs.include? db_name
    @log.debug("drop_collection #{db_name} : #{collection_name}")
    @_mongoClient.db(db_name).drop_collection(collection_name)
  end
end
drop_database(db_name) click to toggle source
# File lib/oplogreplayer/mongobridge.rb, line 111
def drop_database(db_name)
  if @filterDbs and @filterDbs.include? db_name
    @log.debug("drop_database #{db_name}")
    @_mongoClient.drop_database(db_name)
  end
end
drop_index(db_name, collection_name, index_name) click to toggle source
# File lib/oplogreplayer/mongobridge.rb, line 84
def drop_index(db_name, collection_name, index_name)
  if @filterDbs and @filterDbs.include? db_name
    @log.debug("drop_index #{db_name}.#{collection_name} : #{index_name}")
    @_mongoClient.db(db_name).collection(collection_name).drop_index(index_name)
  end
end
getOplogTimestamp() click to toggle source
# File lib/oplogreplayer/mongobridge.rb, line 36
def getOplogTimestamp()
  findStamp = @_mongoClient.db("local").collection("oplog.tracker").find_one()
  if findStamp
    @log.debug("Stamp found: #{findStamp.inspect}")
    # We return timestamp+1 as "timestamp" was already replayed before shutdown.
    findStamp["timestamp"]+1
  else
    @log.debug("No stamp found. That should mean full replay.")
    nil
  end
end
insert(db_name, collection_name, document) click to toggle source
# File lib/oplogreplayer/mongobridge.rb, line 55
def insert(db_name, collection_name, document)
  if @filterDbs and @filterDbs.include? db_name
    @log.debug("insert #{db_name}.#{collection_name} : #{document.inspect}")
    @_mongoClient.db(db_name).collection(collection_name).insert(document)
  end
end
remove(db_name,collection_name, document) click to toggle source
# File lib/oplogreplayer/mongobridge.rb, line 62
def remove(db_name,collection_name, document)
  if @filterDbs and @filterDbs.include? db_name
    @log.debug("remove #{db_name}.#{collection_name} : #{document.inspect}")
    @_mongoClient.db(db_name).collection(collection_name).remove(document)
  end
end
rename_collection(db_name, old_collection_name, new_collection_name) click to toggle source
# File lib/oplogreplayer/mongobridge.rb, line 104
def rename_collection(db_name, old_collection_name, new_collection_name)
  if @filterDbs and @filterDbs.include? db_name
    @log.debug("rename_collection #{db_name} : from #{old_collection_name} to #{new_collection_name}")
    @_mongoClient.db(db_name).rename_collection(old_collection_name,new_collection_name)
  end
end
update(db_name, collection_name,selector,update) click to toggle source
# File lib/oplogreplayer/mongobridge.rb, line 69
def update(db_name, collection_name,selector,update)
  if @filterDbs and @filterDbs.include? db_name
    @log.debug("update #{db_name}.#{collection_name} : #{selector} : #{update}")
    @_mongoClient.db(db_name).collection(collection_name).update(selector,update)
  end
end
update_optime(timestamp) click to toggle source

This is potentially bad - a find and update for every oplog.…

# File lib/oplogreplayer/mongobridge.rb, line 49
def update_optime(timestamp)
  # track what's been written with this - write optime to the fs, perhaps periodically.
  @log.debug("Optime: #{timestamp}")
  @_mongoClient.db("local").collection("oplog.tracker").update({}, {'timestamp' => timestamp}, {:upsert => true})
end

Private Instance Methods

setupLogger() click to toggle source
# File lib/oplogreplayer/mongobridge.rb, line 120
def setupLogger()
  @log = Log4r::Logger.new("Oplogreplay::Mongobridge")
  @log.outputters = Log4r::StdoutOutputter.new(STDERR)
  @log.level = Log4r::INFO
end