Skip to content

Commit 8923b1e

Browse files
committed
Initial template for DurableSet
1 parent 3a06511 commit 8923b1e

File tree

4 files changed

+488
-0
lines changed

4 files changed

+488
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 java.util.Iterator;
21+
22+
import org.apache.mnemonic.Durable;
23+
import org.apache.mnemonic.EntityFactoryProxy;
24+
import org.apache.mnemonic.DurableType;
25+
26+
/**
27+
* this class defines a non-volatile HashSet implementation
28+
*
29+
*/
30+
public abstract class DurableHashSet<E> implements Durable, Iterable<E> {
31+
protected transient EntityFactoryProxy[] m_node_efproxies;
32+
protected transient DurableType[] m_node_gftypes;
33+
34+
/**
35+
* creation callback for initialization
36+
*
37+
*/
38+
@Override
39+
public void initializeAfterCreate() {
40+
// System.out.println("Initializing After Created");
41+
}
42+
43+
/**
44+
* restore callback for initialization
45+
*
46+
*/
47+
@Override
48+
public void initializeAfterRestore() {
49+
// System.out.println("Initializing After Restored");
50+
}
51+
52+
/**
53+
* this function will be invoked by its factory to setup generic related info
54+
* to avoid expensive operations from reflection
55+
*
56+
* @param efproxies
57+
* specify a array of factory to proxy the restoring of its generic
58+
* field objects
59+
*
60+
* @param gftypes
61+
* specify a array of types corresponding to efproxies
62+
*/
63+
@Override
64+
public void setupGenericInfo(EntityFactoryProxy[] efproxies, DurableType[] gftypes) {
65+
m_node_efproxies = efproxies;
66+
m_node_gftypes = gftypes;
67+
}
68+
69+
/**
70+
* checks if set contains the specified element
71+
*
72+
* @return true if set contains the element
73+
*/
74+
public abstract boolean contains(E item);
75+
76+
/**
77+
* adds a specific element to the set
78+
*
79+
* @return true if set did not already contain the element
80+
*/
81+
public abstract boolean add(E item);
82+
83+
/**
84+
* removes a specific element from the set
85+
*
86+
* @return true if set contained the element
87+
*/
88+
public abstract boolean remove(E value);
89+
90+
/**
91+
* Get the number of elements in the set
92+
*
93+
* @return size of the set
94+
*/
95+
public abstract long getSize();
96+
97+
/**
98+
* get an iterator instance of set
99+
*
100+
* @return an iterator of set
101+
*/
102+
public abstract Iterator<E> iterator();
103+
104+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.DurableType;
21+
import org.apache.mnemonic.EntityFactoryProxy;
22+
import org.apache.mnemonic.OutOfHybridMemory;
23+
import org.apache.mnemonic.RestorableAllocator;
24+
import org.apache.mnemonic.RestoreDurableEntityError;
25+
26+
public class DurableHashSetFactory {
27+
public static <A extends RestorableAllocator<A>, E> DurableHashSet<E>
28+
create(A allocator) throws OutOfHybridMemory {
29+
return create(allocator, 0L, false);
30+
}
31+
32+
public static <A extends RestorableAllocator<A>, E> DurableHashSet<E>
33+
create(A allocator, long initialCapacity) throws OutOfHybridMemory {
34+
return create(allocator, initialCapacity, false);
35+
}
36+
37+
public static <A extends RestorableAllocator<A>, E> DurableHashSet<E>
38+
create(A allocator, long initialCapacity, boolean autoreclaim) throws OutOfHybridMemory {
39+
return create(allocator, null, null, initialCapacity, autoreclaim);
40+
}
41+
42+
public static <A extends RestorableAllocator<A>, E> DurableHashSet<E>
43+
create(A allocator, EntityFactoryProxy[] factoryproxys, DurableType[] gfields,
44+
long initialCapacity, boolean autoreclaim) throws OutOfHybridMemory {
45+
DurableHashSetImpl<A, E> entity = new DurableHashSetImpl<A, E>();
46+
entity.setCapacityHint(initialCapacity);
47+
entity.setupGenericInfo(factoryproxys, gfields);
48+
entity.createDurableEntity(allocator, factoryproxys, gfields, autoreclaim);
49+
return entity;
50+
}
51+
52+
public static <A extends RestorableAllocator<A>, E> DurableHashSet<E>
53+
restore(A allocator, long phandler) throws RestoreDurableEntityError {
54+
return restore(allocator, phandler, false);
55+
}
56+
57+
public static <A extends RestorableAllocator<A>, E> DurableHashSet<E>
58+
restore(A allocator, long phandler, boolean autoreclaim) throws RestoreDurableEntityError {
59+
return restore(allocator, null, null, phandler, autoreclaim);
60+
}
61+
62+
public static <A extends RestorableAllocator<A>, E> DurableHashSet<E>
63+
restore(A allocator, EntityFactoryProxy[] factoryproxys, DurableType[] gfields,
64+
long phandler, boolean autoreclaim) throws RestoreDurableEntityError {
65+
DurableHashSetImpl<A, E> entity = new DurableHashSetImpl<A, E>();
66+
entity.setupGenericInfo(factoryproxys, gfields);
67+
entity.restoreDurableEntity(allocator, factoryproxys, gfields, phandler, autoreclaim);
68+
return entity;
69+
}
70+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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

Comments
 (0)