rename FreeableMemory -> RefCountedMemory
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/af97b0f3 Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/af97b0f3 Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/af97b0f3 Branch: refs/heads/trunk Commit: af97b0f3ae080b01cbf1a3db934087082eeedb89 Parents: 98721bf Author: Jonathan Ellis <[email protected]> Authored: Thu Oct 25 22:57:37 2012 -0500 Committer: Jonathan Ellis <[email protected]> Committed: Fri Oct 26 01:06:18 2012 -0700 ---------------------------------------------------------------------- .../org/apache/cassandra/cache/FreeableMemory.java | 55 --------------- .../apache/cassandra/cache/RefCountedMemory.java | 55 +++++++++++++++ .../apache/cassandra/cache/SerializingCache.java | 40 +++++----- src/java/org/apache/cassandra/io/util/Memory.java | 3 + .../cassandra/io/util/MemoryInputStream.java | 6 +- .../apache/cassandra/cache/CacheProviderTest.java | 2 +- 6 files changed, 82 insertions(+), 79 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/af97b0f3/src/java/org/apache/cassandra/cache/FreeableMemory.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cache/FreeableMemory.java b/src/java/org/apache/cassandra/cache/FreeableMemory.java deleted file mode 100644 index d088b6d..0000000 --- a/src/java/org/apache/cassandra/cache/FreeableMemory.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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.cassandra.cache; - -import java.util.concurrent.atomic.AtomicInteger; - -import org.apache.cassandra.io.util.Memory; - -public class FreeableMemory extends Memory -{ - private final AtomicInteger references = new AtomicInteger(1); - - public FreeableMemory(long size) - { - super(size); - } - - /** - * @return true if we succeed in referencing before the reference count reaches zero. - * (A FreeableMemory object is created with a reference count of one.) - */ - public boolean reference() - { - while (true) - { - int n = references.get(); - if (n <= 0) - return false; - if (references.compareAndSet(n, n + 1)) - return true; - } - } - - /** decrement reference count. if count reaches zero, the object is freed. */ - public void unreference() - { - if (references.decrementAndGet() == 0) - free(); - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/af97b0f3/src/java/org/apache/cassandra/cache/RefCountedMemory.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cache/RefCountedMemory.java b/src/java/org/apache/cassandra/cache/RefCountedMemory.java new file mode 100644 index 0000000..887bd86 --- /dev/null +++ b/src/java/org/apache/cassandra/cache/RefCountedMemory.java @@ -0,0 +1,55 @@ +/* + * 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.cassandra.cache; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.cassandra.io.util.Memory; + +public class RefCountedMemory extends Memory +{ + private final AtomicInteger references = new AtomicInteger(1); + + public RefCountedMemory(long size) + { + super(size); + } + + /** + * @return true if we succeed in referencing before the reference count reaches zero. + * (A FreeableMemory object is created with a reference count of one.) + */ + public boolean reference() + { + while (true) + { + int n = references.get(); + if (n <= 0) + return false; + if (references.compareAndSet(n, n + 1)) + return true; + } + } + + /** decrement reference count. if count reaches zero, the object is freed. */ + public void unreference() + { + if (references.decrementAndGet() == 0) + free(); + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/af97b0f3/src/java/org/apache/cassandra/cache/SerializingCache.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cache/SerializingCache.java b/src/java/org/apache/cassandra/cache/SerializingCache.java index bf2a319..6047bed 100644 --- a/src/java/org/apache/cassandra/cache/SerializingCache.java +++ b/src/java/org/apache/cassandra/cache/SerializingCache.java @@ -43,22 +43,22 @@ public class SerializingCache<K, V> implements ICache<K, V> private static final int DEFAULT_CONCURENCY_LEVEL = 64; - private final ConcurrentLinkedHashMap<K, FreeableMemory> map; + private final ConcurrentLinkedHashMap<K, RefCountedMemory> map; private final ISerializer<V> serializer; - private SerializingCache(long capacity, Weigher<FreeableMemory> weigher, ISerializer<V> serializer) + private SerializingCache(long capacity, Weigher<RefCountedMemory> weigher, ISerializer<V> serializer) { this.serializer = serializer; - EvictionListener<K,FreeableMemory> listener = new EvictionListener<K, FreeableMemory>() + EvictionListener<K,RefCountedMemory> listener = new EvictionListener<K, RefCountedMemory>() { - public void onEviction(K k, FreeableMemory mem) + public void onEviction(K k, RefCountedMemory mem) { mem.unreference(); } }; - this.map = new ConcurrentLinkedHashMap.Builder<K, FreeableMemory>() + this.map = new ConcurrentLinkedHashMap.Builder<K, RefCountedMemory>() .weigher(weigher) .maximumWeightedCapacity(capacity) .concurrencyLevel(DEFAULT_CONCURENCY_LEVEL) @@ -66,16 +66,16 @@ public class SerializingCache<K, V> implements ICache<K, V> .build(); } - public static <K, V> SerializingCache<K, V> create(long weightedCapacity, Weigher<FreeableMemory> weigher, ISerializer<V> serializer) + public static <K, V> SerializingCache<K, V> create(long weightedCapacity, Weigher<RefCountedMemory> weigher, ISerializer<V> serializer) { return new SerializingCache<K, V>(weightedCapacity, weigher, serializer); } public static <K, V> SerializingCache<K, V> create(long weightedCapacity, ISerializer<V> serializer) { - return create(weightedCapacity, new Weigher<FreeableMemory>() + return create(weightedCapacity, new Weigher<RefCountedMemory>() { - public int weightOf(FreeableMemory value) + public int weightOf(RefCountedMemory value) { long size = value.size(); assert size < Integer.MAX_VALUE : "Serialized size cannot be more than 2GB"; @@ -84,7 +84,7 @@ public class SerializingCache<K, V> implements ICache<K, V> }, serializer); } - private V deserialize(FreeableMemory mem) + private V deserialize(RefCountedMemory mem) { try { @@ -97,16 +97,16 @@ public class SerializingCache<K, V> implements ICache<K, V> } } - private FreeableMemory serialize(V value) + private RefCountedMemory serialize(V value) { long serializedSize = serializer.serializedSize(value, ENCODED_TYPE_SIZES); if (serializedSize > Integer.MAX_VALUE) throw new IllegalArgumentException("Unable to allocate " + serializedSize + " bytes"); - FreeableMemory freeableMemory; + RefCountedMemory freeableMemory; try { - freeableMemory = new FreeableMemory(serializedSize); + freeableMemory = new RefCountedMemory(serializedSize); } catch (OutOfMemoryError e) { @@ -156,7 +156,7 @@ public class SerializingCache<K, V> implements ICache<K, V> public V get(Object key) { - FreeableMemory mem = map.get(key); + RefCountedMemory mem = map.get(key); if (mem == null) return null; if (!mem.reference()) @@ -173,22 +173,22 @@ public class SerializingCache<K, V> implements ICache<K, V> public void put(K key, V value) { - FreeableMemory mem = serialize(value); + RefCountedMemory mem = serialize(value); if (mem == null) return; // out of memory. never mind. - FreeableMemory old = map.put(key, mem); + RefCountedMemory old = map.put(key, mem); if (old != null) old.unreference(); } public boolean putIfAbsent(K key, V value) { - FreeableMemory mem = serialize(value); + RefCountedMemory mem = serialize(value); if (mem == null) return false; // out of memory. never mind. - FreeableMemory old = map.putIfAbsent(key, mem); + RefCountedMemory old = map.putIfAbsent(key, mem); if (old != null) // the new value was not put, we've uselessly allocated some memory, free it mem.unreference(); @@ -198,12 +198,12 @@ public class SerializingCache<K, V> implements ICache<K, V> public boolean replace(K key, V oldToReplace, V value) { // if there is no old value in our map, we fail - FreeableMemory old = map.get(key); + RefCountedMemory old = map.get(key); if (old == null) return false; // see if the old value matches the one we want to replace - FreeableMemory mem = serialize(value); + RefCountedMemory mem = serialize(value); if (mem == null) return false; // out of memory. never mind. @@ -230,7 +230,7 @@ public class SerializingCache<K, V> implements ICache<K, V> public void remove(K key) { - FreeableMemory mem = map.remove(key); + RefCountedMemory mem = map.remove(key); if (mem != null) mem.unreference(); } http://git-wip-us.apache.org/repos/asf/cassandra/blob/af97b0f3/src/java/org/apache/cassandra/io/util/Memory.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/io/util/Memory.java b/src/java/org/apache/cassandra/io/util/Memory.java index 73f6a96..faef564 100644 --- a/src/java/org/apache/cassandra/io/util/Memory.java +++ b/src/java/org/apache/cassandra/io/util/Memory.java @@ -21,6 +21,9 @@ import sun.misc.Unsafe; import java.lang.reflect.Field; +/** + * An off-heap region of memory that must be manually free'd when no longer needed. + */ public class Memory { private static final Unsafe unsafe; http://git-wip-us.apache.org/repos/asf/cassandra/blob/af97b0f3/src/java/org/apache/cassandra/io/util/MemoryInputStream.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/io/util/MemoryInputStream.java b/src/java/org/apache/cassandra/io/util/MemoryInputStream.java index 4931a6a..b7fb5ab 100644 --- a/src/java/org/apache/cassandra/io/util/MemoryInputStream.java +++ b/src/java/org/apache/cassandra/io/util/MemoryInputStream.java @@ -19,15 +19,15 @@ package org.apache.cassandra.io.util; import java.io.IOException; -import org.apache.cassandra.cache.FreeableMemory; +import org.apache.cassandra.cache.RefCountedMemory; public class MemoryInputStream extends AbstractDataInput { - private final FreeableMemory mem; + private final RefCountedMemory mem; private int position = 0; - public MemoryInputStream(FreeableMemory mem) + public MemoryInputStream(RefCountedMemory mem) { this.mem = mem; } http://git-wip-us.apache.org/repos/asf/cassandra/blob/af97b0f3/test/unit/org/apache/cassandra/cache/CacheProviderTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/cache/CacheProviderTest.java b/test/unit/org/apache/cassandra/cache/CacheProviderTest.java index 8b4d17b..fe0aa7f 100644 --- a/test/unit/org/apache/cassandra/cache/CacheProviderTest.java +++ b/test/unit/org/apache/cassandra/cache/CacheProviderTest.java @@ -117,7 +117,7 @@ public class CacheProviderTest extends SchemaLoader @Test public void testSerializingCache() throws InterruptedException { - ICache<String, IRowCacheEntry> cache = SerializingCache.create(CAPACITY, Weighers.<FreeableMemory>singleton(), new SerializingCacheProvider.RowCacheSerializer()); + ICache<String, IRowCacheEntry> cache = SerializingCache.create(CAPACITY, Weighers.<RefCountedMemory>singleton(), new SerializingCacheProvider.RowCacheSerializer()); ColumnFamily cf = createCF(); simpleCase(cf, cache); concurrentCase(cf, cache);
