redict/utils/redis-sha1.rb
2024-03-21 14:30:47 +01:00

50 lines
1.6 KiB
Ruby

# Copyright (C) 2009 Salvatore Sanfilippo
# SPDX-FileCopyrightText: 2024 Redict Contributors
# SPDX-FileCopyrightText: 2024 Salvatore Sanfilippo <antirez at gmail dot com>
#
# SPDX-License-Identifier: BSD-3-Clause
# SPDX-License-Identifier: GPL-3.0-only
require 'rubygems'
require 'redis'
require 'digest/sha1'
def redisSha1(opts={})
sha1=""
r = Redis.new(opts)
r.keys('*').sort.each{|k|
vtype = r.type?(k)
if vtype == "string"
len = 1
sha1 = Digest::SHA1.hexdigest(sha1+k)
sha1 = Digest::SHA1.hexdigest(sha1+r.get(k))
elsif vtype == "list"
len = r.llen(k)
if len != 0
sha1 = Digest::SHA1.hexdigest(sha1+k)
sha1 = Digest::SHA1.hexdigest(sha1+r.list_range(k,0,-1).join("\x01"))
end
elsif vtype == "set"
len = r.scard(k)
if len != 0
sha1 = Digest::SHA1.hexdigest(sha1+k)
sha1 = Digest::SHA1.hexdigest(sha1+r.set_members(k).to_a.sort.join("\x02"))
end
elsif vtype == "zset"
len = r.zcard(k)
if len != 0
sha1 = Digest::SHA1.hexdigest(sha1+k)
sha1 = Digest::SHA1.hexdigest(sha1+r.zrange(k,0,-1).join("\x01"))
end
end
# puts "#{k} => #{sha1}" if len != 0
}
sha1
end
host = ARGV[0] || "127.0.0.1"
port = ARGV[1] || "6379"
db = ARGV[2] || "0"
puts "Performing SHA1 of Redis server #{host} #{port} DB: #{db}"
p "Dataset SHA1: #{redisSha1(:host => host, :port => port.to_i, :db => db)}"