1 24 package org.objectweb.jalisto.test.query.bench; 25 26 import org.objectweb.jalisto.test.query.JalistoQueryTestCase; 27 import junit.framework.Test; 28 import org.objectweb.jalisto.se.api.ClassDescription; 29 import org.objectweb.jalisto.se.api.query.Index; 30 import org.objectweb.jalisto.se.api.query.Query; 31 import org.objectweb.jalisto.se.api.query.ObjectSet; 32 import org.objectweb.jalisto.se.exception.SchemaException; 33 import org.objectweb.jalisto.se.impl.InFileAddress; 34 import org.objectweb.jalisto.se.impl.LogicalOid; 35 import org.objectweb.jalisto.se.query.IndexManagerImpl; 36 import org.objectweb.jalisto.se.query.btree.BTree; 37 import org.objectweb.jalisto.se.query.index.IndexBTreeImpl; 38 import org.objectweb.jalisto.se.query.result.QueryResultWrapper; 39 import org.objectweb.jalisto.se.test.data.BookWithAuthor; 40 import org.objectweb.jalisto.se.test.data.DVD; 41 import org.objectweb.jalisto.se.test.workbench.JalistoTestSuite; 42 43 import java.util.Iterator ; 44 45 public class BTreeTestCase extends JalistoQueryTestCase { 46 public BTreeTestCase() { 47 } 48 49 public BTreeTestCase(String name) { 50 super(name); 51 } 52 53 public static Test suite() { 54 JalistoTestSuite suite = new JalistoTestSuite(); 55 BTreeTestCase tc = (BTreeTestCase) newTestCase(suite, new BTreeTestCase()); 56 57 tc.initSession(); 58 tc.define(BookWithAuthor.getMetaDescription()); 59 tc.define(DVD.getMetaDescription()); 60 61 tc.cleanUp(BookWithAuthor.class); 62 tc.cleanUp(DVD.class); 63 tc.populateBookWithAuthor(50); 64 tc.populateTestDVD(50); 65 66 tc.buildIndex(BookWithAuthor.class, "title"); 67 tc.printIndex(BookWithAuthor.class, "title"); 68 69 tc.buildIndex(DVD.class, "title"); 70 tc.printIndex(DVD.class, "title"); 71 72 83 tc.deleteClasses(); 84 85 tc.define(BookWithAuthor.getMetaDescription()); 86 tc.define(DVD.getMetaDescription()); 87 tc.buildIndex(BookWithAuthor.class, "title"); 88 tc.buildIndex(DVD.class, "title"); 89 tc.deleteClasses(); 90 91 return suite; 92 } 93 94 97 98 public void populateTestDVD(int nbr) { 99 tx.begin(); 100 for (int i = 0; i < nbr; i++) { 101 session.createObject(DVD.newDVD().toArray(), DVD.class); 102 } 103 tx.commit(); 104 } 105 106 public void deleteClasses() { 107 session.getMetaRepository().removeClass(session, DVD.class.getName()); 108 session.getMetaRepository().removeClass(session, BookWithAuthor.class.getName()); 109 session.reorganize(); 110 tx.begin(); 111 try { 112 session.createObject(BookWithAuthor.newBook().toArray(), BookWithAuthor.class); 113 } catch (SchemaException jalistoSchemaExce) { 114 assertEquals("Should be equals", 115 "Class org.objectweb.jalisto.se.test.data.Book is not define in base...", 116 jalistoSchemaExce.getMessage()); 117 } 118 tx.commit(); 119 tx.begin(); 120 try { 121 session.createObject(DVD.newDVD().toArray(), DVD.class); 122 } catch (SchemaException jalistoSchemaExce) { 123 assertEquals("Should be equals", 124 "Class org.objectweb.jalisto.se.test.data.DVD is not define in base...", 125 jalistoSchemaExce.getMessage()); 126 } 127 tx.commit(); 128 129 tx.begin(); 130 try { 131 getIndex(BookWithAuthor.class, "title"); 132 } catch (SchemaException jalistoSchemaExce) { 133 assertEquals("Should be equals", 134 "Class org.objectweb.jalisto.se.test.data.Book is not define in base...", 135 jalistoSchemaExce.getMessage()); 136 } 137 tx.commit(); 138 139 tx.begin(); 140 try { 141 getIndex(DVD.class, "title"); 142 } catch (SchemaException jalistoSchemaExce) { 143 assertEquals("Should be equals", 144 "Class org.objectweb.jalisto.se.test.data.DVD is not define in base...", 145 jalistoSchemaExce.getMessage()); 146 } 147 tx.commit(); 148 } 149 150 private Index getIndex(Class aClass, String fieldName) { 151 ClassDescription meta = session.getMetaRepository().getMetaData(aClass.getName()); 152 int fieldIndex = meta.getIndex(fieldName); 153 return indexManager.getIndex(meta, fieldIndex); 154 } 155 156 public void insertInBtree(Class aClass) { 157 ClassDescription meta = session.getMetaRepository().getMetaData(aClass.getName()); 158 int titleFieldIndex = meta.getIndex("title"); 159 160 tx.begin(); 161 InFileAddress ifa = new InFileAddress("bTreeTest"); 162 IndexBTreeImpl bTreeIndex = new IndexBTreeImpl(ifa, titleFieldIndex, meta, 163 (IndexManagerImpl) indexManager); 164 BTree tree = bTreeIndex.getTree(); 165 tx.commit(); 166 167 tx.begin(); 168 Iterator extent = session.getExtent(aClass).readFully().iterator(); 169 while (extent.hasNext()) { 170 LogicalOid oid = (LogicalOid) extent.next(); 171 Object [] array = session.readObjectByOid(oid); 172 Object value = array[titleFieldIndex]; 173 tree.addOid(value, oid); 174 } 175 tx.commit(); 176 177 System.out.println(tree); 178 } 179 180 public void removeOidsOnBook() { 181 ClassDescription meta = session.getMetaRepository().getMetaData(BookWithAuthor.class.getName()); 182 int titleFieldIndex = meta.getIndex("title"); 183 tx.begin(); 184 Index index = indexManager.getIndex(meta, titleFieldIndex); 185 tx.commit(); 186 187 deleteTitleOnBook("Death Off Stage", index, titleFieldIndex); 188 deleteTitleOnBook("Fingersmith", index, titleFieldIndex); 189 deleteTitleOnBook("Bittersweet", index, titleFieldIndex); 190 deleteTitleOnBook("A Cold Case of Murder", index, titleFieldIndex); 191 deleteTitleOnBook("A Fountain Filled with Blood", index, titleFieldIndex); 192 deleteTitleOnBook("Innocent Hearts", index, titleFieldIndex); 193 deleteTitleOnBook("Metes and Bounds", index, titleFieldIndex); 194 deleteTitleOnBook("The Jester", index, titleFieldIndex); 195 deleteTitleOnBook("At the Edge", index, titleFieldIndex); 196 deleteTitleOnBook("Tipping the Velvet", index, titleFieldIndex); 197 } 198 199 private void deleteTitleOnBook(String title, Index index, int titleIndex) { 200 System.out.println("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); 201 System.out.println("delete elements one by one for title : " + title); 202 Query query = queryManager.query(); 203 query.constrain(BookWithAuthor.class); 204 query.descend("title").constrain(title).equal(); 205 206 tx.begin(); 207 ObjectSet result = query.execute(); 208 tx.commit(); 209 210 tx.begin(); 211 while (result.hasNext()) { 212 LogicalOid floid = ((QueryResultWrapper) result.next()).getLogicalOid(); 213 Object [] book = session.readObjectByOid(floid); 214 index.remove(book[titleIndex], floid); 215 } 216 tx.commit(); 217 218 System.out.println("index : " + index.toFullString()); 219 } 220 221 224 225 public void initSession() { 226 super.initSession(false); 227 } 228 229 public void define(ClassDescription classDescription) { 230 super.define(classDescription); 231 } 232 233 public void cleanUp(Class aClass) { 234 super.cleanUp(aClass); 235 } 236 237 public void populateBookWithAuthor(int nbr) { 238 super.populateBookWithAuthor(nbr); 239 } 240 241 public void buildIndex(Class aClass, String fieldName) { 242 super.buildIndex(aClass, fieldName); 243 } 244 245 public void deleteIndex(Class aClass, String fieldName) { 246 super.deleteIndex(aClass, fieldName); 247 } 248 249 public void printIndex(Class aClass, String fieldName) { 250 super.printIndex(aClass, fieldName); 251 } 252 253 254 } 255 | Popular Tags |