lib/aerospike/key.rb in aerospike-1.0.12 vs lib/aerospike/key.rb in aerospike-2.0.0
- old
+ new
@@ -15,41 +15,54 @@
# the License.
require 'digest'
require 'aerospike/value/value'
+require 'aerospike/utils/pool'
module Aerospike
class Key
@@digest_pool = Pool.new
@@digest_pool.create_block = Proc.new do
- unless RUBY_PLATFORM == 'java'
- Digest::RMD160.new
+ if RUBY_PLATFORM == 'java'
+ OpenSSL::Digest::RIPEMD160.new
else
- h = OpenSSL::Digest::RIPEMD160.new
+ Digest::RMD160.new
end
end
+ # enable backwards compatibility with v1 client for integer keys
+ # ref. https://github.com/aerospike/aerospike-client-ruby/pull/34
+ def self.enable_v1_compatibility!(comp = true)
+ @v1_compatibility = !!comp
+ end
+ def self.v1_compatible?
+ @v1_compatibility
+ end
attr_reader :namespace, :set_name, :digest
+ attr_reader :v1_compatible
+ alias_method :v1_compatible?, :v1_compatible
- def initialize(ns, set, val, digest=nil)
+ def initialize(ns, set, val, digest=nil, v1_compatible: self.class.v1_compatible?)
@namespace = ns
@set_name = set
@user_key = Value.of(val)
+ @v1_compatible = v1_compatible
unless digest
compute_digest
else
@digest = digest
end
self
end
+
def to_s
"#{@namespace}:#{@set_name}:#{@user_key}:#{@digest.nil? ? '' : @digest.bytes}"
end
def user_key
@@ -73,22 +86,28 @@
private
def compute_digest
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(@user_key.to_bytes)
+ h.update(key_bytes)
@digest = h.digest
# put the hash object back to the pool
@@digest_pool.offer(h)
end