|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.hadoop.hdds.utils.db; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Objects; |
| 23 | +import java.util.Set; |
| 24 | +import java.util.TreeMap; |
| 25 | + |
| 26 | +/** |
| 27 | + * An index for answering "does this point fall within any of these ranges?" efficiently. |
| 28 | + * |
| 29 | + * <p>The indexed ranges are <em>half-open</em> intervals of the form |
| 30 | + * {@code [startInclusive, endExclusive)}. |
| 31 | + * |
| 32 | + * <p><strong>Core idea (sweep-line / prefix-sum over range boundaries):</strong> |
| 33 | + * Instead of scanning every range on each query, this index stores a sorted map from |
| 34 | + * boundary points to a running count of "active" ranges at that point. |
| 35 | + * |
| 36 | + * <ul> |
| 37 | + * <li>For each range {@code [s, e)}, we add a delta {@code +1} at {@code s} and a delta |
| 38 | + * {@code -1} at {@code e}.</li> |
| 39 | + * <li>We then convert the deltas into a prefix sum in key order, so every boundary key |
| 40 | + * stores the number of ranges active at that coordinate.</li> |
| 41 | + * <li>For any query point {@code k}, the active count is {@code floorEntry(k).value}. |
| 42 | + * If it is {@code > 0}, then {@code k} intersects at least one range.</li> |
| 43 | + * </ul> |
| 44 | + * |
| 45 | + * <p><strong>Update model:</strong> this index supports only removing ranges that were part of the |
| 46 | + * initial set. Removal updates the prefix sums for keys in {@code [startInclusive, endExclusive)} |
| 47 | + * (net effect of removing {@code +1} at start and {@code -1} at end). |
| 48 | + * |
| 49 | + * <p><strong>Complexities:</strong> |
| 50 | + * <ul> |
| 51 | + * <li>Build: {@code O(R log B)} where {@code R} is #ranges and {@code B} is #distinct boundaries.</li> |
| 52 | + * <li>{@link #containsIntersectingRange(Object)}: {@code O(log B)}.</li> |
| 53 | + * <li>{@link #removeRange(Range)}: {@code O(log B + K)} where {@code K} is #boundaries in the range.</li> |
| 54 | + * </ul> |
| 55 | + * |
| 56 | + * @param <T> boundary type (must be {@link Comparable} to be stored in a {@link TreeMap}) |
| 57 | + */ |
| 58 | +class RangeQueryIndex<T extends Comparable<T>> { |
| 59 | + |
| 60 | + private final TreeMap<T, Integer> rangeCountIndexMap; |
| 61 | + private final Set<Range<T>> ranges; |
| 62 | + |
| 63 | + RangeQueryIndex(Set<Range<T>> ranges) { |
| 64 | + this.rangeCountIndexMap = new TreeMap<>(); |
| 65 | + this.ranges = ranges; |
| 66 | + init(); |
| 67 | + } |
| 68 | + |
| 69 | + private void init() { |
| 70 | + // Phase 1: store boundary deltas (+1 at start, -1 at end). |
| 71 | + for (Range<T> range : ranges) { |
| 72 | + rangeCountIndexMap.compute(range.startInclusive, (k, v) -> v == null ? 1 : v + 1); |
| 73 | + rangeCountIndexMap.compute(range.endExclusive, (k, v) -> v == null ? -1 : v - 1); |
| 74 | + } |
| 75 | + |
| 76 | + // Phase 2: convert deltas to prefix sums so each key holds the active range count at that coordinate. |
| 77 | + int totalCount = 0; |
| 78 | + for (Map.Entry<T, Integer> entry : rangeCountIndexMap.entrySet()) { |
| 79 | + totalCount += entry.getValue(); |
| 80 | + entry.setValue(totalCount); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Remove a range from the index. |
| 86 | + * |
| 87 | + * <p>This method assumes the range set is "popped" over time (ranges are removed but not added). |
| 88 | + * Internally, removing {@code [s, e)} decreases the active count by 1 for all boundary keys in |
| 89 | + * {@code [s, e)} and leaves counts outside the range unchanged. |
| 90 | + * |
| 91 | + * @throws IOException if the given {@code range} is not part of the indexed set |
| 92 | + */ |
| 93 | + void removeRange(Range<T> range) throws IOException { |
| 94 | + if (!ranges.contains(range)) { |
| 95 | + throw new IOException(String.format("Range %s not found in index structure : %s", range, ranges)); |
| 96 | + } |
| 97 | + ranges.remove(range); |
| 98 | + for (Map.Entry<T, Integer> entry : rangeCountIndexMap.subMap(range.startInclusive, true, |
| 99 | + range.endExclusive, false).entrySet()) { |
| 100 | + entry.setValue(entry.getValue() - 1); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @return true iff {@code key} is contained in at least one indexed range. |
| 106 | + * |
| 107 | + * <p>Implementation detail: uses {@link TreeMap#floorEntry(Object)} to find the last boundary |
| 108 | + * at or before {@code key}, and checks the prefix-summed active count at that point.</p> |
| 109 | + */ |
| 110 | + boolean containsIntersectingRange(T key) { |
| 111 | + Map.Entry<T, Integer> countEntry = rangeCountIndexMap.floorEntry(key); |
| 112 | + if (countEntry == null) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + return countEntry.getValue() > 0; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Returns an intersecting range containing {@code key}, if any. |
| 120 | + * |
| 121 | + * <p>This method first checks {@link #containsIntersectingRange(Comparable)} using the index; |
| 122 | + * if the count indicates an intersection exists, it then scans the backing {@link #ranges} |
| 123 | + * set to find a concrete {@link Range} that contains {@code key}.</p> |
| 124 | + * |
| 125 | + * <p>Note that because {@link #ranges} is a {@link Set}, "first" refers to whatever iteration |
| 126 | + * order that set provides (it is not guaranteed to be deterministic unless the provided set is).</p> |
| 127 | + * |
| 128 | + * @return a containing range, or null if none intersect |
| 129 | + */ |
| 130 | + Range<T> getFirstIntersectingRange(T key) { |
| 131 | + Map.Entry<T, Integer> countEntry = rangeCountIndexMap.floorEntry(key); |
| 132 | + if (countEntry == null) { |
| 133 | + return null; |
| 134 | + } |
| 135 | + for (Range<T> range : ranges) { |
| 136 | + if (range.contains(key)) { |
| 137 | + return range; |
| 138 | + } |
| 139 | + } |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * A half-open interval {@code [startInclusive, endExclusive)}. |
| 145 | + * |
| 146 | + * <p>For a value {@code k} to be contained, it must satisfy: |
| 147 | + * {@code startInclusive <= k < endExclusive} (according to {@link Comparable#compareTo(Object)}).</p> |
| 148 | + */ |
| 149 | + static final class Range<T extends Comparable<T>> { |
| 150 | + private final T startInclusive; |
| 151 | + private final T endExclusive; |
| 152 | + |
| 153 | + Range(T startInclusive, T endExclusive) { |
| 154 | + this.startInclusive = Objects.requireNonNull(startInclusive, "start == null"); |
| 155 | + this.endExclusive = Objects.requireNonNull(endExclusive, "end == null"); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public boolean equals(Object o) { |
| 160 | + return this == o; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public int hashCode() { |
| 165 | + return Objects.hash(startInclusive, endExclusive); |
| 166 | + } |
| 167 | + |
| 168 | + T getStartInclusive() { |
| 169 | + return startInclusive; |
| 170 | + } |
| 171 | + |
| 172 | + T getEndExclusive() { |
| 173 | + return endExclusive; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * @return true iff {@code key} is within {@code [startInclusive, endExclusive)}. |
| 178 | + */ |
| 179 | + public boolean contains(T key) { |
| 180 | + return startInclusive.compareTo(key) <= 0 && key.compareTo(endExclusive) < 0; |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public String toString() { |
| 185 | + return "Range{" + |
| 186 | + "startInclusive=" + startInclusive + |
| 187 | + ", endExclusive=" + endExclusive + |
| 188 | + '}'; |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments