Class: Riakpb::Bucket

Inherits:
Object
  • Object
show all
Includes:
Util::MessageCode, Util::Translation
Defined in:
lib/riakpb/bucket.rb

Overview

Represents and encapsulates operations on a Riakpb bucket. You may retrieve a bucket using Client#bucket, or create it manually and retrieve its meta-information later.

Constant Summary

Constants included from Util::MessageCode

Util::MessageCode::DEL_REQUEST, Util::MessageCode::DEL_RESPONSE, Util::MessageCode::ERROR_RESPONSE, Util::MessageCode::GET_BUCKET_REQUEST, Util::MessageCode::GET_BUCKET_RESPONSE, Util::MessageCode::GET_CLIENT_ID_REQUEST, Util::MessageCode::GET_CLIENT_ID_RESPONSE, Util::MessageCode::GET_REQUEST, Util::MessageCode::GET_RESPONSE, Util::MessageCode::GET_SERVER_INFO_REQUEST, Util::MessageCode::GET_SERVER_INFO_RESPONSE, Util::MessageCode::LIST_BUCKETS_REQUEST, Util::MessageCode::LIST_BUCKETS_RESPONSE, Util::MessageCode::LIST_KEYS_REQUEST, Util::MessageCode::LIST_KEYS_RESPONSE, Util::MessageCode::MAP_REDUCE_REQUEST, Util::MessageCode::MAP_REDUCE_RESPONSE, Util::MessageCode::MC_RESPONSE_FOR, Util::MessageCode::PING_REQUEST, Util::MessageCode::PING_RESPONSE, Util::MessageCode::PUT_REQUEST, Util::MessageCode::PUT_RESPONSE, Util::MessageCode::RESPONSE_CLASS_FOR, Util::MessageCode::SET_BUCKET_REQUEST, Util::MessageCode::SET_BUCKET_RESPONSE, Util::MessageCode::SET_CLIENT_ID_REQUEST, Util::MessageCode::SET_CLIENT_ID_RESPONSE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Translation

#i18n_scope, #t

Constructor Details

#initialize(client, name, options = {}) ⇒ Bucket

Create a Riakpb bucket manually.

Parameters:

  • client (Client)

    the Client for this bucket

  • name (String)

    the name of the bucket

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/riakpb/bucket.rb', line 27

def initialize(client, name, options={})
  options.assert_valid_keys(:n_val, :allow_mult, :return_body)
  raise ArgumentError, t("client_type", :client => client.inspect)  unless client.is_a?(Client)
  raise ArgumentError, t("string_type", :string => name.inspect)    unless name.is_a?(String)

  @client           = client
  @name             = name
  self.n_val      ||= options[:n_val]
  self.allow_mult ||= options[:allow_mult]
  @key_cache        = Hash.new{|k,v| k[v] = Riakpb::Key.new(self, v)}
end

Instance Attribute Details

#allow_multtrue, false

Returns whether the bucket allows divergent siblings.

Returns:

  • (true, false)

    whether the bucket allows divergent siblings



20
21
22
# File 'lib/riakpb/bucket.rb', line 20

def allow_mult
  @allow_mult
end

#clientRiakpb::Client (readonly)

Returns the associated client.

Returns:



11
12
13
# File 'lib/riakpb/bucket.rb', line 11

def client
  @client
end

#key_cacheObject (readonly)

Returns the value of attribute key_cache.



22
23
24
# File 'lib/riakpb/bucket.rb', line 22

def key_cache
  @key_cache
end

#n_valFixnum

Returns the N value, or number of replicas for this bucket.

Returns:

  • (Fixnum)

    the N value, or number of replicas for this bucket



17
18
19
# File 'lib/riakpb/bucket.rb', line 17

def n_val
  @n_val
end

#nameString (readonly)

Returns the bucket name.

Returns:

  • (String)

    the bucket name



14
15
16
# File 'lib/riakpb/bucket.rb', line 14

def name
  @name
end

Instance Method Details

#delete(key, rw = nil) ⇒ Object

Deletes a key from the bucket

Parameters:

  • key (String)

    the key to delete

  • options (Hash)

    quorum options



137
138
139
140
141
# File 'lib/riakpb/bucket.rb', line 137

def delete(key, rw=nil)
  key = key.name if key.is_a?(Riakpb::Key)

  @client.del_request(@name, key, rw)
end

#destroy!(rw = nil) ⇒ Object

Wipes out all keys stored in the bucket, as of execution

Parameters:

  • key (String)

    the key to delete

  • options (Hash)

    quorum options



147
148
149
150
151
152
153
154
# File 'lib/riakpb/bucket.rb', line 147

def destroy!(rw=nil)
  keys!
  
  @keys.each do |key|
    @client.del_request(@name, key, rw)
  end
#      super.destroy
end

#get_linked(bucket, key, options = nil) ⇒ Riakpb::Key

Retrieves a Key from the given Bucket. Originally written for link retrieval.

Parameters:

  • bucket (String)

    the name of the bucket, in which the Key is contained

  • key (String)

    the name of the key to retrieve

  • options (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (options):

  • :quorum (Fixnum)

    read quorum- num of replicas need to agree when retrieving the object

Returns:



102
103
104
# File 'lib/riakpb/bucket.rb', line 102

def get_linked(bucket, key, options=nil)
  @client[bucket].key(key, options)
end

#inspectString

Returns a representation suitable for IRB and debugging output.

Returns:

  • (String)

    a representation suitable for IRB and debugging output



196
197
198
# File 'lib/riakpb/bucket.rb', line 196

def inspect
  "#<Riakpb::Bucket name=#{@name}, props={n_val=>#{@n_val}, allow_mult=#{@allow_mult}}>"
end

#inspect!String

Returns a representation suitable for IRB and debugging output, including keys within this bucket.

Returns:

  • (String)

    a representation suitable for IRB and debugging output, including keys within this bucket



201
202
203
# File 'lib/riakpb/bucket.rb', line 201

def inspect!
  "#<Riakpb::Bucket name=#{@name}, props={n_val=>#{@n_val}, allow_mult=#{@allow_mult}}, keys=#{keys.inspect}>"
end

#junkshot(key, params) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/riakpb/bucket.rb', line 117

def junkshot(key, params)
  raise RuntimeError.new t('siblings_disallowed') unless @allow_mult == true

  params[:links]    = parse_links(params[:links])     if params.has_key?(:links)
  params[:usermeta] = parse_links(params[:usermeta])  if params.has_key?(:usermeta)

  options           = params.slice :return_body, :w, :dw
  content           = params.slice :value, :content_type, :charset, :content_encoding, :links, :usermeta

  key               = key.name if key.is_a?(Riakpb::Key)
  options[:key]     = key
  options[:content] = Riakpb::RpbContent.new(content)

  self.store(options)
end

#key(key, options = {}) ⇒ Riakpb::Key Also known as: []

Retrieve an object from within the bucket.

Parameters:

  • key (String)

    the key of the object to retrieve

  • r (Fixnum)
    • the read quorum for the request - how many nodes should concur on the read

Returns:

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/riakpb/bucket.rb', line 70

def key(key, options={})
  raise ArgumentError, t("fixnum_invalid", :num => options[:r]) unless options[:r].is_a?(NilClass) or options[:r].is_a?(Fixnum)
  raise ArgumentError, t("string_invalid", :string => key)      unless key.is_a?(String)

  if options[:safely] == true and not @key_cache[key].empty?
    return(@key_cache[key])
  end

  response = @client.get_request @name, key, options[:r]

  @key_cache[key].load(response)
end

#key!(key, r = nil) ⇒ Riakpb::Key

Retrieve an object from within the bucket. Will raise an error message if key does not exist.

Parameters:

  • key (String)

    the key of the object to retrieve

  • quorum (Fixnum)
    • the read quorum for the request - how many nodes should concur on the read

Returns:

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
# File 'lib/riakpb/bucket.rb', line 88

def key!(key, r=nil)
  raise ArgumentError, t("string_invalid", :string  => key) unless key.is_a?(String)
  raise ArgumentError, t("fixnum_invalid", :num     => r)   unless r.is_a?(Fixnum) or r.nil?

  response = @client.get_request @name, key, r

  Riakpb::Key.new(self, key).load!(response)
end

#keysArray<String>

Accesses or retrieves a list of keys in this bucket. Needs to have expiration / cacheing, though not now.

Returns:

  • (Array<String>)

    Keys in this bucket



56
57
58
# File 'lib/riakpb/bucket.rb', line 56

def keys
  @keys ||= @client.keys_in @name
end

#keys!Array<String>

Accesses or retrieves a list of keys in this bucket. Needs to have expiration / cacheing, though not now.

Returns:

  • (Array<String>)

    Keys in this bucket



62
63
64
# File 'lib/riakpb/bucket.rb', line 62

def keys!
  @keys = @client.keys_in @name
end

#load(response) ⇒ Bucket

Load information for the bucket from a response given by the Client::HTTPBackend. Used mostly internally - use Client#bucket to get a Riakpb::Bucket instance.

Parameters:

  • response (RpbHash)

    a response from Client::HTTPBackend

Returns:

Raises:

  • (ArgumentError)

See Also:



44
45
46
47
48
49
50
51
52
# File 'lib/riakpb/bucket.rb', line 44

def load(response)
  if response.is_a?(Riakpb::RpbGetBucketResp)
    @n_val      = response.props.n_val
    @allow_mult = response.props.allow_mult

    return(self)
  end
  raise ArgumentError, t("response_type")
end

#store(options) ⇒ RpbPutResp

Retrieves a Key from the given Bucket. Originally written for link retrieval. Inserts a key in this bucket’s namespace, into riak.

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :w (Fixnum) — default: write quorum

    how many replicas to write to before returning a successful response.

  • Fixnum (Fixnum :dw how many replicas to commit to durable storage before returning a successful response.)

    :dw how many replicas to commit to durable storage before returning a successful response.

  • :return_body (Boolean)

    whether to return the contents of the stored object.

Returns:

  • (RpbPutResp)

    the response confirming Key storage and (optionally) the Key’s updated/new data.



112
113
114
115
# File 'lib/riakpb/bucket.rb', line 112

def store(options)
  options[:bucket] = @name
  @client.put_request(options)
end