class Riak::Crdt::Base

Basic and shared code used by the top-level CRDTs. In particular, dirty- tracking, loading, and operating is implemented by this class, and the {Riak::Crdt::Set}, {Riak::Crdt::Counter}, and {Riak::Crdt::Map} classes implement everything else.

@api private

Attributes

bucket[R]
bucket_type[R]
key[R]

Returns the key of this CRDT. Extremely useful when using a Riak-assigned key.

Public Class Methods

new(bucket, key, bucket_type, options = {}) click to toggle source

Base CRDT initialization The bucket type is determined by the first of these sources:

  1. The ‘bucket_type` String argument

  2. A {BucketTyped::Bucket} as the ‘bucket` argument

  3. A ‘bucket_type` Symbol argument is looked up in the `Crdt::Base::DEFAULT_BUCKET_TYPES` hash

@api private

@param [Bucket] bucket the {Riak::Bucket} for this counter @param [String, nil] key The name of the counter. A nil key makes

Riak assign a key.

@param [String] bucket_type The optional bucket type for this counter.

The default is in `Crdt::Base::DEFAULT_BUCKET_TYPES[:counter]`.

@param [Hash] options

# File lib/riak/crdt/base.rb, line 33
def initialize(bucket, key, bucket_type, options = {})
  configure_bucket bucket
  configure_key key
  configure_bucket_type bucket_type
  @options = options

  @dirty = true
end

Public Instance Methods

==(other) click to toggle source
# File lib/riak/crdt/base.rb, line 64
def ==(other)
  return false unless self.class == other.class
  return false unless self.bucket_type == other.bucket_type
  return false unless self.bucket == other.bucket
  return false unless self.key == other.key
  return true
end
context?() click to toggle source

Does this CRDT have the context necessary to remove elements?

@return [Boolean] if the set has a defined context

# File lib/riak/crdt/base.rb, line 60
def context?
  !!@context
end
dirty?() click to toggle source
# File lib/riak/crdt/base.rb, line 42
def dirty?
  @dirty
end
inspect_name() click to toggle source
# File lib/riak/crdt/base.rb, line 92
def inspect_name
  "#<#{self.class.name} bucket=#{@bucket.name} " \
  "key=#{@key} type=#{@bucket_type}>"
end
pretty_print(pp) { || ... } click to toggle source
# File lib/riak/crdt/base.rb, line 72
def pretty_print(pp)
  pp.object_group self do
    pp.breakable
    pp.text "bucket_type=#{@bucket_type}"
    pp.comma_breakable
    pp.text "bucket=#{@bucket.name}"
    pp.comma_breakable
    pp.text "key=#{@key}"

    yield
  end
end
pretty_print_cycle(pp) click to toggle source
# File lib/riak/crdt/base.rb, line 85
def pretty_print_cycle(pp)
  pp.object_group self do
    pp.breakable
    pp.text "#{@bucket_type}/#{@bucket.name}/#{@key}"
  end
end
reload() click to toggle source

Force a reload of this structure from Riak.

# File lib/riak/crdt/base.rb, line 47
def reload
  loader do |l|
    vivify l.load @bucket, @key, @bucket_type
    @context = l.context
  end
  @dirty = false

  self
end

Private Instance Methods

backend(&blk) click to toggle source
# File lib/riak/crdt/base.rb, line 103
def backend(&blk)
  client.backend(&blk)
end
client() click to toggle source
# File lib/riak/crdt/base.rb, line 99
def client
  @bucket.client
end
configure_bucket(bucket) click to toggle source
# File lib/riak/crdt/base.rb, line 166
def configure_bucket(bucket)
  unless bucket.is_a? Bucket
    fail ArgumentError, t('bucket_type', bucket: bucket.inspect)
  end

  @bucket = bucket
end
configure_bucket_type(constructor_type) click to toggle source
# File lib/riak/crdt/base.rb, line 154
def configure_bucket_type(constructor_type)
  @bucket_type = if constructor_type.is_a? String
                   constructor_type
                 elsif constructor_type.is_a? BucketType
                   constructor_type.name
                 elsif @bucket.is_a? BucketTyped::Bucket
                   @bucket.type.name
                 elsif constructor_type.is_a? Symbol
                   DEFAULT_BUCKET_TYPES[constructor_type]
                 end
end
configure_key(key) click to toggle source
# File lib/riak/crdt/base.rb, line 174
def configure_key(key)
  unless key.is_a?(String) || key.nil?
    fail ArgumentError, t('string_type', string: key.inspect)
  end

  @key = key
end
loader() { |crdt_loader| ... } click to toggle source
# File lib/riak/crdt/base.rb, line 107
def loader
  backend do |be|
    yield be.crdt_loader
  end
end
operate(*args) click to toggle source
# File lib/riak/crdt/base.rb, line 119
def operate(*args)
  options = {}
  options = args.pop if args.last.is_a? Hash
  options[:context] ||= @context
  result = operator do |op|
    response = op.operate(bucket.name,
                          key,
                          bucket_type,
                          *args,
                          options
                          )

    break if :empty == response
    @key = response.key if response.key
    response
  end

  @dirty = true
  vivify_returnbody(result)

  true
end
operator() { |crdt_operator| ... } click to toggle source
# File lib/riak/crdt/base.rb, line 113
def operator
  backend do |be|
    yield be.crdt_operator
  end
end
vivify_returnbody(result) click to toggle source
# File lib/riak/crdt/base.rb, line 142
def vivify_returnbody(result)
  loader do |l|
    specific_loader = l.get_loader_for_value result

    return false if specific_loader.nil?

    vivify specific_loader.rubyfy
    @context = result.context unless result.context.nil?
    @dirty = false
  end
end