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



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

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

#async=(val) ⇒ Object



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

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

#async?Boolean

Returns:

  • (Boolean)


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

def async?
  !!async
end

#async_queueObject



48
49
50
# File 'lib/couchbase/async.rb', line 48

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

#end_async_queueObject



52
53
54
# File 'lib/couchbase/async.rb', line 52

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:

  • options (Hash) (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:

  • (nil)

Raises:

Since:

  • 1.0.0



105
106
107
108
109
110
111
112
113
114
# File 'lib/couchbase/async.rb', line 105

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



116
117
118
119
120
121
122
# File 'lib/couchbase/async.rb', line 116

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

#runningObject



40
41
42
# File 'lib/couchbase/async.rb', line 40

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

#running=(val) ⇒ Object



44
45
46
# File 'lib/couchbase/async.rb', line 44

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

#running?Boolean

Returns:

  • (Boolean)


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

def running?
  !!running
end