|
| 1 | +package com.fasterxml.sort; |
| 2 | + |
| 3 | +import com.fasterxml.sort.std.ByteArrayComparator; |
| 4 | +import com.fasterxml.sort.std.RawTextLineReader; |
| 5 | +import com.fasterxml.sort.std.RawTextLineWriter; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.charset.Charset; |
| 9 | + |
| 10 | +public class TestLargeSort extends SortTestBase |
| 11 | +{ |
| 12 | + private static final Charset CHARSET =Charset.forName("UTF-8"); |
| 13 | + private static final int STRING_LENGTH = 256; |
| 14 | + private static final int SORT_MEM_BYTES = 1024 * 1024; // 1MB |
| 15 | + private static final int STRING_COUNT = 10 * (SORT_MEM_BYTES / STRING_LENGTH); |
| 16 | + |
| 17 | + private static class StringGenerator extends DataReader<byte[]> { |
| 18 | + private final int generateCount; |
| 19 | + private final StringBuilder sb; |
| 20 | + private int count; |
| 21 | + |
| 22 | + private StringGenerator(int generateCount, int stringLength) { |
| 23 | + this.generateCount = generateCount; |
| 24 | + this.sb = new StringBuilder(stringLength); |
| 25 | + for(int i = 0; i < stringLength; ++i) { |
| 26 | + sb.append('a'); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public byte[] readNext() { |
| 32 | + if(count >= generateCount) { |
| 33 | + return null; |
| 34 | + } |
| 35 | + int saveLen = sb.length(); |
| 36 | + sb.append(count++); |
| 37 | + String s = sb.toString(); |
| 38 | + sb.setLength(saveLen); |
| 39 | + return s.getBytes(CHARSET); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public int estimateSizeInBytes(byte[] item) { |
| 44 | + return item.length; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void close() { |
| 49 | + // None |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private static class CountingWriter<T> extends DataWriter<T> { |
| 54 | + private int count = 0; |
| 55 | + |
| 56 | + public int getCount() { |
| 57 | + return count; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void writeEntry(T item) { |
| 62 | + ++count; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void close() { |
| 67 | + // None |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + public void testLargeSort() throws IOException { |
| 73 | + Sorter<byte[]> sorter = new Sorter<byte[]>( |
| 74 | + new SortConfig().withMaxMemoryUsage(SORT_MEM_BYTES), |
| 75 | + RawTextLineReader.factory(), |
| 76 | + RawTextLineWriter.factory(), |
| 77 | + new ByteArrayComparator() |
| 78 | + ); |
| 79 | + CountingWriter<byte[]> counter = new CountingWriter<byte[]>(); |
| 80 | + sorter.sort(new StringGenerator(STRING_COUNT, STRING_LENGTH), counter); |
| 81 | + assertEquals("sorted count", STRING_COUNT, counter.getCount()); |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments