views:

568

answers:

1
+3  Q: 

Using RAMDirectory

Hi,

When do i use Lucene RAMDirectory?what's the advantage of using it? Can i have some code sample please?

Thanks.

+4  A: 

When you don’t want to permanently store your index data. I use this for testing purposes. Add data to your RAMDirectory, Do your unit tests in RAMDir.
e.g.

 public static void main(String[] args) {
    try {
      Directory directory = new RAMDirectory();  
      Analyzer analyzer = new SimpleAnalyzer();
      IndexWriter writer = new IndexWriter(directory, analyzer, true);

OR

  public void testRAMDirectory () throws IOException {

    Directory dir = FSDirectory.getDirectory(indexDir);
    MockRAMDirectory ramDir = new MockRAMDirectory(dir);

    // close the underlaying directory
    dir.close();

    // Check size
    assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());

    // open reader to test document count
    IndexReader reader = IndexReader.open(ramDir);
    assertEquals(docsToAdd, reader.numDocs());

    // open search zo check if all doc's are there
    IndexSearcher searcher = new IndexSearcher(reader);

    // search for all documents
    for (int i = 0; i < docsToAdd; i++) {
      Document doc = searcher.doc(i);
      assertTrue(doc.getField("content") != null);
    }

    // cleanup
    reader.close();
    searcher.close();
  }

Usually if things work out with RAMDirectory, it will pretty much work fine with others. i.e. to permanently store your index.
Alternate to this is FSDirectory. You will have to take care of filesystem permissions in this case(which is not valid with RAMDirectory)

Functionally,there is not distinct advantage of RAMDirectory over FSDirectory(other than the fact that RAMDirectory will be visibly faster than FSDirectory). They both server two different needs.

  • RAMDirectory -> Primary memory
  • FSDirectory -> Secondary memory

Pretty similar to RAM & Hard disk .

I am not sure what will happen to RAMDirectory if it exceeds memory limit. I’d except a

OutOfMemoryException : System.SystemException

thrown.

Cherian
hi, thanks for your response...what is the advantage of using RAMDirectory over FSDirectory?And also, what if the size of index exceeds the RAM memory?
Jimmy