gf2121 commented on code in PR #14333: URL: https://github.com/apache/lucene/pull/14333#discussion_r1993802895
########## lucene/core/src/java/org/apache/lucene/codecs/lucene90/blocktree/Trie.java: ########## @@ -0,0 +1,486 @@ +/* + * 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.lucene.codecs.lucene90.blocktree; + +import java.io.IOException; +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; +import java.util.ListIterator; +import java.util.function.BiConsumer; +import org.apache.lucene.store.DataOutput; +import org.apache.lucene.store.IndexOutput; +import org.apache.lucene.store.RandomAccessInput; +import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.BytesRefBuilder; + +/** TODO make it a more memory efficient structure */ +class Trie { + + static final int SIGN_NO_CHILDREN = 0x00; + static final int SIGN_SINGLE_CHILDREN_WITH_OUTPUT = 0x01; + static final int SIGN_SINGLE_CHILDREN_WITHOUT_OUTPUT = 0x02; + static final int SIGN_MULTI_CHILDREN = 0x03; + + record Output(long fp, boolean hasTerms, BytesRef floorData) {} + + private enum Status { + UNSAVED, + SAVED, + DESTROYED + } + + private static class Node { + private final int label; + private final LinkedList<Node> children; + private Output output; + private long fp = -1; + + Node(int label, Output output, LinkedList<Node> children) { + this.label = label; + this.output = output; + this.children = children; + } + } + + private Status status = Status.UNSAVED; + final Node root = new Node(0, null, new LinkedList<>()); + + Trie(BytesRef k, Output v) { + if (k.length == 0) { + root.output = v; + return; + } + Node parent = root; + for (int i = 0; i < k.length; i++) { + int b = k.bytes[i + k.offset] & 0xFF; + Output output = i == k.length - 1 ? v : null; + Node node = new Node(b, output, new LinkedList<>()); + parent.children.add(node); + parent = node; + } + } + + void putAll(Trie trie) { + if (status != Status.UNSAVED || trie.status != Status.UNSAVED) { + throw new IllegalStateException("tries should be unsaved"); + } + trie.status = Status.DESTROYED; + putAll(this.root, trie.root); + } + + private static void putAll(Node n, Node add) { + assert n.label == add.label; + if (add.output != null) { + n.output = add.output; + } + ListIterator<Node> iter = n.children.listIterator(); + // TODO we can do more efficient if there is no intersection, block tree always do that + outer: + for (Node addChild : add.children) { + while (iter.hasNext()) { + Node nChild = iter.next(); + if (nChild.label == addChild.label) { + putAll(nChild, addChild); Review Comment: > but it does mean the length of your terms index prefixes must be under the stack depth recursion limit. +1, we should avoid this recursive implementation. I think we only have limit of 65535 in BytesRefHash, i do not want to add more strict limitation here. -- 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: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org