| 1 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 ; 22 import java.util.*; 23 import java.util.logging.Level ; 24 import java.util.logging.Logger ; 25 26 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 { 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"); properties.setProperty(IndexManager.MAXLEAFNODESIZE.getKey(), "10"); 63 actualTest(properties, 5000); 65 } 66 67 public void testBigNodes() throws Exception { 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"); properties.setProperty(IndexManager.MAXLEAFNODESIZE.getKey(), "1598"); actualTest(properties, DEFAULT_TESTSIZE); 80 } 81 82 public void testSmallCache() throws Exception { 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"); properties.setProperty(IndexManager.MAXLEAFNODESIZE.getKey(), "376"); actualTest(properties, DEFAULT_TESTSIZE); 95 } 96 97 public void testGcListenerCache() throws Exception { 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 properties.setProperty(IndexManager.GENERALINDEXNODECACHE.getKey(), FixedSizeCache.class.getName()); 106 properties.setProperty(IndexManager.MAXBRANCHNODESIZE.getKey(), "474"); properties.setProperty(IndexManager.MAXLEAFNODESIZE.getKey(), "376"); 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 key = new Long (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 (val)) != null; 130 try { 131 indexManager.removeContainerLocation(val); 132 assertTrue(removed); 133 } catch (ObjectNotFoundException e) { 134 assertFalse(removed); 135 } 136 if (!removed) { 137 Long key = (Long ) 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 (val)); 151 } catch (ObjectNotFoundException e) { 155 assertNull(mirror.get(new Long (val))); 156 } 157 } 158 } 159 160 private void actualTest(Properties properties, long capacity) throws Exception { 161 File directory = new File ("IndexManagerTest"); 162 directory.mkdirs(); 166 properties.setProperty(GammaStore.DIRECTORY.getKey(), directory.getAbsolutePath()); 167 168 random = new Random(0); 169 mirror = new HashMap(); 170 171 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 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 indexManager = new IndexManager(properties, "",false); 203 204 checkEquality(); 205 206 randomRemove(capacity / 4); 207 208 checkEquality(); 209 210 indexManager.shutdown(); 211 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 file) { 224 File [] 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 [] args) { 232 TestRunner testRunner = new TestRunner(); 233 testRunner.run(IndexManagerTest.class); 234 } 235 236 } 237 | Popular Tags |