Sha256: 0ef1251b4880527e06960c4c53b72cae6b97ba4f0244136968fd056831129c09

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

# encoding: utf-8
require "mongo"

module SimpleMetrics
  module Mongo
    extend self

    def ensure_collections_exist
      SimpleMetrics.logger.debug "SERVER: MongoDB - found following collections: #{db.collection_names.inspect}"
      Bucket.all.each do |bucket|
        unless db.collection_names.include?(bucket.name)
          db.create_collection(bucket.name, :capped => bucket.capped, :size => bucket.size) 
          SimpleMetrics.logger.debug "SERVER: MongoDB - created collection #{bucket.name}, capped: #{bucket.capped}, size: #{bucket.size}"
        end
        
        db.collection(bucket.name).ensure_index([['ts', ::Mongo::ASCENDING]])
        SimpleMetrics.logger.debug "SERVER: MongoDB - ensure index on column ts for collection #{bucket.name}"
      end 
    end

    def truncate_collections
      Bucket.all.each do |bucket|
        if db.collection_names.include?(bucket.name)
          if bucket.capped?
            collection(bucket.name).drop # capped collections can't remove elements, drop it instead
          else
            collection(bucket.name).remove
          end
          SimpleMetrics.logger.debug "SERVER: MongoDB - truncated collection #{bucket.name}"
        end
      end
    end

    @@collection = {}
    def collection(name)
      @@collection[name] ||= db.collection(name)
    end

    def connection
      @@connection ||= ::Mongo::Connection.new(SimpleMetrics.db_config[:host], SimpleMetrics.db_config[:port])
    end

    def db
      @@db ||= connection.db(SimpleMetrics.db_config[:db_name], SimpleMetrics.db_config[:options])
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
simple_metrics-0.4.0 lib/simple_metrics/mongo.rb
simple_metrics-0.2.3 lib/simple_metrics/mongo.rb
simple_metrics-0.2.2 lib/simple_metrics/mongo.rb