HBASE-2909 SoftValueSortedMap is broken, can generate NPEs
git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@985383 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b3baa8aca7
commit
d80de85a69
|
@ -475,6 +475,7 @@ Release 0.21.0 - Unreleased
|
|||
HBASE-2905 NPE when inserting mass data via REST interface (Sandy Yin via
|
||||
Andrew Purtell)
|
||||
HBASE-2908 Wrong order of null-check [in TIF] (Libor Dener via Stack)
|
||||
HBASE-2909 SoftValueSortedMap is broken, can generate NPEs
|
||||
|
||||
IMPROVEMENTS
|
||||
HBASE-1760 Cleanup TODOs in HTable
|
||||
|
|
|
@ -1,49 +0,0 @@
|
|||
/**
|
||||
* Copyright 2010 The Apache Software Foundation
|
||||
*
|
||||
* 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.hadoop.hbase.util;
|
||||
|
||||
import java.lang.ref.ReferenceQueue;
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A SoftReference derivative so that we can track down what keys to remove.
|
||||
*/
|
||||
class SoftValue<K, V> extends SoftReference<V> implements Map.Entry<K, V> {
|
||||
private final K key;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
SoftValue(K key, V value, ReferenceQueue queue) {
|
||||
super(value, queue);
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public K getKey() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
public V getValue() {
|
||||
return get();
|
||||
}
|
||||
|
||||
public V setValue(V value) {
|
||||
throw new RuntimeException("Not implemented");
|
||||
}
|
||||
}
|
|
@ -1,146 +0,0 @@
|
|||
/**
|
||||
* Copyright 2010 The Apache Software Foundation
|
||||
*
|
||||
* 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.hadoop.hbase.util;
|
||||
|
||||
import java.lang.ref.ReferenceQueue;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A Map that uses Soft Reference values internally. Use as a simple cache.
|
||||
*
|
||||
* @param <K> key class
|
||||
* @param <V> value class
|
||||
*/
|
||||
public class SoftValueMap<K,V> implements Map<K,V> {
|
||||
private final Map<K, SoftValue<K,V>> internalMap =
|
||||
new HashMap<K, SoftValue<K,V>>();
|
||||
private final ReferenceQueue<?> rq;
|
||||
|
||||
public SoftValueMap() {
|
||||
this(new ReferenceQueue());
|
||||
}
|
||||
|
||||
public SoftValueMap(final ReferenceQueue<?> rq) {
|
||||
this.rq = rq;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks soft references and cleans any that have been placed on
|
||||
* ReferenceQueue.
|
||||
* @return How many references cleared.
|
||||
*/
|
||||
@SuppressWarnings({"unchecked"})
|
||||
public int checkReferences() {
|
||||
int i = 0;
|
||||
for (Object obj; (obj = this.rq.poll()) != null;) {
|
||||
i++;
|
||||
this.internalMap.remove(((SoftValue<K,V>)obj).getKey());
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
public V put(K key, V value) {
|
||||
checkReferences();
|
||||
SoftValue<K,V> oldValue = this.internalMap.put(key,
|
||||
new SoftValue<K,V>(key, value, this.rq));
|
||||
return oldValue == null ? null : oldValue.get();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void putAll(Map map) {
|
||||
throw new RuntimeException("Not implemented");
|
||||
}
|
||||
|
||||
@SuppressWarnings({"SuspiciousMethodCalls"})
|
||||
public V get(Object key) {
|
||||
checkReferences();
|
||||
SoftValue<K,V> value = this.internalMap.get(key);
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
if (value.get() == null) {
|
||||
this.internalMap.remove(key);
|
||||
return null;
|
||||
}
|
||||
return value.get();
|
||||
}
|
||||
|
||||
public V remove(Object key) {
|
||||
checkReferences();
|
||||
SoftValue<K,V> value = this.internalMap.remove(key);
|
||||
return value == null ? null : value.get();
|
||||
}
|
||||
|
||||
public boolean containsKey(Object key) {
|
||||
checkReferences();
|
||||
return this.internalMap.containsKey(key);
|
||||
}
|
||||
|
||||
public boolean containsValue(Object value) {
|
||||
/* checkReferences();
|
||||
return internalMap.containsValue(value);*/
|
||||
throw new UnsupportedOperationException("Don't support containsValue!");
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
checkReferences();
|
||||
return this.internalMap.isEmpty();
|
||||
}
|
||||
|
||||
public int size() {
|
||||
checkReferences();
|
||||
return this.internalMap.size();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
checkReferences();
|
||||
this.internalMap.clear();
|
||||
}
|
||||
|
||||
public Set<K> keySet() {
|
||||
checkReferences();
|
||||
return this.internalMap.keySet();
|
||||
}
|
||||
|
||||
public Set<Map.Entry<K,V>> entrySet() {
|
||||
checkReferences();
|
||||
Set<Map.Entry<K, SoftValue<K,V>>> entries = this.internalMap.entrySet();
|
||||
Set<Map.Entry<K, V>> real_entries = new HashSet<Map.Entry<K,V>>();
|
||||
for(Map.Entry<K, SoftValue<K,V>> entry : entries) {
|
||||
real_entries.add(entry.getValue());
|
||||
}
|
||||
return real_entries;
|
||||
}
|
||||
|
||||
public Collection<V> values() {
|
||||
checkReferences();
|
||||
Collection<SoftValue<K,V>> softValues = this.internalMap.values();
|
||||
ArrayList<V> hardValues = new ArrayList<V>();
|
||||
for(SoftValue<K,V> softValue : softValues) {
|
||||
hardValues.add(softValue.get());
|
||||
}
|
||||
return hardValues;
|
||||
}
|
||||
}
|
|
@ -20,6 +20,7 @@
|
|||
package org.apache.hadoop.hbase.util;
|
||||
|
||||
import java.lang.ref.ReferenceQueue;
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
|
@ -72,7 +73,7 @@ public class SoftValueSortedMap<K,V> implements SortedMap<K,V> {
|
|||
for (Object obj; (obj = this.rq.poll()) != null;) {
|
||||
i++;
|
||||
//noinspection unchecked
|
||||
this.internalMap.remove(((SoftValue<K,V>)obj).getKey());
|
||||
this.internalMap.remove(((SoftValue<K,V>)obj).key);
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
@ -171,13 +172,7 @@ public class SoftValueSortedMap<K,V> implements SortedMap<K,V> {
|
|||
}
|
||||
|
||||
public synchronized Set<Map.Entry<K,V>> entrySet() {
|
||||
checkReferences();
|
||||
Set<Map.Entry<K, SoftValue<K,V>>> entries = this.internalMap.entrySet();
|
||||
Set<Map.Entry<K, V>> real_entries = new TreeSet<Map.Entry<K,V>>();
|
||||
for(Map.Entry<K, SoftValue<K,V>> entry : entries) {
|
||||
real_entries.add(entry.getValue());
|
||||
}
|
||||
return real_entries;
|
||||
throw new RuntimeException("Not implemented");
|
||||
}
|
||||
|
||||
public synchronized Collection<V> values() {
|
||||
|
@ -189,4 +184,13 @@ public class SoftValueSortedMap<K,V> implements SortedMap<K,V> {
|
|||
}
|
||||
return hardValues;
|
||||
}
|
||||
|
||||
private static class SoftValue<K,V> extends SoftReference<V> {
|
||||
final K key;
|
||||
|
||||
SoftValue(K key, V value, ReferenceQueue q) {
|
||||
super(value, q);
|
||||
this.key = key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue