Sha256: a0d0d3562c0258d793d7d31b55d70df957d9a425da9b3f98084fe407d2b2fc28

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

require 'mongo'

module Ganymed
  # @private
  class MongoDB
    attr_reader :config

    def initialize(config)
      @config = config
      @collections = {}
      log.info("using MongoDB at #{config.host}:#{config.port}/#{config.database}")
    end

    def connection
      @connection ||= ::Mongo::Connection.new(config.host, config.port,
                                              :pool_size => config.pool_size,
                                              :pool_timeout => config.pool_timeout)
    end

    def db
      @db ||= connection.db(config.database)
    end

    def collection(ns)
      return @collections[ns] if @collections.has_key?(ns)
      col = @collections[ns] = db.collection(ns)
      col.ensure_index([['c', ::Mongo::ASCENDING]])
      col.ensure_index([['o', ::Mongo::ASCENDING]])
      col.ensure_index([['t', ::Mongo::ASCENDING]])
      col.ensure_index([['c', ::Mongo::ASCENDING], ['o', ::Mongo::ASCENDING]])
      col.ensure_index([['o', ::Mongo::ASCENDING], ['t', ::Mongo::ASCENDING]])
      col.ensure_index([['c', ::Mongo::ASCENDING], ['o', ::Mongo::ASCENDING], ['t', ::Mongo::ASCENDING]], :unique => true)
      col
    end

    def [](*args)
      db.collection(*args)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ganymed-0.3.2 lib/ganymed/mongodb.rb