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, RpbIndexReq, RpbIndexResp, RpbLink, RpbListBucketsResp, RpbListKeysReq, RpbListKeysResp, RpbMapRedReq, RpbMapRedResp, RpbPair, RpbPutReq, RpbPutResp, RpbSearchDoc, RpbSearchQueryReq, RpbSearchQueryResp, RpbSetBucketReq, RpbSetClientIdReq

Constant Summary

Constants included from ObjectMethods

ObjectMethods::ENCODING

Constants inherited from ProtobuffsBackend

ProtobuffsBackend::MESSAGE_CODES

Constants included from FeatureDetection

FeatureDetection::VERSION

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

#initialize, simple, #teardown

Methods included from FeatureDetection

#get_server_version, #mapred_phaseless?, #pb_conditionals?, #pb_head?, #pb_indexes?, #pb_search?, #quorum_controls?, #server_version, #tombstone_vclocks?

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

.configured?Boolean

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

#delete_object(bucket, key, options = {}) ⇒ Object



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

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(prune_unsupported_options(:DelReq, options))
  write_protobuff(:DelReq, req)
  decode_response
end

#fetch_object(bucket, key, options = {}) ⇒ Object



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

def fetch_object(bucket, key, options={})
  options = prune_unsupported_options(:GetReq, 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

#get_bucket_props(bucket) ⇒ Object



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

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

#get_index(bucket, index, query) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/riak/client/beefcake_protobuffs_backend.rb', line 128

def get_index(bucket, index, query)
  return super unless pb_indexes?
  if Range === query
    options = {
      :qtype => RpbIndexReq::IndexQueryType::RANGE,
      :range_min => query.begin.to_s,
      :range_max => query.end.to_s
    }
  else
    options = {
      :qtype => RpbIndexReq::IndexQueryType::EQ,
      :key => query.to_s
    }
  end
  req = RpbIndexReq.new(options.merge(:bucket => bucket, :index => index))
  write_protobuff(:IndexReq, req)
  decode_response
end

#list_keys(bucket, &block) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/riak/client/beefcake_protobuffs_backend.rb', line 95

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

#mapred(mr, &block) ⇒ Object

Raises:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/riak/client/beefcake_protobuffs_backend.rb', line 111

def mapred(mr, &block)
  raise MapReduceError.new(t("empty_map_reduce_query")) if mr.query.empty? && !mapred_phaseless?
  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

#reload_object(robject, options = {}) ⇒ Object



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(prune_unsupported_options(:GetReq, options))
  write_protobuff(:GetReq, req)
  decode_response(robject)
end

#search(index, query, options = {}) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/riak/client/beefcake_protobuffs_backend.rb', line 147

def search(index, query, options={})
  return super unless pb_search?
  options = options.symbolize_keys
  options[:op] = options.delete(:'q.op') if options[:'q.op']
  req = RpbSearchQueryReq.new(options.merge(:index => index || 'search', :q => query))
  write_protobuff(:SearchQueryReq, req)
  decode_response
end

#set_bucket_props(bucket, props) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/riak/client/beefcake_protobuffs_backend.rb', line 87

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

#set_client_id(id) ⇒ Object



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

#store_object(robject, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/riak/client/beefcake_protobuffs_backend.rb', line 51

def store_object(robject, options={})
  options = normalize_quorums(options)
  if robject.prevent_stale_writes
    unless pb_conditionals?
      other = fetch_object(robject.bucket, robject.key)
      raise Riak::ProtobuffsFailedRequest.new(:stale_object, t("stale_write_prevented")) unless other.vclock == robject.vclock
    end
    if robject.vclock
      options[:if_not_modified] = true
    else
      options[:if_none_match] = true
    end
  end
  req = dump_object(robject, prune_unsupported_options(:PutReq, options))
  write_protobuff(:PutReq, req)
  decode_response(robject)
end