]> git.saurik.com Git - redis.git/blame - utils/redis-sha1.rb
Invert two sides of if expression in SET to avoid a lookup.
[redis.git] / utils / redis-sha1.rb
CommitLineData
8de4907a 1# redis-sha1.rb - Copyright (C) 2009 Salvatore Sanfilippo
2# BSD license, See the COPYING file for more information.
3#
4# Performs the SHA1 sum of the whole datset.
5# This is useful to spot bugs in persistence related code and to make sure
6# Slaves and Masters are in SYNC.
7#
8# If you hack this code make sure to sort keys and set elements as this are
9# unsorted elements. Otherwise the sum may differ with equal dataset.
10
11require 'rubygems'
12require 'redis'
13require 'digest/sha1'
14
15def redisSha1(opts={})
16 sha1=""
17 r = Redis.new(opts)
18 r.keys('*').sort.each{|k|
8de4907a 19 vtype = r.type?(k)
20 if vtype == "string"
b32627cd 21 len = 1
22 sha1 = Digest::SHA1.hexdigest(sha1+k)
8de4907a 23 sha1 = Digest::SHA1.hexdigest(sha1+r.get(k))
24 elsif vtype == "list"
b32627cd 25 len = r.llen(k)
26 if len != 0
27 sha1 = Digest::SHA1.hexdigest(sha1+k)
28 sha1 = Digest::SHA1.hexdigest(sha1+r.list_range(k,0,-1).join("\x01"))
29 end
8de4907a 30 elsif vtype == "set"
b32627cd 31 len = r.scard(k)
32 if len != 0
33 sha1 = Digest::SHA1.hexdigest(sha1+k)
34 sha1 = Digest::SHA1.hexdigest(sha1+r.set_members(k).to_a.sort.join("\x02"))
35 end
5ad3c8c8 36 elsif vtype == "zset"
b32627cd 37 len = r.zcard(k)
38 if len != 0
39 sha1 = Digest::SHA1.hexdigest(sha1+k)
40 sha1 = Digest::SHA1.hexdigest(sha1+r.zrange(k,0,-1).join("\x01"))
41 end
8de4907a 42 end
b32627cd 43 # puts "#{k} => #{sha1}" if len != 0
8de4907a 44 }
45 sha1
46end
47
57172ffb 48host = ARGV[0] || "127.0.0.1"
49port = ARGV[1] || "6379"
e96e4fbf 50db = ARGV[2] || "0"
51puts "Performing SHA1 of Redis server #{host} #{port} DB: #{db}"
52p "Dataset SHA1: #{redisSha1(:host => host, :port => port.to_i, :db => db)}"