Class: Seabright::Storage::Redis

Inherits:
Adapter
  • Object
show all
Defined in:
lib/redis_object/storage/redis.rb

Constant Summary collapse

DUMP_SEPARATOR =
"---:::RedisObject::DUMP_SEPARATOR:::---"
REC_SEPARATOR =
"---:::RedisObject::REC_SEPARATOR:::---"

Instance Method Summary collapse

Methods inherited from Adapter

#config, #config_opts, #configure, #connection, #connections, #initialize, #reset

Constructor Details

This class inherits a constructor from Seabright::Storage::Adapter

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/redis_object/storage/redis.rb', line 5

def method_missing(sym, *args, &block)
	return super unless connection.respond_to?(sym)
	puts "[Storage::Redis] #{sym}(#{args.inspect.gsub(/\[|\]/m,'')})" if Debug.verbose?
	begin
		connection.send(sym,*args, &block)
	rescue ::Redis::InheritedError => err
		puts "Rescued: #{err.inspect}" if DEBUG
		reset
		connection.send(sym,*args, &block)
	rescue ::Redis::TimeoutError => err
		puts "Rescued connection timeout: #{err.inspect}" if DEBUG
		reset
		connection.send(sym,*args, &block)
	end
end

Instance Method Details

#dump_to_file(file) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/redis_object/storage/redis.rb', line 30

def dump_to_file(file)
	File.open(file,'wb') do |f|
		keys = connection.send(:keys,"*")
		f.write keys.map {|k|
			v = connection.dump(k)
			v.force_encoding(Encoding::BINARY)
			[k,v].join(DUMP_SEPARATOR)
		}.join(REC_SEPARATOR)
	end
end

#new_connection ⇒ Object



21
22
23
24
25
# File 'lib/redis_object/storage/redis.rb', line 21

def new_connection
	require 'redis'
	puts "Connecting to Redis with: #{config_opts(:path, :db, :password, :host, :port, :timeout, :tcp_keepalive).inspect}" if DEBUG
	::Redis.new(config_opts(:path, :db, :password, :host, :port, :timeout, :tcp_keepalive))
end

#rename_class(old_name, new_name) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/redis_object/storage/redis.rb', line 54

def rename_class old_name, new_name
	old_name = old_name.to_s#.split('::').last
	new_name = new_name.to_s#.split('::').last
	old_collection_name = old_name.split('::').last.underscore.pluralize
	new_collection_name = new_name.split('::').last.underscore.pluralize

	# references to type in collection data
	keys("#{old_name}:*:backreferences").each do |backref_key|
		smembers(backref_key).each do |hashref|
			# there are two referenes we need to fix: individual references to items
			# and lists of collection names.
			#
			# this updates the item references in collections
			backref = hashref.sub(/_h$/,'');
			old_collection = "#{backref}:COLLECTION:#{old_collection_name}"
			new_collection = "#{backref}:COLLECTION:#{new_collection_name}"
			zrange(old_collection, 0, 99999, withscores:true).each do |key, score|
				zadd(new_collection, score, key.sub(/^#{old_name}/, new_name))
			end
			del(old_collection)

			# this updates the lists of collection names
			collection_names = "#{hashref}:collections"
			smembers(collection_names).each do |collection_name|
				if collection_name == old_collection_name
					sadd(collection_names, new_collection_name)
					srem(collection_names, old_collection_name)
				end
			end
		end
		rename(backref_key, backref_key.sub(/^#{old_name}/, new_name))
	end

	# type-wide id index
	smembers(old_name.pluralize).each do |key|
		sadd(new_name.pluralize, key.sub(/^#{old_name}/, new_name))
		old_class = hget("#{key}_h", :class)
		old_key = hget("#{key}_h", :key)
		hset("#{key}_h", :class, old_class.sub(/#{old_name}$/, new_name))
		hset("#{key}_h", :key, old_key.sub(/^#{old_name}/, new_name))
		hset("#{key}_h", "#{new_name.downcase}_id", key.sub(/^#{old_name}:/,''))
		hdel("#{key}_h", "#{old_name.downcase}_id")
	end
	del(old_name.pluralize)

	# column indexes
	keys("#{old_name.pluralize}::*").each do |old_index|
		new_index = old_index.sub(/^#{old_name.pluralize}/, new_name.pluralize)
		zrange(old_index, 0, 99999, withscores:true).each do |key, score|
			zadd(new_index, score, key.sub(/^#{old_name}/, new_name))
		end
		del(old_index)
	end

	# top-level keys
	keys("#{old_name}:*").each do |key|
		rename(key, key.sub(/^#{old_name}/, new_name))
	end
	keys("#{old_name.pluralize}:*").each do |key|
		rename(key, key.sub(/^#{old_name.pluralize}/, new_name.pluralize))
	end
end

#restore_from_file(file) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/redis_object/storage/redis.rb', line 41

def restore_from_file(file)
	str = File.read(file)
	str.force_encoding(Encoding::BINARY)
	str.split(REC_SEPARATOR).each do |line|
		line.force_encoding(Encoding::BINARY)
		key, val = line.split(DUMP_SEPARATOR)
		connection.multi do
			connection.del key
			connection.restore key, 0, val
		end
	end
end