|
| 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.mnemonic.collections; |
| 19 | + |
| 20 | +import org.apache.mnemonic.EntityFactoryProxy; |
| 21 | +import org.apache.mnemonic.DurableType; |
| 22 | +import org.apache.mnemonic.MemoryDurableEntity; |
| 23 | +import org.apache.mnemonic.OutOfHybridMemory; |
| 24 | +import org.apache.mnemonic.RestorableAllocator; |
| 25 | +import org.apache.mnemonic.RestoreDurableEntityError; |
| 26 | +import org.apache.mnemonic.RetrieveDurableEntityError; |
| 27 | +import org.apache.mnemonic.Utils; |
| 28 | + |
| 29 | +import sun.misc.Unsafe; |
| 30 | +import java.util.Iterator; |
| 31 | + |
| 32 | +@SuppressWarnings("restriction") |
| 33 | +public class DurableHashSetImpl<A extends RestorableAllocator<A>, E> |
| 34 | + extends DurableHashSet<E> implements MemoryDurableEntity<A> { |
| 35 | + |
| 36 | + private static long[][] fieldInfo; |
| 37 | + private Unsafe unsafe; |
| 38 | + private EntityFactoryProxy[] factoryProxy; |
| 39 | + private DurableType[] genericType; |
| 40 | + private volatile boolean autoReclaim; |
| 41 | + private DurableHashMap<E, Object> map; |
| 42 | + private A allocator; |
| 43 | + private long initialCapacity; |
| 44 | + |
| 45 | + public void setCapacityHint(long capacity) { |
| 46 | + initialCapacity = capacity; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public long getSize() { |
| 51 | + return map.getSize(); |
| 52 | + } |
| 53 | + /** |
| 54 | + * adds a specific element to the set |
| 55 | + * |
| 56 | + * @return true if set did not already contain the element |
| 57 | + */ |
| 58 | + public boolean add(E item) { |
| 59 | + //add logic for adding item |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * removes a specific element from the set |
| 65 | + * |
| 66 | + * @return true if set contained the element |
| 67 | + */ |
| 68 | + public boolean remove(E value) { |
| 69 | + //add logic for removing item |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * checks if set contains the specified element |
| 75 | + * |
| 76 | + * @return true if set contains the element |
| 77 | + */ |
| 78 | + public boolean contains(E item) { |
| 79 | + //add logic to check if set contains the item |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public boolean autoReclaim() { |
| 85 | + return autoReclaim; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public long[][] getNativeFieldInfo() { |
| 90 | + return fieldInfo; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void destroy() throws RetrieveDurableEntityError { |
| 95 | + map.destroy(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void cancelAutoReclaim() { |
| 100 | + map.cancelAutoReclaim(); |
| 101 | + autoReclaim = false; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void registerAutoReclaim() { |
| 106 | + map.registerAutoReclaim(); |
| 107 | + autoReclaim = true; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public long getHandler() { |
| 112 | + return map.getHandler(); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void restoreDurableEntity(A allocator, EntityFactoryProxy[] factoryProxy, |
| 117 | + DurableType[] gType, long phandler, boolean autoReclaim) throws RestoreDurableEntityError { |
| 118 | + initializeDurableEntity(allocator, factoryProxy, gType, autoReclaim); |
| 119 | + if (0L == phandler) { |
| 120 | + throw new RestoreDurableEntityError("Input handler is null on restoreDurableEntity."); |
| 121 | + } |
| 122 | + map = DurableHashMapFactory.restore(allocator, factoryProxy, gType, phandler, autoReclaim); |
| 123 | + if (null == map) { |
| 124 | + throw new RestoreDurableEntityError("Retrieve Entity Failure!"); |
| 125 | + } |
| 126 | + initializeAfterRestore(); |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + @Override |
| 131 | + public void initializeDurableEntity(A allocator, EntityFactoryProxy[] factoryProxy, |
| 132 | + DurableType[] gType, boolean autoReclaim) { |
| 133 | + this.allocator = allocator; |
| 134 | + this.factoryProxy = factoryProxy; |
| 135 | + this.genericType = gType; |
| 136 | + this.autoReclaim = autoReclaim; |
| 137 | + try { |
| 138 | + this.unsafe = Utils.getUnsafe(); |
| 139 | + } catch (Exception e) { |
| 140 | + e.printStackTrace(); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void createDurableEntity(A allocator, EntityFactoryProxy[] factoryProxy, |
| 146 | + DurableType[] gType, boolean autoReclaim) throws OutOfHybridMemory { |
| 147 | + initializeDurableEntity(allocator, factoryProxy, gType, autoReclaim); |
| 148 | + map = DurableHashMapFactory.create(allocator, factoryProxy, gType, initialCapacity, autoReclaim); |
| 149 | + initializeAfterCreate(); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public Iterator<E> iterator() { |
| 154 | + return null; |
| 155 | + } |
| 156 | +} |
0 commit comments