Back to home page

Redis cross reference

 
 

    


0001 # redis-sha1.rb - Copyright (C) 2009 Salvatore Sanfilippo
0002 # BSD license, See the COPYING file for more information.
0003 #
0004 # Performs the SHA1 sum of the whole datset.
0005 # This is useful to spot bugs in persistence related code and to make sure
0006 # Slaves and Masters are in SYNC.
0007 #
0008 # If you hack this code make sure to sort keys and set elements as this are
0009 # unsorted elements. Otherwise the sum may differ with equal dataset.
0010 
0011 require
0012 require
0013 require
0014 
0015 def redisSha1(opts={})
0016     sha1=""
0017     r = Redis.new(opts)
0018     r.keys('*').sort.each{|k|
0019         vtype = r.type?(k)
0020         if vtype == "string"
0021             len = 1
0022             sha1 = Digest::SHA1.hexdigest(sha1+k)
0023             sha1 = Digest::SHA1.hexdigest(sha1+r.get(k))
0024         elsif vtype == "list"
0025             len = r.llen(k)
0026             if len != 0
0027                 sha1 = Digest::SHA1.hexdigest(sha1+k)
0028                 sha1 = Digest::SHA1.hexdigest(sha1+r.list_range(k,0,-1).join("\x01"))
0029             end
0030         elsif vtype == "set"
0031             len = r.scard(k)
0032             if len != 0
0033                 sha1 = Digest::SHA1.hexdigest(sha1+k)
0034                 sha1 = Digest::SHA1.hexdigest(sha1+r.set_members(k).to_a.sort.join("\x02"))
0035             end
0036         elsif vtype == "zset"
0037             len = r.zcard(k)
0038             if len != 0
0039                 sha1 = Digest::SHA1.hexdigest(sha1+k)
0040                 sha1 = Digest::SHA1.hexdigest(sha1+r.zrange(k,0,-1).join("\x01"))
0041             end
0042         end
0043         # puts "#{k} => #{sha1}" if len != 0
0044     }
0045     sha1
0046 end
0047 
0048 host = ARGV[0] || "127.0.0.1"
0049 port = ARGV[1] || "6379"
0050 db = ARGV[2] || "0"
0051 puts "Performing SHA1 of Redis server #{host} #{port} DB: #{db}"
0052 p "Dataset SHA1: #{redisSha1(:host => host, :port => port.to_i, :db => db)}"