redis-datapump 0.1.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0bc5542d412bc18d7e6513203a11ad778f9b0f5b
4
+ data.tar.gz: f45e34c40c43c28aec4dc916758766c8915ed08b
5
+ SHA512:
6
+ metadata.gz: e9af5cfec5614cb2f2c3897d10b030843163a04ea750dc8d0bbd650b871c63bb5ee1dc5a3def07fe8674d74b1a04db3539ced248939c483e3c37b55a28ee1b1d
7
+ data.tar.gz: cae3669f23483794f82c5f52b4c6e7692bb240bfd40ce069ca458694c131a7016bb58812bb3b4918d3073c0152bd2df0d1809f0045c97892de1bd14c610756ec
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor/bundle
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --tty --color --format documentation
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test, :development do
6
+ gem 'coveralls', require: false
7
+ gem 'guard'
8
+ gem 'guard-rspec'
9
+
10
+ gem 'rb-inotify', require: false
11
+ gem 'rb-fsevent', require: false
12
+ gem 'rb-fchange', require: false
13
+ end
@@ -0,0 +1,5 @@
1
+ guard :rspec do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { 'spec' }
5
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Leif Gensert
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,55 @@
1
+ # Redis::Datapump
2
+
3
+ This tool let's you export and import redis database into a json format.
4
+
5
+ It is loosely based on [redis-dump](http://github.com/delano/redis-dump).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'redis-datapump'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install redis-datapump
20
+
21
+ ## Usage
22
+
23
+ There are 2 tools that are shipped with this gem:
24
+
25
+ redis-export -d 0
26
+
27
+ will ouput the keys to the console.
28
+
29
+ You can output it to a file or pipe it to another process.
30
+
31
+ With
32
+
33
+ cat backup.json | redis-import -d 0
34
+
35
+ you'll import the database again
36
+
37
+
38
+ ## Format
39
+
40
+ The format of the dump looks as following:
41
+
42
+ {
43
+ "ttl": -1,
44
+ "value": "b",
45
+ "type": "string",
46
+ "key": "a"
47
+ }
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ task default: :spec
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'redis-datapump'
5
+ require 'multi_json'
6
+
7
+ options = {
8
+ redis_url: 'redis://localhost:6379',
9
+ }
10
+
11
+ OptionParser.new do |opts|
12
+ opts.banner = "Usage: redis-export [options]"
13
+
14
+ opts.on('-u', '--uri=URI', 'Redis URI in format redis://hostname[:port]') do |u|
15
+ options[:redis_url] = u
16
+ end
17
+
18
+ opts.on('-d', '--database=DB', 'Redis database to export') do |d|
19
+ options[:redis_database] = d
20
+ end
21
+ end.parse!
22
+
23
+ json_engine = RUBY_PLATFORM == 'java' ? :jr_jackson : :yajl
24
+
25
+ MultiJson.use json_engine
26
+
27
+ RedisDatapump::Exporter.new(options).export do |value|
28
+ $stdout.puts MultiJson.dump(value, pretty: false)
29
+ end
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'redis-datapump'
5
+ require 'multi_json'
6
+
7
+ options = {
8
+ redis_url: 'redis://localhost:6379',
9
+ }
10
+
11
+ OptionParser.new do |opts|
12
+ opts.banner = "Usage: redis-export [options]"
13
+
14
+ opts.on('-u', '--uri=URI', 'Redis URI in format redis://hostname[:port]') do |u|
15
+ options[:redis_url] = u
16
+ end
17
+
18
+ opts.on('-d', '--database=DB', 'Redis database to export') do |d|
19
+ options[:redis_database] = d
20
+ end
21
+ end.parse!
22
+
23
+ json_engine = RUBY_PLATFORM == 'java' ? :jr_jackson : :yajl
24
+
25
+ MultiJson.use json_engine
26
+
27
+ importer = RedisDatapump::Importer.new(options)
28
+ ARGF.each do |line|
29
+ importer.import(MultiJson.load(line))
30
+ end
@@ -0,0 +1,9 @@
1
+ require 'redis'
2
+ require 'redis-datapump/version'
3
+ require 'redis-datapump/exporter'
4
+ require 'redis-datapump/importer'
5
+ require 'redis-datapump/validator'
6
+ require 'redis-datapump/value_extractor'
7
+
8
+ module RedisDatapump
9
+ end
@@ -0,0 +1,25 @@
1
+ module RedisDatapump
2
+ class Exporter
3
+ def initialize opts
4
+ @options = opts
5
+ end
6
+
7
+ def export &blk
8
+ Validator.new(@options).validate!
9
+ keys('*').each do |key|
10
+ blk.call(ValueExtractor.new(redis_client, key).content)
11
+ end
12
+ end
13
+
14
+ private
15
+ def keys glob
16
+ redis_client.keys(glob)
17
+ end
18
+
19
+ def redis_client
20
+ return @redis_client if @redis_client
21
+ url = "#{@options[:redis_url]}/#{@options[:redis_database]}"
22
+ @redis_client = Redis.connect(url: url)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ module RedisDatapump
2
+ class Importer
3
+ IMPORTER_MAP = {
4
+ 'string' => proc {|client, key, value| client.set(key, value)},
5
+ 'hash' => proc {|client, key, values| client.hmset(key, values)},
6
+ 'set' => proc {|client, key, members| client.sadd(key, members)},
7
+ 'list' => proc {|client, key, values| client.rpush key, values },
8
+ 'zset' => proc {|client, key| client.zrange(key, 0, -1, :with_scores => true) },
9
+ }
10
+ def initialize opts
11
+ @options = opts
12
+ Validator.new(@options).validate!
13
+ end
14
+
15
+ def import content
16
+ IMPORTER_MAP[content['type']].call(redis_client, content['key'], content['value'])
17
+ @redis_client.pexpire(content['key'], content['ttl']) if content['ttl'] != -1
18
+ end
19
+
20
+ def redis_client
21
+ return @redis_client if @redis_client
22
+ url = "#{@options[:redis_url]}/#{@options[:redis_database]}"
23
+ @redis_client = Redis.connect(url: url)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,8 @@
1
+ require 'redis'
2
+ require 'redis_datapump/version'
3
+ require 'redis_datapump/exporter'
4
+ require 'redis_datapump/validator'
5
+ require 'redis_datapump/type_dispatcher'
6
+
7
+ module RedisDatapump
8
+ end
@@ -0,0 +1,13 @@
1
+ module RedisDatapump
2
+ class Validator
3
+ def initialize opts
4
+ @options = opts
5
+ end
6
+
7
+ def validate!
8
+ if @options[:redis_database].nil?
9
+ raise ArgumentError, 'Please specify a database'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,28 @@
1
+ module RedisDatapump
2
+ class ValueExtractor
3
+ VALUE_MAP = {
4
+ 'string' => proc {|client, key| client.get(key)},
5
+ 'hash' => proc {|client, key| client.hgetall(key)},
6
+ 'set' => proc {|client, key| client.smembers(key)},
7
+ 'list' => proc {|client, key| client.lrange key, 0, -1 },
8
+ 'zset' => proc {|client, key| client.zrange(key, 0, -1, :with_scores => true) },
9
+ }
10
+
11
+ def initialize redis_client, key
12
+ @redis_client = redis_client
13
+ @key = key
14
+ end
15
+
16
+ def content
17
+ return @content if @content
18
+
19
+ type = @redis_client.type(@key)
20
+ @content = {
21
+ key: @key,
22
+ type: type,
23
+ value: VALUE_MAP[type].call(@redis_client, @key),
24
+ ttl: @redis_client.pttl(@key),
25
+ }
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module RedisDatapump
2
+ VERSION = '0.1.0.alpha1'
3
+ end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redis-datapump/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'redis-datapump'
8
+ spec.version = RedisDatapump::VERSION
9
+ spec.platform = Gem::Platform::RUBY
10
+ spec.authors = ['Leif Gensert']
11
+ spec.email = ['leif@propertybase.com']
12
+ spec.description = %q{Tool for Importing and Exporting data from/to redis}
13
+ spec.summary = %q{This tool is similar to `redis-dump` but is also working with JRuby}
14
+ spec.homepage = 'http://www.github.com/leifg/redis-datapump'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'redis', '~> 3.0'
23
+ spec.add_dependency 'multi_json', '~> 1.7'
24
+ spec.add_dependency 'json'
25
+
26
+ if RUBY_PLATFORM == 'java'
27
+ spec.add_dependency 'jrjackson'
28
+ else
29
+ spec.add_dependency 'yajl-ruby'
30
+ end
31
+
32
+ spec.add_development_dependency 'bundler', '~> 1.3'
33
+ spec.add_development_dependency 'rake', '~> 10.1'
34
+ spec.add_development_dependency 'mock_redis', '~> 0.8'
35
+
36
+ spec.add_development_dependency 'rspec', '~> 2.14'
37
+ end
@@ -0,0 +1,32 @@
1
+ #encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe RedisDatapump::Exporter do
5
+ subject do
6
+ exporter = described_class.new({
7
+ redis_url: 'redis://localhost:6379',
8
+ redis_database: '0',
9
+ })
10
+ exporter.stub(:redis_client).and_return(mock_redis)
11
+ exporter
12
+ end
13
+
14
+ let(:mock_redis) do
15
+ mr = MockRedis.new
16
+ mr.stub(:pttl).and_return(-1)
17
+ mr
18
+ end
19
+
20
+ describe 'export' do
21
+ it 'calls keys' do
22
+ expect(subject)
23
+ .to receive(:keys).and_return([])
24
+ subject.export
25
+ end
26
+
27
+ it 'yields the content' do
28
+ mock_redis.set('string_key', 'string_value')
29
+ expect{|b| subject.export(&b)}.to yield_control.once
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,18 @@
1
+ #encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe RedisDatapump::Validator do
5
+ describe '#validate!' do
6
+ it 'raises no exception for valid input' do
7
+ expect do
8
+ described_class.new(redis_database: '0').validate!
9
+ end.to_not raise_error
10
+ end
11
+
12
+ it 'raises a RuntimeError for missing redis_database' do
13
+ expect do
14
+ described_class.new({}).validate!
15
+ end.to raise_error(ArgumentError, 'Please specify a database')
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
5
+ SimpleCov.start do
6
+ add_filter 'spec'
7
+ end
8
+
9
+ require 'redis-datapump'
10
+ require 'mock_redis'
11
+
12
+ RSpec.configure do |config|
13
+ config.mock_with :rspec
14
+ config.order = 'random'
15
+ config.filter_run_excluding skip: true
16
+ config.before(:each) do
17
+ Redis.stub(:new) { raise 'Call to real redis in tests' }
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,181 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis-datapump
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.alpha1
5
+ platform: ruby
6
+ authors:
7
+ - Leif Gensert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yajl-ruby
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '10.1'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '10.1'
97
+ - !ruby/object:Gem::Dependency
98
+ name: mock_redis
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '0.8'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '0.8'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '2.14'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ~>
123
+ - !ruby/object:Gem::Version
124
+ version: '2.14'
125
+ description: Tool for Importing and Exporting data from/to redis
126
+ email:
127
+ - leif@propertybase.com
128
+ executables:
129
+ - redis-export
130
+ - redis-import
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - .gitignore
135
+ - .rspec
136
+ - Gemfile
137
+ - Guardfile
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - bin/redis-export
142
+ - bin/redis-import
143
+ - lib/redis-datapump.rb
144
+ - lib/redis-datapump/exporter.rb
145
+ - lib/redis-datapump/importer.rb
146
+ - lib/redis-datapump/redis_datapump.rb
147
+ - lib/redis-datapump/validator.rb
148
+ - lib/redis-datapump/value_extractor.rb
149
+ - lib/redis-datapump/version.rb
150
+ - redis-datapump.gemspec
151
+ - spec/lib/redis-datapump/exporter_spec.rb
152
+ - spec/lib/redis-datapump/validator_spec.rb
153
+ - spec/spec_helper.rb
154
+ homepage: http://www.github.com/leifg/redis-datapump
155
+ licenses:
156
+ - MIT
157
+ metadata: {}
158
+ post_install_message:
159
+ rdoc_options: []
160
+ require_paths:
161
+ - lib
162
+ required_ruby_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ required_rubygems_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - '>'
170
+ - !ruby/object:Gem::Version
171
+ version: 1.3.1
172
+ requirements: []
173
+ rubyforge_project:
174
+ rubygems_version: 2.0.2
175
+ signing_key:
176
+ specification_version: 4
177
+ summary: This tool is similar to `redis-dump` but is also working with JRuby
178
+ test_files:
179
+ - spec/lib/redis-datapump/exporter_spec.rb
180
+ - spec/lib/redis-datapump/validator_spec.rb
181
+ - spec/spec_helper.rb