2
0

redis-sha1.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 dataset.
  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. require 'rubygems'
  11. require 'redis'
  12. require 'digest/sha1'
  13. def redisSha1(opts={})
  14. sha1=""
  15. r = Redis.new(opts)
  16. r.keys('*').sort.each{|k|
  17. vtype = r.type?(k)
  18. if vtype == "string"
  19. len = 1
  20. sha1 = Digest::SHA1.hexdigest(sha1+k)
  21. sha1 = Digest::SHA1.hexdigest(sha1+r.get(k))
  22. elsif vtype == "list"
  23. len = r.llen(k)
  24. if len != 0
  25. sha1 = Digest::SHA1.hexdigest(sha1+k)
  26. sha1 = Digest::SHA1.hexdigest(sha1+r.list_range(k,0,-1).join("\x01"))
  27. end
  28. elsif vtype == "set"
  29. len = r.scard(k)
  30. if len != 0
  31. sha1 = Digest::SHA1.hexdigest(sha1+k)
  32. sha1 = Digest::SHA1.hexdigest(sha1+r.set_members(k).to_a.sort.join("\x02"))
  33. end
  34. elsif vtype == "zset"
  35. len = r.zcard(k)
  36. if len != 0
  37. sha1 = Digest::SHA1.hexdigest(sha1+k)
  38. sha1 = Digest::SHA1.hexdigest(sha1+r.zrange(k,0,-1).join("\x01"))
  39. end
  40. end
  41. # puts "#{k} => #{sha1}" if len != 0
  42. }
  43. sha1
  44. end
  45. host = ARGV[0] || "127.0.0.1"
  46. port = ARGV[1] || "6379"
  47. db = ARGV[2] || "0"
  48. puts "Performing SHA1 of Redis server #{host} #{port} DB: #{db}"
  49. p "Dataset SHA1: #{redisSha1(:host => host, :port => port.to_i, :db => db)}"