使用RAMDirectory [英] Using RAMDirectory
问题描述
何时应使用Lucene的RAMDirectory?与其他存储机制相比,它有什么优势?最后,在哪里可以找到一个简单的代码示例?
When should I use Lucene's RAMDirectory? What are its advantages over other storage mechanisms? Finally, where can I find a simple code example?
推荐答案
当您不想永久存储索引数据时.我将其用于测试目的.将数据添加到RAMDirectory,在RAMDir中进行单元测试.
例如
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();
}
通常,如果RAMDirectory可以正常工作,则可以与其他程序很好地工作.即永久存储您的索引.
替代方法是 FSDirectory .在这种情况下,您将必须注意文件系统权限(这对于RAMDirectory无效)
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)
从功能上讲,RAMDirectory没有比FSDirectory明显的优势(除了RAMDirectory明显比FSDirectory更快的事实之外).他们俩都满足两个不同的需求.
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->主内存
- FSDirectory->辅助内存
非常类似于RAM&硬盘.
Pretty similar to RAM & Hard disk .
我不确定如果RAMDirectory超出内存限制,将会发生什么情况.除了一个
I am not sure what will happen to RAMDirectory if it exceeds memory limit. I’d except a
OutOfMemoryException: System.SystemException
OutOfMemoryException : System.SystemException
投掷.
这篇关于使用RAMDirectory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!