|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.cassandra.utils.btree; |
| 20 | + |
| 21 | +import static org.apache.cassandra.utils.btree.BTree.height; |
| 22 | +import static org.apache.cassandra.utils.btree.BTree.isEmpty; |
| 23 | +import static org.apache.cassandra.utils.btree.BTree.isLeaf; |
| 24 | +import static org.apache.cassandra.utils.btree.BTree.shallowSizeOfBranch; |
| 25 | +import static org.apache.cassandra.utils.btree.BTree.size; |
| 26 | +import static org.apache.cassandra.utils.btree.BTree.sizeMap; |
| 27 | +import static org.apache.cassandra.utils.btree.BTree.sizeOfLeaf; |
| 28 | + |
| 29 | +// A small utility for debugging / printing B-Tree / IntervalBTrees |
| 30 | +// |
| 31 | +// Prints B-Tree in the following format: |
| 32 | +// |
| 33 | +// * (branch): [ 9,10:9 | 15,16:15 ] |
| 34 | +// ├─ [-∞] |
| 35 | +// │ * (branch): [ 3,4:3 | 6,7:6 ] |
| 36 | +// │ ├─ [-∞] |
| 37 | +// │ │ (leaf): [ 0,1:0, 1,2:1, 2,3:2 ] |
| 38 | +// │ ├─ [3,4:3] |
| 39 | +// │ │ (leaf): [ 4,5:4, 5,6:5 ] |
| 40 | +// │ └─ [6,7:6] |
| 41 | +// │ (leaf): [ 7,8:7, 8,9:8 ] |
| 42 | +// ├─ [9,10:9] |
| 43 | +// │ * (branch): [ 12,13:12 ] |
| 44 | +// │ ├─ [-∞] |
| 45 | +// │ │ (leaf): [ 10,11:10, 11,12:11 ] |
| 46 | +// │ └─ [12,13:12] |
| 47 | +// │ (leaf): [ 13,14:13, 14,15:14 ] |
| 48 | +// └─ [15,16:15] |
| 49 | +// * (branch): [ 18,19:18 ] |
| 50 | +// ├─ [-∞] |
| 51 | +// │ (leaf): [ 16,17:16, 17,18:17 ] |
| 52 | +// └─ [18,19:18] |
| 53 | +// (leaf): [ 19,20:19 ] |
| 54 | +@SuppressWarnings("unused") |
| 55 | +public class BTreePrinter |
| 56 | +{ |
| 57 | + private static boolean PRINT_SIZE_MAP = false; |
| 58 | + |
| 59 | + public static String print(Object[] btree) |
| 60 | + { |
| 61 | + if (isEmpty(btree)) |
| 62 | + return "empty"; |
| 63 | + |
| 64 | + StringBuilder sb = new StringBuilder(); |
| 65 | + sb.append("(size=").append(size(btree)) |
| 66 | + .append(", height=").append(height(btree)) |
| 67 | + .append("):\n"); |
| 68 | + printNode(sb, btree, 0, ""); |
| 69 | + return sb.toString(); |
| 70 | + } |
| 71 | + |
| 72 | + private static void printNode(StringBuilder sb, Object[] node, int level, String prefix) |
| 73 | + { |
| 74 | + String indent = " ".repeat(level); |
| 75 | + |
| 76 | + if (isLeaf(node)) |
| 77 | + { |
| 78 | + int leafSize = sizeOfLeaf(node); |
| 79 | + sb.append(prefix).append(indent) |
| 80 | + .append("(leaf): [ "); |
| 81 | + for (int i = 0; i < leafSize; i++) |
| 82 | + { |
| 83 | + if (i > 0) sb.append(", "); |
| 84 | + sb.append(node[i]); |
| 85 | + } |
| 86 | + sb.append(" ]\n"); |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + int keyCount = shallowSizeOfBranch(node); |
| 91 | + int childCount = keyCount + 1; |
| 92 | + int[] sizeMap = sizeMap(node); |
| 93 | + |
| 94 | + sb.append(prefix) |
| 95 | + .append(indent) |
| 96 | + .append("* (branch): [ "); |
| 97 | + |
| 98 | + for (int i = 0; i < keyCount; i++) |
| 99 | + { |
| 100 | + if (i > 0) sb.append(" | "); |
| 101 | + sb.append(node[i]); |
| 102 | + } |
| 103 | + sb.append(" ]\n"); |
| 104 | + |
| 105 | + if (PRINT_SIZE_MAP) |
| 106 | + { |
| 107 | + sb.append(prefix).append(indent).append("├─ sizeMap: "); |
| 108 | + for (int i = 0; i < sizeMap.length; i++) |
| 109 | + { |
| 110 | + if (i > 0) sb.append(", "); |
| 111 | + sb.append(sizeMap[i]); |
| 112 | + } |
| 113 | + sb.append("\n"); |
| 114 | + } |
| 115 | + |
| 116 | + for (int i = 0; i < childCount; i++) |
| 117 | + { |
| 118 | + Object[] child = (Object[]) node[keyCount + i]; |
| 119 | + String childPrefix = prefix + indent; |
| 120 | + String verticalLine = (i == childCount - 1) ? "└─ " : "├─ "; |
| 121 | + String nextPrefix = (i == childCount - 1) ? " " : "│ "; |
| 122 | + |
| 123 | + sb.append(childPrefix).append(verticalLine) |
| 124 | + .append("[") |
| 125 | + .append(i == 0 ? "-∞" : node[i - 1]) |
| 126 | + .append("]\n"); |
| 127 | + |
| 128 | + // Recursing here should be fine, as we do not expect depth to be too large |
| 129 | + printNode(sb, child, level + 1, prefix + indent + nextPrefix); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments