#sorting #algorithm #external #external-sort

bin+lib ext-sort

rust external sort algorithm implementation

5 releases

0.1.4 Apr 16, 2023
0.1.3 Feb 25, 2023
0.1.2 Jan 11, 2022
0.1.1 Jan 9, 2022
0.1.0 Jan 8, 2022

#395 in Algorithms

Download history 1654/week @ 2024-07-22 2689/week @ 2024-07-29 3256/week @ 2024-08-05 2499/week @ 2024-08-12 2790/week @ 2024-08-19 2159/week @ 2024-08-26 2712/week @ 2024-09-02 2707/week @ 2024-09-09 2386/week @ 2024-09-16 2620/week @ 2024-09-23 2920/week @ 2024-09-30 2338/week @ 2024-10-07 2357/week @ 2024-10-14 2660/week @ 2024-10-21 2241/week @ 2024-10-28 1910/week @ 2024-11-04

9,307 downloads per month
Used in 38 crates (9 directly)

Unlicense

47KB
993 lines

Crates.io License Test Status Documentation

Rust external sort

ext-sort is a rust external sort algorithm implementation.

External sorting is a class of sorting algorithms that can handle massive amounts of data. External sorting is required when the data being sorted do not fit into the main memory (RAM) of a computer and instead must be resided in slower external memory, usually a hard disk drive. Sorting is achieved in two passes. During the first pass it sorts chunks of data that each fit in RAM, during the second pass it merges the sorted chunks together. For more information see External Sorting.

Overview

ext-sort supports the following features:

  • Data agnostic: it supports all data types that implement serde serialization/deserialization by default, otherwise you can implement your own serialization/deserialization mechanism.
  • Serialization format agnostic: the library uses MessagePack serialization format by default, but it can be easily substituted by your custom one if MessagePack serialization/deserialization performance is not sufficient for your task.
  • Multithreading support: multi-threaded sorting is supported, which means data is sorted in multiple threads utilizing maximum CPU resources and reducing sorting time.
  • Memory limit support: memory limited sorting is supported. It allows you to limit sorting memory consumption (memory-limit feature required).

Basic example

Activate memory-limit feature of the ext-sort crate on Cargo.toml:

[dependencies]
ext-sort = { version = "^0.1.4", features = ["memory-limit"] }
use std::fs;
use std::io::{self, prelude::*};
use std::path;

use bytesize::MB;
use env_logger;
use log;

use ext_sort::{buffer::mem::MemoryLimitedBufferBuilder, ExternalSorter, ExternalSorterBuilder};

fn main() {
    env_logger::Builder::new().filter_level(log::LevelFilter::Debug).init();

    let input_reader = io::BufReader::new(fs::File::open("input.txt").unwrap());
    let mut output_writer = io::BufWriter::new(fs::File::create("output.txt").unwrap());

    let sorter: ExternalSorter<String, io::Error, MemoryLimitedBufferBuilder> = ExternalSorterBuilder::new()
        .with_tmp_dir(path::Path::new("./"))
        .with_buffer(MemoryLimitedBufferBuilder::new(50 * MB))
        .build()
        .unwrap();

    let sorted = sorter.sort(input_reader.lines()).unwrap();

    for item in sorted.map(Result::unwrap) {
        output_writer.write_all(format!("{}\n", item).as_bytes()).unwrap();
    }
    output_writer.flush().unwrap();
}

Dependencies

~3–14MB
~184K SLoC