Module: Couchbase::Operations::Get

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

Instance Method Summary collapse

Instance Method Details

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



164
165
166
# File 'lib/couchbase/operations/get.rb', line 164

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

#async_get(key) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/couchbase/operations/get.rb', line 184

def async_get(key)
  case key
  when String, Symbol
    meta = { op: :get, key: key }
    future = client.asyncGet(key)
  when Array
    meta = { op: :get }
    future = client.asyncGetBulk(key)
  when Hash
    # async_get_and_touch(key, options, &block)
  end
  register_future(future, meta, &Proc.new) if block_given?
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 30*24*60*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



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

def get(*args, &block)
  key, options = expand_get_args(args)

  if async?
    async_get(key, &block)
    # if block_given?
    #   async_get(key, &Proc.new)
    # else
    #   async_get(key)
    # end
  else
    sync_block_error if block_given?
    get_key(key, options)
  end
end

#get_bulk(keys, options) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/couchbase/operations/get.rb', line 168

def get_bulk(keys, options)
  results = if options[:extended]
              get_bulk_extended(keys)
            else
              client_get_bulk(keys)
            end

  not_found_error(results.size != keys.size, options)

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