Skip to content

Commit

Permalink
Optimize ordering (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tycooon authored Aug 10, 2022
1 parent ef74381 commit 0da2de4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
sequel-batches (2.0.1)
sequel-batches (2.0.2)
sequel

GEM
Expand Down
18 changes: 11 additions & 7 deletions lib/sequel/extensions/batches/yielder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Yielder
attr_writer :pk

def initialize(ds:, **options)
self.ds = ds
self.ds = ds.unordered
self.pk = options.delete(:pk)
self.of = options.delete(:of) || 1000
self.start = options.delete(:start)
Expand All @@ -31,8 +31,8 @@ def call
base_ds
end

working_ds_pk = working_ds.select(*qualified_pk).order(order_by(qualified: true)).limit(of)
current_instance = db.from(working_ds_pk).select(*pk).order(order_by).last or break
working_ds_pk = working_ds.select(*qualified_pk).order(*order_by(qualified: true)).limit(of)
current_instance = db.from(working_ds_pk).select(*pk).order(*order_by).last or break
working_ds = working_ds.where(generate_conditions(current_instance.to_h, sign: sign_to_inclusive))

yield working_ds
Expand Down Expand Up @@ -70,14 +70,18 @@ def sign_to_inclusive
end

def order_by(qualified: false)
columns = qualified ? qualified_pk : pk
asc_order? ? Sequel.asc(columns) : Sequel.desc(columns)
columns = qualified ? qualified_pk : unqualified_pk
asc_order? ? columns.map(&:asc) : columns.map(&:desc)
end

def qualified_pk
@qualified_pk ||= pk.map { |x| Sequel[ds.first_source][x] }
end

def unqualified_pk
@unqualified_pk ||= pk.map { |x| x.is_a?(Symbol) ? Sequel[x] : x }
end

def check_pk(input_pk)
raise InvalidPKError if input_pk.keys != pk
input_pk
Expand All @@ -90,11 +94,11 @@ def generate_conditions(input_pk, sign:)
end

def setup_base_ds
base_ds = ds.order(order_by(qualified: true))
base_ds = ds.order(*order_by(qualified: true))
base_ds = base_ds.where(generate_conditions(check_pk(start), sign: sign_from_inclusive)) if start
base_ds = base_ds.where(generate_conditions(check_pk(finish), sign: sign_to_inclusive)) if finish

pk_ds = db.from(base_ds.select(*qualified_pk)).select(*pk).order(order_by)
pk_ds = db.from(base_ds.select(*qualified_pk)).select(*pk).order(*order_by)
actual_start = pk_ds.first
actual_finish = pk_ds.last

Expand Down
2 changes: 1 addition & 1 deletion sequel-batches.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |spec|
spec.name = "sequel-batches"
spec.version = "2.0.1"
spec.version = "2.0.2"
spec.authors = %w[fiscal-cliff umbrellio]
spec.email = ["oss@umbrellio.biz"]
spec.required_ruby_version = ">= 2.7"
Expand Down

0 comments on commit 0da2de4

Please sign in to comment.