Module: Couchbase::Operations::Get

Defined in:
lib/couchbase/operations/get.rb

Instance Method Summary collapse

Instance Method Details

#[](key, options = {}) ⇒ Object



146
147
148
# File 'lib/couchbase/operations/get.rb', line 146

def [](key, options = {})
  get(key, options)
end

#get(*keys, options = {}) {|ret| ... } ⇒ Object, ... #get(keys, options = {}) ⇒ Hash

Obtain an object stored in Couchbase by given key.

Overloads:

  • #get(*keys, options = {}) {|ret| ... } ⇒ Object, ...

    Returns the value(s) (or tuples in extended mode) associated with the key.

    Examples:

    Get single value in quiet mode (the default)

    c.get("foo")     #=> the associated value or nil
    

    Use alternative hash-like syntax

    c["foo"]         #=> the associated value or nil
    

    Get single value in verbose mode

    c.get("missing-foo", :quiet => false)  #=> raises Couchbase::NotFound
    c.get("missing-foo", :quiet => true)   #=> returns nil
    

    Get and touch single value. The key won't be accessible after 10 seconds

    c.get("foo", :ttl => 10)
    

    Extended get

    val, flags, cas = c.get("foo", :extended => true)
    

    Get multiple keys

    c.get("foo", "bar", "baz")   #=> [val1, val2, val3]
    

    Get multiple keys with assembing result into the Hash

    c.get("foo", "bar", "baz", :assemble_hash => true)
    #=> {"foo" => val1, "bar" => val2, "baz" => val3}
    

    Extended get multiple keys

    c.get("foo", "bar", :extended => true)
    #=> {"foo" => [val1, flags1, cas1], "bar" => [val2, flags2, cas2]}
    

    Asynchronous get

    c.run do
      c.get("foo", "bar", "baz") do |res|
        ret.operation   #=> :get
        ret.success?    #=> true
        ret.key         #=> "foo", "bar" or "baz" in separate calls
        ret.value
        ret.flags
        ret.cas
      end
    end
    

    Get and lock key using default timeout

    c.get("foo", :lock => true)
    

    Determine lock timeout parameters

    c.stats.values_at("ep_getl_default_timeout", "ep_getl_max_timeout")
    #=> [{"127.0.0.1:11210"=>"15"}, {"127.0.0.1:11210"=>"30"}]
    

    Get and lock key using custom timeout

    c.get("foo", :lock => 3)
    

    Get and lock multiple keys using custom timeout

    c.get("foo", "bar", :lock => 3)
    

    Parameters:

    • keys (String, Symbol, Array)

      One or several keys to fetch

    • options (Hash) (defaults to: {})

      Options for operation.

    Options Hash (options):

    • :extended (true, false) — default: false

      If set to true, the operation will return a tuple [value, flags, cas], otherwise (by default) it returns just the value.

    • :ttl (Fixnum) — default: self.default_ttl

      Expiry time for key. Values larger than 302460*60 seconds (30 days) are interpreted as absolute times (from the epoch).

    • :quiet (true, false) — default: self.quiet

      If set to true, the operation won't raise error for missing key, it will return nil. Otherwise it will raise error in synchronous mode. In asynchronous mode this option ignored.

    • :format (Symbol) — default: nil

      Explicitly choose the decoder for this key (+:plain+, :document, :marshal). See Bucket#default_format.

    • :lock (Fixnum, Boolean)

      Lock the keys for time span. If this parameter is true the key(s) will be locked for default timeout. Also you can use number to setup your own timeout in seconds. If it will be lower that zero or exceed the maximum, the server will use default value. You can determine actual default and maximum values calling Bucket#stats without arguments and inspecting keys "ep_getl_default_timeout" and "ep_getl_max_timeout" correspondingly. See overloaded hash syntax to specify custom timeout per each key.

    • :assemble_hash (true, false) — default: false

      Assemble Hash for results. Hash assembled automatically if :extended option is true or in case of "get and touch" multimple keys.

    • :replica (true, false) — default: false

      Read key from replica node. Options :ttl and :lock are not compatible with :replica.

    Yield Parameters:

    • ret (Result)

      the result of operation in asynchronous mode (valid attributes: error, operation, key, value, flags, cas).

    Returns:

    • (Object, Array, Hash)

      the value(s) (or tuples in extended mode) associated with the key.

    Raises:

  • #get(keys, options = {}) ⇒ Hash

    When the method receive hash map, it will behave like it receive list of keys (+keys.keys+), but also touch each key setting expiry time to the corresponding value. But unlike usual get this command always return hash map {key => value} or {key => [value, flags, cas]}.

    Examples:

    Get and touch multiple keys

    c.get("foo" => 10, "bar" => 20)   #=> {"foo" => val1, "bar" => val2}
    

    Extended get and touch multiple keys

    c.get({"foo" => 10, "bar" => 20}, :extended => true)
    #=> {"foo" => [val1, flags1, cas1], "bar" => [val2, flags2, cas2]}
    

    Get and lock multiple keys for chosen period in seconds

    c.get("foo" => 10, "bar" => 20, :lock => true)
    #=> {"foo" => val1, "bar" => val2}
    

    Parameters:

    • keys (Hash)

      Map key-ttl

    • options (Hash) (defaults to: {})

      Options for operation. (see options definition above)

    Returns:

    • (Hash)

      the values (or tuples in extended mode) associated with the keys.

See Also:

Since:

  • 1.0.0



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/couchbase/operations/get.rb', line 131

def get(*args, &block)
  sync_block_error if !async? && block_given?
  key, options = expand_get_args(args)

  if async?
    if block_given?
      async_get(key, options, &Proc.new)
    else
      async_get(key, options)
    end
  else
    sync_get(key, options)
  end
end

#get_multi(keys, options) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/couchbase/operations/get.rb', line 150

def get_multi(keys, options)
  results = if options[:extended]
              get_multi_extended(keys)
            else
              java_get_multi(keys)
            end

  not_found_error(results.size != keys.size, options)
  results = transcode_multi_results(results) unless options[:extended]

  if options[:assemble_hash] || options[:extended]
    results
  else
    ordered_multi_values(keys, results)
  end
end