【问题标题】:Using RAMDirectory使用 RAM 目录
【发布时间】:2009-03-23 15:39:36
【问题描述】:

什么时候应该使用 Lucene 的 RAMDirectory?与其他存储机制相比,它有哪些优势?最后,我在哪里可以找到一个简单的代码示例?

【问题讨论】:

  • 在内部它使用concurrenthasmap来排列文件。

标签: lucene lucene.net


【解决方案1】:

当您不想永久存储索引数据时。我将其用于测试目的。将数据添加到您的 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 目录 -> 主内存
  • FSDirectory -> 辅助内存

非常类似于 RAM 和硬盘。

我不确定如果 RAMDirectory 超出内存限制会发生什么。我除了一个

OutOfMemoryException: System.SystemException

抛出。

【讨论】:

  • 您好,感谢您的回复...与 FSDirectory 相比,使用 RAMDirectory 有什么优势?还有,如果索引的大小超过了 RAM 内存怎么办?
  • 对象大小有 2GM 的限制。如果你的 RAMDirecotry 超过了这个值,即使你有足够的 RAM,你也会得到 OutOfMemoryException。
猜你喜欢
  • 2012-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-12
  • 2016-06-09
  • 1970-01-01
相关资源
最近更新 更多