Skip to content

Instantly share code, notes, and snippets.

@dblock
Created May 30, 2017 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dblock/850aac8c29776b429d58f10bd90db68c to your computer and use it in GitHub Desktop.
Save dblock/850aac8c29776b429d58f10bd90db68c to your computer and use it in GitHub Desktop.
require 'mongoid'
require 'mongoid-collection-snapshot'
Mongo::Logger.logger.level = Logger::INFO
Mongoid.connect_to 'widgets-and-gadgets', max_pool_size: 16
class Widget
include Mongoid::Document
end
class Gadget
include Mongoid::Document
end
class WidgetsAndGadgets
include Mongoid::CollectionSnapshot
document do
belongs_to :widget, inverse_of: nil
belongs_to :gadget, inverse_of: nil
end
def build
Widget.all.each do |widget|
Gadget.all.each do |gadget|
next unless Random.rand(2) == 1
collection_snapshot.insert_one(widget_id: widget.id, gadget_id: gadget.id)
end
end
end
end
Widget.delete_all
Gadget.delete_all
10.times do
Widget.create!
Gadget.create!
end
WidgetsAndGadgets.create!
require 'thread'
workers = (0...8).map do
Thread.new do
begin
while true
STDOUT.write "."
WidgetsAndGadgets.latest.documents.each do |pair|
raise "got a nil widget" unless pair.widget
raise "got a nil gadget" unless pair.gadget
end
end
rescue ThreadError
end
end
end
workers.map(&:join)
puts " done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment