mongodb-mongo 0.9 → 0.10

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,11 +1,6 @@
1
1
  = Introduction
2
2
 
3
- This is a Ruby driver for the 10gen Mongo DB. For more information about
4
- Mongo, see http://www.mongodb.org.
5
-
6
- Start by reading the XGen::Mongo::Driver::Mongo and XGen::Mongo::Driver::DB
7
- documentation, then move on to XGen::Mongo::Driver::Collection and
8
- XGen::Mongo::Driver::Cursor.
3
+ This is a Ruby driver for MongoDB[http://www.mongodb.org].
9
4
 
10
5
  Here is a quick code sample. See the files in the "examples" subdirectory for
11
6
  many more.
@@ -23,8 +18,7 @@ many more.
23
18
  coll.find().each { |doc| puts doc.inspect }
24
19
 
25
20
  This driver also includes an implementation of a GridStore class, a Ruby
26
- interface to Mongo's GridFS storage. NOTE: the GridStore code may be moved to
27
- a separate project.
21
+ interface to Mongo's GridFS storage.
28
22
 
29
23
  = Installation
30
24
 
@@ -78,7 +72,6 @@ Here's how to start mongo and run the "simple.rb" example:
78
72
 
79
73
  See also the test code, especially tests/test_db_api.rb.
80
74
 
81
-
82
75
  = The Driver
83
76
 
84
77
  Here is some simple example code:
@@ -143,25 +136,15 @@ string.
143
136
  == Primary Keys
144
137
 
145
138
  The field _id is a primary key. It is treated specially by the database, and
146
- its use makes many operations more efficient.
147
-
148
- The value of an _id may be of any type. (Older versions of Mongo required that
149
- they be XGen::Mongo::Driver::ObjectID instances.)
150
-
151
- The database itself inserts an _id value if none is specified when a record is
152
- inserted.
153
-
154
- The driver automatically sends the _id field to the database first, which is
155
- how Mongo likes it. You don't have to worry about where the _id field is in
156
- your hash record, or worry if you are using an OrderedHash or not.
139
+ its use makes many operations more efficient. The value of an _id may be of
140
+ any type. The database itself inserts an _id value if none is specified when
141
+ a record is inserted.
157
142
 
158
143
  === Primary Key Factories
159
144
 
160
145
  A primary key factory is a class you supply to a DB object that knows how to
161
- generate _id values. Primary key factories are no longer necessary because
162
- Mongo now inserts an _id value for every record that does not already have
163
- one. However, if you want to control _id values or even their types, using a
164
- PK factory lets you do so.
146
+ generate _id values. If you want to control _id values or even their types,
147
+ using a PK factory lets you do so.
165
148
 
166
149
  You can tell the Ruby Mongo driver how to create primary keys by passing in
167
150
  the :pk option to the Mongo#db method.
@@ -173,7 +156,7 @@ A primary key factory object must respond to :create_pk, which should take a
173
156
  hash and return a hash which merges the original hash with any primary key
174
157
  fields the factory wishes to inject. NOTE: if the object already has a primary
175
158
  key, the factory should not inject a new key; this means that the object is
176
- being used in a repsert but it already exists. The idea here is that when ever
159
+ being used in a repsert but it already exists. The idea here is that whenever
177
160
  a record is inserted, the :pk object's +create_pk+ method will be called and
178
161
  the new hash returned will be inserted.
179
162
 
@@ -285,30 +268,6 @@ Then open the file html/index.html.
285
268
 
286
269
  See the git log comments.
287
270
 
288
-
289
- = To Do
290
-
291
- * Tests for update and repsert.
292
-
293
- * Add a way to specify a collection of databases on startup (a simple array of
294
- IP address/port numbers, perhaps, or a hash or something). The driver would
295
- then find the master and, on each subsequent command, ask that machine if it
296
- is the master before proceeding.
297
-
298
- * Introduce optional per-database and per-collection PKInjector.
299
-
300
- * More tests.
301
-
302
- == Optimizations
303
-
304
- * Only update message sizes once, not after every write of a value. This will
305
- require an explicit call to update_message_length in each message subclass.
306
-
307
- * ensure_index commands should be cached to prevent excessive communication
308
- with the database. (Or, the driver user should be informed that ensure_index
309
- is not a lightweight operation for the particular driver.)
310
-
311
-
312
271
  = Credits
313
272
 
314
273
  Adrian Madrid, aemadrid@gmail.com
@@ -331,6 +290,13 @@ John Nunemaker, http://railstips.org
331
290
  * Collection#create_index takes symbols as well as strings
332
291
  * Fix for Collection#save
333
292
 
293
+ David James, djames@sunlightfoundation.com
294
+ * Fix dates to return as UTC
295
+
296
+ Paul Dlug, paul.dlug@gmail.com
297
+ * Generate _id on the client side if not provided
298
+ * Collection#insert and Collection#save return _id
299
+
334
300
  = License
335
301
 
336
302
  Copyright 2008-2009 10gen Inc.
data/lib/mongo/db.rb CHANGED
@@ -475,9 +475,13 @@ module XGen
475
475
  objects.collect! { |o|
476
476
  @pk_factory.create_pk(o)
477
477
  }
478
+ else
479
+ objects = objects.collect do |o|
480
+ o[:_id] || o['_id'] ? o : o.merge(:_id => ObjectID.new)
481
+ end
478
482
  end
479
483
  send_to_db(InsertMessage.new(@name, collection_name, true, *objects))
480
- objects
484
+ objects.collect { |o| o[:_id] || o['_id'] }
481
485
  }
482
486
  end
483
487
 
@@ -93,6 +93,18 @@ module XGen
93
93
  }
94
94
  end
95
95
 
96
+ # List the contains of all GridFS files stored in the given db and
97
+ # root collection.
98
+ #
99
+ # :db :: the database to use
100
+ #
101
+ # :root_collection :: the root collection to use
102
+ def list(db, root_collection=DEFAULT_ROOT_COLLECTION)
103
+ db.collection("#{root_collection}.files").find().map { |f|
104
+ f['filename']
105
+ }
106
+ end
107
+
96
108
  def readlines(db, name, separator=$/)
97
109
  GridStore.open(db, name, 'r') { |gs|
98
110
  gs.readlines(separator)
@@ -245,7 +245,7 @@ class BSON
245
245
 
246
246
  def deserialize_date_data(buf)
247
247
  millisecs = buf.get_long()
248
- Time.at(millisecs.to_f / 1000.0) # at() takes fractional seconds
248
+ Time.at(millisecs.to_f / 1000.0).utc # at() takes fractional seconds
249
249
  end
250
250
 
251
251
  def deserialize_boolean_data(buf)
@@ -79,7 +79,7 @@ TEST_FILES = ['tests/mongo-qa/_common.rb',
79
79
 
80
80
  Gem::Specification.new do |s|
81
81
  s.name = 'mongo'
82
- s.version = '0.9'
82
+ s.version = '0.10'
83
83
  s.platform = Gem::Platform::RUBY
84
84
  s.summary = 'Ruby driver for the 10gen Mongo DB'
85
85
  s.description = 'A Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.'
data/tests/test_bson.rb CHANGED
@@ -117,6 +117,13 @@ class BSONTest < Test::Unit::TestCase
117
117
  assert_in_delta doc['date'], doc2['date'], 0.001
118
118
  end
119
119
 
120
+ def test_date_returns_as_utc
121
+ doc = {'date' => Time.now}
122
+ @b.serialize(doc)
123
+ doc2 = @b.deserialize
124
+ assert doc2['date'].utc?
125
+ end
126
+
120
127
  def test_dbref
121
128
  oid = ObjectID.new
122
129
  doc = {}
data/tests/test_db.rb CHANGED
@@ -66,19 +66,20 @@ class DBTest < Test::Unit::TestCase
66
66
  coll = db.collection('test')
67
67
  coll.clear
68
68
 
69
+ insert_id = coll.insert('name' => 'Fred', 'age' => 42)
69
70
  # new id gets added to returned object
70
- obj = coll.insert('name' => 'Fred', 'age' => 42)
71
71
  row = coll.find_first({'name' => 'Fred'}, :limit => 1)
72
72
  oid = row['_id']
73
73
  assert_not_nil oid
74
- assert_equal obj, row
74
+ assert_equal insert_id, oid
75
75
 
76
76
  oid = XGen::Mongo::Driver::ObjectID.new
77
- obj = coll.insert('_id' => oid, 'name' => 'Barney', 'age' => 41)
78
- row = coll.find_first({'name' => 'Barney'}, :limit => 1)
77
+ data = {'_id' => oid, 'name' => 'Barney', 'age' => 41}
78
+ coll.insert(data)
79
+ row = coll.find_first({'name' => data['name']}, :limit => 1)
79
80
  db_oid = row['_id']
80
81
  assert_equal oid, db_oid
81
- assert_equal obj, row
82
+ assert_equal data, row
82
83
 
83
84
  coll.clear
84
85
  end
data/tests/test_db_api.rb CHANGED
@@ -13,7 +13,8 @@ class DBAPITest < Test::Unit::TestCase
13
13
 
14
14
  def setup
15
15
  @@coll.clear
16
- @r1 = @@coll.insert('a' => 1) # collection not created until it's used
16
+ @r1 = {'a' => 1}
17
+ @@coll.insert(@r1) # collection not created until it's used
17
18
  @@coll_full_name = 'ruby-mongo-test.test'
18
19
  end
19
20
 
@@ -29,8 +30,8 @@ class DBAPITest < Test::Unit::TestCase
29
30
  end
30
31
 
31
32
  def test_insert
32
- @@coll.insert('a' => 2)
33
- @@coll.insert('b' => 3)
33
+ assert_kind_of ObjectID, @@coll.insert('a' => 2)
34
+ assert_kind_of ObjectID, @@coll.insert('b' => 3)
34
35
 
35
36
  assert_equal 3, @@coll.count
36
37
  docs = @@coll.find().to_a
@@ -46,7 +47,11 @@ class DBAPITest < Test::Unit::TestCase
46
47
  end
47
48
 
48
49
  def test_insert_multiple
49
- @@coll.insert({'a' => 2}, {'b' => 3})
50
+ ids = @@coll.insert({'a' => 2}, {'b' => 3})
51
+
52
+ ids.each do |i|
53
+ assert_kind_of ObjectID, i
54
+ end
50
55
 
51
56
  assert_equal 3, @@coll.count
52
57
  docs = @@coll.find().to_a
@@ -614,7 +619,7 @@ class DBAPITest < Test::Unit::TestCase
614
619
 
615
620
  a = {"hello" => "world"}
616
621
 
617
- @@coll.save(a)
622
+ assert_kind_of ObjectID, @@coll.save(a)
618
623
  assert_equal 1, @@coll.count
619
624
 
620
625
  @@coll.save(@@coll.find_first)
@@ -31,6 +31,16 @@ class GridStoreTest < Test::Unit::TestCase
31
31
  assert !GridStore.exist?(@@db, 'foobar', 'another_root')
32
32
  end
33
33
 
34
+ def test_list
35
+ assert_equal ['foobar'], GridStore.list(@@db)
36
+ assert_equal ['foobar'], GridStore.list(@@db, 'fs')
37
+ assert_equal [], GridStore.list(@@db, 'my_fs')
38
+
39
+ GridStore.open(@@db, 'test', 'w') { |f| f.write("my file") }
40
+
41
+ assert_equal ['foobar', 'test'], GridStore.list(@@db)
42
+ end
43
+
34
44
  def test_small_write
35
45
  rows = @@files.find({'filename' => 'foobar'}).to_a
36
46
  assert_not_nil rows
@@ -67,7 +67,7 @@ EOS
67
67
  XMLToRuby.new.xml_to_ruby(f)
68
68
  }
69
69
 
70
- File.open(File.join(dir, "#{name}.bson"), 'r') { |f|
70
+ File.open(File.join(dir, "#{name}.bson"), 'rb') { |f|
71
71
  # Read the BSON from the file
72
72
  bson = f.read
73
73
  bson = if RUBY_VERSION >= '1.9'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongodb-mongo
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.9"
4
+ version: "0.10"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Menard
@@ -75,6 +75,7 @@ files:
75
75
  - lib/mongo.rb
76
76
  has_rdoc: true
77
77
  homepage: http://www.mongodb.org
78
+ licenses:
78
79
  post_install_message:
79
80
  rdoc_options:
80
81
  - --main
@@ -97,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
98
  requirements: []
98
99
 
99
100
  rubyforge_project:
100
- rubygems_version: 1.2.0
101
+ rubygems_version: 1.3.5
101
102
  signing_key:
102
103
  specification_version: 2
103
104
  summary: Ruby driver for the 10gen Mongo DB