Last active
December 24, 2015 23:39
-
-
Save jiren/6881500 to your computer and use it in GitHub Desktop.
bson_ruby: Temporary patch for generating object_id in ascending order.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bson' | |
# Undefine 'next' method which is define by c ext. | |
BSON::ObjectId::Generator.send :undef_method, :next | |
# Redefine next method. | |
module BSON | |
class ObjectId | |
class Generator | |
def next(time = nil) | |
@mutex.lock | |
begin | |
count = @counter = (@counter + 1) % 0xFFFFFF | |
ensure | |
@mutex.unlock rescue nil | |
end | |
generate(time || ::Time.new.to_i, count) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment