Skip to content

Commit 5c54177

Browse files
author
Johnu George
committed
MNEMONIC-255: Add Insert and Search functionality for balanced BST
1 parent fa04538 commit 5c54177

File tree

6 files changed

+1070
-0
lines changed

6 files changed

+1070
-0
lines changed

build-tools/test.conf

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

42+
# a testcase for module "mnemonic-collection" that requires 'pmalloc' memory service to pass
43+
mvn -Dtest=DurableTreeNGTest test -pl mnemonic-collections -DskipTests=false
44+
4245
# a testcase for module "mnemonic-computing-services/mnemonic-utilities-service" that requires 'pmalloc' memory service to pass
4346
mvn -Dtest=DurableSinglyLinkedListNGPrintTest test -pl mnemonic-computing-services/mnemonic-utilities-service -DskipTests=false
4447

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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 balanced binary tree(Red-Black tree)
28+
*
29+
*/
30+
public abstract class DurableTree<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+
* search item value
71+
*
72+
* @param item
73+
* the item to be set
74+
*
75+
* @return true if item is present in the tree else false
76+
*/
77+
public abstract boolean contains(E item);
78+
79+
/**
80+
* find successor item
81+
*
82+
* @param item
83+
* the item in the traversal
84+
*
85+
* @return successor item in the inorder traversal
86+
*/
87+
public abstract E successor(E item);
88+
89+
/**
90+
* find predecessor item
91+
*
92+
* @param item
93+
* the item in the traversal
94+
*
95+
* @return predecessor item in the inorder traversal
96+
*/
97+
public abstract E predecessor(E item);
98+
99+
/**
100+
* insert a item in the tree
101+
*
102+
* @param item
103+
* the item to be inserted
104+
*/
105+
public abstract void insert(E item);
106+
107+
/**
108+
* print tree
109+
*
110+
*/
111+
public abstract void print();
112+
113+
/**
114+
* check if tree is a valid red black tree
115+
*
116+
* @return true if tree is a valid RB else false
117+
*/
118+
public abstract boolean isValidTree();
119+
120+
/**
121+
* get an iterator instance of this tree
122+
*
123+
* @return an iterator of this tree
124+
*/
125+
@Override
126+
public Iterator<E> iterator() {
127+
return null;
128+
}
129+
130+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 DurableTreeFactory {
27+
public static <A extends RestorableAllocator<A>, E extends Comparable<E>> DurableTree<E>
28+
create(A allocator) throws OutOfHybridMemory {
29+
return create(allocator, false);
30+
}
31+
32+
public static <A extends RestorableAllocator<A>, E extends Comparable<E>> DurableTree<E>
33+
create(A allocator, boolean autoreclaim) throws OutOfHybridMemory {
34+
return create(allocator, null, null, autoreclaim);
35+
}
36+
37+
public static <A extends RestorableAllocator<A>, E extends Comparable<E>> DurableTree<E>
38+
create(A allocator, EntityFactoryProxy[] factoryproxys, DurableType[] gfields,
39+
boolean autoreclaim) throws OutOfHybridMemory {
40+
DurableTreeImpl<A, E> entity = new DurableTreeImpl<A, E>();
41+
entity.setupGenericInfo(factoryproxys, gfields);
42+
entity.createDurableEntity(allocator, factoryproxys, gfields, autoreclaim);
43+
return entity;
44+
}
45+
46+
public static <A extends RestorableAllocator<A>, E extends Comparable<E>> DurableTree<E>
47+
restore(A allocator, long phandler) throws RestoreDurableEntityError {
48+
return restore(allocator, phandler, false);
49+
}
50+
51+
public static <A extends RestorableAllocator<A>, E extends Comparable<E>> DurableTree<E>
52+
restore(A allocator, long phandler, boolean autoreclaim) throws RestoreDurableEntityError {
53+
return restore(allocator, null, null, phandler, autoreclaim);
54+
}
55+
56+
public static <A extends RestorableAllocator<A>, E extends Comparable<E>> DurableTree<E>
57+
restore(A allocator, EntityFactoryProxy[] factoryproxys, DurableType[] gfields,
58+
long phandler, boolean autoreclaim) throws RestoreDurableEntityError {
59+
DurableTreeImpl<A, E> entity = new DurableTreeImpl<A, E>();
60+
entity.setupGenericInfo(factoryproxys, gfields);
61+
entity.restoreDurableEntity(allocator, factoryproxys, gfields, phandler, autoreclaim);
62+
return entity;
63+
}
64+
}

0 commit comments

Comments
 (0)