class Riak::Client::BeefcakeProtobuffsBackend::CrdtLoader

Loads, and deserializes CRDTs from protobuffs into Ruby hashes, sets, strings, and integers. @api private

Attributes

backend[R]
context[R]

Public Class Methods

new(backend) click to toggle source
# File lib/riak/client/beefcake/crdt_loader.rb, line 25
def initialize(backend)
  @backend = backend
end

Public Instance Methods

get_loader_for_value(value) click to toggle source
# File lib/riak/client/beefcake/crdt_loader.rb, line 48
def get_loader_for_value(value)
  return nil if value.nil?

  [CounterLoader, HyperLogLogLoader, MapLoader, SetLoader].map do |loader|
    loader.for_value value
  end.compact.first
end
load(bucket, key, bucket_type, options = {}) click to toggle source

Perform the protobuffs request and return a deserialized CRDT.

# File lib/riak/client/beefcake/crdt_loader.rb, line 30
def load(bucket, key, bucket_type, options = {})
  bucket = bucket.name if bucket.is_a? ::Riak::Bucket
  fetch_args = options.merge(
                             bucket: bucket,
                             key: key,
                             type: bucket_type
                             )
  request = DtFetchReq.new fetch_args

  response = backend.protocol do |p|
    p.write :DtFetchReq, request
    p.expect :DtFetchResp, DtFetchResp
  end

  @context = response.context
  rubyfy response
end

Private Instance Methods

nil_rubyfy(type) click to toggle source

Sometimes a CRDT is empty, provide a sane default.

# File lib/riak/client/beefcake/crdt_loader.rb, line 66
def nil_rubyfy(type)
  case type
  when DtFetchResp::DataType::COUNTER
    0
  when DtFetchResp::DataType::SET
    ::Set.new
  when DtFetchResp::DataType::MAP
    {
      counters: {},
      flags: {},
      maps: {},
      registers: {},
      sets: {},
    }
  when DtFetchResp::DataType::HLL
    0
  end
end
rubyfy(response) click to toggle source

Convert the protobuffs response into low-level Ruby objects.

# File lib/riak/client/beefcake/crdt_loader.rb, line 58
def rubyfy(response)
  loader = get_loader_for_value response.value
  return nil_rubyfy(response.type) if loader.nil?

  return loader.rubyfy
end