Module: Redis::Dump::ClassMethods

Included in:
Redis::Dump
Defined in:
lib/redis/dump.rb

Instance Method Summary collapse

Instance Method Details

#dump(this_redis, key, type = nil) ⇒ Object



181
182
183
184
185
186
187
188
189
190
# File 'lib/redis/dump.rb', line 181

def dump(this_redis, key, type=nil)
  type ||= type(this_redis, key)
  info = { 'db' => this_redis.client.db, 'key' => key }
  info['ttl'] = this_redis.ttl key
  info['type'] = type
  info['value'] = value(this_redis, key, info['type'])
  info['size'] = stringify(this_redis, key, info['type'], info['value']).size
  #ld "dump[#{this_redis.hash}, #{info}]"
  info
end

#dump_strings(this_redis, keys) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/redis/dump.rb', line 191

def dump_strings(this_redis, keys)
  vals = this_redis.mget *keys
  idx = -1
  keys.collect { |key|
    idx += 1
    info = {
      'db' => this_redis.client.db, 'key' => key,
      'ttl' => this_redis.ttl(key), 'type' => 'string',
      'value' => vals[idx].to_s, 'size' => vals[idx].to_s.size
    }
    block_given? ? yield(info) : info
  }
end

#report(this_redis, key) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/redis/dump.rb', line 173

def report(this_redis, key)
  info = { 'db' => this_redis.client.db, 'key' => key }
  info['type'] = type(this_redis, key)
  info['size'] = stringify(this_redis, key, info['type'], info['value']).size
  info['bytes'] = info['size'].to_bytes
  #ld "report[#{this_redis.hash}, #{info}]"
  info
end

#set_value(this_redis, key, type, value, expire = nil) ⇒ Object



204
205
206
207
208
# File 'lib/redis/dump.rb', line 204

def set_value(this_redis, key, type, value, expire=nil)
  t ||= type
  send("set_value_#{t}", this_redis, key, value)
  this_redis.expire key, expire if expire.to_s.to_i > 0
end

#set_value_hash(this_redis, key, hash) ⇒ Object



216
217
218
# File 'lib/redis/dump.rb', line 216

def set_value_hash(this_redis, key, hash)
  hash.keys.each { |k|  this_redis.hset key, k, hash[k] }
end

#set_value_list(this_redis, key, list) ⇒ Object



219
220
221
# File 'lib/redis/dump.rb', line 219

def set_value_list(this_redis, key, list)
  list.each { |value|  this_redis.rpush key, value }
end

#set_value_none(this_redis, key, str) ⇒ Object



231
232
233
# File 'lib/redis/dump.rb', line 231

def set_value_none(this_redis, key, str)
  # ignore
end

#set_value_set(this_redis, key, set) ⇒ Object



222
223
224
# File 'lib/redis/dump.rb', line 222

def set_value_set(this_redis, key, set)
  set.each { |value|  this_redis.sadd key, value }
end

#set_value_string(this_redis, key, str) ⇒ Object



228
229
230
# File 'lib/redis/dump.rb', line 228

def set_value_string(this_redis, key, str)
  this_redis.set key, str
end

#set_value_zset(this_redis, key, zset) ⇒ Object



225
226
227
# File 'lib/redis/dump.rb', line 225

def set_value_zset(this_redis, key, zset)
  zset.each { |pair|  this_redis.zadd key, pair[1].to_f, pair[0] }
end

#stringify(this_redis, key, t = nil, v = nil) ⇒ Object



212
213
214
# File 'lib/redis/dump.rb', line 212

def stringify(this_redis, key, t=nil, v=nil)
  send("stringify_#{t}", this_redis, key, v)
end

#stringify_hash(this_redis, key, v = nil) ⇒ Object



245
# File 'lib/redis/dump.rb', line 245

def stringify_hash  (this_redis, key, v=nil)  (v || value_hash(this_redis, key)).to_a.flatten.compact.join  end

#stringify_list(this_redis, key, v = nil) ⇒ Object



242
# File 'lib/redis/dump.rb', line 242

def stringify_list  (this_redis, key, v=nil)  (v || value_list(this_redis, key)).join                       end

#stringify_none(this_redis, key, v = nil) ⇒ Object



246
# File 'lib/redis/dump.rb', line 246

def stringify_none  (this_redis, key, v=nil)  (v || '')                                                     end

#stringify_set(this_redis, key, v = nil) ⇒ Object



243
# File 'lib/redis/dump.rb', line 243

def stringify_set   (this_redis, key, v=nil)  (v || value_set(this_redis, key)).join                        end

#stringify_string(this_redis, key, v = nil) ⇒ Object



241
# File 'lib/redis/dump.rb', line 241

def stringify_string(this_redis, key, v=nil)  (v || value_string(this_redis, key))                          end

#stringify_zset(this_redis, key, v = nil) ⇒ Object



244
# File 'lib/redis/dump.rb', line 244

def stringify_zset  (this_redis, key, v=nil)  (v || value_zset(this_redis, key)).flatten.compact.join       end

#type(this_redis, key) ⇒ Object

Raises:

  • (TypeError)


168
169
170
171
172
# File 'lib/redis/dump.rb', line 168

def type(this_redis, key)
  type = this_redis.type key
  raise TypeError, "Unknown type: #{type}" if !VALID_TYPES.member?(type)
  type
end

#value(this_redis, key, t = nil) ⇒ Object



209
210
211
# File 'lib/redis/dump.rb', line 209

def value(this_redis, key, t=nil)
  send("value_#{t}", this_redis, key)
end

#value_hash(this_redis, key) ⇒ Object



239
# File 'lib/redis/dump.rb', line 239

def value_hash  (this_redis, key)  this_redis.hgetall(key)                                                  end

#value_list(this_redis, key) ⇒ Object



236
# File 'lib/redis/dump.rb', line 236

def value_list  (this_redis, key)  this_redis.lrange key, 0, -1                                             end

#value_none(this_redis, key) ⇒ Object



240
# File 'lib/redis/dump.rb', line 240

def value_none  (this_redis, key)  ''                                                                       end

#value_set(this_redis, key) ⇒ Object



237
# File 'lib/redis/dump.rb', line 237

def value_set   (this_redis, key)  this_redis.smembers key                                                  end

#value_string(this_redis, key) ⇒ Object



235
# File 'lib/redis/dump.rb', line 235

def value_string(this_redis, key)  this_redis.get key                                                       end

#value_zset(this_redis, key) ⇒ Object



238
# File 'lib/redis/dump.rb', line 238

def value_zset  (this_redis, key)  this_redis.zrange(key, 0, -1, :with_scores => true).flatten.tuple        end