Class: MongodbLogger::Adapers::Mongo

Inherits:
Base
  • Object
show all
Defined in:
lib/mongodb_logger/adapters/mongo.rb

Instance Attribute Summary

Attributes inherited from Base

#authenticated, #collection, #configuration, #connection, #connection_type

Instance Method Summary collapse

Methods inherited from Base

#authenticated?, #check_for_collection, #collection_name, #collection_stats_hash, #rename_collection_command, #reset_collection

Constructor Details

#initialize(options = {}) ⇒ Mongo

Returns a new instance of Mongo.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mongodb_logger/adapters/mongo.rb', line 5

def initialize(options = {})
  @authenticated = false
  @configuration = options
  if @configuration[:url]
    uri = URI.parse(@configuration[:url])
    @configuration[:database] = uri.path.gsub(/^\//, '')
    @connection ||= mongo_connection_object.db(@configuration[:database])
    @authenticated = true
  else
    @connection ||= mongo_connection_object.db(@configuration[:database])
    if @configuration[:username] && @configuration[:password]
      # the driver stores credentials in case reconnection is required
      @authenticated = @connection.authenticate(@configuration[:username],
                                                    @configuration[:password])
    end
  end
end

Instance Method Details

#calculate_mapreduce(map, reduce, params = {}) ⇒ Object



50
51
52
# File 'lib/mongodb_logger/adapters/mongo.rb', line 50

def calculate_mapreduce(map, reduce, params = {})
  @collection.map_reduce(map, reduce, { query: params[:conditions], sort: ['$natural', -1], out: { inline: true }, raw: true }).find()
end

#collection_statsObject



33
34
35
# File 'lib/mongodb_logger/adapters/mongo.rb', line 33

def collection_stats
  collection_stats_hash(@collection.stats)
end

#create_collectionObject



23
24
25
26
# File 'lib/mongodb_logger/adapters/mongo.rb', line 23

def create_collection
  @connection.create_collection(collection_name,
    { capped: true, size: @configuration[:capsize].to_i })
end

#filter_by_conditions(filter) ⇒ Object

filter



42
43
44
# File 'lib/mongodb_logger/adapters/mongo.rb', line 42

def filter_by_conditions(filter)
  @collection.find(filter.get_mongo_conditions).limit(filter.get_mongo_limit).sort('$natural', -1)
end

#find_by_id(id) ⇒ Object



46
47
48
# File 'lib/mongodb_logger/adapters/mongo.rb', line 46

def find_by_id(id)
  @collection.find_one(::BSON::ObjectId.from_string(id))
end

#insert_log_record(record, options = {}) ⇒ Object



28
29
30
31
# File 'lib/mongodb_logger/adapters/mongo.rb', line 28

def insert_log_record(record, options = {})
  record[:_id] = ::BSON::ObjectId.new
  @collection.insert(record, options[:write_options])
end

#rename_collection(to, drop_target = false) ⇒ Object



37
38
39
# File 'lib/mongodb_logger/adapters/mongo.rb', line 37

def rename_collection(to, drop_target = false)
  rename_collection_command(mongo_connection_object.db("admin"), to, drop_target)
end