Skip to content

Commit 3a06511

Browse files
author
Johnu George
committed
MNEMONIC-221: Implement get and set for durable array
1 parent f56caa8 commit 3a06511

File tree

5 files changed

+808
-0
lines changed

5 files changed

+808
-0
lines changed

build-tools/test.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ mvn -Dtest=DurablePersonNGTest test -pl mnemonic-collections -DskipTests=false
3636
# a testcase for module "mnemonic-collection" that requires 'pmalloc' memory service to pass
3737
mvn -Dtest=DurableHashMapNGTest test -pl mnemonic-collections -DskipTests=false
3838

39+
# a testcase for module "mnemonic-collection" that requires 'pmalloc' memory service to pass
40+
mvn -Dtest=DurableArrayNGTest test -pl mnemonic-collections -DskipTests=false
41+
3942
# a testcase for module "mnemonic-computing-services/mnemonic-utilities-service" that requires 'pmalloc' memory service to pass
4043
mvn -Dtest=DurableSinglyLinkedListNGPrintTest test -pl mnemonic-computing-services/mnemonic-utilities-service -DskipTests=false
4144

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 array implementation
28+
*
29+
*/
30+
public abstract class DurableArray<E> implements Durable, Iterable<E> {
31+
protected transient EntityFactoryProxy[] m_node_efproxies;
32+
protected transient DurableType[] m_node_gftypes;
33+
protected int arraySize = 0;
34+
35+
public DurableArray(int size) {
36+
arraySize = size;
37+
}
38+
39+
/**
40+
* creation callback for initialization
41+
*
42+
*/
43+
@Override
44+
public void initializeAfterCreate() {
45+
// System.out.println("Initializing After Created");
46+
}
47+
48+
/**
49+
* restore callback for initialization
50+
*
51+
*/
52+
@Override
53+
public void initializeAfterRestore() {
54+
// System.out.println("Initializing After Restored");
55+
}
56+
57+
/**
58+
* this function will be invoked by its factory to setup generic related info
59+
* to avoid expensive operations from reflection
60+
*
61+
* @param efproxies
62+
* specify a array of factory to proxy the restoring of its generic
63+
* field objects
64+
*
65+
* @param gftypes
66+
* specify a array of types corresponding to efproxies
67+
*/
68+
@Override
69+
public void setupGenericInfo(EntityFactoryProxy[] efproxies, DurableType[] gftypes) {
70+
m_node_efproxies = efproxies;
71+
m_node_gftypes = gftypes;
72+
}
73+
74+
/**
75+
* get the item at a given index
76+
*
77+
* @return the item value of this node
78+
*/
79+
public abstract E get(int index);
80+
81+
/**
82+
* set a value at a given index
83+
*
84+
* @param value
85+
* the value to be set
86+
*/
87+
public abstract void set(int index, E value);
88+
89+
/**
90+
* set a value at a given index
91+
*
92+
* @param value
93+
* the value to be set
94+
*
95+
* @param destroy
96+
* true if want to destroy exist one
97+
*
98+
*/
99+
public abstract void set(int index, E value, boolean destroy);
100+
101+
/**
102+
* get an iterator instance of this list
103+
*
104+
* @return an iterator of this list
105+
*/
106+
public abstract Iterator<E> iterator();
107+
108+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 DurableArrayFactory {
27+
public static <A extends RestorableAllocator<A>, E> DurableArray<E>
28+
create(A allocator) throws OutOfHybridMemory {
29+
return create(allocator, 0, false);
30+
}
31+
32+
public static <A extends RestorableAllocator<A>, E> DurableArray<E>
33+
create(A allocator, int size) throws OutOfHybridMemory {
34+
return create(allocator, size, false);
35+
}
36+
37+
public static <A extends RestorableAllocator<A>, E> DurableArray<E>
38+
create(A allocator, int size, boolean autoreclaim) throws OutOfHybridMemory {
39+
return create(allocator, null, null, size, autoreclaim);
40+
}
41+
42+
public static <A extends RestorableAllocator<A>, E> DurableArray<E>
43+
create(A allocator, EntityFactoryProxy[] factoryproxys, DurableType[] gfields,
44+
int size, boolean autoreclaim) throws OutOfHybridMemory {
45+
DurableArrayImpl<A, E> entity = new DurableArrayImpl<A, E>(size);
46+
entity.setupGenericInfo(factoryproxys, gfields);
47+
entity.createDurableEntity(allocator, factoryproxys, gfields, autoreclaim);
48+
return entity;
49+
}
50+
51+
public static <A extends RestorableAllocator<A>, E> DurableArray<E>
52+
restore(A allocator, long phandler) throws RestoreDurableEntityError {
53+
return restore(allocator, phandler, false);
54+
}
55+
56+
public static <A extends RestorableAllocator<A>, E> DurableArray<E>
57+
restore(A allocator, long phandler, boolean autoreclaim) throws RestoreDurableEntityError {
58+
return restore(allocator, null, null, phandler, autoreclaim);
59+
}
60+
61+
public static <A extends RestorableAllocator<A>, E> DurableArray<E>
62+
restore(A allocator, EntityFactoryProxy[] factoryproxys, DurableType[] gfields,
63+
long phandler, boolean autoreclaim) throws RestoreDurableEntityError {
64+
DurableArrayImpl<A, E> entity = new DurableArrayImpl<A, E>();
65+
entity.setupGenericInfo(factoryproxys, gfields);
66+
entity.restoreDurableEntity(allocator, factoryproxys, gfields, phandler, autoreclaim);
67+
return entity;
68+
}
69+
}

0 commit comments

Comments
 (0)