MCPcopy Index your code
hub / github.com/cowtowncoder/java-merge-sort

github.com/cowtowncoder/java-merge-sort @java-merge-sort-1.1.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release java-merge-sort-1.1.0 ↗ · + Follow
185 symbols 348 edges 31 files 41 documented · 22%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Overview

This project implements basic disk-backed multi-way merge sort, with configurable input and output formats (i.e. not just textual sort). It should be useful for systems that process large amounts of data, as a simple building block for sort phases.

Documentation

Checkout project wiki for more documentation, including Javadocs.

License

Library is licensed under Apache License 2.0.

Usage

Programmatic access

Main class to interact with is com.fasterxml.sort.Sorter, which needs to be constructed with four things:

  • Configuration settings (default SortConfig works fine)
  • DataReaderFactory which is used for creating readers for intermediate sort files (and input, if stream passed)
  • DataWriterFactory which is used for creating writers for intermediate sort files (and results, if stream passed)
  • Comparator for data items

An example of how this can be done can be found from com.fasterxml.sort.std.TextFileSorter. Basic implementations exist for line-based text input (in package com.fasterxml.sort.std), and additional implementations may be added: for example, a JSON data sorter could be implement as an extension module of Jackson. Fortunately implementing your own readers and writers is trivial.

With a Sorter instance, you can call one of two main sort methods:

public void sort(InputStream source, OutputStream destination)
public boolean sort(DataReader<T>  inputReader, DataWriter<T> resultWriter)

where former takes input as streams and uses configured reader/writer factories to construct DataReader for input and DataWriter for output; and latter just uses pre-constructed instances.

In addition to core sorting functionality, Sorter instance also gives access to progress information (it implements SortingState interface with accessor methods).

A very simple example of sorting a text file using line-by-line comparison is:

TextSorter sorter = new TextFileSorter(new SortConfig().withMaxMemoryUsage(20 * 1000 * 1000));
sorter.sort(new FileInputStream("input.txt"), new FileOutputStream("output.txt"));

which would read text from file "input.txt", sort using about 20 megs of heap (note: estimates for memory usage are rough), use temporary files if necessary (i.e. for small files it's just in-memoryu sort, for bigger real merge sort), and write output as file "output.txt".

Command-line utility

Project jar is packaged such that it can be used as a primitive 'sort' tool like so:

java -jar java-merge-sort-1.0.2.jar [input-file]

where sorted output gets printed to stdout; and argument is optional (if missing, reads input from stdout). (implementation note: this uses standard TextFileSorter mentioned above)

Format is assumed to be basic text lines, similar to unix sort, and sorting order basic byte sorting (which works for most common encodings).

More documentation

Here are some external links:

Getting involved

To access source, just clone project

Extension points exported contracts — how you extend this code

TempFileProvider (Interface)
Interface used for object that can handle constructing of temporary files that are needed during sort and non-final merg [2 …
src/main/java/com/fasterxml/sort/TempFileProvider.java
SortingState (Interface)
Interface that defines how calling application can interact with a Sorter; both by accessing progress informatio [2 implementers]
src/main/java/com/fasterxml/sort/SortingState.java

Core symbols most depended-on inside this repo

close
called by 9
src/main/java/com/fasterxml/sort/IteratingSorter.java
_calculateRoundCount
called by 8
src/main/java/com/fasterxml/sort/SorterBase.java
compare
called by 8
src/main/java/com/fasterxml/sort/std/ByteArrayComparator.java
close
called by 7
src/main/java/com/fasterxml/sort/DataReader.java
readNext
called by 6
src/main/java/com/fasterxml/sort/DataReader.java
sort
called by 6
src/main/java/com/fasterxml/sort/Sorter.java
hasNext
called by 6
src/main/java/com/fasterxml/sort/util/CastingIterator.java
writeEntry
called by 4
src/main/java/com/fasterxml/sort/DataWriter.java

Shape

Method 140
Class 42
Interface 2
Enum 1

Languages

Java100%

Modules by API surface

src/main/java/com/fasterxml/sort/SorterBase.java20 symbols
src/test/java/com/fasterxml/sort/JsonFileSorter.java17 symbols
src/main/java/com/fasterxml/sort/util/SegmentedBuffer.java14 symbols
src/main/java/com/fasterxml/sort/std/RawTextLineReader.java11 symbols
src/test/java/com/fasterxml/sort/SortTestBase.java10 symbols
src/main/java/com/fasterxml/sort/SortingState.java10 symbols
src/main/java/com/fasterxml/sort/IteratingSorter.java9 symbols
src/test/java/com/fasterxml/sort/TestLongLineSort.java8 symbols
src/main/java/com/fasterxml/sort/std/RawTextLineWriter.java8 symbols
src/main/java/com/fasterxml/sort/Merger.java8 symbols
src/main/java/com/fasterxml/sort/SortConfig.java7 symbols
src/main/java/com/fasterxml/sort/Sorter.java6 symbols

For agents

$ claude mcp add java-merge-sort \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact