omnistore 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/omnistore/storage/local.rb +62 -0
- data/lib/omnistore/storage/s3.rb +8 -0
- data/lib/omnistore/version.rb +1 -1
- data/spec/omnistore/storage/s3_spec.rb +66 -32
- data/spec/spec_helper.rb +1 -2
- metadata +4 -4
@@ -4,6 +4,7 @@ module OmniStore
|
|
4
4
|
extend self
|
5
5
|
|
6
6
|
class Mountpoint
|
7
|
+
MEGABYTE = 1024*1024
|
7
8
|
attr_reader :dir
|
8
9
|
|
9
10
|
def initialize(name, dir)
|
@@ -27,6 +28,26 @@ module OmniStore
|
|
27
28
|
FileUtils.rm(expand(path))
|
28
29
|
end
|
29
30
|
|
31
|
+
def read(path, options = {}, &block)
|
32
|
+
size = options[:chunk_size] || MEGABYTE
|
33
|
+
open(expand(path), 'rb') do |f|
|
34
|
+
block.call(f.read(size)) until f.eof?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def write(path, options_or_data = nil, options = {})
|
39
|
+
opts = convert_args_to_options_hash(options_or_data, options)
|
40
|
+
size = opts[:chunk_size] || MEGABYTE
|
41
|
+
data = convert_data_to_io_obj(opts)
|
42
|
+
begin
|
43
|
+
open(expand(path), 'wb') do |f|
|
44
|
+
f.write(data.read(size)) until data.eof?
|
45
|
+
end
|
46
|
+
ensure
|
47
|
+
data.close unless data.closed?
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
30
51
|
def move(src, dest, other = self, options = {})
|
31
52
|
src_path = expand(src)
|
32
53
|
dest_path = expand(dest, other.dir)
|
@@ -39,6 +60,39 @@ module OmniStore
|
|
39
60
|
def expand(path, dir = @dir)
|
40
61
|
File.expand_path(path, dir)
|
41
62
|
end
|
63
|
+
|
64
|
+
def convert_args_to_options_hash(*args)
|
65
|
+
case args.count
|
66
|
+
when 0 then {}
|
67
|
+
when 1 then args[0].is_a?(Hash) ? args[0] : { :data => args[0] }
|
68
|
+
when 2 then args[1].merge(:data => args[0])
|
69
|
+
else
|
70
|
+
msg = "expected 0, 1 or 2 arguments, got #{args.count}"
|
71
|
+
raise ArgumentError, msg
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def convert_data_to_io_obj(options)
|
76
|
+
data = options.delete(:data)
|
77
|
+
if data.is_a?(String)
|
78
|
+
data.force_encoding("BINARY") if data.respond_to?(:force_encoding)
|
79
|
+
StringIO.new(data)
|
80
|
+
elsif data.is_a?(Pathname)
|
81
|
+
open_file(data.to_s)
|
82
|
+
elsif data.respond_to?(:read) and data.respond_to?(:eof?)
|
83
|
+
data
|
84
|
+
else
|
85
|
+
msg = "invalid :data option, expected a String, Pathname or "
|
86
|
+
msg << "an object that responds to #read and #eof?"
|
87
|
+
raise ArgumentError, msg
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def open_file(path)
|
92
|
+
file_opts = ['rb']
|
93
|
+
file_opts << { :encoding => "BINARY" } if Object.const_defined?(:Encoding)
|
94
|
+
File.open(path, *file_opts)
|
95
|
+
end
|
42
96
|
end
|
43
97
|
|
44
98
|
def mount!
|
@@ -62,6 +116,14 @@ module OmniStore
|
|
62
116
|
mp.delete(path)
|
63
117
|
end
|
64
118
|
|
119
|
+
def read(path, options = {}, mp = mountpoint, &block)
|
120
|
+
mp.read(path, options, &block)
|
121
|
+
end
|
122
|
+
|
123
|
+
def write(path, options_or_data = nil, options = {}, mp = mountpoint)
|
124
|
+
mp.write(path, options_or_data, options)
|
125
|
+
end
|
126
|
+
|
65
127
|
def each(&block)
|
66
128
|
if block_given?
|
67
129
|
@@mountpoint.each{|m| yield m }
|
data/lib/omnistore/storage/s3.rb
CHANGED
@@ -33,6 +33,10 @@ module OmniStore
|
|
33
33
|
bucket.objects[key].delete(options)
|
34
34
|
end
|
35
35
|
|
36
|
+
def read(key, options = {}, &block)
|
37
|
+
bucket.objects[key].read(options, &block)
|
38
|
+
end
|
39
|
+
|
36
40
|
def write(key, options_or_data = nil, options = {})
|
37
41
|
bucket.objects[key].write(options_or_data, options)
|
38
42
|
end
|
@@ -65,6 +69,10 @@ module OmniStore
|
|
65
69
|
mp.delete(path, options)
|
66
70
|
end
|
67
71
|
|
72
|
+
def read(path, options = {}, mp = mountpoint, &block)
|
73
|
+
mp.read(path, options, &block)
|
74
|
+
end
|
75
|
+
|
68
76
|
def write(path, options_or_data = nil, options = {}, mp = mountpoint)
|
69
77
|
mp.write(path, options_or_data, options)
|
70
78
|
end
|
data/lib/omnistore/version.rb
CHANGED
@@ -3,15 +3,8 @@ require 'fileutils'
|
|
3
3
|
|
4
4
|
describe "OmniStore::Storage::S3" do
|
5
5
|
|
6
|
-
def create_object(src, key = AWS_BUCKET)
|
7
|
-
OmniStore::Storage::S3.mountpoint(key).bucket.objects[src].write('Hello World!')
|
8
|
-
end
|
9
|
-
|
10
|
-
def delete_object(src, key = AWS_BUCKET)
|
11
|
-
OmniStore::Storage::S3.mountpoint(key).bucket.objects[src].delete
|
12
|
-
end
|
13
|
-
|
14
6
|
before(:each) do
|
7
|
+
AWS::S3::Bucket.any_instance.stub(:exists?).and_return(true)
|
15
8
|
OmniStore::Config.storage = 's3'
|
16
9
|
OmniStore::Config.mountpoint = AWS_BUCKET
|
17
10
|
OmniStore::Storage.remount!
|
@@ -30,7 +23,7 @@ describe "OmniStore::Storage::S3" do
|
|
30
23
|
end
|
31
24
|
|
32
25
|
context 'when specified a bucket name that does not exists' do
|
33
|
-
before {
|
26
|
+
before { AWS::S3::Bucket.any_instance.stub(:exists?).and_return(false) }
|
34
27
|
it { should raise_error OmniStore::Errors::InvalidMountpoint }
|
35
28
|
end
|
36
29
|
end
|
@@ -45,31 +38,40 @@ describe "OmniStore::Storage::S3" do
|
|
45
38
|
subject { OmniStore::Storage::S3.exist?(src) }
|
46
39
|
|
47
40
|
context 'when specified a object that does not exist' do
|
41
|
+
before { AWS::S3::S3Object.any_instance.stub(:exists?).and_return(false) }
|
48
42
|
it { should be_false }
|
49
43
|
end
|
50
44
|
|
51
45
|
context 'when specified a object that exist' do
|
52
|
-
before {
|
53
|
-
after { delete_object(src) }
|
46
|
+
before { AWS::S3::S3Object.any_instance.stub(:exists?).and_return(true) }
|
54
47
|
it { should be_true }
|
55
48
|
end
|
56
49
|
end
|
57
50
|
|
58
51
|
describe '#delete' do
|
59
52
|
let(:src) { TEST_FILENAME }
|
53
|
+
before { OmniStore::Storage::S3::Mountpoint.any_instance.stub(:delete).with(src, {}) }
|
60
54
|
subject { lambda { OmniStore::Storage::S3.delete(src) } }
|
61
55
|
|
62
|
-
|
63
|
-
|
64
|
-
end
|
56
|
+
it { should_not raise_error }
|
57
|
+
end
|
65
58
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
59
|
+
describe '#read' do
|
60
|
+
let(:src) { TEST_FILENAME }
|
61
|
+
let(:data) { 'Hello World' }
|
62
|
+
before { OmniStore::Storage::S3::Mountpoint.any_instance.stub(:read).with(src, {}).and_yield(data) }
|
63
|
+
subject { OmniStore::Storage::S3.read(src){|chunk| chunk } }
|
64
|
+
|
65
|
+
it { should eq data }
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '#write' do
|
69
|
+
let(:src) { TEST_FILENAME }
|
70
|
+
let(:data) { 'Hello World' }
|
71
|
+
before { OmniStore::Storage::S3::Mountpoint.any_instance.stub(:write).with(src, nil, {}) }
|
72
|
+
subject { lambda { OmniStore::Storage::S3.write(src) } }
|
73
|
+
|
74
|
+
it { should_not raise_error }
|
73
75
|
end
|
74
76
|
|
75
77
|
describe '#each' do
|
@@ -107,7 +109,7 @@ describe "OmniStore::Storage::S3" do
|
|
107
109
|
|
108
110
|
context 'when key specified' do
|
109
111
|
let(:key) { 'test' }
|
110
|
-
it { should match "#{AWS_BUCKET}
|
112
|
+
it { should match "#{AWS_BUCKET}/#{key}" }
|
111
113
|
end
|
112
114
|
|
113
115
|
context 'when secure is true' do
|
@@ -124,6 +126,32 @@ describe "OmniStore::Storage::S3" do
|
|
124
126
|
|
125
127
|
end
|
126
128
|
|
129
|
+
describe '#delete' do
|
130
|
+
let(:src) { TEST_FILENAME }
|
131
|
+
before { AWS::S3::S3Object.any_instance.stub(:delete).with({}) }
|
132
|
+
subject { lambda { OmniStore::Storage::S3.mountpoint.delete(src) } }
|
133
|
+
|
134
|
+
it { should_not raise_error }
|
135
|
+
end
|
136
|
+
|
137
|
+
describe '#read' do
|
138
|
+
let(:src) { TEST_FILENAME }
|
139
|
+
let(:data) { 'Hello World' }
|
140
|
+
before { AWS::S3::S3Object.any_instance.stub(:read).and_yield(data) }
|
141
|
+
subject { OmniStore::Storage::S3.mountpoint.read(src){|chunk| chunk } }
|
142
|
+
|
143
|
+
it { should eq data }
|
144
|
+
end
|
145
|
+
|
146
|
+
describe '#write' do
|
147
|
+
let(:src) { TEST_FILENAME }
|
148
|
+
let(:data) { 'Hello World' }
|
149
|
+
before { AWS::S3::S3Object.any_instance.stub(:write).with(data, {}) }
|
150
|
+
subject { lambda { OmniStore::Storage::S3.mountpoint.write(src, data) } }
|
151
|
+
|
152
|
+
it { should_not raise_error }
|
153
|
+
end
|
154
|
+
|
127
155
|
describe '#move' do
|
128
156
|
let(:src) { TEST_FILENAME }
|
129
157
|
let(:dst) { TEST_FILENAME + Time.new.to_i.to_s }
|
@@ -131,23 +159,29 @@ describe "OmniStore::Storage::S3" do
|
|
131
159
|
subject { lambda { OmniStore::Storage::S3.mountpoint.move(src, dst, other) } }
|
132
160
|
|
133
161
|
before do
|
134
|
-
OmniStore::Config.mountpoint = { :a => AWS_BUCKET, :b => AWS_BUCKET }
|
162
|
+
OmniStore::Config.mountpoint = { :a => AWS_BUCKET, :b => AWS_BUCKET + 'b' }
|
135
163
|
OmniStore::Storage.remount!
|
136
164
|
end
|
137
165
|
|
138
|
-
context 'when
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
166
|
+
context 'when move to same mountpoint' do
|
167
|
+
before do
|
168
|
+
AWS::S3::S3Object.any_instance.stub(:move_to).with do |*args|
|
169
|
+
args[0].should eq dst
|
170
|
+
args[1][:bucket_name].should eq AWS_BUCKET
|
171
|
+
true
|
172
|
+
end
|
173
|
+
end
|
145
174
|
it { should_not raise_error }
|
146
175
|
end
|
147
176
|
|
148
177
|
context 'when move to another mountpoint' do
|
149
|
-
before
|
150
|
-
|
178
|
+
before do
|
179
|
+
AWS::S3::S3Object.any_instance.stub(:move_to).with do |*args|
|
180
|
+
args[0].should eq dst
|
181
|
+
args[1][:bucket_name].should eq other.bucket.name
|
182
|
+
true
|
183
|
+
end
|
184
|
+
end
|
151
185
|
let(:other) { OmniStore::Storage::S3.mountpoint(:b) }
|
152
186
|
it { should_not raise_error }
|
153
187
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -8,10 +8,9 @@ require 'aws-sdk'
|
|
8
8
|
require 'omnistore'
|
9
9
|
|
10
10
|
TMPDIR = ENV['TMPDIR'] || ENV['TMP'] || ENV['TEMP'] || '/tmp'
|
11
|
-
#MOUNTPOINT = File.join(File.dirname(__FILE__), '/../data')
|
12
11
|
MOUNTPOINT = File.expand_path(File.join(File.dirname(__FILE__), '/../data'))
|
13
12
|
TEST_FILENAME = 'test.txt'
|
14
|
-
AWS_BUCKET =
|
13
|
+
AWS_BUCKET = 'AWS_BUCKET'
|
15
14
|
|
16
15
|
OmniStore.configure do |config|
|
17
16
|
config.storage = 'local'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: omnistore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-09-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: aws-sdk
|
@@ -119,7 +119,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
119
119
|
version: '0'
|
120
120
|
segments:
|
121
121
|
- 0
|
122
|
-
hash:
|
122
|
+
hash: 1003914863
|
123
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
124
|
none: false
|
125
125
|
requirements:
|
@@ -128,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
128
|
version: '0'
|
129
129
|
segments:
|
130
130
|
- 0
|
131
|
-
hash:
|
131
|
+
hash: 1003914863
|
132
132
|
requirements: []
|
133
133
|
rubyforge_project:
|
134
134
|
rubygems_version: 1.8.24
|