Class: Riak::Client::ProtobuffsBackend

Inherits:
Object
  • Object
show all
Includes:
FeatureDetection, Util::Escape, Util::Translation
Defined in:
lib/riak/client/protobuffs_backend.rb

Direct Known Subclasses

BeefcakeProtobuffsBackend

Constant Summary collapse

MESSAGE_CODES =

Message Codes Enum

%W[
   ErrorResp
   PingReq
   PingResp
   GetClientIdReq
   GetClientIdResp
   SetClientIdReq
   SetClientIdResp
   GetServerInfoReq
   GetServerInfoResp
   GetReq
   GetResp
   PutReq
   PutResp
   DelReq
   DelResp
   ListBucketsReq
   ListBucketsResp
   ListKeysReq
   ListKeysResp
   GetBucketReq
   GetBucketResp
   SetBucketReq
   SetBucketResp
   MapRedReq
   MapRedResp
   IndexReq
   IndexResp
   SearchQueryReq
   SearchQueryResp
].map {|s| s.intern }.freeze

Constants included from FeatureDetection

FeatureDetection::VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FeatureDetection

#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

#initialize(client, node) ⇒ ProtobuffsBackend

Returns a new instance of ProtobuffsBackend.



57
58
59
60
# File 'lib/riak/client/protobuffs_backend.rb', line 57

def initialize(client, node)
  @client = client
  @node = node
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



55
56
57
# File 'lib/riak/client/protobuffs_backend.rb', line 55

def client
  @client
end

#nodeObject

Returns the value of attribute node.



56
57
58
# File 'lib/riak/client/protobuffs_backend.rb', line 56

def node
  @node
end

Class Method Details

.simple(method, code) ⇒ Object



48
49
50
51
52
53
# File 'lib/riak/client/protobuffs_backend.rb', line 48

def self.simple(method, code)
  define_method method do
    socket.write([1, MESSAGE_CODES.index(code)].pack('NC'))
    decode_response
  end
end

Instance Method Details

#get_index(bucket, index, query) ⇒ Array<String>

Performs a secondary-index query via emulation through MapReduce.

Parameters:

  • bucket (String, Bucket)

    the bucket to query

  • index (String)

    the index to query

  • query (String, Integer, Range)

    the equality query or range query to perform

Returns:

  • (Array<String>)

    a list of keys matching the query



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

def get_index(bucket, index, query)
  mr = Riak::MapReduce.new(client).index(bucket, index, query)
  unless mapred_phaseless?
    mr.reduce(%w[riak_kv_mapreduce reduce_identity], :arg => {:reduce_phase_only_1 => true}, :keep => true)
  end
  mapred(mr).map {|p| p.last }
end

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

Performs search query via emulation through MapReduce. This has more limited capabilites than native queries. Essentially, only the ‘id’ field of matched documents will ever be returned, the ‘fl’ and other options have no effect.

Parameters:

  • index (String)

    the index to query

  • query (String)

    the Lucene-style search query

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

    ignored in MapReduce emulation

Returns:

  • (Hash)

    the search results



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

def search(index, query, options={})
  mr = Riak::MapReduce.new(client).search(index || 'search', query)
  unless mapred_phaseless?
    mr.reduce(%w[riak_kv_mapreduce reduce_identity], :arg => {:reduce_phase_only_1 => true}, :keep => true)
  end
  docs = mapred(mr).map {|d| {'id' => d[1] } }
  # Since we don't get this information back from the MapReduce,
  # we have to fake the max_score and num_found.
  { 'docs' => docs,
    'num_found' => docs.size,
    'max_score' => 0.0 }
end

#teardownObject

Gracefully shuts down this connection.



103
104
105
# File 'lib/riak/client/protobuffs_backend.rb', line 103

def teardown
  reset_socket
end