-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add rubocop * stop checking coverage for jruby * extract out Yielder * refactor Yielder * update readme * add is_jruby? method * fix coverage check
- Loading branch information
Showing
9 changed files
with
179 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
inherit_gem: | ||
rubocop-config-umbrellio: lib/rubocop.yml | ||
|
||
AllCops: | ||
DisplayCopNames: true | ||
TargetRubyVersion: 2.3 | ||
|
||
Naming/UncommunicativeMethodParamName: | ||
Enabled: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/gem_tasks" | ||
require "rspec/core/rake_task" | ||
require "rubocop/rake_task" | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
RuboCop::RakeTask.new(:lint) | ||
|
||
task :default => :spec | ||
task default: %i[lint spec] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sequel | ||
module Extensions | ||
module Batches | ||
MissingPKError = Class.new(StandardError) | ||
NullPKError = Class.new(StandardError) | ||
InvalidPKError = Class.new(StandardError) | ||
|
||
def in_batches(pk: nil, of: 1000, start: nil, finish: nil) | ||
pk ||= db.schema(first_source).select { |x| x[1][:primary_key] }.map(&:first) | ||
raise MissingPKError if pk.empty? | ||
|
||
qualified_pk = pk.map { |x| Sequel[first_source][x] } | ||
|
||
check_pk = lambda do |input_pk| | ||
raise InvalidPKError if input_pk.keys != pk | ||
input_pk | ||
end | ||
|
||
conditions = lambda do |pk, sign:| | ||
raise NullPKError if pk.values.any?(&:nil?) | ||
row_expr = Sequel.function(:row, *pk.values) | ||
Sequel.function(:row, *qualified_pk).public_send(sign, row_expr) | ||
end | ||
|
||
base_ds = order(*qualified_pk) | ||
base_ds = base_ds.where(conditions.call(check_pk.call(start), sign: :>=)) if start | ||
base_ds = base_ds.where(conditions.call(check_pk.call(finish), sign: :<=)) if finish | ||
|
||
pk_ds = db.from(base_ds).select(*pk).order(*pk) | ||
actual_start = pk_ds.first | ||
actual_finish = pk_ds.last | ||
|
||
return unless actual_start && actual_finish | ||
|
||
base_ds = base_ds.where(conditions.call(actual_start, sign: :>=)) | ||
base_ds = base_ds.where(conditions.call(actual_finish, sign: :<=)) | ||
|
||
current_instance = nil | ||
|
||
loop do | ||
if current_instance | ||
working_ds = base_ds.where(conditions.call(current_instance.to_h, sign: :>)) | ||
else | ||
working_ds = base_ds | ||
end | ||
|
||
current_instance = db.from(working_ds.limit(of)).select(*pk).order(*pk).last or break | ||
working_ds = working_ds.where(conditions.call(current_instance.to_h, sign: :<=)) | ||
|
||
yield working_ds | ||
end | ||
def in_batches(**options, &block) | ||
Sequel::Extensions::Batches::Yielder.new(ds: self, **options).call(&block) | ||
end | ||
|
||
private | ||
|
||
::Sequel::Dataset.register_extension(:batches, Batches) | ||
end | ||
end | ||
end | ||
|
||
::Sequel::Dataset.register_extension(:batches, Sequel::Extensions::Batches) | ||
|
||
require_relative "batches/yielder" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sequel::Extensions::Batches | ||
class Yielder | ||
attr_accessor :ds, :of, :start, :finish | ||
attr_writer :pk | ||
|
||
def initialize(ds:, pk: nil, of: 1000, start: nil, finish: nil) | ||
self.ds = ds | ||
self.pk = pk | ||
self.of = of | ||
self.start = start | ||
self.finish = finish | ||
end | ||
|
||
def call | ||
base_ds = setup_base_ds or return | ||
|
||
current_instance = nil | ||
|
||
loop do | ||
working_ds = | ||
if current_instance | ||
base_ds.where(generate_conditions(current_instance.to_h, sign: :>)) | ||
else | ||
base_ds | ||
end | ||
|
||
current_instance = db.from(working_ds.limit(of)).select(*pk).order(*pk).last or break | ||
working_ds = working_ds.where(generate_conditions(current_instance.to_h, sign: :<=)) | ||
|
||
yield working_ds | ||
end | ||
end | ||
|
||
private | ||
|
||
def db | ||
ds.db | ||
end | ||
|
||
def pk | ||
@pk ||= begin | ||
pk = db.schema(ds.first_source).select { |x| x[1][:primary_key] }.map(&:first) | ||
raise MissingPKError if pk.empty? | ||
pk | ||
end | ||
end | ||
|
||
def qualified_pk | ||
@qualified_pk ||= pk.map { |x| Sequel[ds.first_source][x] } | ||
end | ||
|
||
def check_pk(input_pk) | ||
raise InvalidPKError if input_pk.keys != pk | ||
input_pk | ||
end | ||
|
||
def generate_conditions(input_pk, sign:) | ||
raise NullPKError if input_pk.values.any?(&:nil?) | ||
row_expr = Sequel.function(:row, *input_pk.values) | ||
Sequel.function(:row, *qualified_pk).public_send(sign, row_expr) | ||
end | ||
|
||
def setup_base_ds | ||
base_ds = ds.order(*qualified_pk) | ||
base_ds = base_ds.where(generate_conditions(check_pk(start), sign: :>=)) if start | ||
base_ds = base_ds.where(generate_conditions(check_pk(finish), sign: :<=)) if finish | ||
|
||
pk_ds = db.from(base_ds).select(*pk).order(*pk) | ||
actual_start = pk_ds.first | ||
actual_finish = pk_ds.last | ||
|
||
return unless actual_start && actual_finish | ||
|
||
base_ds = base_ds.where(generate_conditions(actual_start, sign: :>=)) | ||
base_ds = base_ds.where(generate_conditions(actual_finish, sign: :<=)) | ||
|
||
base_ds | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
lib = File.expand_path("../lib", __FILE__) | ||
lib = File.expand_path("lib", __dir__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "sequel-batches" | ||
spec.version = "0.2.1" | ||
spec.authors = ["fiscal-cliff", "umbrellio"] | ||
spec.email = ["[email protected]"] | ||
spec.name = "sequel-batches" | ||
spec.version = "0.2.1" | ||
spec.authors = ["fiscal-cliff", "umbrellio"] | ||
spec.email = ["[email protected]"] | ||
|
||
spec.summary = %q{The extension mimics AR5 batches api} | ||
spec.description = %q{Allows you to split your dataset in batches} | ||
spec.homepage = "https://github.com/umbrellio/sequel-batches" | ||
spec.license = "MIT" | ||
spec.summary = "The extension mimics AR5 batches api" | ||
spec.description = "Allows you to split your dataset in batches" | ||
spec.homepage = "https://github.com/umbrellio/sequel-batches" | ||
spec.license = "MIT" | ||
|
||
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' | ||
# to allow pushing to a single host or delete this section to allow pushing to any host. | ||
if spec.respond_to?(:metadata) | ||
spec.metadata["allowed_push_host"] = "https://rubygems.org" | ||
else | ||
raise "RubyGems 2.0 or newer is required to protect against " \ | ||
"public gem pushes." | ||
end | ||
|
||
spec.files = `git ls-files -z`.split("\x0").reject do |f| | ||
f.match(%r{^(test|spec|features)/}) | ||
end | ||
spec.bindir = "exe" | ||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } | ||
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } | ||
spec.require_paths = ["lib"] | ||
|
||
spec.add_runtime_dependency "sequel" | ||
|
||
spec.add_development_dependency "bundler" | ||
spec.add_development_dependency "coveralls" | ||
spec.add_development_dependency "pry" | ||
spec.add_development_dependency "rake" | ||
spec.add_development_dependency "rspec" | ||
|
||
spec.add_runtime_dependency "sequel" | ||
spec.add_development_dependency "rubocop-config-umbrellio" | ||
spec.add_development_dependency "simplecov" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.