gf2121 commented on code in PR #14333:
URL: https://github.com/apache/lucene/pull/14333#discussion_r2035743271


##########
lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/TrieBuilder.java:
##########
@@ -0,0 +1,640 @@
+/*
+ * 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.lucene103.blocktree;
+
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.Arrays;
+import java.util.Deque;
+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;
+
+/**
+ * A builder to build prefix tree (trie) as the index of block tree, and can 
be saved to disk.
+ *
+ * <p>TODO make this trie builder a more memory efficient structure.
+ */
+class TrieBuilder {
+
+  static final int SIGN_NO_CHILDREN = 0x00;
+  static final int SIGN_SINGLE_CHILD_WITH_OUTPUT = 0x01;
+  static final int SIGN_SINGLE_CHILD_WITHOUT_OUTPUT = 0x02;
+  static final int SIGN_MULTI_CHILDREN = 0x03;
+
+  static final int LEAF_NODE_HAS_TERMS = 1 << 5;
+  static final int LEAF_NODE_HAS_FLOOR = 1 << 6;
+  static final long NON_LEAF_NODE_HAS_TERMS = 1L << 1;
+  static final long NON_LEAF_NODE_HAS_FLOOR = 1L << 0;
+
+  /**
+   * The output describing the term block the prefix point to.
+   *
+   * @param fp the file pointer to the on-disk terms block which a trie node 
points to.
+   * @param hasTerms false if this on-disk block consists entirely of pointers 
to child blocks.
+   * @param floorData will be non-null when a large block of terms sharing a 
single trie prefix is
+   *     split into multiple on-disk blocks.
+   */
+  record Output(long fp, boolean hasTerms, BytesRef floorData) {}
+
+  private enum Status {
+    BUILDING,
+    SAVED,
+    DESTROYED
+  }
+
+  private static class Node {
+
+    // The utf8 digit that leads to this Node, 0 for root node
+    private final int label;
+    // The output of this node.
+    private Output output;
+    // The number of children of this node.
+    private int childrenNum;
+    // Pointers to relative nodes
+    private Node next;
+    private Node firstChild;
+    private Node lastChild;
+
+    // Vars used during saving:
+
+    // The file pointer point to where the node saved. -1 means the node has 
not been saved.
+    private long fp = -1;
+    // The latest child that have been saved. null means no child has been 
saved.
+    private Node savedTo;
+
+    Node(int label, Output output) {
+      this.label = label;
+      this.output = output;
+    }
+  }
+
+  private final Node root = new Node(0, null);
+  private final BytesRef minKey;
+  private BytesRef maxKey;
+  private Status status = Status.BUILDING;
+
+  static TrieBuilder bytesRefToTrie(BytesRef k, Output v) {
+    return new TrieBuilder(k, v);
+  }
+
+  private TrieBuilder(BytesRef k, Output v) {
+    minKey = maxKey = BytesRef.deepCopyOf(k);
+    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);
+      parent.firstChild = parent.lastChild = node;
+      parent.childrenNum = 1;
+      parent = node;
+    }
+  }
+
+  /**
+   * Append all (K, V) pairs from the given trie into this one. The given trie 
builder need to
+   * ensure its keys greater or equals than max key of this one.
+   *
+   * <p>Note: the given trie will be destroyed after appending.
+   */
+  void append(TrieBuilder trieBuilder) {
+    if (status != Status.BUILDING || trieBuilder.status != Status.BUILDING) {
+      throw new IllegalStateException("tries have wrong status.");
+    }
+    assert this.maxKey.compareTo(trieBuilder.minKey) < 0;
+
+    int mismatch =
+        Arrays.mismatch(
+            this.maxKey.bytes,
+            this.maxKey.offset,
+            this.maxKey.offset + this.maxKey.length,
+            trieBuilder.minKey.bytes,
+            trieBuilder.minKey.offset,
+            trieBuilder.minKey.offset + trieBuilder.minKey.length);
+    Node a = this.root;
+    Node b = trieBuilder.root;
+
+    for (int i = 0; i < mismatch; i++) {
+      final Node aLast = a.lastChild;
+      final Node bFirst = b.firstChild;
+      assert aLast.label == bFirst.label;
+
+      if (b.childrenNum > 1) {
+        aLast.next = bFirst.next;
+        a.childrenNum += b.childrenNum - 1;
+        a.lastChild = b.lastChild;
+        assertChildrenLabelInOrder(a);

Review Comment:
   Oops, yes!



-- 
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

Reply via email to