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 def method_missing(method, *args) db.send(method, *args) end end end