Class: Riak::Client::BeefcakeProtobuffsBackend

Inherits:
ProtobuffsBackend show all
Includes:
ObjectMethods
Defined in:
lib/riak/client/beefcake/messages.rb,
lib/riak/client/beefcake/object_methods.rb,
lib/riak/client/beefcake_protobuffs_backend.rb

Defined Under Namespace

Modules: ObjectMethods Classes: RpbBucketProps, RpbContent, RpbDelReq, RpbErrorResp, RpbGetBucketReq, RpbGetBucketResp, RpbGetClientIdResp, RpbGetReq, RpbGetResp, RpbGetServerInfoResp, RpbLink, RpbListBucketsResp, RpbListKeysReq, RpbListKeysResp, RpbMapRedReq, RpbMapRedResp, RpbPair, RpbPutReq, RpbPutResp, RpbSetBucketReq, RpbSetClientIdReq

Constant Summary

Constant Summary

Constants included from ObjectMethods

ObjectMethods::ENCODING

Constants inherited from ProtobuffsBackend

ProtobuffsBackend::MESSAGE_CODES, ProtobuffsBackend::QUORUMS, ProtobuffsBackend::UINTMAX

Instance Attribute Summary

Attributes inherited from ProtobuffsBackend

#client, #node

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from ObjectMethods

#dump_object, #load_object

Methods inherited from ProtobuffsBackend

#get_index, #initialize, simple, #teardown

Methods included from Util::Escape

#escape, #maybe_escape, #maybe_unescape, #unescape

Methods included from Util::Translation

#i18n_scope, #t

Constructor Details

This class inherits a constructor from Riak::Client::ProtobuffsBackend

Class Method Details

+ (Boolean) configured?

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
# File 'lib/riak/client/beefcake_protobuffs_backend.rb', line 10

def self.configured?
  begin
    require 'beefcake'
    require 'riak/client/beefcake/messages'
    require 'riak/client/beefcake/object_methods'
    true
  rescue LoadError, NameError
    false
  end
end

Instance Method Details

- (Object) delete_object(bucket, key, options = {})



62
63
64
65
66
67
68
69
70
71
# File 'lib/riak/client/beefcake_protobuffs_backend.rb', line 62

def delete_object(bucket, key, options={})
  bucket = Bucket === bucket ? bucket.name : bucket
  options = normalize_quorums(options)
  options[:bucket] = maybe_encode(bucket)
  options[:key] = maybe_encode(key)
  options[:vclock] = Base64.decode64(options[:vclock]) if options[:vclock]
  req = RpbDelReq.new(options)
  write_protobuff(:DelReq, req)
  decode_response
end

- (Object) fetch_object(bucket, key, options = {})



33
34
35
36
37
38
39
# File 'lib/riak/client/beefcake_protobuffs_backend.rb', line 33

def fetch_object(bucket, key, options={})
  options = normalize_quorums(options)
  bucket = Bucket === bucket ? bucket.name : bucket
  req = RpbGetReq.new(options.merge(:bucket => maybe_encode(bucket), :key => maybe_encode(key)))
  write_protobuff(:GetReq, req)
  decode_response(RObject.new(client.bucket(bucket), key))
end

- (Object) get_bucket_props(bucket)



73
74
75
76
77
78
# File 'lib/riak/client/beefcake_protobuffs_backend.rb', line 73

def get_bucket_props(bucket)
  bucket = bucket.name if Bucket === bucket
  req = RpbGetBucketReq.new(:bucket => maybe_encode(bucket))
  write_protobuff(:GetBucketReq, req)
  decode_response
end

- (Object) list_keys(bucket, &block)



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/riak/client/beefcake_protobuffs_backend.rb', line 88

def list_keys(bucket, &block)
  bucket = bucket.name if Bucket === bucket
  req = RpbListKeysReq.new(:bucket => maybe_encode(bucket))
  write_protobuff(:ListKeysReq, req)
  keys = []
  while msg = decode_response
    break if msg.done
    if block_given?
      yield msg.keys
    else
      keys += msg.keys
    end
  end
  block_given? || keys
end

- (Object) mapred(mr, &block)



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/riak/client/beefcake_protobuffs_backend.rb', line 104

def mapred(mr, &block)
  req = RpbMapRedReq.new(:request => mr.to_json, :content_type => "application/json")
  write_protobuff(:MapRedReq, req)
  results = []
  while msg = decode_response
    break if msg.done
    if block_given?
      yield msg.phase, JSON.parse(msg.response)
    else
      results[msg.phase] ||= []
      results[msg.phase] += JSON.parse(msg.response)
    end
  end
  block_given? || results.compact.size == 1 ? results.last : results
end

- (Object) reload_object(robject, options = {})



41
42
43
44
45
46
47
48
49
# File 'lib/riak/client/beefcake_protobuffs_backend.rb', line 41

def reload_object(robject, options={})
  options = normalize_quorums(options)
  options[:bucket] = maybe_encode(robject.bucket.name)
  options[:key] = maybe_encode(robject.key)
  options[:if_modified] = maybe_encode Base64.decode64(robject.vclock) if robject.vclock
  req = RpbGetReq.new(options)
  write_protobuff(:GetReq, req)
  decode_response(robject)
end

- (Object) set_bucket_props(bucket, props)



80
81
82
83
84
85
86
# File 'lib/riak/client/beefcake_protobuffs_backend.rb', line 80

def set_bucket_props(bucket, props)
  bucket = bucket.name if Bucket === bucket
  props = props.slice('n_val', 'allow_mult')
  req = RpbSetBucketReq.new(:bucket => maybe_encode(bucket), :props => RpbBucketProps.new(props))
  write_protobuff(:SetBucketReq, req)
  decode_response
end

- (Object) set_client_id(id)



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/riak/client/beefcake_protobuffs_backend.rb', line 21

def set_client_id(id)
  value = case id
          when Integer
            [id].pack("N")
          else
            id.to_s
          end
  req = RpbSetClientIdReq.new(:client_id => value)
  write_protobuff(:SetClientIdReq, req)
  decode_response
end

- (Object) store_object(robject, options = {})



51
52
53
54
55
56
57
58
59
60
# File 'lib/riak/client/beefcake_protobuffs_backend.rb', line 51

def store_object(robject, options={})
  if robject.prevent_stale_writes
    other = fetch_object(robject.bucket, robject.key)
    raise Riak::ProtobuffsFailedRequest(:stale_object, t("stale_write_prevented")) unless other.vclock == robject.vclock
  end
  options = normalize_quorums(options)
  req = dump_object(robject, options)
  write_protobuff(:PutReq, req)
  decode_response(robject)
end