|
| 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.spark.util.collection |
| 19 | + |
| 20 | + |
| 21 | +/** |
| 22 | + * A fast hash map implementation for nullable keys. This hash map supports insertions and updates, |
| 23 | + * but not deletions. This map is about 5X faster than java.util.HashMap, while using much less |
| 24 | + * space overhead. |
| 25 | + * |
| 26 | + * Under the hood, it uses our OpenHashSet implementation. |
| 27 | + */ |
| 28 | +private[spark] |
| 29 | +class OpenHashMap[K >: Null : ClassManifest, @specialized(Long, Int, Double) V: ClassManifest]( |
| 30 | + initialCapacity: Int) |
| 31 | + extends Iterable[(K, V)] |
| 32 | + with Serializable { |
| 33 | + |
| 34 | + def this() = this(64) |
| 35 | + |
| 36 | + protected var _keySet = new OpenHashSet[K](initialCapacity) |
| 37 | + |
| 38 | + // Init in constructor (instead of in declaration) to work around a Scala compiler specialization |
| 39 | + // bug that would generate two arrays (one for Object and one for specialized T). |
| 40 | + private var _values: Array[V] = _ |
| 41 | + _values = new Array[V](_keySet.capacity) |
| 42 | + |
| 43 | + @transient private var _oldValues: Array[V] = null |
| 44 | + |
| 45 | + // Treat the null key differently so we can use nulls in "data" to represent empty items. |
| 46 | + private var haveNullValue = false |
| 47 | + private var nullValue: V = null.asInstanceOf[V] |
| 48 | + |
| 49 | + override def size: Int = if (haveNullValue) _keySet.size + 1 else _keySet.size |
| 50 | + |
| 51 | + /** Get the value for a given key */ |
| 52 | + def apply(k: K): V = { |
| 53 | + if (k == null) { |
| 54 | + nullValue |
| 55 | + } else { |
| 56 | + val pos = _keySet.getPos(k) |
| 57 | + if (pos < 0) { |
| 58 | + null.asInstanceOf[V] |
| 59 | + } else { |
| 60 | + _values(pos) |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** Set the value for a key */ |
| 66 | + def update(k: K, v: V) { |
| 67 | + if (k == null) { |
| 68 | + haveNullValue = true |
| 69 | + nullValue = v |
| 70 | + } else { |
| 71 | + val pos = _keySet.addWithoutResize(k) & OpenHashSet.POSITION_MASK |
| 72 | + _values(pos) = v |
| 73 | + _keySet.rehashIfNeeded(k, grow, move) |
| 74 | + _oldValues = null |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * If the key doesn't exist yet in the hash map, set its value to defaultValue; otherwise, |
| 80 | + * set its value to mergeValue(oldValue). |
| 81 | + * |
| 82 | + * @return the newly updated value. |
| 83 | + */ |
| 84 | + def changeValue(k: K, defaultValue: => V, mergeValue: (V) => V): V = { |
| 85 | + if (k == null) { |
| 86 | + if (haveNullValue) { |
| 87 | + nullValue = mergeValue(nullValue) |
| 88 | + } else { |
| 89 | + haveNullValue = true |
| 90 | + nullValue = defaultValue |
| 91 | + } |
| 92 | + nullValue |
| 93 | + } else { |
| 94 | + val pos = _keySet.addWithoutResize(k) |
| 95 | + if ((pos & OpenHashSet.NONEXISTENCE_MASK) != 0) { |
| 96 | + val newValue = defaultValue |
| 97 | + _values(pos & OpenHashSet.POSITION_MASK) = newValue |
| 98 | + _keySet.rehashIfNeeded(k, grow, move) |
| 99 | + newValue |
| 100 | + } else { |
| 101 | + _values(pos) = mergeValue(_values(pos)) |
| 102 | + _values(pos) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + override def iterator = new Iterator[(K, V)] { |
| 108 | + var pos = -1 |
| 109 | + var nextPair: (K, V) = computeNextPair() |
| 110 | + |
| 111 | + /** Get the next value we should return from next(), or null if we're finished iterating */ |
| 112 | + def computeNextPair(): (K, V) = { |
| 113 | + if (pos == -1) { // Treat position -1 as looking at the null value |
| 114 | + if (haveNullValue) { |
| 115 | + pos += 1 |
| 116 | + return (null.asInstanceOf[K], nullValue) |
| 117 | + } |
| 118 | + pos += 1 |
| 119 | + } |
| 120 | + pos = _keySet.nextPos(pos) |
| 121 | + if (pos >= 0) { |
| 122 | + val ret = (_keySet.getValue(pos), _values(pos)) |
| 123 | + pos += 1 |
| 124 | + ret |
| 125 | + } else { |
| 126 | + null |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + def hasNext = nextPair != null |
| 131 | + |
| 132 | + def next() = { |
| 133 | + val pair = nextPair |
| 134 | + nextPair = computeNextPair() |
| 135 | + pair |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // The following member variables are declared as protected instead of private for the |
| 140 | + // specialization to work (specialized class extends the non-specialized one and needs access |
| 141 | + // to the "private" variables). |
| 142 | + // They also should have been val's. We use var's because there is a Scala compiler bug that |
| 143 | + // would throw illegal access error at runtime if they are declared as val's. |
| 144 | + protected var grow = (newCapacity: Int) => { |
| 145 | + _oldValues = _values |
| 146 | + _values = new Array[V](newCapacity) |
| 147 | + } |
| 148 | + |
| 149 | + protected var move = (oldPos: Int, newPos: Int) => { |
| 150 | + _values(newPos) = _oldValues(oldPos) |
| 151 | + } |
| 152 | +} |
0 commit comments