[ https://issues.apache.org/jira/browse/MNEMONIC-208?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15898167#comment-15898167 ]
ASF GitHub Bot commented on MNEMONIC-208: ----------------------------------------- Github user bigdata-memory commented on a diff in the pull request: https://github.com/apache/incubator-mnemonic/pull/29#discussion_r104529676 --- Diff: mnemonic-collections/src/main/java/org/apache/mnemonic/collections/DurableHashMapImpl.java --- @@ -0,0 +1,447 @@ +/* + * 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.mnemonic.collections; + +import org.apache.mnemonic.EntityFactoryProxy; +import org.apache.mnemonic.DurableType; +import org.apache.mnemonic.MemChunkHolder; +import org.apache.mnemonic.MemoryDurableEntity; +import org.apache.mnemonic.OutOfHybridMemory; +import org.apache.mnemonic.RestorableAllocator; +import org.apache.mnemonic.RestoreDurableEntityError; +import org.apache.mnemonic.RetrieveDurableEntityError; +import org.apache.mnemonic.Utils; +import org.apache.commons.lang3.tuple.Pair; +import org.apache.commons.lang3.ArrayUtils; +import sun.misc.Unsafe; + +@SuppressWarnings("restriction") +public class DurableHashMapImpl<A extends RestorableAllocator<A>, K, V> + extends DurableHashMap<K, V> implements MemoryDurableEntity<A> { + + private static final long DEFAULT_MAP_SIZE = 16; + private static final float DEFAULT_MAP_LOAD_FACTOR = 0.75f; + private static final long MAX_OBJECT_SIZE = 8; + private static long[][] fieldInfo; + private Unsafe unsafe; + private EntityFactoryProxy[] factoryProxy; + private EntityFactoryProxy[] listefproxies; + private DurableType[] genericField; + private DurableType[] listgftypes; + private volatile boolean autoResize = true; + private volatile boolean autoReclaim; + private MemChunkHolder<A> holder; + private MemChunkHolder<A> chunkAddr; + private A allocator; + /** + * Set initial capacity for a hashmap. It can grow in size. + * + * @param capacity + * Initial capacity to be set + */ + public void setCapacityHint(long capacity) { + if (0 == capacity) { + totalCapacity = DEFAULT_MAP_SIZE; + } else { + totalCapacity = 1; + while (totalCapacity < capacity) { + totalCapacity <<= 1; + } + } + threshold = (long) (totalCapacity * DEFAULT_MAP_LOAD_FACTOR); + } + + /** + * Add a new key-value pair to map + * + * @param key + * the key to be set + * + * @param value + * the value to be set + * + * @return previous value with key else return null + */ + @Override + public V put(K key, V value) { + int hash = hash(key.hashCode()); + long bucketIndex = getBucketIndex(hash); + long bucketAddr = holder.get() + MAX_OBJECT_SIZE * bucketIndex; + V retVal = addEntry(key, value, bucketAddr); + if (autoResize && (mapSize >= threshold)) { + resize(2 * totalCapacity); + } + return retVal; + } + + /** + * Add a new key-value pair to map at a given bucket address + * + * @param key + * the key to be set + * + * @param value + * the value to be set + * + * @param bucketAddr + * the addr of the bucket where key is hashed + * + * @return previous value with key else return null + */ + public V addEntry(K key, V value, long bucketAddr) { + V retValue = null; + long handler = unsafe.getAddress(bucketAddr); + if (0L == handler) { + DurableSinglyLinkedList<MapEntry<K, V>> head = DurableSinglyLinkedListFactory.create(allocator, + listefproxies, listgftypes, false); + MapEntry<K, V> entry = MapEntryFactory.create(allocator, factoryProxy, genericField, false); + entry.setKey(key, false); + entry.setValue(value, false); + head.setItem(entry, false); + unsafe.putLong(bucketAddr, head.getHandler()); + mapSize++; + } else { + DurableSinglyLinkedList<MapEntry<K, V>> head = DurableSinglyLinkedListFactory.restore(allocator, + listefproxies, listgftypes, handler, false); + DurableSinglyLinkedList<MapEntry<K, V>> prev = head; + boolean found = false; + while (null != head) { + MapEntry<K, V> mapEntry = head.getItem(); + K entryKey = mapEntry.getKey(); + if (entryKey == key || entryKey.equals(key)) { + retValue = mapEntry.getValue(); + mapEntry.setValue(value, false); + found = true; + break; + } + prev = head; + head = head.getNext(); + } + if (true != found) { + DurableSinglyLinkedList<MapEntry<K, V>> newNode = DurableSinglyLinkedListFactory.create(allocator, + listefproxies, listgftypes, false); + MapEntry<K, V> entry = MapEntryFactory.create(allocator, factoryProxy, genericField, false); --- End diff -- In this place, a OutofHybridMemory Exception could be thrown out, It might be better to catch this exception and destory newNode manually if the autoreclaim is flagged as false. > Implement GET/PUT/DELETE Map operations > ----------------------------------------- > > Key: MNEMONIC-208 > URL: https://issues.apache.org/jira/browse/MNEMONIC-208 > Project: Mnemonic > Issue Type: Sub-task > Components: Collection > Reporter: Johnu George > Assignee: Johnu George > Fix For: 0.5.0-incubating > > -- This message was sent by Atlassian JIRA (v6.3.15#6346)