Class: CSVUtils::CSVSort

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_utils/csv_sort.rb

Overview

Sorts a CSV file too large to load into memory with an external merge sort: batches of rows are sorted into temporary files, which are then merged up to MERGE_WIDTH at a time, so most files are sorted with a single merge pass.

Examples:

CSVUtils::CSVSort.new('input.csv', 'sorted.csv').sort { |a, b| a[0].to_i <=> b[0].to_i }
CSVUtils::CSVSort.new('input.csv', 'sorted.csv').sort_by { |row| row[0].to_i }

Constant Summary collapse

MERGE_WIDTH =

Returns temporary files merged at once, which bounds the files open at the same time.

Returns:

  • (Integer) —

    temporary files merged at once, which bounds the files open at the same time

64

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(csv_file, new_csv_file, has_headers = true, csv_options = {}) ⇒ CSVSort

Returns a new instance of CSVSort.

Parameters:

  • csv_file (String) —

    path of the file to sort

  • new_csv_file (String) —

    path of the sorted file to write

  • has_headers (Boolean) (defaults to: true) —

    whether the first row is a header row

  • csv_options (Hash) (defaults to: {}) —

    options passed to CSV.open; the file is read with its :encoding, 'bom|utf-8' by default, and the sorted file written in the encoding values were decoded to (see EncodingOptions)



37
38
39
40
41
42
43
# File 'lib/csv_utils/csv_sort.rb', line 37

def initialize(csv_file, new_csv_file, has_headers = true, csv_options = {})
  @csv_file = csv_file
  @new_csv_file = new_csv_file
  @has_headers = has_headers
  @csv_options = csv_options
  @temp_files = []
end

Instance Attribute Details

#csv_file ⇒ String (readonly)

Returns path of the file to sort.

Returns:

  • (String) —

    path of the file to sort



22
23
24
# File 'lib/csv_utils/csv_sort.rb', line 22

def csv_file
  @csv_file
end

#csv_options ⇒ Hash (readonly)

Returns options passed to CSV.open.

Returns:

  • (Hash) —

    options passed to CSV.open



28
29
30
# File 'lib/csv_utils/csv_sort.rb', line 28

def csv_options
  @csv_options
end

#has_headers ⇒ Boolean (readonly)

Returns whether the first row is a header row, kept at the top.

Returns:

  • (Boolean) —

    whether the first row is a header row, kept at the top



26
27
28
# File 'lib/csv_utils/csv_sort.rb', line 26

def has_headers
  @has_headers
end

#headers ⇒ Array<String>? (readonly)

Returns the header row, once #sort has read it.

Returns:

  • (Array<String>, nil) —

    the header row, once #sort has read it



30
31
32
# File 'lib/csv_utils/csv_sort.rb', line 30

def headers
  @headers
end

#new_csv_file ⇒ String (readonly)

Returns path of the sorted file to write.

Returns:

  • (String) —

    path of the sorted file to write



24
25
26
# File 'lib/csv_utils/csv_sort.rb', line 24

def new_csv_file
  @new_csv_file
end

Instance Method Details

#sort(batch_size = 100_000, tmp_dir: nil) {|row1, row2| ... } ⇒ void

This method returns an undefined value.

Writes the file sorted by comparing rows. Temporary files are removed even when sorting fails.

Parameters:

  • batch_size (Integer) (defaults to: 100_000) —

    rows held in memory and sorted at a time

  • tmp_dir (String, nil) (defaults to: nil) —

    directory for the temporary files; the sorted file's directory by default

Yield Parameters:

  • row1 (Array<String>)
  • row2 (Array<String>)

Yield Returns:

  • (Integer) —

    negative, 0 or positive like <=>; without a block rows are compared as arrays



52
53
54
55
# File 'lib/csv_utils/csv_sort.rb', line 52

def sort(batch_size = 100_000, tmp_dir: nil, &block)
  compare = block || KEY_COMPARE
  run_sort(batch_size, tmp_dir, ROW_KEY, compare) { |batch| batch.sort!(&compare) }
end

#sort_by(batch_size = 100_000, tmp_dir: nil) {|row| ... } ⇒ void

This method returns an undefined value.

Writes the file sorted by a key computed once per row, which is faster than #sort when the comparison would otherwise convert values (ex: to_i) on every comparison.

Parameters:

  • batch_size (Integer) (defaults to: 100_000) —

    rows held in memory and sorted at a time

  • tmp_dir (String, nil) (defaults to: nil) —

    directory for the temporary files; the sorted file's directory by default

Yield Parameters:

  • row (Array<String>)

Yield Returns:

  • (Comparable) —

    the sort key; keys are compared with <=>, so use arrays for several columns



64
65
66
# File 'lib/csv_utils/csv_sort.rb', line 64

def sort_by(batch_size = 100_000, tmp_dir: nil, &key)
  run_sort(batch_size, tmp_dir, key, KEY_COMPARE) { |batch| batch.sort_by!(&key) }
end