1 14 15 package org.apache.activemq.kaha.impl.index.hash; 16 17 import java.io.File ; 18 import java.io.IOException ; 19 import junit.framework.TestCase; 20 import org.apache.activemq.kaha.Store; 21 import org.apache.activemq.kaha.impl.index.IndexItem; 22 import org.apache.activemq.kaha.impl.index.IndexManager; 23 import org.apache.activemq.util.IOHelper; 24 25 29 public class HashTest extends TestCase{ 30 31 private static int COUNT=1000; 32 private HashIndex hashIndex; 33 private File directory; 34 private IndexManager indexManager; 35 36 37 41 protected void setUp() throws Exception { 42 super.setUp(); 43 directory=new File (IOHelper.getDefaultDataDirectory()); 44 directory.mkdirs(); 45 indexManager=new IndexManager(directory,"im-hash-test","rw",null); 46 this.hashIndex=new HashIndex(directory,"testHash",indexManager); 47 this.hashIndex.setKeyMarshaller(Store.StringMarshaller); 48 } 49 50 51 52 public void testHashIndex() throws Exception { 53 doTest(300); 54 hashIndex.clear(); 55 hashIndex.unload(); 56 doTest(600); 57 hashIndex.clear(); 58 hashIndex.unload(); 59 doTest(1024*4); 60 } 61 62 public void doTest(int pageSize) throws Exception { 63 String keyRoot="key:"; 64 hashIndex.setPageSize(pageSize); 65 this.hashIndex.load(); 66 doInsert(keyRoot); 67 checkRetrieve(keyRoot); 68 doRemove(keyRoot); 69 doInsert(keyRoot); 70 doRemoveBackwards(keyRoot); 71 } 72 73 void doInsert(String keyRoot) throws Exception { 74 for(int i=0;i<COUNT;i++){ 75 IndexItem value=indexManager.createNewIndex(); 76 indexManager.storeIndex(value); 77 hashIndex.store(keyRoot+i,value); 78 79 } 80 } 81 82 void checkRetrieve(String keyRoot) throws IOException { 83 for(int i=0;i<COUNT;i++){ 84 IndexItem item=(IndexItem)hashIndex.get(keyRoot+i); 85 assertNotNull(item); 86 } 87 } 88 89 void doRemove(String keyRoot) throws Exception { 90 for(int i=0;i<COUNT;i++){ 91 hashIndex.remove(keyRoot+i); 92 } 93 for(int i=0;i<COUNT;i++){ 94 IndexItem item=(IndexItem)hashIndex.get(keyRoot+i); 95 assertNull(item); 96 } 97 } 98 99 void doRemoveBackwards(String keyRoot) throws Exception { 100 for(int i=COUNT-1;i>=0;i--){ 101 hashIndex.remove(keyRoot+i); 102 } 103 for(int i=0;i<COUNT;i++){ 104 IndexItem item=(IndexItem)hashIndex.get(keyRoot+i); 105 assertNull(item); 106 } 107 } 108 109 113 protected void tearDown() throws Exception { 114 super.tearDown(); 115 File [] files=directory.listFiles(); 116 for(File file:files){ 117 file.delete(); 118 } 119 } 120 } 121 | Popular Tags |