2008-11-22 01:00:51 +00:00
|
|
|
# Copyright (C) 2008 10gen Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Affero General Public License, version 3, as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
|
|
|
# for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
require 'socket'
|
|
|
|
require 'mongo/collection'
|
|
|
|
require 'mongo/message'
|
|
|
|
require 'mongo/query'
|
2008-12-08 16:38:42 +00:00
|
|
|
require 'mongo/util/ordered_hash.rb'
|
2008-11-22 01:00:51 +00:00
|
|
|
|
|
|
|
module XGen
|
|
|
|
module Mongo
|
|
|
|
module Driver
|
|
|
|
|
|
|
|
class DB
|
|
|
|
SYSTEM_NAMESPACE_COLLECTION = "system.namespaces"
|
|
|
|
SYSTEM_INDEX_COLLECTION = "system.indexes"
|
|
|
|
SYSTEM_COMMAND_COLLECTION = "$cmd"
|
|
|
|
|
|
|
|
attr_reader :name, :socket
|
|
|
|
|
|
|
|
def initialize(db_name, host, port)
|
|
|
|
raise "Invalid DB name" if !db_name || (db_name && db_name.length > 0 && db_name.include?("."))
|
|
|
|
@name, @host, @port = db_name, host, port
|
|
|
|
@socket = TCPSocket.new(@host, @port)
|
|
|
|
end
|
|
|
|
|
|
|
|
def collection_names
|
|
|
|
names = collections_info.collect { |doc| doc['name'] || '' }
|
|
|
|
names.delete('')
|
|
|
|
names
|
|
|
|
end
|
|
|
|
|
|
|
|
def collections_info(coll_name=nil)
|
|
|
|
selector = {}
|
|
|
|
selector[:name] = "#{@name}.#{coll_name}" if coll_name
|
|
|
|
query(SYSTEM_NAMESPACE_COLLECTION, Query.new(selector))
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_collection(name, options={})
|
|
|
|
# First check existence
|
|
|
|
return Collection.new(self, name) if collection_names.include?(name)
|
|
|
|
|
|
|
|
# Create new collection
|
2008-12-08 16:38:42 +00:00
|
|
|
oh = OrderedHash.new
|
|
|
|
oh[:create] = name
|
|
|
|
doc = db_command(oh.merge(options))
|
2008-11-22 01:00:51 +00:00
|
|
|
o = doc['ok']
|
|
|
|
return Collection.new(self, name) if o.kind_of?(Numeric) && (o.to_i == 1 || o.to_i == 0)
|
|
|
|
raise "Error creating collection: #{doc.inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def admin
|
2008-12-02 12:22:32 +00:00
|
|
|
# TODO
|
|
|
|
raise "not implemented"
|
2008-11-22 01:00:51 +00:00
|
|
|
Admin.new(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def collection(name)
|
2008-12-02 12:20:29 +00:00
|
|
|
# We do not implement the Java driver's optional strict mode, which
|
|
|
|
# throws an exception if the collection does not exist.
|
2008-11-22 01:00:51 +00:00
|
|
|
create_collection(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def drop_collection(name)
|
|
|
|
coll = collection(name)
|
|
|
|
return true if coll == nil
|
2008-12-02 12:20:29 +00:00
|
|
|
coll.drop_indexes # Mongo requires that we drop indexes manually
|
2008-11-22 01:00:51 +00:00
|
|
|
|
|
|
|
doc = db_command(:drop => name)
|
2008-12-02 12:20:29 +00:00
|
|
|
o = doc['ok']
|
2008-11-22 01:00:51 +00:00
|
|
|
return o.kind_of?(Numeric) && o.to_i == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
@socket.close
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_message(msg)
|
|
|
|
send_to_db(MsgMessage.new(msg))
|
|
|
|
end
|
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
def query(collection_name, query)
|
2008-11-22 01:00:51 +00:00
|
|
|
# TODO synchronize
|
2008-12-08 16:38:42 +00:00
|
|
|
send_to_db(QueryMessage.new(@name, collection_name, query))
|
|
|
|
return Cursor.new(self, collection_name)
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
def remove_from_db(collection_name, selector)
|
2008-11-22 01:00:51 +00:00
|
|
|
# TODO synchronize
|
2008-12-08 16:38:42 +00:00
|
|
|
send_to_db(RemoveMessage.new(@name, collection_name, selector))
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
def replace_in_db(collection_name, selector, obj)
|
2008-11-22 01:00:51 +00:00
|
|
|
# TODO synchronize
|
2008-12-08 16:38:42 +00:00
|
|
|
send_to_db(UpdateMessage.new(@name, collection_name, selector, obj, false))
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
alias_method :modify_in_db, :replace_in_db
|
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
def repsert_in_db(collection_name, selector, obj)
|
2008-11-22 01:00:51 +00:00
|
|
|
# TODO if PKInjector, inject
|
|
|
|
# TODO synchronize
|
2008-12-08 16:38:42 +00:00
|
|
|
send_to_db(UpdateMessage.new(@name, collection_name, selector, obj, true))
|
2008-11-22 01:00:51 +00:00
|
|
|
obj
|
|
|
|
end
|
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
def count(collection_name, selector)
|
|
|
|
oh = OrderedHash.new
|
|
|
|
oh[:count] = collection_name
|
|
|
|
oh[:query] = selector
|
|
|
|
doc = db_command(oh)
|
2008-11-22 01:00:51 +00:00
|
|
|
o = doc['ok']
|
|
|
|
return doc['n'].to_i if o.to_i == 1
|
2008-12-02 00:36:20 +00:00
|
|
|
raise "Error with count command: #{doc.inspect}"
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
def drop_index(collection_name, name)
|
|
|
|
oh = OrderedHash.new
|
|
|
|
oh[:deleteIndexes] = collection_name
|
|
|
|
oh[:index] = name
|
|
|
|
doc = db_command(oh)
|
|
|
|
o = doc['ok']
|
|
|
|
raise "Error with drop_index command: #{doc.inspect}" unless o.kind_of?(Numeric) && o.to_i == 1
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
def index_information(collection_name)
|
|
|
|
sel = {:ns => full_coll_name(collection_name)}
|
2008-11-22 01:00:51 +00:00
|
|
|
# TODO synchronize
|
|
|
|
query(SYSTEM_INDEX_COLLECTION, Query.new(sel)).collect { |row|
|
|
|
|
h = {:name => row['name']}
|
2008-12-08 16:38:42 +00:00
|
|
|
raise "Name of index on return from db was nil. Coll = #{full_coll_name(collection_name)}" unless h[:name]
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2008-12-08 13:33:29 +00:00
|
|
|
h[:keys] = row['key']
|
2008-12-08 16:38:42 +00:00
|
|
|
raise "Keys for index on return from db was nil. Coll = #{full_coll_name(collection_name)}" unless h[:keys]
|
2008-11-22 01:00:51 +00:00
|
|
|
|
|
|
|
h[:ns] = row['ns']
|
2008-12-08 16:38:42 +00:00
|
|
|
raise "Namespace for index on return from db was nil. Coll = #{full_coll_name(collection_name)}" unless h[:ns]
|
2008-11-22 01:00:51 +00:00
|
|
|
h[:ns].sub!(/.*\./, '')
|
2008-12-08 16:38:42 +00:00
|
|
|
raise "Error: ns != collection" unless h[:ns] == collection_name
|
2008-11-22 01:00:51 +00:00
|
|
|
|
|
|
|
h
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
def create_index(collection_name, index_name, fields)
|
|
|
|
sel = {:name => index_name, :ns => full_coll_name(collection_name)}
|
2008-11-22 01:00:51 +00:00
|
|
|
field_h = {}
|
|
|
|
fields.each { |f| field_h[f] = 1 }
|
2008-12-08 16:38:42 +00:00
|
|
|
sel[:key] = field_h
|
2008-11-22 01:00:51 +00:00
|
|
|
# TODO synchronize
|
|
|
|
send_to_db(InsertMessage.new(@name, SYSTEM_INDEX_COLLECTION, sel))
|
|
|
|
end
|
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
def insert_into_db(collection_name, objects)
|
2008-11-22 01:00:51 +00:00
|
|
|
# TODO synchronize
|
2008-12-08 16:38:42 +00:00
|
|
|
objects.each { |o| send_to_db(InsertMessage.new(@name, collection_name, o)) }
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def send_to_db(message)
|
|
|
|
@socket.print(message.buf.to_s)
|
|
|
|
end
|
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
def full_coll_name(collection_name)
|
|
|
|
"#{@name}.#{collection_name}"
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
|
2008-12-02 12:26:45 +00:00
|
|
|
protected
|
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
# DB commands need to be ordered, so selector must be an OrderedHash
|
|
|
|
# (or a Hash with only one element). What DB commands really need is
|
|
|
|
# that the "command" key be first.
|
2008-11-22 01:00:51 +00:00
|
|
|
def db_command(selector)
|
2008-12-08 16:38:42 +00:00
|
|
|
if !selector.kind_of?(OrderedHash)
|
|
|
|
if !selector.kind_of?(Hash) || selector.keys.length > 1
|
|
|
|
raise "db_command must be given an OrderedHash when there is more than one key"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-22 01:00:51 +00:00
|
|
|
# TODO synchronize
|
|
|
|
q = Query.new(selector)
|
|
|
|
q.number_to_return = 1
|
|
|
|
query(SYSTEM_COMMAND_COLLECTION, q).next_object
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|