def initialize(name, db, opts={})
if db.is_a?(String) && name.is_a?(Mongo::DB)
warn "Warning: the order of parameters to initialize a collection have changed. " +
"Please specify the collection name first, followed by the db. This will be made permanent" +
"in v2.0."
db, name = name, db
end
case name
when Symbol, String
else
raise TypeError, "new_name must be a string or symbol"
end
name = name.to_s
if name.empty? or name.include? ".."
raise Mongo::InvalidNSName, "collection names cannot be empty"
end
if name.include? "$"
raise Mongo::InvalidNSName, "collection names must not contain '$'" unless name =~ /((^\$cmd)|(oplog\.\$main))/
end
if name.match(/^\./) or name.match(/\.$/)
raise Mongo::InvalidNSName, "collection names must not start or end with '.'"
end
if opts.respond_to?(:create_pk) || !opts.is_a?(Hash)
warn "The method for specifying a primary key factory on a Collection has changed.\n" +
"Please specify it as an option (e.g., :pk => PkFactory)."
pk_factory = opts
else
pk_factory = nil
end
@db, @name = db, name
@connection = @db.connection
@logger = @connection.logger
@cache_time = @db.cache_time
@cache = Hash.new(0)
unless pk_factory
@write_concern = get_write_concern(opts, db)
@read = opts[:read] || @db.read
Mongo::ReadPreference::validate(@read)
@tag_sets = opts.fetch(:tag_sets, @db.tag_sets)
@acceptable_latency = opts.fetch(:acceptable_latency, @db.acceptable_latency)
end
@pk_factory = pk_factory || opts[:pk] || BSON::ObjectId
@hint = nil
end