Skip to content

Instantly share code, notes, and snippets.

@s1monw
Created April 4, 2014 08:36
Show Gist options
  • Save s1monw/9970568 to your computer and use it in GitHub Desktop.
Save s1monw/9970568 to your computer and use it in GitHub Desktop.
TestReuseDirectory.java
package org.apache.lucene.index;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.LuceneTestCase;
import java.io.IOException;
/**
*/
public class TestReuseDirectory extends LuceneTestCase {
public void testBasicReuse() throws IOException {
Directory dir = new RAMDirectory();
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
Document document = new Document();
document.add(new TextField("id", "1", Field.Store.YES));
writer.addDocument(document);
DirectoryReader open = DirectoryReader.open(writer, true);
assertEquals(open.numDocs(), 1);
writer.rollback();
writer.close();
writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
document = new Document();
document.add(new TextField("id", "2", Field.Store.YES));
writer.addDocument(document);
writer.commit();
writer.close();
open.close(); // if you comment this out we are good!
open = DirectoryReader.open(dir); // we fail here with java.io.FileNotFoundException: _0.si
assertEquals(open.numDocs(), 1);
StoredDocument storedDocument = open.document(0);
assertEquals(storedDocument.get("id"), "2");
open.close();
dir.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment