diff --git a/examples/index_test.rb b/examples/index_test.rb new file mode 100644 index 0000000..39a503d --- /dev/null +++ b/examples/index_test.rb @@ -0,0 +1,128 @@ +require "rubygems" +require "benchwarmer" + +class Exception + def errmsg + "%s: %s\n%s" % [self.class, message, (backtrace || []).join("\n") << "\n"] + end +end + +$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib') +require 'mongo' + +include XGen::Mongo::Driver + +host = ARGV[0] || 'localhost' +port = ARGV[1] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT + +puts ">> Connecting to #{host}:#{port}" +db = Mongo.new(host, port).db('ruby-mongo-index_test') + +puts ">> Dropping collection test" +begin + res = db.drop_collection('test') + puts "dropped : #{res.inspect}" +rescue => e + puts "Error: #{e.errmsg}" +end + +puts ">> Creating collection test" +begin + coll = db.collection('test') + puts "created : #{coll.inspect}" +rescue => e + puts "Error: #{e.errmsg}" +end + +OBJS_COUNT = 100 + +puts ">> Generating test data" +msgs = %w{hola hello aloha ciao} +arr = OBJS_COUNT.times.map {|x| { :number => x, :rndm => (rand(5)+1), :msg => msgs[rand(4)] }} +puts "generated" + +puts ">> Inserting data (#{arr.size})" +coll.insert(arr) +puts "inserted" + +puts ">> Creating index" +res = coll.create_index "all", :_id => 1, :number => 1, :rndm => 1, :msg => 1 +# res = coll.create_index "all", '_id' => 1, 'number' => 1, 'rndm' => 1, 'msg' => 1 +puts "created index: #{res.inspect}" +# ============================ Mongo Log ============================ +# Fri Dec 5 14:45:02 Adding all existing records for ruby-mongo-console.test to new index +# *** +# Bad data or size in BSONElement::size() +# bad type:30 +# totalsize:11 fieldnamesize:4 +# lastrec: +# Fri Dec 5 14:45:02 ruby-mongo-console.system.indexes Assertion failure false jsobj.cpp a0 +# Fri Dec 5 14:45:02 database: ruby-mongo-console op:7d2 0 +# Fri Dec 5 14:45:02 ns: ruby-mongo-console.system.indexes + +puts ">> Gathering index information" +begin + res = coll.index_information + puts "index_information : #{res.inspect}" +rescue => e + puts "Error: #{e.errmsg}" +end +# ============================ Console Output ============================ +# RuntimeError: Keys for index on return from db was nil. Coll = ruby-mongo-console.test +# from ./bin/../lib/mongo/db.rb:135:in `index_information' +# from (irb):11:in `collect' +# from ./bin/../lib/mongo/cursor.rb:47:in `each' +# from ./bin/../lib/mongo/db.rb:130:in `collect' +# from ./bin/../lib/mongo/db.rb:130:in `index_information' +# from ./bin/../lib/mongo/collection.rb:74:in `index_information' +# from (irb):11 + +puts ">> Dropping index" +begin + res = coll.drop_index "all" + puts "dropped : #{res.inspect}" +rescue => e + puts "Error: #{e.errmsg}" +end + +# ============================ Console Output ============================ +# => {"nIndexesWas"=>2.0, "ok"=>1.0} +# ============================ Mongo Log ============================ +# 0x41802a 0x411549 0x42bac6 0x42c1f6 0x42c55b 0x42e6f7 0x41631e 0x41a89d 0x41ade2 0x41b448 0x4650d2 0x4695ad +# db/db(_Z12sayDbContextPKc+0x17a) [0x41802a] +# db/db(_Z8assertedPKcS0_j+0x9) [0x411549] +# db/db(_ZNK11BSONElement4sizeEv+0x1f6) [0x42bac6] +# db/db(_ZN7BSONObj8getFieldEPKc+0xa6) [0x42c1f6] +# db/db(_ZN7BSONObj14getFieldDottedEPKc+0x11b) [0x42c55b] +# db/db(_ZN7BSONObj19extractFieldsDottedES_R14BSONObjBuilder+0x87) [0x42e6f7] +# db/db(_ZN12IndexDetails17getKeysFromObjectER7BSONObjRSt3setIS0_St4lessIS0_ESaIS0_EE+0x24e) [0x41631e] +# db/db(_Z12_indexRecordR12IndexDetailsR7BSONObj7DiskLoc+0x5d) [0x41a89d] +# db/db(_Z18addExistingToIndexPKcR12IndexDetails+0xb2) [0x41ade2] +# db/db(_ZN11DataFileMgr6insertEPKcPKvib+0x508) [0x41b448] +# db/db(_Z14receivedInsertR7MessageRSt18basic_stringstreamIcSt11char_traitsIcESaIcEE+0x112) [0x4650d2] +# db/db(_Z10connThreadv+0xb4d) [0x4695ad] +# Fri Dec 5 14:45:02 ruby-mongo-console.system.indexes Caught Assertion insert, continuing +# Fri Dec 5 14:47:59 CMD: deleteIndexes ruby-mongo-console.test +# d->nIndexes was 2 +# alpha implementation, space not reclaimed + +puts ">> Gathering index information" +begin + res = coll.index_information + puts "index_information : #{res.inspect}" +rescue => e + puts "Error: #{e.errmsg}" +end +# ============================ Console Output ============================ +# RuntimeError: Keys for index on return from db was nil. Coll = ruby-mongo-console.test +# from ./bin/../lib/mongo/db.rb:135:in `index_information' +# from (irb):15:in `collect' +# from ./bin/../lib/mongo/cursor.rb:47:in `each' +# from ./bin/../lib/mongo/db.rb:130:in `collect' +# from ./bin/../lib/mongo/db.rb:130:in `index_information' +# from ./bin/../lib/mongo/collection.rb:74:in `index_information' +# from (irb):15 + +puts ">> Closing connection" +db.close +puts "closed" \ No newline at end of file