blambov commented on code in PR #2267: URL: https://github.com/apache/cassandra/pull/2267#discussion_r1171198000
########## src/java/org/apache/cassandra/io/tries/IncrementalDeepTrieWriterPageAware.java: ########## @@ -0,0 +1,398 @@ +/* + * 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.cassandra.io.tries; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.cassandra.io.util.DataOutputPlus; + +/** + * This class is a variant of {@link IncrementalTrieWriterPageAware} which is able to build even very deep + * tries. While the parent class uses recursion for clarity, it may end up with stack overflow for tries with + * very long keys. This implementation can switch processing from stack to heap at a certain depth (provided + * as a constructor param). + */ +public class IncrementalDeepTrieWriterPageAware<VALUE> extends IncrementalTrieWriterPageAware<VALUE> +{ + private final int maxRecursionDepth; + + public IncrementalDeepTrieWriterPageAware(TrieSerializer<VALUE, ? super DataOutputPlus> trieSerializer, DataOutputPlus dest, int maxRecursionDepth) + { + super(trieSerializer, dest); + this.maxRecursionDepth = maxRecursionDepth; + } + + public IncrementalDeepTrieWriterPageAware(TrieSerializer<VALUE, ? super DataOutputPlus> trieSerializer, DataOutputPlus dest) + { + this(trieSerializer, dest, 64); + } + + @Override + protected int recalcTotalSize(Node<VALUE> node, long nodePosition) throws IOException + { + return recalcTotalSizeRecursiveOnStack(node, nodePosition, 0); + } + + private int recalcTotalSizeRecursiveOnStack(Node<VALUE> node, long nodePosition, int depth) throws IOException + { + if (node.hasOutOfPageInBranch) + { + int sz = 0; + for (Node<VALUE> child : node.children) + { + if (depth < maxRecursionDepth) + sz += recalcTotalSizeRecursiveOnStack(child, nodePosition + sz, depth + 1); + else + sz += recalcTotalSizeRecursiveOnHeap(child, nodePosition + sz); + } + node.branchSize = sz; + } + + // The sizing below will use the branch size calculated above. Since that can change on out-of-page in branch, + // we need to recalculate the size if either flag is set. + if (node.hasOutOfPageChildren || node.hasOutOfPageInBranch) + node.nodeSize = serializer.sizeofNode(node, nodePosition + node.branchSize); + + return node.branchSize + node.nodeSize; + } + + @Override + protected long write(Node<VALUE> node) throws IOException + { + return writeRecursiveOnStack(node, 0); + } + + private long writeRecursiveOnStack(Node<VALUE> node, int depth) throws IOException + { + long nodePosition = dest.position(); + for (Node<VALUE> child : node.children) + if (child.filePos == -1) + { + if (depth < maxRecursionDepth) + child.filePos = writeRecursiveOnStack(child, depth + 1); + else + child.filePos = writeRecursiveOnHeap(child); + } + + nodePosition += node.branchSize; + assert dest.position() == nodePosition + : "Expected node position to be " + nodePosition + " but got " + dest.position() + " after writing children.\n" + dumpNode(node, dest.position()); + + serializer.write(dest, node, nodePosition); + + assert dest.position() == nodePosition + node.nodeSize + || dest.paddedPosition() == dest.position() // For PartitionIndexTest.testPointerGrowth where position may jump on page boundaries. + : "Expected node position to be " + (nodePosition + node.nodeSize) + " but got " + dest.position() + " after writing node, nodeSize " + node.nodeSize + ".\n" + dumpNode(node, nodePosition); + return nodePosition; + } + + @Override + protected long writePartial(Node<VALUE> node, DataOutputPlus dest, long baseOffset) throws IOException + { + return writePartialRecursiveOnStack(node, dest, baseOffset, 0); + } + + private long writePartialRecursiveOnStack(Node<VALUE> node, DataOutputPlus dest, long baseOffset, int depth) throws IOException + { + long startPosition = dest.position() + baseOffset; + + List<Node<VALUE>> childrenToClear = new ArrayList<>(); + for (Node<VALUE> child : node.children) + { + if (child.filePos == -1) + { + childrenToClear.add(child); + if (depth < maxRecursionDepth) + child.filePos = writePartialRecursiveOnStack(child, dest, baseOffset, depth + 1); + else + child.filePos = writePartialRecursiveOnHeap(child, dest, baseOffset); + } + } + + long nodePosition = dest.position() + baseOffset; Review Comment: Refactored ########## src/java/org/apache/cassandra/utils/ByteBufferUtil.java: ########## @@ -866,4 +869,27 @@ private static boolean startsWith(ByteBuffer src, ByteBuffer prefix, int offset) return true; } -} + + /** + * Returns true if the buffer at the current position in the input matches given buffer. + * If true, the input is positioned at the end of the consumed buffer. + * If false, the position of the input is undefined. + * <p> + * The matched buffer is unchanged + * + * @throws IOException Review Comment: Removed -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

