Module: Couchbase::Async

Included in:
Bucket
Defined in:
lib/couchbase/async.rb,
lib/couchbase/async/queue.rb,
lib/couchbase/async/callback.rb

Defined Under Namespace

Classes: Callback, Queue

Instance Method Summary collapse

Instance Method Details

#asyncObject



11
12
13
# File 'lib/couchbase/async.rb', line 11

def async
  Thread.current[:bucket_async] ||= @async
end

#async=(val) ⇒ Object



15
16
17
# File 'lib/couchbase/async.rb', line 15

def async=(val)
  Thread.current[:bucket_async] = val
end

#async?Boolean

Returns:



7
8
9
# File 'lib/couchbase/async.rb', line 7

def async?
  !!async
end

#async_queueObject



31
32
33
# File 'lib/couchbase/async.rb', line 31

def async_queue
  Thread.current[:bucket_async_queue] ||= Couchbase::Async::Queue.new(self)
end

#end_async_queueObject



35
36
37
# File 'lib/couchbase/async.rb', line 35

def end_async_queue
  Thread.current[:bucket_async_queue] = nil
end

#run(options = {}) {|bucket| ... } ⇒ nil

Run the event loop.

Examples:

Use block to run the loop

c = Couchbase.new
c.run do
  c.get("foo") {|ret| puts ret.value}
end

Use lambda to run the loop

c = Couchbase.new
operations = lambda do |c|
  c.get("foo") {|ret| puts ret.value}
end
c.run(&operations)

Use threshold to send out commands automatically

c = Couchbase.connect
sent = 0
c.run(:send_threshold => 8192) do  # 8Kb
  c.set("foo1", "x" * 100) {|r| sent += 1}
  # 128 bytes buffered, sent is 0 now
  c.set("foo2", "x" * 10000) {|r| sent += 1}
  # 10028 bytes added, sent is 2 now
  c.set("foo3", "x" * 100) {|r| sent += 1}
end
# all commands were executed and sent is 3 now

Use Bucket#run without block for async connection

c = Couchbase.new(:async => true)
c.run      # ensure that instance connected
c.set("foo", "bar"){|r| puts r.cas}
c.run

Parameters:

  • (defaults to: {})

    The options for operation for connection

Options Hash (options):

  • :send_threshold (Fixnum) — default: 0

    if the internal command buffer will exceeds this value, then the library will start network interaction and block the current thread until all scheduled commands will be completed.

Yield Parameters:

  • bucket (Bucket)

    the bucket instance

Returns:

Raises:

Since:

  • 1.0.0



88
89
90
91
92
93
94
95
96
97
# File 'lib/couchbase/async.rb', line 88

def run(options = {})
  do_async_setup(block_given?)
  yield(self)
  async_queue.join

  # TODO: deal with exceptions
  nil
ensure
  do_async_ensure
end

#run_async(options = {}) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/couchbase/async.rb', line 99

def run_async(options = {})
  do_async_setup(block_given?)
  yield(self)
  nil
ensure
  do_async_ensure
end

#runningObject



23
24
25
# File 'lib/couchbase/async.rb', line 23

def running
  Thread.current[:bucket_running] ||= false
end

#running=(val) ⇒ Object



27
28
29
# File 'lib/couchbase/async.rb', line 27

def running=(val)
  Thread.current[:bucket_running] = val
end

#running?Boolean

Returns:



19
20
21
# File 'lib/couchbase/async.rb', line 19

def running?
  !!running
end