mongodb-mongo 0.5.3 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
data/examples/types.rb ADDED
@@ -0,0 +1,40 @@
1
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'mongo'
3
+ require 'pp'
4
+
5
+ include XGen::Mongo::Driver
6
+
7
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
8
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
9
+
10
+ puts "Connecting to #{host}:#{port}"
11
+ db = Mongo.new(host, port).db('ruby-mongo-examples')
12
+ coll = db.collection('test')
13
+
14
+ # Remove all records, if any
15
+ coll.clear
16
+
17
+ # Insert record with all sorts of values
18
+ coll.insert('array' => [1, 2, 3],
19
+ 'string' => 'hello',
20
+ 'hash' => {'a' => 1, 'b' => 2},
21
+ 'date' => Time.now, # milliseconds only; microseconds are not stored
22
+ 'oid' => ObjectID.new,
23
+ 'binary' => Binary.new([1, 2, 3]),
24
+ 'int' => 42,
25
+ 'float' => 33.33333,
26
+ 'regex' => /foobar/i,
27
+ 'boolean' => true,
28
+ '$where' => 'this.x == 3', # special case of string
29
+ 'dbref' => DBRef.new(nil, 'dbref', db, coll.name, ObjectID.new),
30
+
31
+ # NOTE: the undefined type is not saved to the database properly. This is a
32
+ # Mongo bug. However, the undefined type may go away completely.
33
+ # 'undef' => Undefined.new,
34
+
35
+ 'null' => nil,
36
+ 'symbol' => :zildjian)
37
+
38
+ pp coll.find().next_object
39
+
40
+ coll.clear
@@ -26,8 +26,8 @@ module XGen
26
26
  attr_reader :db, :name, :hint
27
27
 
28
28
  def initialize(db, name)
29
- @db = db
30
- @name = name
29
+ @db, @name = db, name
30
+ @hint = nil
31
31
  end
32
32
 
33
33
  # Set hint fields to use and return +self+. hint may be a single field
@@ -65,6 +65,14 @@ module XGen
65
65
  @db.query(self, Query.new(selector, fields, offset, limit, sort, hint))
66
66
  end
67
67
 
68
+ # Find the first record that matches +selector+. See #find.
69
+ def find_first(selector={}, options={})
70
+ h = options.dup
71
+ h[:limit] = 1
72
+ cursor = find(selector, h)
73
+ cursor.next_object # don't need to explicitly close b/c of limit
74
+ end
75
+
68
76
  # Insert +objects+, which are hashes. "<<" is aliased to this method.
69
77
  # Returns either the single inserted object or a new array containing
70
78
  # +objects+. The object(s) may have been modified by the database's PK
data/lib/mongo/cursor.rb CHANGED
@@ -38,6 +38,7 @@ module XGen
38
38
  @closed = false
39
39
  @can_call_to_a = true
40
40
  @query_run = false
41
+ @rows = nil
41
42
  end
42
43
 
43
44
  def closed?; @closed; end
@@ -133,6 +134,10 @@ module XGen
133
134
  end
134
135
 
135
136
  # Close the cursor.
137
+ #
138
+ # Note: if a cursor is read until exhausted (read until OP_QUERY or
139
+ # OP_GETMORE returns zero for the cursor id), there is no need to
140
+ # close it by calling this method.
136
141
  def close
137
142
  @db.send_to_db(KillCursorsMessage.new(@cursor_id)) if @cursor_id
138
143
  @cache = []
data/lib/mongo/db.rb CHANGED
@@ -122,6 +122,7 @@ module XGen
122
122
  @auto_reconnect = options[:auto_reconnect]
123
123
  @semaphore = Object.new
124
124
  @semaphore.extend Mutex_m
125
+ @socket = nil
125
126
  connect_to_master
126
127
  end
127
128
 
@@ -132,6 +133,7 @@ module XGen
132
133
  @host, @port = *hp
133
134
  begin
134
135
  @socket = TCPSocket.new(@host, @port)
136
+ @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
135
137
 
136
138
  # Check for master. Can't call master? because it uses mutex,
137
139
  # which may already be in use during this call.
@@ -8,8 +8,6 @@ module XGen
8
8
  module GridFS
9
9
 
10
10
  # A chunk stores a portion of GridStore data.
11
- #
12
- # TODO: user-defined chunk size
13
11
  class Chunk
14
12
 
15
13
  DEFAULT_CHUNK_SIZE = 1024 * 256
@@ -201,13 +201,13 @@ module XGen
201
201
 
202
202
  def gets(separator=$/)
203
203
  str = ''
204
- byte = getc
204
+ byte = self.getc
205
205
  return nil if byte == nil # EOF
206
206
  while byte != nil
207
207
  s = byte.chr
208
208
  str << s
209
209
  break if s == separator
210
- byte = getc
210
+ byte = self.getc
211
211
  end
212
212
  @lineno += 1
213
213
  str
@@ -215,17 +215,17 @@ module XGen
215
215
 
216
216
  def read(len=nil, buf=nil)
217
217
  buf ||= ''
218
- byte = getc
218
+ byte = self.getc
219
219
  while byte != nil && (len == nil || len > 0)
220
220
  buf << byte.chr
221
221
  len -= 1 if len
222
- byte = getc if (len == nil || len > 0)
222
+ byte = self.getc if (len == nil || len > 0)
223
223
  end
224
224
  buf
225
225
  end
226
226
 
227
227
  def readchar
228
- byte = getc
228
+ byte = self.getc
229
229
  raise EOFError.new if byte == nil
230
230
  byte
231
231
  end
@@ -250,10 +250,10 @@ module XGen
250
250
  alias_method :each_line, :each
251
251
 
252
252
  def each_byte
253
- byte = getc
253
+ byte = self.getc
254
254
  while byte
255
255
  yield byte
256
- byte = getc
256
+ byte = self.getc
257
257
  end
258
258
  end
259
259
 
@@ -280,14 +280,14 @@ module XGen
280
280
  objs = [$_] if objs == nil || objs.empty?
281
281
  objs.each { |obj|
282
282
  str = obj.to_s
283
- str.each_byte { |byte| putc(byte) }
283
+ str.each_byte { |byte| self.putc(byte) }
284
284
  }
285
285
  nil
286
286
  end
287
287
 
288
288
  def puts(*objs)
289
289
  if objs == nil || objs.empty?
290
- putc(10)
290
+ self.putc(10)
291
291
  else
292
292
  print(*objs.collect{ |obj|
293
293
  str = obj.to_s
@@ -307,7 +307,7 @@ module XGen
307
307
  raise "#@filename not opened for write" unless @mode[0] == ?w
308
308
  count = 0
309
309
  string.each_byte { |byte|
310
- putc byte
310
+ self.putc byte
311
311
  count += 1
312
312
  }
313
313
  count
@@ -16,7 +16,7 @@ module XGen
16
16
  write_int(0)
17
17
  write_string("#{db_name}.#{collection_name}")
18
18
  write_int(query.number_to_skip)
19
- write_int(query.number_to_return)
19
+ write_int(-query.number_to_return) # Negative means hard limit
20
20
  sel = query.selector
21
21
  if query.contains_special_fields
22
22
  sel = OrderedHash.new
data/lib/mongo/query.rb CHANGED
@@ -65,6 +65,7 @@ module XGen
65
65
  def initialize(sel={}, return_fields=nil, number_to_skip=0, number_to_return=0, order_by=nil, hint=nil)
66
66
  @number_to_skip, @number_to_return, @order_by, @hint =
67
67
  number_to_skip, number_to_return, order_by, hint
68
+ @explain = nil
68
69
  self.selector = sel
69
70
  self.fields = return_fields
70
71
  end
@@ -64,7 +64,7 @@ module XGen
64
64
  # Given a string representation of an ObjectID, return a new ObjectID
65
65
  # with that value.
66
66
  def self.from_string(str)
67
- raise "illegal ObjectID format" unless legal_oid_string(str)
67
+ raise "illegal ObjectID format" unless legal?(str)
68
68
  data = []
69
69
  BYTE_ORDER.each_with_index { |string_position, data_index|
70
70
  data[data_index] = str[string_position * 2, 2].to_i(16)
@@ -72,7 +72,7 @@ module XGen
72
72
  self.new(data)
73
73
  end
74
74
 
75
- def self.legal_oid_string(str)
75
+ def self.legal?(str)
76
76
  len = BYTE_ORDER.length * 2
77
77
  str =~ /([0-9a-f]+)/i
78
78
  match = $1
@@ -54,7 +54,7 @@ class BSON
54
54
  end
55
55
  else
56
56
  def self.to_utf8(str)
57
- str # TODO punt for now
57
+ str # TODO Ruby 1.8 punt for now
58
58
  end
59
59
  end
60
60
 
@@ -117,7 +117,7 @@ class BSON
117
117
  when UNDEFINED
118
118
  serialize_undefined_element(@buf, k)
119
119
  when CODE_W_SCOPE
120
- # TODO
120
+ # TODO CODE_W_SCOPE unimplemented; may be removed
121
121
  raise "unimplemented type #{type}"
122
122
  else
123
123
  raise "unhandled type #{type}"
@@ -177,7 +177,7 @@ class BSON
177
177
  key = deserialize_cstr(@buf)
178
178
  doc[key] = deserialize_binary_data(@buf)
179
179
  when CODE_W_SCOPE
180
- # TODO
180
+ # TODO CODE_W_SCOPE unimplemented; may be removed
181
181
  raise "unimplemented type #{type}"
182
182
  when EOO
183
183
  break
@@ -393,7 +393,7 @@ class BSON
393
393
 
394
394
  def deserialize_cstr(buf)
395
395
  chars = ""
396
- while 1
396
+ while true
397
397
  b = buf.get
398
398
  break if b == 0
399
399
  chars << b.chr
@@ -1,69 +1,88 @@
1
+ PACKAGE_FILES = ['README.rdoc', 'Rakefile', 'mongo-ruby-driver.gemspec',
2
+ 'bin/mongo_console',
3
+ 'bin/run_test_script',
4
+ 'bin/standard_benchmark',
5
+ 'examples/admin.rb',
6
+ 'examples/benchmarks.rb',
7
+ 'examples/blog.rb',
8
+ 'examples/capped.rb',
9
+ 'examples/cursor.rb',
10
+ 'examples/gridfs.rb',
11
+ 'examples/index_test.rb',
12
+ 'examples/info.rb',
13
+ 'examples/queries.rb',
14
+ 'examples/simple.rb',
15
+ 'examples/strict.rb',
16
+ 'examples/types.rb',
17
+ 'lib/mongo/admin.rb',
18
+ 'lib/mongo/collection.rb',
19
+ 'lib/mongo/cursor.rb',
20
+ 'lib/mongo/db.rb',
21
+ 'lib/mongo/gridfs/chunk.rb',
22
+ 'lib/mongo/gridfs/grid_store.rb',
23
+ 'lib/mongo/gridfs.rb',
24
+ 'lib/mongo/message/get_more_message.rb',
25
+ 'lib/mongo/message/insert_message.rb',
26
+ 'lib/mongo/message/kill_cursors_message.rb',
27
+ 'lib/mongo/message/message.rb',
28
+ 'lib/mongo/message/message_header.rb',
29
+ 'lib/mongo/message/msg_message.rb',
30
+ 'lib/mongo/message/opcodes.rb',
31
+ 'lib/mongo/message/query_message.rb',
32
+ 'lib/mongo/message/remove_message.rb',
33
+ 'lib/mongo/message/update_message.rb',
34
+ 'lib/mongo/message.rb',
35
+ 'lib/mongo/mongo.rb',
36
+ 'lib/mongo/query.rb',
37
+ 'lib/mongo/types/binary.rb',
38
+ 'lib/mongo/types/dbref.rb',
39
+ 'lib/mongo/types/objectid.rb',
40
+ 'lib/mongo/types/regexp_of_holding.rb',
41
+ 'lib/mongo/types/undefined.rb',
42
+ 'lib/mongo/util/bson.rb',
43
+ 'lib/mongo/util/byte_buffer.rb',
44
+ 'lib/mongo/util/ordered_hash.rb',
45
+ 'lib/mongo/util/xml_to_ruby.rb',
46
+ 'lib/mongo.rb']
47
+
48
+ TEST_FILES = ['tests/mongo-qa/_common.rb',
49
+ 'tests/mongo-qa/admin',
50
+ 'tests/mongo-qa/capped',
51
+ 'tests/mongo-qa/count1',
52
+ 'tests/mongo-qa/dbs',
53
+ 'tests/mongo-qa/find',
54
+ 'tests/mongo-qa/find1',
55
+ 'tests/mongo-qa/indices',
56
+ 'tests/mongo-qa/remove',
57
+ 'tests/mongo-qa/stress1',
58
+ 'tests/mongo-qa/test1',
59
+ 'tests/mongo-qa/update',
60
+ 'tests/test_admin.rb',
61
+ 'tests/test_bson.rb',
62
+ 'tests/test_byte_buffer.rb',
63
+ 'tests/test_chunk.rb',
64
+ 'tests/test_cursor.rb',
65
+ 'tests/test_db.rb',
66
+ 'tests/test_db_api.rb',
67
+ 'tests/test_db_connection.rb',
68
+ 'tests/test_grid_store.rb',
69
+ 'tests/test_message.rb',
70
+ 'tests/test_mongo.rb',
71
+ 'tests/test_objectid.rb',
72
+ 'tests/test_ordered_hash.rb',
73
+ 'tests/test_round_trip.rb']
74
+
1
75
  Gem::Specification.new do |s|
2
76
  s.name = 'mongo'
3
- s.version = '0.5.3'
77
+ s.version = '0.6.2'
4
78
  s.platform = Gem::Platform::RUBY
5
79
  s.summary = 'Simple pure-Ruby driver for the 10gen Mongo DB'
6
80
  s.description = 'A pure-Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.'
7
81
 
8
82
  s.require_paths = ['lib']
9
83
 
10
- s.files = ['bin/mongo_console', 'bin/run_test_script',
11
- 'examples/benchmarks.rb',
12
- 'examples/blog.rb',
13
- 'examples/index_test.rb',
14
- 'examples/simple.rb',
15
- 'lib/mongo.rb',
16
- 'lib/mongo/admin.rb',
17
- 'lib/mongo/collection.rb',
18
- 'lib/mongo/cursor.rb',
19
- 'lib/mongo/db.rb',
20
- 'lib/mongo/gridfs/chunk.rb',
21
- 'lib/mongo/gridfs/grid_store.rb',
22
- 'lib/mongo/gridfs.rb',
23
- 'lib/mongo/message/get_more_message.rb',
24
- 'lib/mongo/message/insert_message.rb',
25
- 'lib/mongo/message/kill_cursors_message.rb',
26
- 'lib/mongo/message/message.rb',
27
- 'lib/mongo/message/message_header.rb',
28
- 'lib/mongo/message/msg_message.rb',
29
- 'lib/mongo/message/opcodes.rb',
30
- 'lib/mongo/message/query_message.rb',
31
- 'lib/mongo/message/remove_message.rb',
32
- 'lib/mongo/message/update_message.rb',
33
- 'lib/mongo/message.rb',
34
- 'lib/mongo/mongo.rb',
35
- 'lib/mongo/query.rb',
36
- 'lib/mongo/types/binary.rb',
37
- 'lib/mongo/types/dbref.rb',
38
- 'lib/mongo/types/objectid.rb',
39
- 'lib/mongo/types/regexp_of_holding.rb',
40
- 'lib/mongo/types/undefined.rb',
41
- 'lib/mongo/util/bson.rb',
42
- 'lib/mongo/util/byte_buffer.rb',
43
- 'lib/mongo/util/ordered_hash.rb',
44
- 'lib/mongo/util/xml_to_ruby.rb',
45
- 'README.rdoc', 'Rakefile', 'mongo-ruby-driver.gemspec']
46
- s.test_files = ['tests/mongo-qa/_common.rb',
47
- 'tests/mongo-qa/capped',
48
- 'tests/mongo-qa/circular',
49
- 'tests/mongo-qa/count1',
50
- 'tests/mongo-qa/find',
51
- 'tests/mongo-qa/remove',
52
- 'tests/mongo-qa/test1',
53
- 'tests/test_admin.rb',
54
- 'tests/test_bson.rb',
55
- 'tests/test_byte_buffer.rb',
56
- 'tests/test_chunk.rb',
57
- 'tests/test_cursor.rb',
58
- 'tests/test_db.rb',
59
- 'tests/test_db_api.rb',
60
- 'tests/test_db_connection.rb',
61
- 'tests/test_grid_store.rb',
62
- 'tests/test_message.rb',
63
- 'tests/test_mongo.rb',
64
- 'tests/test_objectid.rb',
65
- 'tests/test_ordered_hash.rb',
66
- 'tests/test_round_trip.rb']
84
+ s.files = PACKAGE_FILES
85
+ s.test_files = TEST_FILES
67
86
 
68
87
  s.has_rdoc = true
69
88
  s.rdoc_options = ['--main', 'README.rdoc', '--inline-source']
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.join(File.dirname(__FILE__), '_common.rb')
4
+ db = Mongo.new(DEFAULT_HOST, DEFAULT_PORT).db(DEFAULT_DB)
5
+
6
+ db.collection('test').insert({'test' => 1})
7
+ admin = db.admin
8
+
9
+ if $DEBUG
10
+ db.drop_collection('tester')
11
+ admin.profiling_level = :slow_only
12
+ end
13
+
14
+ ['test', 'pdlskwmf', '$'].each { |name|
15
+ begin
16
+ admin.validate_collection(name)
17
+ puts 'true'
18
+ rescue => ex
19
+ puts 'false'
20
+ end
21
+ }
22
+
23
+ level_xlation = {:off => 'off', :all => 'all', :slow_only => 'slowOnly'}
24
+ puts level_xlation[admin.profiling_level]
25
+ admin.profiling_level = :off
26
+ puts level_xlation[admin.profiling_level]
@@ -3,6 +3,11 @@
3
3
  require File.join(File.dirname(__FILE__), '_common.rb')
4
4
  db = Mongo.new(DEFAULT_HOST, DEFAULT_PORT).db(DEFAULT_DB)
5
5
 
6
+ if $DEBUG
7
+ db.drop_collection('capped1')
8
+ db.drop_collection('capped2')
9
+ end
10
+
6
11
  db.create_collection('capped1', :capped => true, :size => 500)
7
12
  coll = db.collection('capped1')
8
13
  coll.insert('x' => 1)