Java Code Examples for org.apache.lucene.codecs.FieldsProducer#close()

The following examples show how to use org.apache.lucene.codecs.FieldsProducer#close() . 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: DirectPostingsFormat.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public FieldsProducer fieldsProducer(SegmentReadState state) throws IOException {
  FieldsProducer postings = PostingsFormat.forName("Lucene84").fieldsProducer(state);
  if (state.context.context != IOContext.Context.MERGE) {
    FieldsProducer loadedPostings;
    try {
      postings.checkIntegrity();
      loadedPostings = new DirectFields(state, postings, minSkipCount, lowFreqCutoff);
    } finally {
      postings.close();
    }
    return loadedPostings;
  } else {
    // Don't load postings for merge:
    return postings;
  }
}
 
Example 2
Source File: BasePostingsFormatTestCase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testRandom() throws Exception {

    int iters = 5;

    for(int iter=0;iter<iters;iter++) {
      Path path = createTempDir("testPostingsFormat");
      Directory dir = newFSDirectory(path);

      boolean indexPayloads = random().nextBoolean();
      // TODO test thread safety of buildIndex too
      FieldsProducer fieldsProducer = postingsTester.buildIndex(getCodec(), dir, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS, indexPayloads, false);

      postingsTester.testFields(fieldsProducer);

      // NOTE: you can also test "weaker" index options than
      // you indexed with:
      postingsTester.testTerms(fieldsProducer, EnumSet.allOf(RandomPostingsTester.Option.class), IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS, false);

      fieldsProducer.close();
      fieldsProducer = null;

      dir.close();
    }
  }
 
Example 3
Source File: BasePostingsFormatTestCase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testPostingsEnumReuse() throws Exception {

    Path path = createTempDir("testPostingsEnumReuse");
    Directory dir = newFSDirectory(path);

    FieldsProducer fieldsProducer = postingsTester.buildIndex(getCodec(), dir, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS, random().nextBoolean(), true);
    Collections.shuffle(postingsTester.allTerms, random());
    RandomPostingsTester.FieldAndTerm fieldAndTerm = postingsTester.allTerms.get(0);

    Terms terms = fieldsProducer.terms(fieldAndTerm.field);
    TermsEnum te = terms.iterator();

    te.seekExact(fieldAndTerm.term);
    checkReuse(te, PostingsEnum.FREQS, PostingsEnum.ALL, false);
    if (isPostingsEnumReuseImplemented()) {
      checkReuse(te, PostingsEnum.ALL, PostingsEnum.ALL, true);
    }

    fieldsProducer.close();
    dir.close();
  }
 
Example 4
Source File: RandomPostingsTester.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Indexes all fields/terms at the specified
 *  IndexOptions, and fully tests at that IndexOptions. */
public void testFull(Codec codec, Path path, IndexOptions options, boolean withPayloads) throws Exception {
  Directory dir = LuceneTestCase.newFSDirectory(path);

  // TODO test thread safety of buildIndex too
  FieldsProducer fieldsProducer = buildIndex(codec, dir, options, withPayloads, true);

  testFields(fieldsProducer);

  IndexOptions[] allOptions = IndexOptions.values();
  int maxIndexOption = Arrays.asList(allOptions).indexOf(options);

  for(int i=0;i<=maxIndexOption;i++) {
    testTerms(fieldsProducer, EnumSet.allOf(Option.class), allOptions[i], options, true);
    if (withPayloads) {
      // If we indexed w/ payloads, also test enums w/o accessing payloads:
      testTerms(fieldsProducer, EnumSet.complementOf(EnumSet.of(Option.PAYLOADS)), allOptions[i], options, true);
    }
  }

  fieldsProducer.close();
  dir.close();
}
 
Example 5
Source File: TestCodecs.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testFixedPostings() throws Throwable {
  final int NUM_TERMS = 100;
  final TermData[] terms = new TermData[NUM_TERMS];
  for(int i=0;i<NUM_TERMS;i++) {
    final int[] docs = new int[] {i};
    final String text = Integer.toString(i, Character.MAX_RADIX);
    terms[i] = new TermData(text, docs, null);
  }

  final FieldInfos.Builder builder = new FieldInfos.Builder(new FieldInfos.FieldNumbers(null));

  final FieldData field = new FieldData("field", builder, terms, true, false);
  final FieldData[] fields = new FieldData[] {field};
  final FieldInfos fieldInfos = builder.finish();
  final Directory dir = newDirectory();
  Codec codec = Codec.getDefault();
  final SegmentInfo si = new SegmentInfo(dir, Version.LATEST, Version.LATEST, SEGMENT, 10000, false, codec, Collections.emptyMap(), StringHelper.randomId(), new HashMap<>(), null);
  
  this.write(si, fieldInfos, dir, fields);
  final FieldsProducer reader = codec.postingsFormat().fieldsProducer(new SegmentReadState(dir, si, fieldInfos, newIOContext(random())));

  final Iterator<String> fieldsEnum = reader.iterator();
  String fieldName = fieldsEnum.next();
  assertNotNull(fieldName);
  final Terms terms2 = reader.terms(fieldName);
  assertNotNull(terms2);

  final TermsEnum termsEnum = terms2.iterator();

  PostingsEnum postingsEnum = null;
  for(int i=0;i<NUM_TERMS;i++) {
    final BytesRef term = termsEnum.next();
    assertNotNull(term);
    assertEquals(terms[i].text2, term.utf8ToString());

    // do this twice to stress test the codec's reuse, ie,
    // make sure it properly fully resets (rewinds) its
    // internal state:
    for(int iter=0;iter<2;iter++) {
      postingsEnum = TestUtil.docs(random(), termsEnum, postingsEnum, PostingsEnum.NONE);
      assertEquals(terms[i].docs[0], postingsEnum.nextDoc());
      assertEquals(DocIdSetIterator.NO_MORE_DOCS, postingsEnum.nextDoc());
    }
  }
  assertNull(termsEnum.next());

  for(int i=0;i<NUM_TERMS;i++) {
    assertEquals(termsEnum.seekCeil(new BytesRef(terms[i].text2)), TermsEnum.SeekStatus.FOUND);
  }

  assertFalse(fieldsEnum.hasNext());
  reader.close();
  dir.close();
}