|
| 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 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.accumulo.manager.compaction.queue; |
| 20 | + |
| 21 | +import java.util.AbstractMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.TreeMap; |
| 24 | + |
| 25 | +import com.google.common.base.Preconditions; |
| 26 | + |
| 27 | +/** |
| 28 | + * This class wraps a treemap and tracks the data size of everything added and removed from the |
| 29 | + * treemap. |
| 30 | + */ |
| 31 | +class SizeTrackingTreeMap<K,V> { |
| 32 | + |
| 33 | + private static class ValueWrapper<V2> { |
| 34 | + final V2 val; |
| 35 | + final long computedSize; |
| 36 | + |
| 37 | + private ValueWrapper(V2 val, long computedSize) { |
| 38 | + this.val = val; |
| 39 | + this.computedSize = computedSize; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private final TreeMap<K,ValueWrapper<V>> map = new TreeMap<>(); |
| 44 | + private long dataSize = 0; |
| 45 | + private Weigher<V> weigher; |
| 46 | + |
| 47 | + private Map.Entry<K,V> unwrap(Map.Entry<K,ValueWrapper<V>> wrapperEntry) { |
| 48 | + if (wrapperEntry == null) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + return new AbstractMap.SimpleImmutableEntry<>(wrapperEntry.getKey(), |
| 52 | + wrapperEntry.getValue().val); |
| 53 | + } |
| 54 | + |
| 55 | + private void incrementDataSize(ValueWrapper<V> val) { |
| 56 | + Preconditions.checkState(dataSize >= 0); |
| 57 | + dataSize += val.computedSize; |
| 58 | + } |
| 59 | + |
| 60 | + private void decrementDataSize(Map.Entry<K,ValueWrapper<V>> entry) { |
| 61 | + if (entry != null) { |
| 62 | + decrementDataSize(entry.getValue()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private void decrementDataSize(ValueWrapper<V> val) { |
| 67 | + if (val != null) { |
| 68 | + Preconditions.checkState(dataSize >= val.computedSize); |
| 69 | + dataSize -= val.computedSize; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + interface Weigher<V2> { |
| 74 | + long weigh(V2 val); |
| 75 | + } |
| 76 | + |
| 77 | + public SizeTrackingTreeMap(Weigher<V> weigher) { |
| 78 | + this.weigher = weigher; |
| 79 | + } |
| 80 | + |
| 81 | + public boolean isEmpty() { |
| 82 | + return map.isEmpty(); |
| 83 | + } |
| 84 | + |
| 85 | + public long dataSize() { |
| 86 | + return dataSize; |
| 87 | + } |
| 88 | + |
| 89 | + public int entrySize() { |
| 90 | + return map.size(); |
| 91 | + } |
| 92 | + |
| 93 | + public K lastKey() { |
| 94 | + return map.lastKey(); |
| 95 | + } |
| 96 | + |
| 97 | + public Map.Entry<K,V> firstEntry() { |
| 98 | + return unwrap(map.firstEntry()); |
| 99 | + } |
| 100 | + |
| 101 | + public void remove(K key) { |
| 102 | + var prev = map.remove(key); |
| 103 | + decrementDataSize(prev); |
| 104 | + } |
| 105 | + |
| 106 | + public Map.Entry<K,V> pollFirstEntry() { |
| 107 | + var first = map.pollFirstEntry(); |
| 108 | + decrementDataSize(first); |
| 109 | + return unwrap(first); |
| 110 | + } |
| 111 | + |
| 112 | + public Map.Entry<K,V> pollLastEntry() { |
| 113 | + var last = map.pollLastEntry(); |
| 114 | + decrementDataSize(last); |
| 115 | + return unwrap(last); |
| 116 | + } |
| 117 | + |
| 118 | + public void put(K key, V val) { |
| 119 | + var wrapped = new ValueWrapper<>(val, weigher.weigh(val)); |
| 120 | + var prev = map.put(key, wrapped); |
| 121 | + decrementDataSize(prev); |
| 122 | + incrementDataSize(wrapped); |
| 123 | + } |
| 124 | + |
| 125 | + public void clear() { |
| 126 | + map.clear(); |
| 127 | + dataSize = 0; |
| 128 | + } |
| 129 | +} |
0 commit comments