Search in sources :

Example 1 with MemoryPostingsFormat

use of org.apache.lucene.codecs.memory.MemoryPostingsFormat in project lucene-solr by apache.

the class TestRollingUpdates method testRollingUpdates.

// Just updates the same set of N docs over and over, to
// stress out deletions
@Test
public void testRollingUpdates() throws Exception {
    Random random = new Random(random().nextLong());
    final BaseDirectoryWrapper dir = newDirectory();
    final LineFileDocs docs = new LineFileDocs(random);
    //provider.register(new MemoryCodec());
    if (random().nextBoolean()) {
        Codec.setDefault(TestUtil.alwaysPostingsFormat(new MemoryPostingsFormat(random().nextBoolean(), random.nextFloat())));
    }
    MockAnalyzer analyzer = new MockAnalyzer(random());
    analyzer.setMaxTokenLength(TestUtil.nextInt(random(), 1, IndexWriter.MAX_TERM_LENGTH));
    final IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(analyzer));
    final int SIZE = atLeast(20);
    int id = 0;
    IndexReader r = null;
    IndexSearcher s = null;
    final int numUpdates = (int) (SIZE * (2 + (TEST_NIGHTLY ? 200 * random().nextDouble() : 5 * random().nextDouble())));
    if (VERBOSE) {
        System.out.println("TEST: numUpdates=" + numUpdates);
    }
    int updateCount = 0;
    // TODO: sometimes update ids not in order...
    for (int docIter = 0; docIter < numUpdates; docIter++) {
        final Document doc = docs.nextDoc();
        final String myID = Integer.toString(id);
        if (id == SIZE - 1) {
            id = 0;
        } else {
            id++;
        }
        if (VERBOSE) {
            System.out.println("  docIter=" + docIter + " id=" + id);
        }
        ((Field) doc.getField("docid")).setStringValue(myID);
        Term idTerm = new Term("docid", myID);
        final boolean doUpdate;
        if (s != null && updateCount < SIZE) {
            TopDocs hits = s.search(new TermQuery(idTerm), 1);
            assertEquals(1, hits.totalHits);
            doUpdate = w.tryDeleteDocument(r, hits.scoreDocs[0].doc) == -1;
            if (VERBOSE) {
                if (doUpdate) {
                    System.out.println("  tryDeleteDocument failed");
                } else {
                    System.out.println("  tryDeleteDocument succeeded");
                }
            }
        } else {
            doUpdate = true;
            if (VERBOSE) {
                System.out.println("  no searcher: doUpdate=true");
            }
        }
        updateCount++;
        if (doUpdate) {
            if (random().nextBoolean()) {
                w.updateDocument(idTerm, doc);
            } else {
                // It's OK to not be atomic for this test (no separate thread reopening readers):
                w.deleteDocuments(new TermQuery(idTerm));
                w.addDocument(doc);
            }
        } else {
            w.addDocument(doc);
        }
        if (docIter >= SIZE && random().nextInt(50) == 17) {
            if (r != null) {
                r.close();
            }
            final boolean applyDeletions = random().nextBoolean();
            if (VERBOSE) {
                System.out.println("TEST: reopen applyDeletions=" + applyDeletions);
            }
            r = w.getReader(applyDeletions, false);
            if (applyDeletions) {
                s = newSearcher(r);
            } else {
                s = null;
            }
            assertTrue("applyDeletions=" + applyDeletions + " r.numDocs()=" + r.numDocs() + " vs SIZE=" + SIZE, !applyDeletions || r.numDocs() == SIZE);
            updateCount = 0;
        }
    }
    if (r != null) {
        r.close();
    }
    w.commit();
    assertEquals(SIZE, w.numDocs());
    w.close();
    TestIndexWriter.assertNoUnreferencedFiles(dir, "leftover files after rolling updates");
    docs.close();
    // LUCENE-4455:
    SegmentInfos infos = SegmentInfos.readLatestCommit(dir);
    long totalBytes = 0;
    for (SegmentCommitInfo sipc : infos) {
        totalBytes += sipc.sizeInBytes();
    }
    long totalBytes2 = 0;
    for (String fileName : dir.listAll()) {
        if (IndexFileNames.CODEC_FILE_PATTERN.matcher(fileName).matches()) {
            totalBytes2 += dir.fileLength(fileName);
        }
    }
    assertEquals(totalBytes2, totalBytes);
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) TopDocs(org.apache.lucene.search.TopDocs) Random(java.util.Random) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) MemoryPostingsFormat(org.apache.lucene.codecs.memory.MemoryPostingsFormat) Test(org.junit.Test)

Example 2 with MemoryPostingsFormat

use of org.apache.lucene.codecs.memory.MemoryPostingsFormat in project lucene-solr by apache.

the class TestAddIndexes method testAddIndexMissingCodec.

/*
   * simple test that ensures we getting expected exceptions 
   */
public void testAddIndexMissingCodec() throws IOException {
    BaseDirectoryWrapper toAdd = newDirectory();
    // Disable checkIndex, else we get an exception because
    // of the unregistered codec:
    toAdd.setCheckIndexOnClose(false);
    {
        IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
        conf.setCodec(new UnRegisteredCodec());
        IndexWriter w = new IndexWriter(toAdd, conf);
        Document doc = new Document();
        FieldType customType = new FieldType();
        customType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
        doc.add(newField("foo", "bar", customType));
        w.addDocument(doc);
        w.close();
    }
    {
        Directory dir = newDirectory();
        IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
        conf.setCodec(TestUtil.alwaysPostingsFormat(new MemoryPostingsFormat()));
        IndexWriter w = new IndexWriter(dir, conf);
        expectThrows(IllegalArgumentException.class, () -> {
            w.addIndexes(toAdd);
        });
        w.close();
        IndexReader open = DirectoryReader.open(dir);
        assertEquals(0, open.numDocs());
        open.close();
        dir.close();
    }
    expectThrows(IllegalArgumentException.class, () -> {
        DirectoryReader.open(toAdd);
    });
    toAdd.close();
}
Also used : MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) BaseDirectoryWrapper(org.apache.lucene.store.BaseDirectoryWrapper) MemoryPostingsFormat(org.apache.lucene.codecs.memory.MemoryPostingsFormat) Document(org.apache.lucene.document.Document) FieldType(org.apache.lucene.document.FieldType) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory)

Example 3 with MemoryPostingsFormat

use of org.apache.lucene.codecs.memory.MemoryPostingsFormat in project lucene-solr by apache.

the class TestPerFieldPostingsFormat2 method testSameCodecDifferentInstance.

public void testSameCodecDifferentInstance() throws Exception {
    Codec codec = new AssertingCodec() {

        @Override
        public PostingsFormat getPostingsFormatForField(String field) {
            if ("id".equals(field)) {
                return new MemoryPostingsFormat();
            } else if ("date".equals(field)) {
                return new MemoryPostingsFormat();
            } else {
                return super.getPostingsFormatForField(field);
            }
        }
    };
    doTestMixedPostings(codec);
}
Also used : AssertingCodec(org.apache.lucene.codecs.asserting.AssertingCodec) AssertingCodec(org.apache.lucene.codecs.asserting.AssertingCodec) Codec(org.apache.lucene.codecs.Codec) MemoryPostingsFormat(org.apache.lucene.codecs.memory.MemoryPostingsFormat)

Aggregations

MemoryPostingsFormat (org.apache.lucene.codecs.memory.MemoryPostingsFormat)3 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)2 Random (java.util.Random)1 Codec (org.apache.lucene.codecs.Codec)1 AssertingCodec (org.apache.lucene.codecs.asserting.AssertingCodec)1 Document (org.apache.lucene.document.Document)1 FieldType (org.apache.lucene.document.FieldType)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 TermQuery (org.apache.lucene.search.TermQuery)1 TopDocs (org.apache.lucene.search.TopDocs)1 BaseDirectoryWrapper (org.apache.lucene.store.BaseDirectoryWrapper)1 Directory (org.apache.lucene.store.Directory)1 RAMDirectory (org.apache.lucene.store.RAMDirectory)1 Test (org.junit.Test)1