thrash 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ -c -f d --fail-fast
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Add dependencies to develop your gem here.
4
+ # Include everything needed to run rake, tests, features, etc.
5
+ group :development do
6
+ gem "bundler"
7
+ gem "jeweler"
8
+ gem "rspec"
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Austin G. Davis-Richardson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ require './lib/thrash/version.rb'
6
+
7
+ begin
8
+ Bundler.setup(:default, :development)
9
+ rescue Bundler::BundlerError => e
10
+ $stderr.puts e.message
11
+ $stderr.puts "Run `bundle install` to install missing gems"
12
+ exit e.status_code
13
+ end
14
+ require 'rake'
15
+
16
+ require 'jeweler'
17
+ Jeweler::Tasks.new do |gem|
18
+ gem.name = "thrash"
19
+ gem.homepage = "http://github.com/audy/thrash"
20
+ gem.license = "MIT"
21
+ gem.summary = %Q{Randomly write to many, many files}
22
+ gem.description = %Q{Randomly write to many, many files using an in-memory buffer}
23
+ gem.email = "harekrishna@gmail.com"
24
+ gem.authors = ["Austin G. Davis-Richardson"]
25
+ gem.version = Thrash::Version::STRING
26
+ # dependencies defined in Gemfile
27
+ end
28
+
29
+ Jeweler::RubygemsDotOrgTasks.new
@@ -0,0 +1,57 @@
1
+ class Thrash
2
+
3
+ def initialize(args={})
4
+ # number of items to keep in buffer before writing
5
+ @buffer_max = args[:buffer_max] || 100_000
6
+ create_buffer!
7
+ end
8
+
9
+ def add(bucket, obj)
10
+ @buffer[bucket] << obj
11
+ check_and_write bucket
12
+ end
13
+
14
+ # write and flush remaining buckets
15
+ def finalize
16
+ @buffer.each_key do |bucket|
17
+ write_and_flush bucket
18
+ end
19
+ clear_buffer!
20
+ nil
21
+ end
22
+
23
+ private
24
+
25
+ def create_buffer!
26
+ @buffer = Hash.new { |h, k| h[k] = Array.new }
27
+ end
28
+
29
+ # check if a buffer is full
30
+ # if so, write out and flush
31
+ def check_and_write(bucket)
32
+ if bucket_full? bucket
33
+ write_and_flush bucket
34
+ end
35
+ end
36
+
37
+ # check if a buffer is over capacity
38
+ def bucket_full?(bucket)
39
+ @buffer[bucket].size >= @buffer_max
40
+ end
41
+
42
+ # write out a bucket and flush its
43
+ # contents
44
+ def write_and_flush(bucket)
45
+ File.open(bucket, 'a+') do |o|
46
+ @buffer[bucket].each do |v|
47
+ o.write(v)
48
+ end
49
+ end
50
+ end
51
+
52
+ # empty the buffer
53
+ def clear_buffer!
54
+ @buffer.clear
55
+ end
56
+
57
+ end
@@ -0,0 +1,8 @@
1
+ class Thrash
2
+ module Version
3
+ MAJOR = 1
4
+ MINOR = 0
5
+ PATCH = 0
6
+ STRING = [MAJOR, MINOR, PATCH].join('.')
7
+ end
8
+ end
data/lib/thrash.rb ADDED
@@ -0,0 +1 @@
1
+ Dir.glob(File.join(File.dirname(__FILE__), 'thrash', '*.rb')).each { |x| require x }
data/readme.md ADDED
@@ -0,0 +1,19 @@
1
+ # 🔥 Thrash
2
+
3
+ Randomly write to many, many files. Inspired by desperation.
4
+
5
+ ```ruby
6
+
7
+ thrasher = Thrash.new
8
+
9
+ 10_000.times do |x|
10
+ thrasher["file-#{x}"].write("text\n")
11
+ end
12
+
13
+ thrasher.finalize
14
+ ```
15
+
16
+ ## Copyright
17
+
18
+ Copyright (c) 2012 Austin G. Davis-Richardson. See LICENSE.txt for
19
+ further details.
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+
12
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
14
+
15
+ require 'thrash'
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ TEST_DIR = ENV['TEST_DIR'] || "/tmp/thrash_test_#{(0...8).map{65.+(rand(25)).chr}.join}/"
4
+
5
+
6
+ describe Thrash do
7
+
8
+ let(:test_dir) { TEST_DIR }
9
+ let(:thrasher) { Thrash.new }
10
+ let(:files) { files = 5.times.map { |x| File.join(test_dir, "test-#{x}") } }
11
+ let(:test_data) { "test" }
12
+
13
+ before(:all) { `mkdir -p #{test_dir}` }
14
+
15
+ it 'can be initialized' do
16
+ thrasher.should_not be_nil
17
+ end
18
+
19
+ it '#add accepts data' do
20
+ files.each do |out_file|
21
+ thrasher.add(out_file, test_data)
22
+ end
23
+ end
24
+
25
+ it '#finalize finalizes output and returns nil' do
26
+ thrasher.finalize.should be_nil
27
+ end
28
+
29
+ it 'writes data as expected' do
30
+ files.each do |f|
31
+ File.read(f).should == test_data
32
+ end
33
+ end
34
+
35
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thrash
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Austin G. Davis-Richardson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: jeweler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Randomly write to many, many files using an in-memory buffer
63
+ email: harekrishna@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files:
67
+ - LICENSE.txt
68
+ files:
69
+ - .rspec
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - Rakefile
73
+ - lib/thrash.rb
74
+ - lib/thrash/thrash.rb
75
+ - lib/thrash/version.rb
76
+ - readme.md
77
+ - spec/spec_helper.rb
78
+ - spec/thrash_spec.rb
79
+ homepage: http://github.com/audy/thrash
80
+ licenses:
81
+ - MIT
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ segments:
93
+ - 0
94
+ hash: -3631999329443079020
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.24
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Randomly write to many, many files
107
+ test_files: []