ddbuffer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.rspec +3 -0
- data/.rubocop.yml +64 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +49 -0
- data/Rakefile +15 -0
- data/ddbuffer.gemspec +19 -0
- data/lib/ddbuffer/version.rb +5 -0
- data/lib/ddbuffer.rb +49 -0
- data/spec/ddbuffer_spec.rb +107 -0
- data/spec/spec_helper.rb +36 -0
- metadata +72 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 574fdd8a2e913618eb41f95db6ea7d3eaf16ee46
|
4
|
+
data.tar.gz: c013a57234f5dc71e365a4e228056504d78b7502
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cfb8bcd4801d0dd214037d85d470d35be418eabe1b115305c16a75c565e64b8bae2db7d34453278fd62c63f657b0e28bcf2cd012229e800ce21dff3da653beb3
|
7
|
+
data.tar.gz: 1de6bfb037df2cb87f47b4df54c30cab9c555a5d163f1658d3a283cecceda8c786c54d736203ff67480d79406a90d0ad34bff5ae7e67245a52348199af8f5075
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# ----- CONFIGURED -----
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
TargetRubyVersion: 2.3
|
5
|
+
DisplayCopNames: true
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
# ----- DISABLED (metrics) -----
|
10
|
+
|
11
|
+
# Cops for metrics are disabled because they should not cause tests to fail.
|
12
|
+
|
13
|
+
Metrics/AbcSize:
|
14
|
+
Enabled: false
|
15
|
+
|
16
|
+
Metrics/BlockLength:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Metrics/BlockNesting:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Metrics/ClassLength:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
Metrics/CyclomaticComplexity:
|
26
|
+
Enabled: false
|
27
|
+
|
28
|
+
Metrics/LineLength:
|
29
|
+
Enabled: false
|
30
|
+
|
31
|
+
Metrics/MethodLength:
|
32
|
+
Enabled: false
|
33
|
+
|
34
|
+
Metrics/ModuleLength:
|
35
|
+
Enabled: false
|
36
|
+
|
37
|
+
Metrics/ParameterLists:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
Metrics/PerceivedComplexity:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
# ----- DISABLED (opinionated) -----
|
46
|
+
|
47
|
+
# We should embrace UTF-8, not avoid it. Since the Encoding cop is enabled,
|
48
|
+
# there’s no point in enforcing ASCII comments.
|
49
|
+
AsciiComments:
|
50
|
+
Enabled: false
|
51
|
+
|
52
|
+
# It does not make sense to enforce everything to have documentation.
|
53
|
+
Documentation:
|
54
|
+
Enabled: false
|
55
|
+
|
56
|
+
Style/TrailingCommaInArguments:
|
57
|
+
EnforcedStyleForMultiline: comma
|
58
|
+
|
59
|
+
Style/TrailingCommaInLiteral:
|
60
|
+
EnforcedStyleForMultiline: comma
|
61
|
+
|
62
|
+
# This does not always semantically make sense.
|
63
|
+
GuardClause:
|
64
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at denis.defreyne@stoneship.org. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Denis Defreyne
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# DDBuffer
|
2
|
+
|
3
|
+
Provides a way to buffer Ruby enumerables/enumerators.
|
4
|
+
|
5
|
+
This is particularly useful when reading from a slow source and reading to a slow sink, because the two will be able to work concurrently. For example:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
# Read articles (an Enumerator)
|
9
|
+
articles = my_web_service.each_article
|
10
|
+
|
11
|
+
# Buffer 100 articles at a time
|
12
|
+
articles = DDBuffer.new(100).call(articles)
|
13
|
+
|
14
|
+
# Write buffered articles
|
15
|
+
articles.each_slice(50) do |slice|
|
16
|
+
my_slow_db.insert_articles(slice)
|
17
|
+
end
|
18
|
+
```
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
To install, add the `ddbuffer` gem to your Gemfile, and run `bundle`.
|
23
|
+
|
24
|
+
To buffer an enumerable `enum` with a buffer size of `size`:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
buffered_enum = DDBuffer.new(size).call(enum)
|
28
|
+
```
|
29
|
+
|
30
|
+
## Development
|
31
|
+
|
32
|
+
To run the tests:
|
33
|
+
|
34
|
+
```
|
35
|
+
bundle install
|
36
|
+
bundle exec rake
|
37
|
+
```
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ddfreyne/ddbuffer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
42
|
+
|
43
|
+
## License
|
44
|
+
|
45
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
46
|
+
|
47
|
+
## Code of Conduct
|
48
|
+
|
49
|
+
Everyone interacting in the DDBuffer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ddfreyne/ddbuffer/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rubocop/rake_task'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
require 'rake/testtask'
|
6
|
+
|
7
|
+
RuboCop::RakeTask.new(:rubocop)
|
8
|
+
|
9
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
10
|
+
t.verbose = false
|
11
|
+
end
|
12
|
+
|
13
|
+
task test: %i[spec rubocop]
|
14
|
+
|
15
|
+
task default: :test
|
data/ddbuffer.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/ddbuffer/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'ddbuffer'
|
7
|
+
spec.version = DDBuffer::VERSION
|
8
|
+
spec.authors = ['Denis Defreyne']
|
9
|
+
spec.email = ['denis.defreyne@stoneship.org']
|
10
|
+
|
11
|
+
spec.summary = 'Buffer enumerables'
|
12
|
+
spec.homepage = 'https://github.com/ddfreyne/ddbuffer'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.require_paths = ['lib']
|
17
|
+
|
18
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
19
|
+
end
|
data/lib/ddbuffer.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ddbuffer/version'
|
4
|
+
|
5
|
+
class DDBuffer
|
6
|
+
module Impl
|
7
|
+
STOP_OK = Object.new
|
8
|
+
STOP_ERR = Object.new
|
9
|
+
|
10
|
+
def self.gen_collector_thread(enum, queue)
|
11
|
+
Thread.new do
|
12
|
+
begin
|
13
|
+
enum.each { |e| queue << e }
|
14
|
+
queue << STOP_OK
|
15
|
+
rescue => e
|
16
|
+
queue << STOP_ERR
|
17
|
+
queue << e
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.gen_enumerator(queue, collector_thread)
|
23
|
+
Enumerator.new do |y|
|
24
|
+
loop do
|
25
|
+
e = queue.pop
|
26
|
+
|
27
|
+
if STOP_OK.equal?(e)
|
28
|
+
break
|
29
|
+
elsif STOP_ERR.equal?(e)
|
30
|
+
raise queue.pop
|
31
|
+
end
|
32
|
+
|
33
|
+
y << e
|
34
|
+
end
|
35
|
+
collector_thread.join
|
36
|
+
end.lazy
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def initialize(size)
|
41
|
+
@size = size
|
42
|
+
end
|
43
|
+
|
44
|
+
def call(enum)
|
45
|
+
queue = SizedQueue.new(@size)
|
46
|
+
thread = Impl.gen_collector_thread(enum, queue)
|
47
|
+
Impl.gen_enumerator(queue, thread)
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe DDBuffer do
|
6
|
+
it 'has a version number' do
|
7
|
+
expect(DDBuffer::VERSION).not_to be nil
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '.new + #call' do
|
11
|
+
subject { described_class.new(size).call(enum) }
|
12
|
+
|
13
|
+
let(:enum) { [] }
|
14
|
+
let(:size) { 10 }
|
15
|
+
|
16
|
+
context 'empty array' do
|
17
|
+
example do
|
18
|
+
expect(subject.to_a).to eq([])
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'small array' do
|
23
|
+
let(:enum) { [1, 2, 3] }
|
24
|
+
|
25
|
+
example do
|
26
|
+
expect(subject.to_a).to eq([1, 2, 3])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'infinite array' do
|
31
|
+
let(:enum) { (1..(1.0 / 0)) }
|
32
|
+
|
33
|
+
example do
|
34
|
+
expect(subject.take(3).to_a).to eq([1, 2, 3])
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'error in taken elements' do
|
39
|
+
let(:enum) do
|
40
|
+
Enumerator.new do |y|
|
41
|
+
y << 1
|
42
|
+
y << 2
|
43
|
+
raise 'boom'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'does not raise right away' do
|
48
|
+
subject
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'does not raise when only taken' do
|
52
|
+
subject.take(3)
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'raises when evaluated' do
|
56
|
+
expect { subject.take(3).to_a }
|
57
|
+
.to raise_error(RuntimeError, 'boom')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'error past taken elements' do
|
62
|
+
let(:enum) do
|
63
|
+
Enumerator.new do |y|
|
64
|
+
y << 1
|
65
|
+
y << 2
|
66
|
+
y << 3
|
67
|
+
raise 'boom'
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'does not raise right away' do
|
72
|
+
subject
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'does not raise when only taken' do
|
76
|
+
subject.take(3)
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'does not raise when evaluated' do
|
80
|
+
expect(subject.take(3).to_a).to eq([1, 2, 3])
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'slow source' do
|
85
|
+
let(:enum) do
|
86
|
+
Enumerator.new do |y|
|
87
|
+
20.times do |i|
|
88
|
+
y << i
|
89
|
+
sleep 0.1
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'takes a while to take elements when unbuffered' do
|
95
|
+
expect { subject.take(5).to_a }
|
96
|
+
.to finish_in(0.5, 0.1)
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'takes no time to take elements when buffered' do
|
100
|
+
subject
|
101
|
+
sleep 0.5
|
102
|
+
expect { subject.take(5).to_a }
|
103
|
+
.to finish_in(0.0, 0.1)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ddbuffer'
|
4
|
+
|
5
|
+
require 'fuubar'
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.expect_with :rspec do |c|
|
9
|
+
c.syntax = :expect
|
10
|
+
end
|
11
|
+
|
12
|
+
config.fuubar_progress_bar_options = {
|
13
|
+
format: '%c/%C |<%b>%i| %p%%',
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
RSpec::Matchers.define :finish_in do |expected, delta|
|
18
|
+
supports_block_expectations
|
19
|
+
|
20
|
+
match do |actual|
|
21
|
+
before = Time.now
|
22
|
+
actual.call
|
23
|
+
after = Time.now
|
24
|
+
@actual_duration = after - before
|
25
|
+
range = (expected - delta..expected + delta)
|
26
|
+
range.include?(@actual_duration)
|
27
|
+
end
|
28
|
+
|
29
|
+
failure_message do |_actual|
|
30
|
+
"expected that proc would finish in #{expected}s (±#{delta}s), but took #{format '%0.1fs', @actual_duration}"
|
31
|
+
end
|
32
|
+
|
33
|
+
failure_message_when_negated do |_actual|
|
34
|
+
"expected that proc would not finish in #{expected}s (±#{delta}s), but took #{format '%0.1fs', @actual_duration}"
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ddbuffer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Denis Defreyne
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- denis.defreyne@stoneship.org
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- ".rspec"
|
36
|
+
- ".rubocop.yml"
|
37
|
+
- ".travis.yml"
|
38
|
+
- CODE_OF_CONDUCT.md
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- ddbuffer.gemspec
|
44
|
+
- lib/ddbuffer.rb
|
45
|
+
- lib/ddbuffer/version.rb
|
46
|
+
- spec/ddbuffer_spec.rb
|
47
|
+
- spec/spec_helper.rb
|
48
|
+
homepage: https://github.com/ddfreyne/ddbuffer
|
49
|
+
licenses:
|
50
|
+
- MIT
|
51
|
+
metadata: {}
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 2.6.12
|
69
|
+
signing_key:
|
70
|
+
specification_version: 4
|
71
|
+
summary: Buffer enumerables
|
72
|
+
test_files: []
|