【发布时间】:2009-03-23 15:39:36
【问题描述】:
什么时候应该使用 Lucene 的 RAMDirectory?与其他存储机制相比,它有哪些优势?最后,我在哪里可以找到一个简单的代码示例?
【问题讨论】:
-
在内部它使用concurrenthasmap来排列文件。
标签: lucene lucene.net
什么时候应该使用 Lucene 的 RAMDirectory?与其他存储机制相比,它有哪些优势?最后,我在哪里可以找到一个简单的代码示例?
【问题讨论】:
标签: lucene lucene.net
当您不想永久存储索引数据时。我将其用于测试目的。将数据添加到您的 RAMDirectory,在 RAMDir 中进行单元测试。
例如
public static void main(String[] args) {
try {
Directory directory = new RAMDirectory();
Analyzer analyzer = new SimpleAnalyzer();
IndexWriter writer = new IndexWriter(directory, analyzer, true);
或
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 无效)
从功能上讲,RAMDirectory 与 FSDirectory 相比并没有明显的优势(除了 RAMDirectory 明显快于 FSDirectory 的事实)。他们都满足两种不同的需求。
非常类似于 RAM 和硬盘。
我不确定如果 RAMDirectory 超出内存限制会发生什么。我除了一个
OutOfMemoryException: System.SystemException
抛出。
【讨论】: