nfsantos commented on code in PR #1156: URL: https://github.com/apache/jackrabbit-oak/pull/1156#discussion_r1369899582
########## oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/sort/ExternalSortByteArray.java: ########## @@ -0,0 +1,212 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.oak.commons.sort; + +import org.apache.jackrabbit.oak.commons.Compression; + +import java.io.BufferedOutputStream; +import java.io.ByteArrayOutputStream; +import java.io.EOFException; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.PriorityQueue; +import java.util.function.Function; + +/** + * Variation of ExternalSort that stores the lines read from intermediate files as byte arrays to avoid the conversion + * from byte[] to String and then back. + */ +public class ExternalSortByteArray { + public static <T> void mergeSortedFilesBinary(List<Path> files, BufferedOutputStream fbw, final Comparator<T> cmp, + boolean distinct, Compression algorithm, + Function<T, byte[]> typeToByteArray, Function<byte[], T> byteArrayToType) + throws IOException { + ArrayList<BinaryFileBuffer<T>> bfbs = new ArrayList<>(); + try { + for (Path f : files) { + InputStream in = algorithm.getInputStream(Files.newInputStream(f)); + bfbs.add(new BinaryFileBuffer<>(in, byteArrayToType)); + } + mergeBinary(fbw, cmp, distinct, bfbs, typeToByteArray); + } finally { + for (BinaryFileBuffer<T> buffer : bfbs) { + try { + buffer.close(); + } catch (Exception ignored) { + } + } + for (Path f : files) { + Files.deleteIfExists(f); + } + } + } + + private static <T> int mergeBinary(BufferedOutputStream fbw, final Comparator<T> cmp, boolean distinct, + List<BinaryFileBuffer<T>> buffers, Function<T, byte[]> typeToByteArray) + throws IOException { + PriorityQueue<BinaryFileBuffer<T>> pq = new PriorityQueue<>( + 11, + (i, j) -> cmp.compare(i.peek(), j.peek()) + ); + for (BinaryFileBuffer<T> bfb : buffers) { + if (!bfb.empty()) { + pq.add(bfb); + } + } + int rowcounter = 0; + T lastLine = null; + while (!pq.isEmpty()) { + BinaryFileBuffer<T> bfb = pq.poll(); + T r = bfb.pop(); + // Skip duplicate lines + if (!distinct || lastLine == null || cmp.compare(r, lastLine) != 0) { + fbw.write(typeToByteArray.apply(r)); + fbw.write('\n'); + lastLine = r; + } + ++rowcounter; + if (bfb.empty()) { + bfb.fbr.close(); + } else { + pq.add(bfb); // add it back + } + } + return rowcounter; + } + + /** + * WARNING: Uses '\n' as a line separator, it will not work with other line separators. + */ + private static class BinaryFileBuffer<T> { + private final static int BUFFER_SIZE = 64 * 1024; Review Comment: Made it configurable with a default of 16KB. In my tests, I saw some improvements from 8 to 16KB. And at 16KB, if we have 1000 files to merge, that's 16MB of memory, I don't having 16 instead of 8KB should be an issue at all, as anyway, we should always be much below 1000 files to merge. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
