KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > ozoneDB > core > storage > gammaStore > IndexManagerTest


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// Copyright (C) 2004 Leo Mekenkamp. All rights reserved.
5
//
6
// $Id: IndexManagerTest.java,v 1.3 2004/03/28 16:14:40 per_nyfelt Exp $
7

8 package test.ozoneDB.core.storage.gammaStore;
9
10 import junit.framework.Test;
11 import junit.framework.TestSuite;
12 import junit.textui.TestRunner;
13 import org.ozoneDB.ObjectNotFoundException;
14 import org.ozoneDB.core.storage.FixedSizeCache;
15 import org.ozoneDB.core.storage.gammaStore.ContainerLocation;
16 import org.ozoneDB.core.storage.gammaStore.FileStreamStorageFactory;
17 import org.ozoneDB.core.storage.gammaStore.GammaStore;
18 import org.ozoneDB.core.storage.gammaStore.IndexManager;
19 import test.OzoneTestCase;
20
21 import java.io.File JavaDoc;
22 import java.util.*;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25
26 /**
27  *
28  * @author leo
29  */

30 public class IndexManagerTest extends OzoneTestCase {
31     
32     private Random random;
33     private IndexManager indexManager;
34     private Map mirror;
35     
36     private static final long DEFAULT_TESTSIZE = 10000;
37     
38     public IndexManagerTest() {
39         super("testIndexManager");
40         Logger.getLogger(IndexManager.class.getName()).setLevel(Level.ALL);
41     }
42     
43     public static Test suite() {
44         TestSuite suite = new TestSuite();
45
46         suite.addTest(new IndexManagerTest());
47         return suite;
48     }
49
50     public void testSmallNodes() throws Exception JavaDoc {
51         Properties properties = new Properties();
52         properties.setProperty(IndexManager.INDEXNODESTORAGEFACTORY.getKey(), FileStreamStorageFactory.class.getName());
53         properties.setProperty(IndexManager.INDEXNODESTORAGEFACTORY.getKey() + FileStreamStorageFactory.DIRECTORY.getKey(), "index");
54         properties.setProperty(IndexManager.INDEXNODESTORAGEFACTORY.getKey() + FileStreamStorageFactory.SUBDIRECTORYDEPTH.getKey(), "0");
55         properties.setProperty(IndexManager.INDEXNODESTORAGEFACTORY.getKey() + FileStreamStorageFactory.SUBDIRECTORYCOUNT.getKey(), "50");
56         properties.setProperty(IndexManager.GENERALINDEXNODECACHE.getKey(), FixedSizeCache.class.getName());
57         properties.setProperty(IndexManager.GENERALINDEXNODECACHE.getKey() + FixedSizeCache.MAXCAPACITY.getKey(), "2000");
58         properties.setProperty(IndexManager.MAXBRANCHNODESIZE.getKey(), "10"); // 93:2K; 226: 4K; 474:8K; 982:16K; 1998:32K
59
properties.setProperty(IndexManager.MAXLEAFNODESIZE.getKey(), "10"); // 72:2K; 179:4K; 376:8K; 784:16K; 1598:32K
60
// FIXME: Uncommentet so the test at least compiles
61
//properties.setProperty(IndexManager.FINALIZATIONTHRESHHOLD.getKey(), "0.001");
62

63         // in order to run this test with more items you need to increase stack space
64
actualTest(properties, 5000);
65     }
66     
67     public void testBigNodes() throws Exception JavaDoc {
68         Properties properties = new Properties();
69         properties.setProperty(IndexManager.INDEXNODESTORAGEFACTORY.getKey(), FileStreamStorageFactory.class.getName());
70         properties.setProperty(IndexManager.INDEXNODESTORAGEFACTORY.getKey() + FileStreamStorageFactory.DIRECTORY.getKey(), "index");
71         properties.setProperty(IndexManager.INDEXNODESTORAGEFACTORY.getKey() + FileStreamStorageFactory.SUBDIRECTORYDEPTH.getKey(), "0");
72         properties.setProperty(IndexManager.INDEXNODESTORAGEFACTORY.getKey() + FileStreamStorageFactory.SUBDIRECTORYCOUNT.getKey(), "50");
73         properties.setProperty(IndexManager.GENERALINDEXNODECACHE.getKey(), FixedSizeCache.class.getName());
74         properties.setProperty(IndexManager.GENERALINDEXNODECACHE.getKey() + FixedSizeCache.MAXCAPACITY.getKey(), "20");
75         properties.setProperty(IndexManager.MAXBRANCHNODESIZE.getKey(), "1998"); // 93:2K; 226: 4K; 474:8K; 982:16K; 1998:32K
76
properties.setProperty(IndexManager.MAXLEAFNODESIZE.getKey(), "1598"); // 72:2K; 179:4K; 376:8K; 784:16K; 1598:32K
77
// FIXME: Uncommentet so the test at least compiles
78
//properties.setProperty(IndexManager.FINALIZATIONTHRESHHOLD.getKey(), "0.001");
79
actualTest(properties, DEFAULT_TESTSIZE);
80     }
81     
82     public void testSmallCache() throws Exception JavaDoc {
83         Properties properties = new Properties();
84         properties.setProperty(IndexManager.INDEXNODESTORAGEFACTORY.getKey(), FileStreamStorageFactory.class.getName());
85         properties.setProperty(IndexManager.INDEXNODESTORAGEFACTORY.getKey() + FileStreamStorageFactory.DIRECTORY.getKey(), "index");
86         properties.setProperty(IndexManager.INDEXNODESTORAGEFACTORY.getKey() + FileStreamStorageFactory.SUBDIRECTORYDEPTH.getKey(), "2");
87         properties.setProperty(IndexManager.INDEXNODESTORAGEFACTORY.getKey() + FileStreamStorageFactory.SUBDIRECTORYCOUNT.getKey(), "10");
88         properties.setProperty(IndexManager.GENERALINDEXNODECACHE.getKey(), FixedSizeCache.class.getName());
89         properties.setProperty(IndexManager.GENERALINDEXNODECACHE.getKey() + FixedSizeCache.MAXCAPACITY.getKey(), "2");
90         properties.setProperty(IndexManager.MAXBRANCHNODESIZE.getKey(), "474"); // 93:2K; 226: 4K; 474:8K; 982:16K; 1998:32K
91
properties.setProperty(IndexManager.MAXLEAFNODESIZE.getKey(), "376"); // 72:2K; 179:4K; 376:8K; 784:16K; 1598:32K
92
// FIXME: Uncommentet so the test at least compiles
93
// properties.setProperty(IndexManager.FINALIZATIONTHRESHHOLD.getKey(), "0.001");
94
actualTest(properties, DEFAULT_TESTSIZE);
95     }
96     
97     public void testGcListenerCache() throws Exception JavaDoc {
98         Properties properties = new Properties();
99         properties.setProperty(IndexManager.INDEXNODESTORAGEFACTORY.getKey(), FileStreamStorageFactory.class.getName());
100         properties.setProperty(IndexManager.INDEXNODESTORAGEFACTORY.getKey() + FileStreamStorageFactory.DIRECTORY.getKey(), "index");
101         properties.setProperty(IndexManager.INDEXNODESTORAGEFACTORY.getKey() + FileStreamStorageFactory.SUBDIRECTORYDEPTH.getKey(), "3");
102         properties.setProperty(IndexManager.INDEXNODESTORAGEFACTORY.getKey() + FileStreamStorageFactory.SUBDIRECTORYCOUNT.getKey(), "10");
103         // FIXME: just made up some replacements for the below line
104
//properties.setProperty(IndexManager.INDEXNODECACHE.getKey(), GcListenerCache.class.getName());
105
properties.setProperty(IndexManager.GENERALINDEXNODECACHE.getKey(), FixedSizeCache.class.getName());
106         properties.setProperty(IndexManager.MAXBRANCHNODESIZE.getKey(), "474"); // 93:2K; 226: 4K; 474:8K; 982:16K; 1998:32K
107
properties.setProperty(IndexManager.MAXLEAFNODESIZE.getKey(), "376"); // 72:2K; 179:4K; 376:8K; 784:16K; 1598:32K
108
// FIXME: Uncommentet so the test at least compiles
109
// properties.setProperty(IndexManager.FINALIZATIONTHRESHHOLD.getKey(), "0.001");
110
actualTest(properties, DEFAULT_TESTSIZE);
111     }
112
113     private void randomFill(long targetSize) {
114         while (mirror.size() < targetSize) {
115             assertEquals(indexManager.getSize(), mirror.size());
116             long val = Math.abs(random.nextInt());
117             ContainerLocation containerLocation = new ContainerLocation((int) val, (int)val);
118             Long JavaDoc key = new Long JavaDoc(val);
119             mirror.put(key, containerLocation);
120             indexManager.putContainerLocation(val, containerLocation);
121             assertEquals(indexManager.getSize(), mirror.size());
122         }
123     }
124
125     private void randomRemove(long targetSize) {
126         for (Iterator i = mirror.keySet().iterator(); i.hasNext() && indexManager.getSize() > targetSize; ) {
127             assertEquals("indexManager size: " + indexManager.getSize() + "; mirror size: " + mirror.size(), (int) indexManager.getSize(), mirror.size());
128             long val = Math.abs(random.nextInt());
129             boolean removed = mirror.remove(new Long JavaDoc(val)) != null;
130             try {
131                 indexManager.removeContainerLocation(val);
132                 assertTrue(removed);
133             } catch (ObjectNotFoundException e) {
134                 assertFalse(removed);
135             }
136             if (!removed) {
137                 Long JavaDoc key = (Long JavaDoc) i.next();
138                 i.remove();
139                 indexManager.removeContainerLocation(key.longValue());
140             }
141             assertEquals("indexManager size: " + indexManager.getSize() + "; mirror size: " + mirror.size(), (int) indexManager.getSize(), mirror.size());
142         }
143     }
144
145     private void checkEquality() {
146         for (int i = 0; i < mirror.size() * 2; i++) {
147             long val = Math.abs(random.nextInt());
148             try {
149                 ContainerLocation fromIndex = indexManager.getContainerLocation(val);
150                 ContainerLocation fromMirror = (ContainerLocation) mirror.get(new Long JavaDoc(val));
151                 // uncommented to make it compile
152
//assertEquals(fromIndex.clusterId, fromMirror.clusterId);
153
//assertEquals(fromIndex.position, fromMirror.position);
154
} catch (ObjectNotFoundException e) {
155                 assertNull(mirror.get(new Long JavaDoc(val)));
156             }
157         }
158     }
159     
160     private void actualTest(Properties properties, long capacity) throws Exception JavaDoc {
161         File JavaDoc directory = new File JavaDoc("IndexManagerTest");
162 // if (directory.exists()) {
163
// throw new RuntimeException(directory.getAbsoluteFile() + " is needed for test. Please remove before proceeding.");
164
// }
165
directory.mkdirs();
166         properties.setProperty(GammaStore.DIRECTORY.getKey(), directory.getAbsolutePath());
167
168         random = new Random(0);
169         mirror = new HashMap();
170
171         // TODO not sure what the prefix should be
172
indexManager = new IndexManager(properties,"", true);
173         
174         checkEquality();
175         
176         randomFill(capacity);
177
178         checkEquality();
179
180         randomRemove(capacity / 2);
181         
182         checkEquality();
183
184         randomFill(capacity);
185         
186         indexManager.shutdown();
187         // TODO not sure what the prefix should be
188
indexManager = new IndexManager(properties,"", false);
189
190         checkEquality();
191
192         randomRemove(0);
193
194         checkEquality();
195
196         randomFill(capacity / 2 );
197
198         checkEquality();
199
200         indexManager.shutdown();
201         // TODO not sure what the prefix should be
202
indexManager = new IndexManager(properties, "",false);
203
204         checkEquality();
205
206         randomRemove(capacity / 4);
207         
208         checkEquality();
209
210         indexManager.shutdown();
211         // TODO not sure what the prefix should be
212
indexManager = new IndexManager(properties, "", false);
213
214         checkEquality();
215
216         randomRemove(0);
217
218         checkEquality();
219
220         indexManager.shutdown();
221     }
222     
223     private void deleteAll(File JavaDoc file) {
224         File JavaDoc[] children = file.listFiles();
225         for (int i = 0; i < children.length; i++) {
226             deleteAll(children[i]);
227         }
228         file.delete();
229     }
230     
231     public static void main(String JavaDoc[] args) {
232         TestRunner testRunner = new TestRunner();
233         testRunner.run(IndexManagerTest.class);
234     }
235         
236 }
237
Popular Tags