Java Code Examples for org.apache.lucene.util.Bits#length()

The following examples show how to use org.apache.lucene.util.Bits#length() . 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: Lucene.java    From crate with Apache License 2.0 6 votes vote down vote up
DirectoryReaderWithAllLiveDocs(DirectoryReader in) throws IOException {
    super(in, new SubReaderWrapper() {
        @Override
        public LeafReader wrap(LeafReader leaf) {
            SegmentReader segmentReader = segmentReader(leaf);
            Bits hardLiveDocs = segmentReader.getHardLiveDocs();
            if (hardLiveDocs == null) {
                return new LeafReaderWithLiveDocs(leaf, null, leaf.maxDoc());
            }
            // TODO: Can we avoid calculate numDocs by using SegmentReader#getSegmentInfo with LUCENE-8458?
            int numDocs = 0;
            for (int i = 0; i < hardLiveDocs.length(); i++) {
                if (hardLiveDocs.get(i)) {
                    numDocs++;
                }
            }
            return new LeafReaderWithLiveDocs(segmentReader, hardLiveDocs, numDocs);
        }
    });
}
 
Example 2
Source File: SimpleTextLiveDocsFormat.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void writeLiveDocs(Bits bits, Directory dir, SegmentCommitInfo info, int newDelCount, IOContext context) throws IOException {
  int size = bits.length();
  BytesRefBuilder scratch = new BytesRefBuilder();
  
  String fileName = IndexFileNames.fileNameFromGeneration(info.info.name, LIVEDOCS_EXTENSION, info.getNextDelGen());
  IndexOutput out = null;
  boolean success = false;
  try {
    out = dir.createOutput(fileName, context);
    SimpleTextUtil.write(out, SIZE);
    SimpleTextUtil.write(out, Integer.toString(size), scratch);
    SimpleTextUtil.writeNewline(out);
    
    for (int i = 0; i < size; ++i) {
      if (bits.get(i)) {
        SimpleTextUtil.write(out, DOC);
        SimpleTextUtil.write(out, Integer.toString(i), scratch);
        SimpleTextUtil.writeNewline(out);
      }
    }
    
    SimpleTextUtil.write(out, END);
    SimpleTextUtil.writeNewline(out);
    SimpleTextUtil.writeChecksum(out, scratch);
    success = true;
  } finally {
    if (success) {
      IOUtils.close(out);
    } else {
      IOUtils.closeWhileHandlingException(out);
    }
  }
}
 
Example 3
Source File: TestSoftDeletesRetentionMergePolicy.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public LeafReader wrap(LeafReader reader) {
  while (reader instanceof FilterLeafReader) {
    reader = ((FilterLeafReader) reader).getDelegate();
  }
  Bits hardLiveDocs = ((SegmentReader) reader).getHardLiveDocs();
  final int numDocs;
  if (hardLiveDocs == null) {
    numDocs = reader.maxDoc();
  } else {
    int bits = 0;
    for (int i = 0; i < hardLiveDocs.length(); i++) {
      if (hardLiveDocs.get(i)) {
        bits++;
      }
    }
    numDocs = bits;
  }
  return new FilterLeafReader(reader) {
    @Override
    public int numDocs() {
      return numDocs;
    }

    @Override
    public Bits getLiveDocs() {
      return hardLiveDocs;
    }

    @Override
    public CacheHelper getCoreCacheHelper() {
      return null;
    }

    @Override
    public CacheHelper getReaderCacheHelper() {
      return null;
    }
  };
}
 
Example 4
Source File: SecureAtomicReader.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
public static Bits getSecureLiveDocs(Bits bits, int maxDoc, final AccessControlReader accessControlReader) {
  final Bits liveDocs;
  if (bits == null) {
    liveDocs = getMatchAll(maxDoc);
  } else {
    liveDocs = bits;
  }
  final int length = liveDocs.length();
  Bits secureLiveDocs = new Bits() {
    @Override
    public boolean get(int index) {
      if (liveDocs.get(index)) {
        try {
          if (accessControlReader.hasAccess(ReadType.DOCS_ENUM, index)) {
            return true;
          }
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return false;
    }

    @Override
    public int length() {
      return length;
    }
  };
  return secureLiveDocs;
}
 
Example 5
Source File: LegacyNumericDocValuesWrapper.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public LegacyNumericDocValuesWrapper(Bits docsWithField, LegacyNumericDocValues values) {
  this.docsWithField = docsWithField;
  this.values = values;
  this.maxDoc = docsWithField.length();
}
 
Example 6
Source File: LegacyBinaryDocValuesWrapper.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public LegacyBinaryDocValuesWrapper(Bits docsWithField, LegacyBinaryDocValues values) {
  this.docsWithField = docsWithField;
  this.values = values;
  this.maxDoc = docsWithField.length();
}
 
Example 7
Source File: BaseGroupSelectorTestCase.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testGroupHeadsWithSort() throws IOException {

    Shard shard = new Shard();
    indexRandomDocs(shard.writer);
    IndexSearcher searcher = shard.getIndexSearcher();

    String[] query = new String[]{ "foo", "bar", "baz" };
    Query topLevel = new TermQuery(new Term("text", query[random().nextInt(query.length)]));

    Sort sort = new Sort(new SortField("sort1", SortField.Type.STRING), new SortField("sort2", SortField.Type.LONG));
    GroupSelector<T> groupSelector = getGroupSelector();
    GroupingSearch grouping = new GroupingSearch(groupSelector);
    grouping.setAllGroups(true);
    grouping.setAllGroupHeads(true);
    grouping.setSortWithinGroup(sort);

    grouping.search(searcher, topLevel, 0, 1);
    Collection<T> matchingGroups = grouping.getAllMatchingGroups();

    Bits groupHeads = grouping.getAllGroupHeads();
    int cardinality = 0;
    for (int i = 0; i < groupHeads.length(); i++) {
      if (groupHeads.get(i)) {
        cardinality++;
      }
    }
    assertEquals(matchingGroups.size(), cardinality);   // We should have one set bit per matching group

    // Each group head should correspond to the topdoc of a search filtered by
    // that group using the same within-group sort
    for (T groupValue : matchingGroups) {
      Query filtered = new BooleanQuery.Builder()
          .add(topLevel, BooleanClause.Occur.MUST)
          .add(filterQuery(groupValue), BooleanClause.Occur.FILTER)
          .build();
      TopDocs td = searcher.search(filtered, 1, sort);
      assertTrue(groupHeads.get(td.scoreDocs[0].doc));
    }

    shard.close();
  }
 
Example 8
Source File: TestPerSegmentDeletes.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public static void printDelDocs(Bits bits) {
  if (bits == null) return;
  for (int x = 0; x < bits.length(); x++) {
    System.out.println(x + ":" + bits.get(x));
  }
}
 
Example 9
Source File: TestFieldCacheVsDocValues.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void assertEquals(Bits expected, Bits actual) throws Exception {
  assertEquals(expected.length(), actual.length());
  for (int i = 0; i < expected.length(); i++) {
    assertEquals(expected.get(i), actual.get(i));
  }
}
 
Example 10
Source File: TestLegacyFieldCache.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void test() throws IOException {
  FieldCache cache = FieldCache.DEFAULT;
  NumericDocValues doubles = cache.getNumerics(reader, "theDouble", FieldCache.LEGACY_DOUBLE_PARSER);
  for (int i = 0; i < NUM_DOCS; i++) {
    assertEquals(i, doubles.nextDoc());
    assertEquals(Double.doubleToLongBits(Double.MAX_VALUE - i), doubles.longValue());
  }
  
  NumericDocValues longs = cache.getNumerics(reader, "theLong", FieldCache.LEGACY_LONG_PARSER);
  for (int i = 0; i < NUM_DOCS; i++) {
    assertEquals(i, longs.nextDoc());
    assertEquals(Long.MAX_VALUE - i, longs.longValue());
  }

  NumericDocValues ints = cache.getNumerics(reader, "theInt", FieldCache.LEGACY_INT_PARSER);
  for (int i = 0; i < NUM_DOCS; i++) {
    assertEquals(i, ints.nextDoc());
    assertEquals(Integer.MAX_VALUE - i, ints.longValue());
  }
  
  NumericDocValues floats = cache.getNumerics(reader, "theFloat", FieldCache.LEGACY_FLOAT_PARSER);
  for (int i = 0; i < NUM_DOCS; i++) {
    assertEquals(i, floats.nextDoc());
    assertEquals(Float.floatToIntBits(Float.MAX_VALUE - i), floats.longValue());
  }

  Bits docsWithField = cache.getDocsWithField(reader, "theLong", null);
  assertSame("Second request to cache return same array", docsWithField, cache.getDocsWithField(reader, "theLong", null));
  assertTrue("docsWithField(theLong) must be class Bits.MatchAllBits", docsWithField instanceof Bits.MatchAllBits);
  assertTrue("docsWithField(theLong) Size: " + docsWithField.length() + " is not: " + NUM_DOCS, docsWithField.length() == NUM_DOCS);
  for (int i = 0; i < docsWithField.length(); i++) {
    assertTrue(docsWithField.get(i));
  }
  
  docsWithField = cache.getDocsWithField(reader, "sparse", null);
  assertSame("Second request to cache return same array", docsWithField, cache.getDocsWithField(reader, "sparse", null));
  assertFalse("docsWithField(sparse) must not be class Bits.MatchAllBits", docsWithField instanceof Bits.MatchAllBits);
  assertTrue("docsWithField(sparse) Size: " + docsWithField.length() + " is not: " + NUM_DOCS, docsWithField.length() == NUM_DOCS);
  for (int i = 0; i < docsWithField.length(); i++) {
    assertEquals(i%2 == 0, docsWithField.get(i));
  }

  FieldCache.DEFAULT.purgeByCacheKey(reader.getCoreCacheHelper().getKey());
}