pingtimeout commented on code in PR #2: URL: https://github.com/apache/polaris-tools/pull/2#discussion_r2024267700
########## benchmarks/src/gatling/scala/org/apache/polaris/benchmarks/NAryTreeBuilder.scala: ########## @@ -0,0 +1,105 @@ +/* + * 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.polaris.benchmarks + +/** + * Builds a complete N-ary tree structure for organizing namespaces in synthetic datasets. + * + * This builder is used to create a hierarchical namespace structure where each non-leaf namespace + * has exactly `nsWidth` child namespaces. The tree structure is used to organize tables and views + * in a deterministic way. Tables and views are placed in the leaf namespaces of the tree. + * + * @param nsWidth The number of children each non-leaf namespace will have (N) + * @param nsDepth The total depth of the tree, including the root namespace + */ +case class NAryTreeBuilder(nsWidth: Int, nsDepth: Int) { + + /** + * Computes the path from the root node to the given ordinal. + * + * @param ordinal the ordinal of the node + * @param acc the accumulator for the path + * @return the path from the root node (included) to the given ordinal (included) + */ + @scala.annotation.tailrec + final def pathToRoot(ordinal: Int, acc: List[Int] = Nil): List[Int] = + if (ordinal == 0) { + ordinal :: acc + } else { + val parent = (ordinal - 1) / nsWidth + pathToRoot(parent, ordinal :: acc) + } + + /** + * Calculates the depth of a node in the n-ary tree based on its ordinal. + * + * @param ordinal The ordinal of the node. + * @return The depth of the node in the tree. + */ + def depthOf(ordinal: Int): Int = { + if (ordinal == 0) return 0 + if (nsWidth == 1) return ordinal + + // Using the formula: floor(log_n((x * (n-1)) + 1)) + val numerator = (ordinal * (nsWidth - 1)) + 1 + (math.log(numerator) / math.log(nsWidth)).floor.toInt + } + + /** + * Calculate the total number of nodes in a complete n-ary tree. + * + * @return The total number of nodes in the tree. + */ + val numberOfNodes: Int = { + print(s"Computing number of nodes for n-ary tree with width $nsWidth and depth $nsDepth...") 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]
