]> git.saurik.com Git - redis.git/blob - utils/redis-sha1.rb
redis-sha1 utility added
[redis.git] / utils / redis-sha1.rb
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
11 require 'rubygems'
12 require 'redis'
13 require 'digest/sha1'
14
15 def redisSha1(opts={})
16 sha1=""
17 r = Redis.new(opts)
18 r.keys('*').sort.each{|k|
19 sha1 = Digest::SHA1.hexdigest(sha1+k)
20 vtype = r.type?(k)
21 if vtype == "string"
22 sha1 = Digest::SHA1.hexdigest(sha1+r.get(k))
23 elsif vtype == "list"
24 sha1 = Digest::SHA1.hexdigest(sha1+r.list_range(k,0,-1).join("\x01"))
25 elsif vtype == "set"
26 sha1 = Digest::SHA1.hexdigest(sha1+r.set_members(k).to_a.sort.join("\x02"))
27 end
28 }
29 sha1
30 end
31
32 p "Dataset SHA1: #{redisSha1()}"