cowtowncoder / java-merge-sort

Basic stand-alone disk-based N-way merge sort component for Java
Apache License 2.0
84 stars 31 forks source link

Temp files that result from two-phase merge are not deleted #16

Closed wheezil closed 5 years ago

wheezil commented 5 years ago

The attached program sorts 1000 items in a very small amount of memory. So small that it creates 501 temp files, which are pre-merged into six files, which I can see in my temp folder:

-rwx------+ 1 jlilley Domain Users 2600 Sep 17 18:28 j-merge-sort-3015695007988242713.tmp
-rwx------+ 1 jlilley Domain Users   13 Sep 17 18:28 j-merge-sort-3903033334011859833.tmp
-rwx------+ 1 jlilley Domain Users 2600 Sep 17 18:28 j-merge-sort-4734711900361229763.tmp
-rwx------+ 1 jlilley Domain Users 2600 Sep 17 18:28 j-merge-sort-5609443384695274597.tmp
-rwx------+ 1 jlilley Domain Users 2477 Sep 17 18:28 j-merge-sort-8785061516420350339.tmp
-rwx------+ 1 jlilley Domain Users 2600 Sep 17 18:28 j-merge-sort-9037742217201471621.tmp

However, when I finish the output iterator and call sort.close(), it attempts to remove the original 501 temp files, not the files resulting from the pre-merge.

            for (File input : _mergerInputs) {
                input.delete();
            }

I believe that this is the fix:

public Iterator<T> sort(DataReader<T> inputReader)
    ...
    _mergerInputs = presorted;
    _merger = _createMergeReader(merge(presorted));

It should look more like

    _mergerInputs = presorted;
        _mergerInputs = merge(presorted);
    _merger = _createMergeReader(_mergeInputs);

If this seems right, I'll post a patch.

Test.zip

cowtowncoder commented 5 years ago

Merged, included in just-now-released 1.0.2.

Thank you for the fix!