igrigorik / em-synchrony

Fiber aware EventMachine clients and convenience classes
http://www.igvita.com/2010/03/22/untangling-evented-code-with-ruby-fibers
MIT License
1.04k stars 151 forks source link

Support Rethinkdb? #204

Open miguelalarcos opened 8 years ago

miguelalarcos commented 8 years ago

I was wondering if there are plans to support Rethinkdb.

Thanks for the good job!

dgutov commented 8 years ago

This discussion seems relevant: https://github.com/rethinkdb/rethinkdb/issues/4630

igrigorik commented 8 years ago

I'd be happy to accept a PR if someone wants to own it on an on-going basis.

miguelalarcos commented 8 years ago

What about this as a starting point? (the important funcs are rsync and rmsync)

require 'eventmachine'
require 'fiber'
require 'rethinkdb'

$r = RethinkDB::RQL.new
$conn = $r.connect()

def get_array predicate
  ret = []
  docs_with_count(predicate) do |count, row|
    ret << row
    yield ret if ret.length == count
  end
end

def docs_with_count predicate
  predicate.count().em_run($conn) do |count|
    predicate.em_run($conn) do |doc|
      yield count, doc
    end
  end
end

def rsync pred
  aux = Fiber.new do |current|
    pred.em_run($conn) do |doc|
      current.transfer doc
    end
  end
  aux.transfer Fiber.current
end

def rmsync pred
  aux = Fiber.new do |current|
    get_array(pred){|docs| current.transfer docs}
  end
  aux.transfer Fiber.current
end

fib = Fiber.new do
  doc = rsync $r.table('aux').get('94fcf756-ae81-4eb6-a50e-4004717c948a')
  print doc
  puts
  docs = rmsync $r.table('aux')
  print docs
  puts
  EM.stop
end

EM.run do
  fib.resume
end

Sorry if the code is not clear enough