class Aerospike::Key

Attributes

digest[R]
namespace[R]
set_name[R]

Public Class Methods

disable_unsupported_key_warning!(enable_warning = false) click to toggle source

Keys other than integers, strings and bytes are unsupported and will trigger a warning if used. Starting with v3 the client will raise an error instead of a warning. ref. github.com/aerospike/aerospike-client-ruby/issues/43

# File lib/aerospike/key.rb, line 47
def self.disable_unsupported_key_warning!(enable_warning = false)
  @unsupported_key_warning = enable_warning
end
enable_v1_compatibility!(comp = true) click to toggle source

enable backwards compatibility with v1 client for integer keys ref. github.com/aerospike/aerospike-client-ruby/pull/34

# File lib/aerospike/key.rb, line 37
def self.enable_v1_compatibility!(comp = true)
  @v1_compatibility = !!comp
end
new(ns, set, val, digest=nil, bval: nil, v1_compatible: self.class.v1_compatible?) click to toggle source
# File lib/aerospike/key.rb, line 56
def initialize(ns, set, val, digest=nil, bval: nil, v1_compatible: self.class.v1_compatible?)
  @namespace = ns
  @set_name = set
  @user_key = Value.of(val)
  check_key!(@namespace, @set_name, @user_key, !digest.nil?)
  @digest = digest || compute_digest(v1_compatible)
  @bval = bval
end
v1_compatible?() click to toggle source
# File lib/aerospike/key.rb, line 40
def self.v1_compatible?
  @v1_compatibility
end
warn_unsupported_key?() click to toggle source
# File lib/aerospike/key.rb, line 50
def self.warn_unsupported_key?
  @unsupported_key_warning
end

Public Instance Methods

==(other) click to toggle source
# File lib/aerospike/key.rb, line 82
def ==(other)
  other && other.is_a?(Key) &&
    other.digest == @digest &&
    other.namespace == @namespace
end
Also aliased as: eql?
bval() click to toggle source
# File lib/aerospike/key.rb, line 66
def bval
  @bval
end
eql?(other)
Alias for: ==
hash() click to toggle source
# File lib/aerospike/key.rb, line 89
def hash
  @digest.hash
end
partition_id() click to toggle source
# File lib/aerospike/key.rb, line 93
def partition_id
  (@digest[0..3].unpack(Partition::UNPACK_FORMAT)[0] & 0xFFFF) % Node::PARTITIONS
end
to_s() click to toggle source
# File lib/aerospike/key.rb, line 70
def to_s
  "#{@namespace}:#{@set_name}:#{@user_key}:#{@digest.nil? ? '' : @digest.bytes}"
end
user_key() click to toggle source
# File lib/aerospike/key.rb, line 74
def user_key
  @user_key.get if @user_key
end
user_key_as_value() click to toggle source
# File lib/aerospike/key.rb, line 78
def user_key_as_value
  @user_key
end

Private Instance Methods

check_key!(ns, set, value, has_digest) click to toggle source
# File lib/aerospike/key.rb, line 106
def check_key!(ns, set, value, has_digest)
  if self.class.warn_unsupported_key? && !valid_key?(value, has_digest)
    warn("Unsupported key type: #{value.class.name} - only Integer, String and Bytes are supported")
  end
end
compute_digest(v1_compatible = false) click to toggle source
# File lib/aerospike/key.rb, line 112
def compute_digest(v1_compatible = false)
  key_type = @user_key.type
  key_bytes = @user_key.to_bytes

  if key_type == Aerospike::ParticleType::NULL
    raise Aerospike::Exceptions::Aerospike.new(Aerospike::ResultCode::PARAMETER_ERROR, "Invalid key: nil")
  end

  # v1.0.12 and prior computed integer key digest using little endian byte order
  if key_type == Aerospike::ParticleType::INTEGER && v1_compatible
    key_bytes.reverse!
  end

  # get a hash from pool and make it ready for work
  h = @@digest_pool.poll
  h.reset

  # Compute a complete digest
  h.update(@set_name)
  h.update(key_type.chr)
  h.update(key_bytes)
  digest = h.digest

  # put the hash object back to the pool
  @@digest_pool.offer(h)

  digest
end
valid_key?(value, has_digest) click to toggle source
# File lib/aerospike/key.rb, line 99
def valid_key?(value, has_digest)
  value.is_a?(IntegerValue) ||
    value.is_a?(StringValue) ||
    value.is_a?(BytesValue) ||
    (has_digest && value.is_a?(NullValue))
end