Skip to content

Commit

Permalink
default is RAMDirectory. We also have DefaultRAMDiskIndexFactory so t…
Browse files Browse the repository at this point in the history
…hat ram index can be hosted on a ram disk or /tmp in nix systems
  • Loading branch information
xiaoyanggu committed Dec 8, 2010
1 parent 758aa28 commit 231fb90
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 75 deletions.
@@ -0,0 +1,42 @@
package proj.zoie.impl.indexing.internal;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;

import proj.zoie.api.indexing.IndexReaderDecorator;

/**
* @author "Xiaoyang Gu<xgu@linkedin.com>"
*
* @param <R>
*/
public class DefaultRAMDiskIndexFactory<R extends IndexReader> extends RAMIndexFactory<R>
{
private static final Logger log = Logger.getLogger(DefaultRAMIndexFactory.class);
private static int fold = 10000;

@Override
public synchronized RAMSearchIndex<R> newInstance(long version, IndexReaderDecorator<R> decorator, SearchIndexManager<R> idxMgr)
{
Directory ramIdxDir;
try
{
File backingdir = new File("/tmp/ram" + fold);// /Volumes/ramdisk/
ramIdxDir = new SimpleFSDirectory(backingdir);
fold++;
return new RAMSearchIndex<R>(version, decorator, idxMgr, ramIdxDir, backingdir);
} catch (IOException e)
{
// TODO Auto-generated catch block
log.error(e);
e.printStackTrace();
}// new RAMDirectory();
return null;
}

}
26 changes: 3 additions & 23 deletions java/proj/zoie/impl/indexing/internal/DefaultRAMIndexFactory.java
@@ -1,42 +1,22 @@
package proj.zoie.impl.indexing.internal;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.store.SimpleFSDirectory;

import proj.zoie.api.indexing.IndexReaderDecorator;

/**
* @author "Xiaoyang Gu<xgu@linkedin.com>"
*
*
* @param <R>
*/
public class DefaultRAMIndexFactory<R extends IndexReader> extends RAMIndexFactory<R>
{
private static final Logger log = Logger.getLogger(DefaultRAMIndexFactory.class);
private static int fold =10000;

@Override
public synchronized RAMSearchIndex<R> newInstance(long version, IndexReaderDecorator<R> decorator, SearchIndexManager<R> idxMgr)
{
Directory ramIdxDir;
try
{
File backingdir = new File("/tmp/ram"+fold);///Volumes/ramdisk/
ramIdxDir = new SimpleFSDirectory(backingdir );
fold++;
return new RAMSearchIndex<R>(version, decorator, idxMgr, ramIdxDir, backingdir);
} catch (IOException e)
{
// TODO Auto-generated catch block
log.error(e);
e.printStackTrace();
}//new RAMDirectory();
return null;
return new RAMSearchIndex<R>(version, decorator, idxMgr, new RAMDirectory(), null);
}

}
2 changes: 1 addition & 1 deletion java/proj/zoie/impl/indexing/internal/RAMSearchIndex.java
Expand Up @@ -80,7 +80,7 @@ public void close()
try
{
_directory.close();
FileUtil.rmDir(_backingdir);
if (_backingdir != null) FileUtil.rmDir(_backingdir);
} catch (IOException e)
{
log.error(e);
Expand Down
117 changes: 66 additions & 51 deletions test/proj/zoie/test/HourglassTest.java
Expand Up @@ -45,19 +45,22 @@

/**
* @author "Xiaoyang Gu<xgu@linkedin.com>"
*
*
*/
public class HourglassTest extends ZoieTestCase
{
static Logger log = Logger.getLogger(HourglassTest.class);

public HourglassTest()
{

}

public HourglassTest(String name)
{
super(name);
}

public void testHourglassDirectoryManagerFactory() throws IOException, InterruptedException, ZoieException
{
File idxDir = getIdxDir();
Expand Down Expand Up @@ -89,101 +92,105 @@ public void testHourglassDirectoryManagerFactory() throws IOException, Interrupt
}
if (log.isInfoEnabled())
{
log
.info("[contextInitialized]: " + count
+ " log4j MBeans registered.");
log.info("[contextInitialized]: " + count + " log4j MBeans registered.");
}
} catch (Exception e)
{
log.error("[contextInitialized]: Exception catched: ", e);
}
String schedule = "07 15 20";
long numTestContent = 20250;
long numTestContent = 10250;
oneTest(idxDir, schedule, numTestContent); // test starting from empty index
oneTest(idxDir, schedule, numTestContent); // test index pick up
return;
}

private void oneTest(File idxDir, String schedule, long numTestContent) throws IOException, InterruptedException
{
HourglassDirectoryManagerFactory factory = new HourglassDirectoryManagerFactory(idxDir, new HourGlassScheduler(HourGlassScheduler.FREQUENCY.MINUTELY, schedule, 100));
HourglassDirectoryManagerFactory factory = new HourglassDirectoryManagerFactory(idxDir, new HourGlassScheduler(
HourGlassScheduler.FREQUENCY.MINUTELY, schedule, 100));
ZoieConfig zConfig = new ZoieConfig();
zConfig.setBatchSize(3);
zConfig.setBatchDelay(10);
zConfig.setFreshness(10);
Hourglass<IndexReader, String> hourglass = new Hourglass<IndexReader, String>(factory, new HourglassTestInterpreter(), new IndexReaderDecorator<IndexReader>(){
Hourglass<IndexReader, String> hourglass = new Hourglass<IndexReader, String>(factory, new HourglassTestInterpreter(),
new IndexReaderDecorator<IndexReader>()
{

public IndexReader decorate(ZoieIndexReader<IndexReader> indexReader)
throws IOException
{
return indexReader;
}
public IndexReader decorate(ZoieIndexReader<IndexReader> indexReader) throws IOException
{
return indexReader;
}

public IndexReader redecorate(IndexReader decorated,
ZoieIndexReader<IndexReader> copy, boolean withDeletes) throws IOException
{
// TODO Auto-generated method stub
return decorated;
}
public IndexReader redecorate(IndexReader decorated, ZoieIndexReader<IndexReader> copy, boolean withDeletes)
throws IOException
{
// TODO Auto-generated method stub
return decorated;
}

public void setDeleteSet(IndexReader reader, DocIdSet docIds)
{
// do nothing
}}, zConfig);
public void setDeleteSet(IndexReader reader, DocIdSet docIds)
{
// do nothing
}
}, zConfig);
HourglassAdmin mbean = new HourglassAdmin(hourglass);
// HourglassAdminMBean mbean = admin.getMBean();
// HourglassAdminMBean mbean = admin.getMBean();
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
try
{
mbeanServer.registerMBean(mbean, new ObjectName("HouseGlass:name=hourglass"));
} catch(Exception e)
} catch (Exception e)
{
System.out.println(e);
}
MemoryStreamDataProvider<String> memoryProvider=new MemoryStreamDataProvider<String>();
MemoryStreamDataProvider<String> memoryProvider = new MemoryStreamDataProvider<String>();
memoryProvider.setMaxEventsPerMinute(Long.MAX_VALUE);
memoryProvider.setDataConsumer(hourglass);
memoryProvider.start();
int initNumDocs = getTotalNumDocs(hourglass);
System.out.println("initial number of DOCs: " + initNumDocs);

long accumulatedTime = 0;
for(int i=initNumDocs; i<initNumDocs + numTestContent; i++)
for (int i = initNumDocs; i < initNumDocs + numTestContent; i++)
{
List<DataEvent<String>> list=new ArrayList<DataEvent<String>>(2);
list.add(new DataEvent<String>(i,"" +i));
List<DataEvent<String>> list = new ArrayList<DataEvent<String>>(2);
list.add(new DataEvent<String>(i, "" + i));
memoryProvider.addEvents(list);
if (i%113 !=0) continue;
if (i % 113 != 0)
continue;
long flushtime = System.currentTimeMillis();
int numDoc = -1;
List<ZoieIndexReader<IndexReader>> readers=null;
List<ZoieIndexReader<IndexReader>> readers = null;
IndexReader reader = null;
Searcher searcher = null;
int oldNum = -1;
while(numDoc < i + 1)
while (numDoc < i + 1)
{
if (reader!=null && readers!=null)
if (reader != null && readers != null)
{
searcher.close();
searcher = null;
reader.close();
hourglass.returnIndexReaders(readers);
}
readers = hourglass.getIndexReaders();
reader = new MultiReader(readers.toArray(new IndexReader[0]),false);
reader = new MultiReader(readers.toArray(new IndexReader[0]), false);
searcher = new IndexSearcher(reader);
TopDocs hitsall = searcher.search(new MatchAllDocsQuery(), 10);
numDoc = hitsall.totalHits;
if (numDoc!=oldNum)System.out.println("numDoc: " + numDoc);
if (numDoc != oldNum)
System.out.println("numDoc: " + numDoc);
oldNum = numDoc;
Thread.sleep(30);
}
accumulatedTime += (System.currentTimeMillis() - flushtime);
TopDocs hits = searcher.search(new TermQuery(new Term("contents",""+i)), 10);
TopDocs hits = searcher.search(new TermQuery(new Term("contents", "" + i)), 10);
TopDocs hitsall = searcher.search(new MatchAllDocsQuery(), 10);
try
{
assertEquals("one hit for " + i, 1, hits.totalHits);
assertEquals("MatchAllDocsHit ", i+1, hitsall.totalHits);
assertEquals("MatchAllDocsHit ", i + 1, hitsall.totalHits);
} finally
{
searcher.close();
Expand All @@ -193,7 +200,7 @@ public void setDeleteSet(IndexReader reader, DocIdSet docIds)
hourglass.returnIndexReaders(readers);
readers = null;
}
System.out.println(((i-initNumDocs)*100/numTestContent) + "%");
System.out.println(((i - initNumDocs) * 100 / numTestContent) + "%");
}
try
{
Expand All @@ -206,14 +213,15 @@ public void setDeleteSet(IndexReader reader, DocIdSet docIds)
memoryProvider.stop();
hourglass.shutdown();
}

private int getTotalNumDocs(Hourglass<IndexReader, String> hourglass)
{
int numDocs = 0;
List<ZoieIndexReader<IndexReader>> readers = null;
try
{
readers = hourglass.getIndexReaders();
for(ZoieIndexReader<IndexReader> reader : readers)
for (ZoieIndexReader<IndexReader> reader : readers)
{
numDocs += reader.numDocs();
}
Expand All @@ -228,47 +236,54 @@ private int getTotalNumDocs(Hourglass<IndexReader, String> hourglass)
}
return numDocs;
}

public static class TestHourglassIndexable extends HourglassIndexable
{
protected static long nextUID = System.currentTimeMillis();
public final long UID;
final String _str;

public TestHourglassIndexable(String str)
{
UID = getNextUID();
_str = str;
}

public static final synchronized long getNextUID()
{
return nextUID++;
}

public final long getUID()
{
return UID;
}
public Document buildDocument(){
Document doc=new Document();
doc.add(new Field("contents",_str,Store.YES,Index.ANALYZED));

public Document buildDocument()
{
Document doc = new Document();
doc.add(new Field("contents", _str, Store.YES, Index.ANALYZED));
try
{
Thread.sleep(25); // slow down indexing process
}
catch (InterruptedException e)
} catch (InterruptedException e)
{
}
return doc;
}

public IndexingReq[] buildIndexingReqs(){
return new IndexingReq[]{new IndexingReq(buildDocument(),null)};

public IndexingReq[] buildIndexingReqs()
{
return new IndexingReq[] { new IndexingReq(buildDocument(), null) };
}

public boolean isSkip()
{
return false;
}

}

public static class HourglassTestInterpreter implements HourglassIndexableInterpreter<String>
{

Expand All @@ -277,6 +292,6 @@ public HourglassIndexable convertAndInterpret(String src)
log.info("converting " + src);
return new TestHourglassIndexable(src);
}

}
}

0 comments on commit 231fb90

Please sign in to comment.