Class: CSVUtils::CSVSort
- Inherits:
-
Object
- Object
- CSVUtils::CSVSort
- 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.
Constant Summary collapse
- MERGE_WIDTH =
Returns temporary files merged at once, which bounds the files open at the same time.
64
Instance Attribute Summary collapse
-
#csv_file ⇒ String
readonly
Path of the file to sort.
-
#csv_options ⇒ Hash
readonly
Options passed to CSV.open.
-
#has_headers ⇒ Boolean
readonly
Whether the first row is a header row, kept at the top.
-
#headers ⇒ Array<String>?
readonly
The header row, once #sort has read it.
-
#new_csv_file ⇒ String
readonly
Path of the sorted file to write.
Instance Method Summary collapse
-
#initialize(csv_file, new_csv_file, has_headers = true, csv_options = {}) ⇒ CSVSort
constructor
A new instance of CSVSort.
-
#sort(batch_size = 100_000, tmp_dir: nil) {|row1, row2| ... } ⇒ void
Writes the file sorted by comparing rows.
-
#sort_by(batch_size = 100_000, tmp_dir: nil) {|row| ... } ⇒ void
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.
Constructor Details
#initialize(csv_file, new_csv_file, has_headers = true, csv_options = {}) ⇒ CSVSort
Returns a new instance of CSVSort.
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_file = csv_file @new_csv_file = new_csv_file @has_headers = has_headers @csv_options = @temp_files = [] end |
Instance Attribute Details
#csv_file ⇒ String (readonly)
Returns 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.
28 29 30 |
# File 'lib/csv_utils/csv_sort.rb', line 28 def @csv_options end |
#has_headers ⇒ Boolean (readonly)
Returns 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.
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.
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.
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.
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 |