fabriziofortino commented on code in PR #1156: URL: https://github.com/apache/jackrabbit-oak/pull/1156#discussion_r1365187930
########## oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/FlatFileStoreUtils.java: ########## @@ -75,6 +77,11 @@ public static BufferedOutputStream createOutputStream(File file, Compression alg return new BufferedOutputStream(algorithm.getOutputStream(out)); } + public static BufferedOutputStream createOutputStream(Path file, Compression algorithm) throws IOException { + OutputStream out = Files.newOutputStream(file); + return new BufferedOutputStream(algorithm.getOutputStream(out)); + } + Review Comment: can we remove this? It is never used and the class is deprecated. ########## oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/sort/ExternalSortByteArray.java: ########## @@ -0,0 +1,219 @@ +/* + * 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)); + BinaryFileBuffer<T> bfb = new BinaryFileBuffer<>(in, byteArrayToType); + bfbs.add(bfb); + } + 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); + } + } + } + + public 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; + try (fbw) { Review Comment: the BufferedOutputStream `fwb` is closed twice: here and where it gets created (PipelineMergeSortTask.sortStoreFile#line 314). I think we can remove it from here. ```suggestion try { ``` ########## oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/NodeStateHolderFactory.java: ########## @@ -0,0 +1,83 @@ +/* + * 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.index.indexer.document.flatfile.pipelined; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.function.Function; + +final class NodeStateHolderFactory implements Function<byte[], NodeStateHolder> { + private final static byte PIPE = (byte) '|'; + private final static byte PATH_SEPARATOR = (byte) '/'; + + // In UTF-8, the ASCII characters have similar encoding as in ASCII, so we can search for then without decoding the Review Comment: typo ```suggestion // In UTF-8, the ASCII characters have similar encoding as in ASCII, so we can search for them without decoding the ``` ########## oak-run-commons/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelinedIT.java: ########## @@ -41,12 +41,15 @@ import org.jetbrains.annotations.NotNull; import org.junit.After; import org.junit.Assume; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.contrib.java.lang.system.RestoreSystemProperties; import org.junit.rules.TemporaryFolder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; Review Comment: unused ```suggestion ``` ########## oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/sort/ExternalSortByteArray.java: ########## @@ -0,0 +1,219 @@ +/* + * 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)); + BinaryFileBuffer<T> bfb = new BinaryFileBuffer<>(in, byteArrayToType); + bfbs.add(bfb); + } + 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); + } + } + } + + public static <T> int mergeBinary(BufferedOutputStream fbw, final Comparator<T> cmp, boolean distinct, Review Comment: should this be private? It's only used by `mergeSortedFilesBinary` and accepts `BinaryFileBuffer` as a parameter which has private visibility. -- 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: dev-unsubscr...@jackrabbit.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org