lib/mongodb_logger/adapters/moped.rb in mongodb_logger-0.4.2 vs lib/mongodb_logger/adapters/moped.rb in mongodb_logger-0.5.0

- old
+ new

@@ -1,9 +1,9 @@ module MongodbLogger module Adapers class Moped < Base - + def initialize(options = {}) @configuration = options if @configuration['url'] uri = URI.parse(@configuration['url']) @configuration['database'] = uri.path.gsub(/^\//, '') @@ -18,33 +18,39 @@ @authenticated = @connection.login(@configuration['username'], @configuration['password']) end end end - + def create_collection @connection.command(create: collection_name, capped: true, size: @configuration['capsize'].to_i) end - + def insert_log_record(record, options = {}) record[:_id] = ::Moped::BSON::ObjectId.new @connection.with(safe: options[:write_options])[collection_name].insert(record) end def collection_stats collection_stats_hash(@connection.command(collStats: collection_name)) end - + + def rename_collection(to, drop_target = false) + @connection.with(database: "admin", consistency: :strong) do |session| + rename_collection_command(session, to, drop_target) + end + end + # filter def filter_by_conditions(filter) @collection.find(filter.get_mongo_conditions).sort('$natural' => -1).limit(filter.get_mongo_limit) end - + def find_by_id(id) @collection.find("_id" => ::Moped::BSON::ObjectId.from_string(id)).first end - + def tail_log_from_params(params = {}) logs = [] last_id = nil if params[:log_last_id] && !params[:log_last_id].blank? log_last_id = params[:log_last_id] @@ -55,17 +61,17 @@ logs.reverse! else log = @collection.find.sort('$natural' => -1).first log_last_id = log["_id"].to_s unless log.blank? end - { - :log_last_id => log_last_id, + { + :log_last_id => log_last_id, :time => Time.now.strftime("%F %T"), :logs => logs } end - + def calculate_mapreduce(map, reduce, params = {}) @connection.command( mapreduce: collection_name, map: map, reduce: reduce, @@ -74,11 +80,11 @@ raw: true ).find() end private - + def mongo_connection_object if @configuration['hosts'] conn = ::Moped::Session.new(@configuration['hosts'].map{|(host,port)| "#{host}:#{port}"}, :timeout => 6) @configuration['replica_set'] = true elsif @configuration['url'] @@ -87,9 +93,9 @@ conn = ::Moped::Session.new(["#{@configuration['host']}:#{@configuration['port']}"], :timeout => 6) end @connection_type = conn.class conn end - + end end end \ No newline at end of file