Java Code Examples for org.apache.lucene.util.TestUtil#checkIndex()

The following examples show how to use org.apache.lucene.util.TestUtil#checkIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestIndexWriterReader.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testAfterClose() throws Exception {
  Directory dir1 = getAssertNoDeletesDirectory(newDirectory());
  IndexWriter writer = new IndexWriter(dir1, newIndexWriterConfig(new MockAnalyzer(random())));

  // create the index
  createIndexNoClose(false, "test", writer);

  DirectoryReader r = writer.getReader();
  writer.close();

  TestUtil.checkIndex(dir1);

  // reader should remain usable even after IndexWriter is closed:
  assertEquals(100, r.numDocs());
  Query q = new TermQuery(new Term("indexname", "test"));
  IndexSearcher searcher = newSearcher(r);
  assertEquals(100, searcher.count(q));

  expectThrows(AlreadyClosedException.class, () -> {
    DirectoryReader.openIfChanged(r);
  });

  r.close();
  dir1.close();
}
 
Example 2
Source File: TestCoreBackup.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Simple check that the backup exists, is a valid index, and contains the expected number of docs.  
 * If expectedSegmentsFileName is non null then confirms that file exists in the bakup dir 
 * <em>and</em> that it is reported as the current segment file when opening a reader on that backup.
 */
private static void simpleBackupCheck(final File backup, final int numDocs,
                                      final String expectedSegmentsFileName) throws IOException {
  assertNotNull(backup);
  assertTrue("Backup doesn't exist" + backup.toString(), backup.exists());
  if (null != expectedSegmentsFileName) {
    assertTrue(expectedSegmentsFileName + " doesn't exist in " + backup.toString(),
               new File(backup, expectedSegmentsFileName).exists());
  }
  try (Directory dir = FSDirectory.open(backup.toPath())) {
    TestUtil.checkIndex(dir, true, true, null);
    try (DirectoryReader r = DirectoryReader.open(dir)) {
      assertEquals("numDocs in " + backup.toString(),
                   numDocs, r.numDocs());
      if (null != expectedSegmentsFileName) {
        assertEquals("segmentsFile of IndexCommit for: " + backup.toString(),
                     expectedSegmentsFileName, r.getIndexCommit().getSegmentsFileName());
      }
    }
  }
  
}
 
Example 3
Source File: TestBackwardsCompatibility.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testSortedIndex() throws Exception {
  for(String name : oldSortedNames) {
    Path path = createTempDir("sorted");
    InputStream resource = TestBackwardsCompatibility.class.getResourceAsStream(name + ".zip");
    assertNotNull("Sorted index index " + name + " not found", resource);
    TestUtil.unzip(resource, path);

    // TODO: more tests
    Directory dir = newFSDirectory(path);

    DirectoryReader reader = DirectoryReader.open(dir);
    assertEquals(1, reader.leaves().size());
    Sort sort = reader.leaves().get(0).reader().getMetaData().getSort();
    assertNotNull(sort);
    assertEquals("<long: \"dateDV\">!", sort.toString());
    reader.close();

    // this will confirm the docs really are sorted:
    TestUtil.checkIndex(dir);
    dir.close();
  }
}
 
Example 4
Source File: TestStressThreadBackup.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** 
 * Validates a backup dir exists, passes check index, and contains a number of "real" documents
 * that match it's name
 * 
 * @see #getNumRealDocsFromBackupName
 */
private void validateBackup(final File backup) throws IOException {
  log.info("Checking Validity of {}", backup);
  assertTrue(backup.toString() + ": isDir?", backup.isDirectory());
  final Matcher m = ENDS_WITH_INT_DIGITS.matcher(backup.getName());
  assertTrue("Backup dir name does not end with int digits: " + backup.toString(), m.find());
  final int numRealDocsExpected = Integer.parseInt(m.group());
  
  try (Directory dir = FSDirectory.open(backup.toPath())) {
    TestUtil.checkIndex(dir, true, true, null);
    try (DirectoryReader r = DirectoryReader.open(dir)) {
      assertEquals("num real docs in " + backup.toString(),
                   numRealDocsExpected, r.docFreq(new Term("type_s","real")));
    }
  }
}
 
Example 5
Source File: IndexReplicationClientTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Boolean call() throws Exception {
  if (reader == null) {
    reader = DirectoryReader.open(indexDir);
    lastGeneration = reader.getIndexCommit().getGeneration();
  } else {
    DirectoryReader newReader = DirectoryReader.openIfChanged(reader);
    assertNotNull("should not have reached here if no changes were made to the index", newReader);
    long newGeneration = newReader.getIndexCommit().getGeneration();
    assertTrue("expected newer generation; current=" + lastGeneration + " new=" + newGeneration, newGeneration > lastGeneration);
    reader.close();
    reader = newReader;
    lastGeneration = newGeneration;
    TestUtil.checkIndex(indexDir);
  }
  return null;
}
 
Example 6
Source File: TestPerFieldPostingsFormat2.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testMergeUnusedPerFieldCodec() throws IOException {
  Directory dir = newDirectory();
  IndexWriterConfig iwconf = newIndexWriterConfig(new MockAnalyzer(random()))
                               .setOpenMode(OpenMode.CREATE).setCodec(new MockCodec());
  IndexWriter writer = newWriter(dir, iwconf);
  addDocs(writer, 10);
  writer.commit();
  addDocs3(writer, 10);
  writer.commit();
  addDocs2(writer, 10);
  writer.commit();
  assertEquals(30, writer.getDocStats().maxDoc);
  TestUtil.checkIndex(dir);
  writer.forceMerge(1);
  assertEquals(30, writer.getDocStats().maxDoc);
  writer.close();
  dir.close();
}
 
Example 7
Source File: BaseStoredFieldsFormatTestCase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testBulkMergeWithDeletes() throws IOException {
  final int numDocs = atLeast(200);
  Directory dir = newDirectory();
  RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig(new MockAnalyzer(random())).setMergePolicy(NoMergePolicy.INSTANCE));
  for (int i = 0; i < numDocs; ++i) {
    Document doc = new Document();
    doc.add(new StringField("id", Integer.toString(i), Store.YES));
    doc.add(new StoredField("f", TestUtil.randomSimpleString(random())));
    w.addDocument(doc);
  }
  final int deleteCount = TestUtil.nextInt(random(), 5, numDocs);
  for (int i = 0; i < deleteCount; ++i) {
    final int id = random().nextInt(numDocs);
    w.deleteDocuments(new Term("id", Integer.toString(id)));
  }
  w.commit();
  w.close();
  w = new RandomIndexWriter(random(), dir);
  w.forceMerge(TestUtil.nextInt(random(), 1, 3));
  w.commit();
  w.close();
  TestUtil.checkIndex(dir);
  dir.close();
}
 
Example 8
Source File: TestIndexWriterReader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Slow
public void testAddIndexesAndDoDeletesThreads() throws Throwable {
  final int numIter = TEST_NIGHTLY ? 2 : 1;
  int numDirs = TEST_NIGHTLY ? 3 : 2;
  
  Directory mainDir = getAssertNoDeletesDirectory(newDirectory());

  IndexWriter mainWriter = new IndexWriter(mainDir, newIndexWriterConfig(new MockAnalyzer(random()))
                                                      .setMergePolicy(newLogMergePolicy()));
  TestUtil.reduceOpenFiles(mainWriter);

  AddDirectoriesThreads addDirThreads = new AddDirectoriesThreads(numIter, mainWriter);
  addDirThreads.launchThreads(numDirs);
  addDirThreads.joinThreads();
  
  //assertEquals(100 + numDirs * (3 * numIter / 4) * addDirThreads.numThreads
  //    * addDirThreads.NUM_INIT_DOCS, addDirThreads.mainwriter.getDocStats().numDocs);
  assertEquals(addDirThreads.count.intValue(), addDirThreads.mainWriter.getDocStats().numDocs);

  addDirThreads.close(true);
  
  assertTrue(addDirThreads.failures.size() == 0);

  TestUtil.checkIndex(mainDir);

  IndexReader reader = DirectoryReader.open(mainDir);
  assertEquals(addDirThreads.count.intValue(), reader.numDocs());
  //assertEquals(100 + numDirs * (3 * numIter / 4) * addDirThreads.numThreads
  //    * addDirThreads.NUM_INIT_DOCS, reader.numDocs());
  reader.close();

  addDirThreads.closeDir();
  mainDir.close();
}
 
Example 9
Source File: TestIndexWriterReader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testAfterCommit() throws Exception {
  Directory dir1 = getAssertNoDeletesDirectory(newDirectory());
  IndexWriter writer = new IndexWriter(dir1, newIndexWriterConfig(new MockAnalyzer(random()))
                                               .setMergeScheduler(new ConcurrentMergeScheduler()));
  writer.commit();

  // create the index
  createIndexNoClose(false, "test", writer);

  // get a reader to put writer into near real-time mode
  DirectoryReader r1 = writer.getReader();
  TestUtil.checkIndex(dir1);
  writer.commit();
  TestUtil.checkIndex(dir1);
  assertEquals(100, r1.numDocs());

  for (int i = 0; i < 10; i++) {
    writer.addDocument(DocHelper.createDocument(i, "test", 4));
  }
  ((ConcurrentMergeScheduler) writer.getConfig().getMergeScheduler()).sync();

  DirectoryReader r2 = DirectoryReader.openIfChanged(r1);
  if (r2 != null) {
    r1.close();
    r1 = r2;
  }
  assertEquals(110, r1.numDocs());
  writer.close();
  r1.close();
  dir1.close();
}
 
Example 10
Source File: BaseDirectoryWrapper.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void close() throws IOException {
  if (isOpen) {
    isOpen = false;
    if (checkIndexOnClose && DirectoryReader.indexExists(this)) {
      TestUtil.checkIndex(this, doSlowChecksOnClose);
    }
  }
  super.close();
}
 
Example 11
Source File: TestDocumentWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Test adding two fields with the same name, one indexed
 * the other stored only. The omitNorms and omitTermFreqAndPositions setting
 * of the stored field should not affect the indexed one (LUCENE-1590)
 */
public void testLUCENE_1590() throws Exception {
  Document doc = new Document();
  // f1 has no norms
  FieldType customType = new FieldType(TextField.TYPE_NOT_STORED);
  customType.setOmitNorms(true);
  FieldType customType2 = new FieldType();
  customType2.setStored(true);
  doc.add(newField("f1", "v1", customType));
  doc.add(newField("f1", "v2", customType2));
  // f2 has no TF
  FieldType customType3 = new FieldType(TextField.TYPE_NOT_STORED);
  customType3.setIndexOptions(IndexOptions.DOCS);
  Field f = newField("f2", "v1", customType3);
  doc.add(f);
  doc.add(newField("f2", "v2", customType2));

  IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
  writer.addDocument(doc);
  writer.forceMerge(1); // be sure to have a single segment
  writer.close();

  TestUtil.checkIndex(dir);

  LeafReader reader = getOnlyLeafReader(DirectoryReader.open(dir));
  FieldInfos fi = reader.getFieldInfos();
  // f1
  assertFalse("f1 should have no norms", fi.fieldInfo("f1").hasNorms());
  assertEquals("omitTermFreqAndPositions field bit should not be set for f1", IndexOptions.DOCS_AND_FREQS_AND_POSITIONS, fi.fieldInfo("f1").getIndexOptions());
  // f2
  assertTrue("f2 should have norms", fi.fieldInfo("f2").hasNorms());
  assertEquals("omitTermFreqAndPositions field bit should be set for f2", IndexOptions.DOCS, fi.fieldInfo("f2").getIndexOptions());
  reader.close();
}
 
Example 12
Source File: TestManyPointsInOldIndex.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testCheckOldIndex() throws IOException {
  assumeTrue("Reenable when 7.0 is released", false);
  Path path = createTempDir("manypointsindex");
  InputStream resource = getClass().getResourceAsStream("manypointsindex.zip");
  assertNotNull("manypointsindex not found", resource);
  TestUtil.unzip(resource, path);
  BaseDirectoryWrapper dir = newFSDirectory(path);
  // disable default checking...
  dir.setCheckIndexOnClose(false);

  // ... because we check ourselves here:
  TestUtil.checkIndex(dir, false, true, null);
  dir.close();
}
 
Example 13
Source File: TestBackwardsCompatibility.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testMoreTerms() throws Exception {
  Path oldIndexDir = createTempDir("moreterms");
  TestUtil.unzip(getDataInputStream(moreTermsIndex), oldIndexDir);
  Directory dir = newFSDirectory(oldIndexDir);
  verifyUsesDefaultCodec(dir, moreTermsIndex);
  // TODO: more tests
  TestUtil.checkIndex(dir);
  dir.close();
}
 
Example 14
Source File: TestHighFreqTerms.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUpClass() throws Exception {
  dir = newDirectory();
  writer = new IndexWriter(dir, newIndexWriterConfig(random(),
     new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false))
     .setMaxBufferedDocs(2));
  indexDocs(writer);
  reader = DirectoryReader.open(dir);
  TestUtil.checkIndex(dir);
}
 
Example 15
Source File: IndexAndTaxonomyReplicationClientTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean call() throws Exception {
  if (indexReader == null) {
    indexReader = DirectoryReader.open(indexDir);
    lastIndexGeneration = indexReader.getIndexCommit().getGeneration();
    taxoReader = new DirectoryTaxonomyReader(taxoDir);
  } else {
    // verify search index
    DirectoryReader newReader = DirectoryReader.openIfChanged(indexReader);
    assertNotNull("should not have reached here if no changes were made to the index", newReader);
    long newGeneration = newReader.getIndexCommit().getGeneration();
    assertTrue("expected newer generation; current=" + lastIndexGeneration + " new=" + newGeneration, newGeneration > lastIndexGeneration);
    indexReader.close();
    indexReader = newReader;
    lastIndexGeneration = newGeneration;
    TestUtil.checkIndex(indexDir);
    
    // verify taxonomy index
    DirectoryTaxonomyReader newTaxoReader = TaxonomyReader.openIfChanged(taxoReader);
    if (newTaxoReader != null) {
      taxoReader.close();
      taxoReader = newTaxoReader;
    }
    TestUtil.checkIndex(taxoDir);
    
    // verify faceted search
    int id = Integer.parseInt(indexReader.getIndexCommit().getUserData().get(VERSION_ID), 16);
    IndexSearcher searcher = new IndexSearcher(indexReader);
    FacetsCollector fc = new FacetsCollector();
    searcher.search(new MatchAllDocsQuery(), fc);
    Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
    assertEquals(1, facets.getSpecificValue("A", Integer.toString(id, 16)).intValue());
    
    DrillDownQuery drillDown = new DrillDownQuery(config);
    drillDown.add("A", Integer.toString(id, 16));
    TopDocs docs = searcher.search(drillDown, 10);
    assertEquals(1, docs.totalHits.value);
  }
  return null;
}
 
Example 16
Source File: TestIndexWriterOnDiskFull.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testCorruptionAfterDiskFullDuringMerge() throws IOException {
  MockDirectoryWrapper dir = newMockDirectory();
  //IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random)).setReaderPooling(true));
  IndexWriter w = new IndexWriter(
      dir,
      newIndexWriterConfig(new MockAnalyzer(random()))
        .setMergeScheduler(new SerialMergeScheduler())
        .setReaderPooling(true)
        .setMergePolicy(new FilterMergePolicy(newLogMergePolicy(2)) {
          @Override
          public boolean keepFullyDeletedSegment(IOSupplier<CodecReader> readerIOSupplier) throws IOException {
            // we can do this because we add/delete/add (and dont merge to "nothing")
            return true;
          }
        })
  );
  Document doc = new Document();

  doc.add(newTextField("f", "doctor who", Field.Store.NO));
  w.addDocument(doc);
  w.commit();

  w.deleteDocuments(new Term("f", "who"));
  w.addDocument(doc);
  
  // disk fills up!
  FailTwiceDuringMerge ftdm = new FailTwiceDuringMerge();
  ftdm.setDoFail();
  dir.failOn(ftdm);

  expectThrows(IOException.class, () -> {
    w.commit();
  });
  assertTrue(ftdm.didFail1 || ftdm.didFail2);

  TestUtil.checkIndex(dir);
  ftdm.clearDoFail();
  expectThrows(AlreadyClosedException.class, () -> {
    w.addDocument(doc);
  });

  dir.close();
}
 
Example 17
Source File: Test2BTerms.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void test2BTerms() throws IOException {

    System.out.println("Starting Test2B");
    final long TERM_COUNT = ((long) Integer.MAX_VALUE) + 100000000;

    final int TERMS_PER_DOC = TestUtil.nextInt(random(), 100000, 1000000);

    List<BytesRef> savedTerms = null;

    BaseDirectoryWrapper dir = newFSDirectory(createTempDir("2BTerms"));
    //MockDirectoryWrapper dir = newFSDirectory(new File("/p/lucene/indices/2bindex"));
    if (dir instanceof MockDirectoryWrapper) {
      ((MockDirectoryWrapper)dir).setThrottling(MockDirectoryWrapper.Throttling.NEVER);
    }
    dir.setCheckIndexOnClose(false); // don't double-checkindex

    if (true) {

      IndexWriter w = new IndexWriter(dir,
                                      new IndexWriterConfig(new MockAnalyzer(random()))
                                      .setMaxBufferedDocs(IndexWriterConfig.DISABLE_AUTO_FLUSH)
                                      .setRAMBufferSizeMB(256.0)
                                      .setMergeScheduler(new ConcurrentMergeScheduler())
                                      .setMergePolicy(newLogMergePolicy(false, 10))
                                      .setOpenMode(IndexWriterConfig.OpenMode.CREATE)
                                      .setCodec(TestUtil.getDefaultCodec()));

      MergePolicy mp = w.getConfig().getMergePolicy();
      if (mp instanceof LogByteSizeMergePolicy) {
        // 1 petabyte:
        ((LogByteSizeMergePolicy) mp).setMaxMergeMB(1024*1024*1024);
      }

      Document doc = new Document();
      final MyTokenStream ts = new MyTokenStream(random(), TERMS_PER_DOC);

      FieldType customType = new FieldType(TextField.TYPE_NOT_STORED);
      customType.setIndexOptions(IndexOptions.DOCS);
      customType.setOmitNorms(true);
      Field field = new Field("field", ts, customType);
      doc.add(field);
      //w.setInfoStream(System.out);
      final int numDocs = (int) (TERM_COUNT/TERMS_PER_DOC);

      System.out.println("TERMS_PER_DOC=" + TERMS_PER_DOC);
      System.out.println("numDocs=" + numDocs);

      for(int i=0;i<numDocs;i++) {
        final long t0 = System.currentTimeMillis();
        w.addDocument(doc);
        System.out.println(i + " of " + numDocs + " " + (System.currentTimeMillis()-t0) + " msec");
      }
      savedTerms = ts.savedTerms;

      System.out.println("TEST: full merge");
      w.forceMerge(1);
      System.out.println("TEST: close writer");
      w.close();
    }

    System.out.println("TEST: open reader");
    final IndexReader r = DirectoryReader.open(dir);
    if (savedTerms == null) {
      savedTerms = findTerms(r);
    }
    final int numSavedTerms = savedTerms.size();
    final List<BytesRef> bigOrdTerms = new ArrayList<>(savedTerms.subList(numSavedTerms-10, numSavedTerms));
    System.out.println("TEST: test big ord terms...");
    testSavedTerms(r, bigOrdTerms);
    System.out.println("TEST: test all saved terms...");
    testSavedTerms(r, savedTerms);
    r.close();

    System.out.println("TEST: now CheckIndex...");
    CheckIndex.Status status = TestUtil.checkIndex(dir);
    final long tc = status.segmentInfos.get(0).termIndexStatus.termCount;
    assertTrue("count " + tc + " is not > " + Integer.MAX_VALUE, tc > Integer.MAX_VALUE);

    dir.close();
    System.out.println("TEST: done!");
  }
 
Example 18
Source File: Test2BPoints.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void test1D() throws Exception {
  Directory dir = FSDirectory.open(createTempDir("2BPoints1D"));

  IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()))
    .setCodec(getCodec())
    .setMaxBufferedDocs(IndexWriterConfig.DISABLE_AUTO_FLUSH)
    .setRAMBufferSizeMB(256.0)
    .setMergeScheduler(new ConcurrentMergeScheduler())
    .setMergePolicy(newLogMergePolicy(false, 10))
    .setOpenMode(IndexWriterConfig.OpenMode.CREATE);

  ((ConcurrentMergeScheduler) iwc.getMergeScheduler()).setMaxMergesAndThreads(6, 3);
  
  IndexWriter w = new IndexWriter(dir, iwc);

  MergePolicy mp = w.getConfig().getMergePolicy();
  if (mp instanceof LogByteSizeMergePolicy) {
   // 1 petabyte:
   ((LogByteSizeMergePolicy) mp).setMaxMergeMB(1024*1024*1024);
  }

  final int numDocs = (Integer.MAX_VALUE / 26) + 1;
  int counter = 0;
  for (int i = 0; i < numDocs; i++) {
    Document doc = new Document();
    for (int j=0;j<26;j++) {
      long x = (((long) random().nextInt() << 32)) | (long) counter;
      doc.add(new LongPoint("long", x));
      counter++;
    }
    w.addDocument(doc);
    if (VERBOSE && i % 100000 == 0) {
      System.out.println(i + " of " + numDocs + "...");
    }
  }
  w.forceMerge(1);
  DirectoryReader r = DirectoryReader.open(w);
  IndexSearcher s = new IndexSearcher(r);
  assertEquals(numDocs, s.count(LongPoint.newRangeQuery("long", Long.MIN_VALUE, Long.MAX_VALUE)));
  assertTrue(r.leaves().get(0).reader().getPointValues("long").size() > Integer.MAX_VALUE);
  r.close();
  w.close();
  System.out.println("TEST: now CheckIndex");
  TestUtil.checkIndex(dir);
  dir.close();
}
 
Example 19
Source File: Test2BPoints.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void test2D() throws Exception {
  Directory dir = FSDirectory.open(createTempDir("2BPoints2D"));

  IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()))
    .setCodec(getCodec())
    .setMaxBufferedDocs(IndexWriterConfig.DISABLE_AUTO_FLUSH)
    .setRAMBufferSizeMB(256.0)
    .setMergeScheduler(new ConcurrentMergeScheduler())
    .setMergePolicy(newLogMergePolicy(false, 10))
    .setOpenMode(IndexWriterConfig.OpenMode.CREATE);
  
  ((ConcurrentMergeScheduler) iwc.getMergeScheduler()).setMaxMergesAndThreads(6, 3);

  IndexWriter w = new IndexWriter(dir, iwc);

  MergePolicy mp = w.getConfig().getMergePolicy();
  if (mp instanceof LogByteSizeMergePolicy) {
   // 1 petabyte:
   ((LogByteSizeMergePolicy) mp).setMaxMergeMB(1024*1024*1024);
  }

  final int numDocs = (Integer.MAX_VALUE / 26) + 1;
  int counter = 0;
  for (int i = 0; i < numDocs; i++) {
    Document doc = new Document();
    for (int j=0;j<26;j++) {
      long x = (((long) random().nextInt() << 32)) | (long) counter;
      long y = (((long) random().nextInt() << 32)) | (long) random().nextInt();
      doc.add(new LongPoint("long", x, y));
      counter++;
    }
    w.addDocument(doc);
    if (VERBOSE && i % 100000 == 0) {
      System.out.println(i + " of " + numDocs + "...");
    }
  }
  w.forceMerge(1);
  DirectoryReader r = DirectoryReader.open(w);
  IndexSearcher s = new IndexSearcher(r);
  assertEquals(numDocs, s.count(LongPoint.newRangeQuery("long", new long[] {Long.MIN_VALUE, Long.MIN_VALUE}, new long[] {Long.MAX_VALUE, Long.MAX_VALUE})));
  assertTrue(r.leaves().get(0).reader().getPointValues("long").size() > Integer.MAX_VALUE);
  r.close();
  w.close();
  System.out.println("TEST: now CheckIndex");
  TestUtil.checkIndex(dir);
  dir.close();
}
 
Example 20
Source File: TestAddIndexes.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testSimpleCaseCustomCodec() throws IOException {
  // main directory
  Directory dir = newDirectory();
  // two auxiliary directories
  Directory aux = newDirectory();
  Directory aux2 = newDirectory();
  Codec codec = new CustomPerFieldCodec();
  IndexWriter writer = null;

  writer = newWriter(dir, newIndexWriterConfig(new MockAnalyzer(random()))
                            .setOpenMode(OpenMode.CREATE).setCodec(codec));
  // add 100 documents
  addDocsWithID(writer, 100, 0);
  assertEquals(100, writer.getDocStats().maxDoc);
  writer.commit();
  writer.close();
  TestUtil.checkIndex(dir);

  writer = newWriter(
      aux,
      newIndexWriterConfig(new MockAnalyzer(random())).
          setOpenMode(OpenMode.CREATE).
          setCodec(codec).
          setMaxBufferedDocs(10).
          setMergePolicy(newLogMergePolicy(false))
  );
  // add 40 documents in separate files
  addDocs(writer, 40);
  assertEquals(40, writer.getDocStats().maxDoc);
  writer.commit();
  writer.close();

  writer = newWriter(
      aux2,
      newIndexWriterConfig(new MockAnalyzer(random())).
          setOpenMode(OpenMode.CREATE).
          setCodec(codec)
  );
  // add 40 documents in compound files
  addDocs2(writer, 50);
  assertEquals(50, writer.getDocStats().maxDoc);
  writer.commit();
  writer.close();

  // test doc count before segments are merged
  writer = newWriter(
      dir,
      newIndexWriterConfig(new MockAnalyzer(random())).
          setOpenMode(OpenMode.APPEND).
          setCodec(codec)
  );
  assertEquals(100, writer.getDocStats().maxDoc);
  writer.addIndexes(aux, aux2);
  assertEquals(190, writer.getDocStats().maxDoc);
  writer.close();

  dir.close();
  aux.close();
  aux2.close();
}