Skip to content

Instantly share code, notes, and snippets.

@jiren
Last active December 24, 2015 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiren/6881500 to your computer and use it in GitHub Desktop.
Save jiren/6881500 to your computer and use it in GitHub Desktop.
bson_ruby: Temporary patch for generating object_id in ascending order.
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