package test; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import org.infinispan.Cache; import org.infinispan.config.Configuration; import org.infinispan.eviction.EvictionStrategy; import org.infinispan.eviction.EvictionThreadPolicy; import org.infinispan.loaders.jdbm.JdbmCacheStoreConfig; import org.infinispan.lucene.InfinispanDirectory; import org.infinispan.manager.DefaultCacheManager; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.File; /** * Created by IntelliJ IDEA. * User: nardonep * Date: 04-Oct-2010 * Time: 11:38:02 * To change this template use File | Settings | File Templates. */ public class InfinispanLuceneTest { private static DefaultCacheManager defaultCacheManager; private static File fsDirectoryBase = new File("C:\\TEMP\\InfinispanLuceneTest\\FS\\"); private static File fsDirectoryTest1 = new File(fsDirectoryBase,"test1"); private static File fsDirectoryTest2 = new File(fsDirectoryBase,"test2"); @BeforeClass public static void setup(){ Configuration ccl=new Configuration(); ccl.setCacheMode(Configuration.CacheMode.LOCAL); ccl.setInvocationBatchingEnabled(true); JdbmCacheStoreConfig cacheStoreConfig; defaultCacheManager=new DefaultCacheManager(ccl); File dir10 = new File("C:\\TEMP\\InfinispanLuceneTest\\Infinispan\\"); dir10.mkdirs(); for(File f : dir10.listFiles()){ f.delete(); } fsDirectoryTest1.mkdirs(); for(File f : fsDirectoryTest1.listFiles()){ f.delete(); } fsDirectoryTest2.mkdirs(); for(File f : fsDirectoryTest2.listFiles()){ f.delete(); } Configuration cclLIRS4=new Configuration(); cacheStoreConfig = new JdbmCacheStoreConfig(); cacheStoreConfig.setLocation(dir10.getAbsolutePath()); cacheStoreConfig.setPurgeOnStartup(Boolean.TRUE); cacheStoreConfig.setIgnoreModifications(Boolean.FALSE); cclLIRS4.getCacheLoaderManagerConfig().addCacheLoaderConfig(cacheStoreConfig); cclLIRS4.getCacheLoaderManagerConfig().setPassivation(Boolean.TRUE); cclLIRS4.setEvictionStrategy(EvictionStrategy.LIRS); cclLIRS4.setEvictionMaxEntries(4); cclLIRS4.setEvictionWakeUpInterval(10); cclLIRS4.setEvictionThreadPolicy(EvictionThreadPolicy.PIGGYBACK); defaultCacheManager.defineConfiguration("cclLIRS4",cclLIRS4); Configuration cclNONE=new Configuration(); cacheStoreConfig = new JdbmCacheStoreConfig(); cacheStoreConfig.setLocation(dir10.getAbsolutePath()); cacheStoreConfig.setPurgeOnStartup(Boolean.TRUE); cacheStoreConfig.setIgnoreModifications(Boolean.FALSE); cclNONE.getCacheLoaderManagerConfig().addCacheLoaderConfig(cacheStoreConfig); cclNONE.getCacheLoaderManagerConfig().setPassivation(Boolean.TRUE); cclNONE.setEvictionStrategy(EvictionStrategy.NONE); defaultCacheManager.defineConfiguration("cclNONES4",cclNONE); } @Test public void testInfinispanWithEviction(){ runTestInfinispan("cclLIRS4"); } @Test public void testInfinispanNoEviction(){ runTestInfinispan("cclNONES4"); } @Test public void testFS(){ runTestFS(); } private void runTestInfinispan(String n) { Cache testCache=defaultCacheManager.getCache(n); testCache.clear(); try { Directory dir=new InfinispanDirectory(testCache,"test1",256); StandardAnalyzer sa=new StandardAnalyzer(Version.LUCENE_29); IndexWriter iw=new IndexWriter(dir,sa, IndexWriter.MaxFieldLength.UNLIMITED); for(int i=0;i<10;i++){ Document d=new Document(); d.add(new Field("ID",Integer.toString(i),Field.Store.YES,Field.Index.NO)); d.add(new Field("X",Integer.toString(i),Field.Store.NO,Field.Index.ANALYZED)); d.add(new Field("Y",Integer.toString(i),Field.Store.NO,Field.Index.ANALYZED)); d.add(new Field("VALID",Boolean.TRUE.toString(),Field.Store.NO,Field.Index.ANALYZED)); iw.addDocument(d); //testCache.put(Integer.valueOf(i),"X"); } iw.commit(); iw.waitForMerges(); iw.close(true); IndexReader ir=IndexReader.open(dir,true); IndexSearcher is=new IndexSearcher(ir); TopDocs td = is.search(new TermQuery(new Term("VALID","true")),1); Assert.assertEquals(td.totalHits,10); is.close(); ir.close(); Directory dir2=new InfinispanDirectory(testCache,"test2",256); Directory.copy(dir,dir2,false); ir=IndexReader.open(dir,true); is=new IndexSearcher(ir); td = is.search(new TermQuery(new Term("VALID","true")),1); Assert.assertEquals(td.totalHits,10); is.close(); ir.close(); dir.close(); dir=dir2; ir=IndexReader.open(dir,true); is=new IndexSearcher(ir); td = is.search(new TermQuery(new Term("VALID","true")),1); Assert.assertEquals(td.totalHits,10); is.close(); ir.close(); dir.close(); } catch (Exception e) { throw new RuntimeException(e); } } private void runTestFS() { try { Directory dir=FSDirectory.open(fsDirectoryTest1); StandardAnalyzer sa=new StandardAnalyzer(Version.LUCENE_29); IndexWriter iw=new IndexWriter(dir,sa, IndexWriter.MaxFieldLength.UNLIMITED); for(int i=0;i<10;i++){ Document d=new Document(); d.add(new Field("ID",Integer.toString(i),Field.Store.YES,Field.Index.NO)); d.add(new Field("X",Integer.toString(i),Field.Store.NO,Field.Index.ANALYZED)); d.add(new Field("Y",Integer.toString(i),Field.Store.NO,Field.Index.ANALYZED)); d.add(new Field("VALID",Boolean.TRUE.toString(),Field.Store.NO,Field.Index.ANALYZED)); iw.addDocument(d); } iw.commit(); iw.waitForMerges(); iw.close(true); IndexReader ir=IndexReader.open(dir,true); IndexSearcher is=new IndexSearcher(ir); TopDocs td = is.search(new TermQuery(new Term("VALID","true")),1); Assert.assertEquals(td.totalHits,10); is.close(); ir.close(); Directory dir2=FSDirectory.open(fsDirectoryTest2); Directory.copy(dir,dir2,false); ir=IndexReader.open(dir,true); is=new IndexSearcher(ir); td = is.search(new TermQuery(new Term("VALID","true")),1); Assert.assertEquals(td.totalHits,10); is.close(); ir.close(); dir.close(); dir=dir2; ir=IndexReader.open(dir,true); is=new IndexSearcher(ir); td = is.search(new TermQuery(new Term("VALID","true")),1); Assert.assertEquals(td.totalHits,10); is.close(); ir.close(); dir.close(); } catch (Exception e) { throw new RuntimeException(e); } } }