Class: RedisLoad::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/redis-load/loader.rb

Overview

Exposes support for loading and saving different types of redis data structures to and from files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis) ⇒ Loader

Returns a new instance of Loader.



9
10
11
# File 'lib/redis-load/loader.rb', line 9

def initialize(redis)
  @redis = redis      
end

Instance Attribute Details

#redisObject (readonly)

Returns the value of attribute redis.



7
8
9
# File 'lib/redis-load/loader.rb', line 7

def redis
  @redis
end

Instance Method Details

#load_json(file, path = nil) ⇒ Object

Load the contents of a JSON file into Redis

Each key in the JSON file becomes a redis key. If the value is a literal then this is added as a redis key-value pair. If an array, then a List. If a hash, then a Hash.

Nested objects will be flattened.

file:: json file
path:: optional, path in the json file indicating the subsection to load


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
# File 'lib/redis-load/loader.rb', line 54

def load_json(file, path=nil)
  f = File.new( file )
  json = JSON.parse( f.read )
  if path != nil
    json = Siren.query(query, json)
  end
  counter = 0
  #TODO could support proper results, not just hash?
  if json != nil
    json.each_pair do |key,value|
      if value.class() == Array
        value.each do |v|
          @redis.lpush( key, v )
        end          
      elsif value.class == Hash
        value.each_pair do |field, v|
          #TODO better handling of nesting?
          #E.g. convert hash or array back into JSON?
          @redis.hset(key, field, v)
        end
      else
        @redis.set(key, value)
      end
      counter = counter + 1      
    end        
  end
  return counter
end

#load_keys(file) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/redis-load/loader.rb', line 35

def load_keys(file)
  f = File.new( file )
  counter = process_lines(f) do |redis,line|
    line = line.chomp
    parts = line.split(",")
    redis.set( parts[0], parts[1] )
  end
  return counter
end

#load_list(key, file) ⇒ Object

Load the contents of a file into a Redis list

key:: name of the list
file:: name of file (string)


87
88
89
90
91
92
93
# File 'lib/redis-load/loader.rb', line 87

def load_list(key, file)
  f = File.new( file )
  counter = process_lines(f) do |redis,line|
    redis.lpush( key , line.chomp)
  end
  return counter            
end

#load_set(key, file) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/redis-load/loader.rb', line 95

def load_set(key, file)
  f = File.new( file )
  counter = process_lines(f) do |redis,line|
    redis.sadd( key , line.chomp)
  end
  return counter
end

#load_zset(key, file) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/redis-load/loader.rb', line 103

def load_zset(key, file)
  f = File.new( file )
  counter = process_lines(f) do |redis,line|
    line = line.chomp
    parts = line.split(",")
    redis.set( key , parts[1], parts[0] )
  end
  return counter
end

#process_lines(io) ⇒ Object

Process lines

io:: IO Object


158
159
160
161
162
163
164
165
# File 'lib/redis-load/loader.rb', line 158

def process_lines(io)
  counter = 0
  io.each_line do |line|
    yield @redis, line
    counter = counter + 1
  end
  return counter      
end

#save_json(file, pattern = "*", limit = -1,, opts = nil) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/redis-load/loader.rb', line 113

def save_json(file, pattern="*", limit=-1, opts=nil)
  counter = 0
  File.open( file, "w" ) do |file|
    json, count = to_json(pattern, limit, opts)
    file.write( json )
    counter = count
  end
  return counter
end

#save_list(key, file) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/redis-load/loader.rb', line 123

def save_list(key, file)
  size = @redis.llen( key )
  count = 0
  File.open( file , "w") do |file|
    while (count < size)
      file.puts( @redis.lindex(key , count) )
      count = count + 1
    end
  end
  return size      
end

#save_set(key, file) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/redis-load/loader.rb', line 135

def save_set(key, file)
  members = @redis.smembers( key )
  File.open( file , "w") do |file|
    members.each do |m|
      file.puts(m)
    end
  end
  return members.length      
end

#save_zset(key, file) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/redis-load/loader.rb', line 145

def save_zset(key, file)
  members = @redis.zrange( key , 0, -1, :with_scores => true)
  File.open( file , "w") do |file|
    members.each_slice(2) do |member,value|
      file.puts("#{member},#{value}")
    end
  end
  return members.length/2  
end

#to_json(pattern = "*", limit = -1,, opts = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/redis-load/loader.rb', line 13

def to_json( pattern="*", limit=-1, opts=nil )
  keys = @redis.keys(pattern)
  h = Hash.new
  counter = 0
  keys.each do |key|
    counter = counter + 1
    type = @redis.type(key)
    if type == "hash"
      h[key] = @redis.hgetall(key)
    elsif type == "list"
      h[key] = @redis.lrange(key, 0, limit)
    elsif type == "set"
      h[key] = @redis.smembers(key)
    elsif type == "zset"
      h[key] = @redis.zrange(key, 0, limit)
    else
      h[key] = @redis.get(key)
    end  
  end     
  return JSON.fast_generate(h, opts), counter
end