@Override
 public void close() {
   // We only attempt to check open/closed state if there were no other test
   // failures.
   try {
     if (failureMarker.wasSuccessful() && dir.isOpen()) {
       Assert.fail("Directory not closed: " + dir);
     }
   } finally {
     // TODO: perform real close of the delegate: LUCENE-4058
     // dir.close();
   }
 }
Exemplo n.º 2
0
  @Test
  public void testRollingUpdates() throws Exception {
    Random random = new Random(random().nextLong());
    final BaseDirectoryWrapper dir = newDirectory();
    // test checks for no unref'ed files with the IW helper method, which isn't aware of "tried to
    // delete files"
    if (dir instanceof MockDirectoryWrapper) {
      ((MockDirectoryWrapper) dir).setEnableVirusScanner(false);
    }

    final LineFileDocs docs = new LineFileDocs(random, true);

    // 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);
        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);
        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();
  }
  @Test
  public void testGetLeave() throws IOException {
    Path file = createTempDir();
    final int iters = scaledRandomIntBetween(10, 100);
    for (int i = 0; i < iters; i++) {
      {
        BaseDirectoryWrapper dir = newFSDirectory(file);
        FSDirectory directory =
            DirectoryUtils.getLeaf(new FilterDirectory(dir) {}, FSDirectory.class, null);
        assertThat(directory, notNullValue());
        assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
        dir.close();
      }

      {
        BaseDirectoryWrapper dir = newFSDirectory(file);
        FSDirectory directory = DirectoryUtils.getLeaf(dir, FSDirectory.class, null);
        assertThat(directory, notNullValue());
        assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
        dir.close();
      }

      {
        Set<String> stringSet = Collections.emptySet();
        BaseDirectoryWrapper dir = newFSDirectory(file);
        FSDirectory directory =
            DirectoryUtils.getLeaf(
                new FileSwitchDirectory(stringSet, dir, dir, random().nextBoolean()),
                FSDirectory.class,
                null);
        assertThat(directory, notNullValue());
        assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
        dir.close();
      }

      {
        Set<String> stringSet = Collections.emptySet();
        BaseDirectoryWrapper dir = newFSDirectory(file);
        FSDirectory directory =
            DirectoryUtils.getLeaf(
                new FilterDirectory(
                    new FileSwitchDirectory(stringSet, dir, dir, random().nextBoolean())) {},
                FSDirectory.class,
                null);
        assertThat(directory, notNullValue());
        assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
        dir.close();
      }

      {
        Set<String> stringSet = Collections.emptySet();
        BaseDirectoryWrapper dir = newFSDirectory(file);
        RAMDirectory directory =
            DirectoryUtils.getLeaf(
                new FilterDirectory(
                    new FileSwitchDirectory(stringSet, dir, dir, random().nextBoolean())) {},
                RAMDirectory.class,
                null);
        assertThat(directory, nullValue());
        dir.close();
      }
    }
  }