Skip to content

Commit 7702d67

Browse files
author
Mike Dirolf
committed
Use C objectid_generate for ObjectId in addition to ObjectID.
Also, remove broken defined? check from objectid.rb and object_id.rb - it was never doing anything since we require those files at the top of cbson.c.
1 parent 9eee412 commit 7702d67

File tree

3 files changed

+27
-30
lines changed

3 files changed

+27
-30
lines changed

ext/cbson/cbson.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,7 @@ void Init_cbson() {
949949
DigestMD5 = rb_const_get(Digest, rb_intern("MD5"));
950950

951951
rb_define_method(ObjectID, "generate", objectid_generate, 0);
952+
rb_define_method(ObjectId, "generate", objectid_generate, 0);
952953

953954
rb_define_method(rb_cArray, "fast_pack", fast_pack, 0);
954955
}

lib/bson/types/object_id.rb

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,26 +155,24 @@ def generation_time
155155

156156
private
157157

158-
# We need to define this method only if CBson isn't loaded.
159-
unless defined? CBson
160-
def generate
161-
oid = ''
158+
# This gets overwritten by the C extension if it loads.
159+
def generate
160+
oid = ''
162161

163-
# 4 bytes current time
164-
time = Time.new.to_i
165-
oid += [time].pack("N")
162+
# 4 bytes current time
163+
time = Time.new.to_i
164+
oid += [time].pack("N")
166165

167-
# 3 bytes machine
168-
oid += Digest::MD5.digest(Socket.gethostname)[0, 3]
166+
# 3 bytes machine
167+
oid += Digest::MD5.digest(Socket.gethostname)[0, 3]
169168

170-
# 2 bytes pid
171-
oid += [Process.pid % 0xFFFF].pack("n")
169+
# 2 bytes pid
170+
oid += [Process.pid % 0xFFFF].pack("n")
172171

173-
# 3 bytes inc
174-
oid += [get_inc].pack("N")[1, 3]
172+
# 3 bytes inc
173+
oid += [get_inc].pack("N")[1, 3]
175174

176-
oid.unpack("C12")
177-
end
175+
oid.unpack("C12")
178176
end
179177

180178
def get_inc

lib/bson/types/objectid.rb

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,26 +158,24 @@ def generation_time
158158

159159
private
160160

161-
# We need to define this method only if CBson isn't loaded.
162-
unless defined? CBson
163-
def generate
164-
oid = ''
161+
# This gets overwritten by the C extension if it loads.
162+
def generate
163+
oid = ''
165164

166-
# 4 bytes current time
167-
time = Time.new.to_i
168-
oid += [time].pack("N")
165+
# 4 bytes current time
166+
time = Time.new.to_i
167+
oid += [time].pack("N")
169168

170-
# 3 bytes machine
171-
oid += Digest::MD5.digest(Socket.gethostname)[0, 3]
169+
# 3 bytes machine
170+
oid += Digest::MD5.digest(Socket.gethostname)[0, 3]
172171

173-
# 2 bytes pid
174-
oid += [Process.pid % 0xFFFF].pack("n")
172+
# 2 bytes pid
173+
oid += [Process.pid % 0xFFFF].pack("n")
175174

176-
# 3 bytes inc
177-
oid += [get_inc].pack("N")[1, 3]
175+
# 3 bytes inc
176+
oid += [get_inc].pack("N")[1, 3]
178177

179-
oid.unpack("C12")
180-
end
178+
oid.unpack("C12")
181179
end
182180

183181
def get_inc

0 commit comments

Comments
 (0)