Java Code Examples for org.apache.lucene.util.ArrayUtil#copyOfSubArray()

The following examples show how to use org.apache.lucene.util.ArrayUtil#copyOfSubArray() . 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: TestSimpleExplanationsWithFillerDocs.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Adjusts <code>expDocNrs</code> based on the filler docs injected in the index, 
 * and if neccessary wraps the <code>q</code> in a BooleanQuery that will filter out all 
 * filler docs using the {@link #EXTRA} field.
 * 
 * @see #replaceIndex
 */
@Override
public void qtest(Query q, int[] expDocNrs) throws Exception {

  expDocNrs = ArrayUtil.copyOfSubArray(expDocNrs, 0, expDocNrs.length);
  for (int i=0; i < expDocNrs.length; i++) {
    expDocNrs[i] = PRE_FILLER_DOCS + ((NUM_FILLER_DOCS + 1) * expDocNrs[i]);
  }

  if (null != EXTRA) {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(new BooleanClause(q, BooleanClause.Occur.MUST));
    builder.add(new BooleanClause(new TermQuery(new Term(EXTRA, EXTRA)), BooleanClause.Occur.MUST_NOT));
    q = builder.build();
  }
  super.qtest(q, expDocNrs);
}
 
Example 2
Source File: SearchImpl.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<SearchResults> prevPage() {
  if (currentPage < 0 || query == null) {
    throw new LukeException(new IllegalStateException("Search session not started."));
  }

  // return to previous page
  currentPage -= 1;

  if (currentPage < 0) {
    log.warn("No more previous search results are available.");
    return Optional.empty();
  }

  try {
    // there should be cached results for this page
    int from = currentPage * pageSize;
    int to = Math.min(from + pageSize, docs.length);
    ScoreDoc[] part = ArrayUtil.copyOfSubArray(docs, from, to);
    return Optional.of(SearchResults.of(totalHits, part, from, searcher, fieldsToLoad));
  } catch (IOException e) {
    throw new LukeException("Search Failed.", e);
  }
}
 
Example 3
Source File: BlendedTermQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public final Query rewrite(IndexReader reader) throws IOException {
  final TermStates[] contexts = ArrayUtil.copyOfSubArray(this.contexts, 0, this.contexts.length);
  for (int i = 0; i < contexts.length; ++i) {
    if (contexts[i] == null || contexts[i].wasBuiltFor(reader.getContext()) == false) {
      contexts[i] = TermStates.build(reader.getContext(), terms[i], true);
    }
  }

  // Compute aggregated doc freq and total term freq
  // df will be the max of all doc freqs
  // ttf will be the sum of all total term freqs
  int df = 0;
  long ttf = 0;
  for (TermStates ctx : contexts) {
    df = Math.max(df, ctx.docFreq());
    ttf += ctx.totalTermFreq();
  }

  for (int i = 0; i < contexts.length; ++i) {
    contexts[i] = adjustFrequencies(reader.getContext(), contexts[i], df, ttf);
  }

  Query[] termQueries = new Query[terms.length];
  for (int i = 0; i < terms.length; ++i) {
    termQueries[i] = new TermQuery(terms[i], contexts[i]);
    if (boosts[i] != 1f) {
      termQueries[i] = new BoostQuery(termQueries[i], boosts[i]);
    }
  }
  return rewriteMethod.rewrite(termQueries);
}
 
Example 4
Source File: TestLongValuesSource.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
Sort randomSort() throws Exception {
  boolean reversed = random().nextBoolean();
  SortField fields[] = new SortField[] {
      new SortField("int", SortField.Type.INT, reversed),
      new SortField("long", SortField.Type.LONG, reversed)
  };
  Collections.shuffle(Arrays.asList(fields), random());
  int numSorts = TestUtil.nextInt(random(), 1, fields.length);
  return new Sort(ArrayUtil.copyOfSubArray(fields, 0, numSorts));
}
 
Example 5
Source File: AbstractTestCompressionMode.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
static byte[] compress(Compressor compressor, byte[] decompressed, int off, int len) throws IOException {
  byte[] compressed = new byte[len * 2 + 16]; // should be enough
  ByteArrayDataOutput out = new ByteArrayDataOutput(compressed);
  compressor.compress(decompressed, off, len, out);
  final int compressedLen = out.getPosition();
  return ArrayUtil.copyOfSubArray(compressed, 0, compressedLen);
}
 
Example 6
Source File: DeltaPackedLongValues.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public DeltaPackedLongValues build() {
  finish();
  pending = null;
  final PackedInts.Reader[] values = ArrayUtil.copyOfSubArray(this.values, 0, valuesOff);
  final long[] mins = ArrayUtil.copyOfSubArray(this.mins, 0, valuesOff);
  final long ramBytesUsed = DeltaPackedLongValues.BASE_RAM_BYTES_USED
      + RamUsageEstimator.sizeOf(values) + RamUsageEstimator.sizeOf(mins);
  return new DeltaPackedLongValues(pageShift, pageMask, values, mins, size, ramBytesUsed);
}
 
Example 7
Source File: MonotonicLongValues.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public MonotonicLongValues build() {
  finish();
  pending = null;
  final PackedInts.Reader[] values = ArrayUtil.copyOfSubArray(this.values, 0, valuesOff);
  final long[] mins = ArrayUtil.copyOfSubArray(this.mins, 0, valuesOff);
  final float[] averages = ArrayUtil.copyOfSubArray(this.averages, 0, valuesOff);
  final long ramBytesUsed = MonotonicLongValues.BASE_RAM_BYTES_USED
      + RamUsageEstimator.sizeOf(values) + RamUsageEstimator.sizeOf(mins)
      + RamUsageEstimator.sizeOf(averages);
  return new MonotonicLongValues(pageShift, pageMask, values, mins, averages, size, ramBytesUsed);
}
 
Example 8
Source File: PackedLongValues.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Build a {@link PackedLongValues} instance that contains values that
 *  have been added to this builder. This operation is destructive. */
public PackedLongValues build() {
  finish();
  pending = null;
  final PackedInts.Reader[] values = ArrayUtil.copyOfSubArray(this.values, 0, valuesOff);
  final long ramBytesUsed = PackedLongValues.BASE_RAM_BYTES_USED + RamUsageEstimator.sizeOf(values);
  return new PackedLongValues(pageShift, pageMask, values, size, ramBytesUsed);
}
 
Example 9
Source File: SortingLeafReader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public SortedSetDocValues getSortedSetDocValues(String field) throws IOException {
  SortedSetDocValues oldDocValues = in.getSortedSetDocValues(field);
  if (oldDocValues == null) {
    return null;
  }

  long[][] ords;
  synchronized (cachedSortedSetDVs) {
    ords = cachedSortedSetDVs.get(field);
    if (ords == null) {
      ords = new long[maxDoc()][];
      int docID;
      while ((docID = oldDocValues.nextDoc()) != NO_MORE_DOCS) {
        int newDocID = docMap.oldToNew(docID);
        long[] docOrds = new long[1];
        int upto = 0;
        while (true) {
          long ord = oldDocValues.nextOrd();
          if (ord == NO_MORE_ORDS) {
            break;
          }
          if (upto == docOrds.length) {
            docOrds = ArrayUtil.grow(docOrds);
          }
          docOrds[upto++] = ord;
        }
        ords[newDocID] = ArrayUtil.copyOfSubArray(docOrds, 0, upto);
      }
      cachedSortedSetDVs.put(field, ords);
    }
  }

  return new SortingSortedSetDocValues(oldDocValues, ords);
}
 
Example 10
Source File: TestPerSegmentDeletes.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static int[] toArray(PostingsEnum postingsEnum) throws IOException {
  int[] docs = new int[0];
  int numDocs = 0;
  while (postingsEnum.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
    int docID = postingsEnum.docID();
    docs = ArrayUtil.grow(docs, numDocs + 1);
    docs[numDocs + 1] = docID;
  }
  return ArrayUtil.copyOfSubArray(docs, 0, numDocs);
}
 
Example 11
Source File: DistanceStrategyTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void checkDistValueSource(Point pt, float... distances) throws IOException {
  float multiplier = random().nextFloat() * 100f;
  float[] dists2 = ArrayUtil.copyOfSubArray(distances, 0, distances.length);
  for (int i = 0; i < dists2.length; i++) {
    dists2[i] *= multiplier;
  }
  checkValueSource(strategy.makeDistanceValueSource(pt, multiplier), dists2, 1.0e-3f);
}
 
Example 12
Source File: BlendedTermQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Build the {@link BlendedTermQuery}. */
public BlendedTermQuery build() {
  return new BlendedTermQuery(
      ArrayUtil.copyOfSubArray(terms, 0, numTerms),
      ArrayUtil.copyOfSubArray(boosts, 0, numTerms),
      ArrayUtil.copyOfSubArray(contexts, 0, numTerms),
      rewriteMethod);
}
 
Example 13
Source File: TestPhraseQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testTopPhrases() throws IOException {
  Directory dir = newDirectory();
  IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
  String[] docs = ArrayUtil.copyOfSubArray(DOCS, 0, DOCS.length);
  Collections.shuffle(Arrays.asList(docs), random());
  for (String value : DOCS) {
    Document doc = new Document();
    doc.add(new TextField("f", value, Store.NO));
    w.addDocument(doc);
  }
  IndexReader r = DirectoryReader.open(w);
  w.close();
  IndexSearcher searcher = newSearcher(r);
  for (Query query : Arrays.asList(
      new PhraseQuery("f", "b", "c"), // common phrase
      new PhraseQuery("f", "e", "f"), // always appear next to each other
      new PhraseQuery("f", "d", "d")  // repeated term
      )) {
    for (int topN = 1; topN <= 2; ++topN) {
      TopScoreDocCollector collector1 = TopScoreDocCollector.create(topN, null, Integer.MAX_VALUE);
      searcher.search(query, collector1);
      ScoreDoc[] hits1 = collector1.topDocs().scoreDocs;
      TopScoreDocCollector collector2 = TopScoreDocCollector.create(topN, null, 1);
      searcher.search(query, collector2);
      ScoreDoc[] hits2 = collector2.topDocs().scoreDocs;
      assertTrue("" + query, hits1.length > 0);
      CheckHits.checkEqual(query, hits1, hits2);
    }
  }
  r.close();
  dir.close();
}
 
Example 14
Source File: BytesReference.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a compact array from the given BytesReference. The returned array won't be copied unless necessary. If you need
 * to modify the returned array use {@code BytesRef.deepCopyOf(reference.toBytesRef()} instead
 */
public static byte[] toBytes(BytesReference reference) {
    final BytesRef bytesRef = reference.toBytesRef();
    if (bytesRef.offset == 0 && bytesRef.length == bytesRef.bytes.length) {
        return bytesRef.bytes;
    }
    return ArrayUtil.copyOfSubArray(bytesRef.bytes, bytesRef.offset, bytesRef.offset + bytesRef.length);
}
 
Example 15
Source File: CachingCollector.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
float[] cachedScores() {
  return docs == null ? null : ArrayUtil.copyOfSubArray(scores, 0, docCount);
}
 
Example 16
Source File: CachingCollector.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
int[] cachedDocs() {
  return docs == null ? null : ArrayUtil.copyOfSubArray(docs, 0, docCount);
}
 
Example 17
Source File: LegacyFieldsIndexReader.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
LegacyFieldsIndexReader(IndexInput fieldsIndexIn, SegmentInfo si) throws IOException {
  maxDoc = si.maxDoc();
  int[] docBases = new int[16];
  long[] startPointers = new long[16];
  int[] avgChunkDocs = new int[16];
  long[] avgChunkSizes = new long[16];
  PackedInts.Reader[] docBasesDeltas = new PackedInts.Reader[16];
  PackedInts.Reader[] startPointersDeltas = new PackedInts.Reader[16];

  final int packedIntsVersion = fieldsIndexIn.readVInt();

  int blockCount = 0;

  for (;;) {
    final int numChunks = fieldsIndexIn.readVInt();
    if (numChunks == 0) {
      break;
    }
    if (blockCount == docBases.length) {
      final int newSize = ArrayUtil.oversize(blockCount + 1, 8);
      docBases = ArrayUtil.growExact(docBases, newSize);
      startPointers = ArrayUtil.growExact(startPointers, newSize);
      avgChunkDocs = ArrayUtil.growExact(avgChunkDocs, newSize);
      avgChunkSizes = ArrayUtil.growExact(avgChunkSizes, newSize);
      docBasesDeltas = ArrayUtil.growExact(docBasesDeltas, newSize);
      startPointersDeltas = ArrayUtil.growExact(startPointersDeltas, newSize);
    }

    // doc bases
    docBases[blockCount] = fieldsIndexIn.readVInt();
    avgChunkDocs[blockCount] = fieldsIndexIn.readVInt();
    final int bitsPerDocBase = fieldsIndexIn.readVInt();
    if (bitsPerDocBase > 32) {
      throw new CorruptIndexException("Corrupted bitsPerDocBase: " + bitsPerDocBase, fieldsIndexIn);
    }
    docBasesDeltas[blockCount] = PackedInts.getReaderNoHeader(fieldsIndexIn, PackedInts.Format.PACKED, packedIntsVersion, numChunks, bitsPerDocBase);

    // start pointers
    startPointers[blockCount] = fieldsIndexIn.readVLong();
    avgChunkSizes[blockCount] = fieldsIndexIn.readVLong();
    final int bitsPerStartPointer = fieldsIndexIn.readVInt();
    if (bitsPerStartPointer > 64) {
      throw new CorruptIndexException("Corrupted bitsPerStartPointer: " + bitsPerStartPointer, fieldsIndexIn);
    }
    startPointersDeltas[blockCount] = PackedInts.getReaderNoHeader(fieldsIndexIn, PackedInts.Format.PACKED, packedIntsVersion, numChunks, bitsPerStartPointer);

    ++blockCount;
  }

  this.docBases = ArrayUtil.copyOfSubArray(docBases, 0, blockCount);
  this.startPointers = ArrayUtil.copyOfSubArray(startPointers, 0, blockCount);
  this.avgChunkDocs = ArrayUtil.copyOfSubArray(avgChunkDocs, 0, blockCount);
  this.avgChunkSizes = ArrayUtil.copyOfSubArray(avgChunkSizes, 0, blockCount);
  this.docBasesDeltas = ArrayUtil.copyOfSubArray(docBasesDeltas, 0, blockCount);
  this.startPointersDeltas = ArrayUtil.copyOfSubArray(startPointersDeltas, 0, blockCount);
}
 
Example 18
Source File: AutomatonTestUtil.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public int[] getRandomAcceptedString(Random r) {
  int[] codePoints = new int[0];
  int codepointCount = 0;

  int s = 0;

  while(true) {
  
    if (a.isAccept(s)) {
      if (a.getNumTransitions(s) == 0) {
        // stop now
        break;
      } else {
        if (r.nextBoolean()) {
          break;
        }
      }
    }

    if (a.getNumTransitions(s) == 0) {
      throw new RuntimeException("this automaton has dead states");
    }

    boolean cheat = r.nextBoolean();

    final Transition t;
    if (cheat) {
      // pick a transition that we know is the fastest
      // path to an accept state
      List<Transition> toAccept = new ArrayList<>();
      for(Transition t0 : transitions[s]) {
        if (leadsToAccept.containsKey(t0)) {
          toAccept.add(t0);
        }
      }
      if (toAccept.size() == 0) {
        // this is OK -- it means we jumped into a cycle
        t = transitions[s][r.nextInt(transitions[s].length)];
      } else {
        t = toAccept.get(r.nextInt(toAccept.size()));
      }
    } else {
      t = transitions[s][r.nextInt(transitions[s].length)];
    }
    codePoints = ArrayUtil.grow(codePoints, codepointCount + 1);
    codePoints[codepointCount++] = getRandomCodePoint(r, t.min, t.max);
    s = t.dest;
  }
  return ArrayUtil.copyOfSubArray(codePoints, 0, codepointCount);
}
 
Example 19
Source File: TestHalfFloatPoint.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testRounding() {
  float[] values = new float[0];
  int o = 0;
  for (int i = Short.MIN_VALUE; i <= Short.MAX_VALUE; ++i) {
    float v = HalfFloatPoint.sortableShortToHalfFloat((short) i);
    if (Float.isFinite(v)) {
      if (o == values.length) {
        values = ArrayUtil.grow(values);
      }
      values[o++] = v;
    }
  }
  values = ArrayUtil.copyOfSubArray(values, 0, o);

  int iters = atLeast(1000000);
  for (int iter = 0; iter < iters; ++iter) {
    float f;
    if (random().nextBoolean()) {
      int floatBits = random().nextInt();
      f = Float.intBitsToFloat(floatBits);
    } else {
      f =  (float) ((2 * random().nextFloat() - 1) * Math.pow(2, TestUtil.nextInt(random(), -16, 16)));
    }
    float rounded = HalfFloatPoint.shortBitsToHalfFloat(HalfFloatPoint.halfFloatToShortBits(f));
    if (Float.isFinite(f) == false) {
      assertEquals(Float.floatToIntBits(f), Float.floatToIntBits(rounded), 0f);
    } else if (Float.isFinite(rounded) == false) {
      assertFalse(Float.isNaN(rounded));
      assertTrue(Math.abs(f) >= 65520);
    } else {
      int index = Arrays.binarySearch(values, f);
      float closest;
      if (index >= 0) {
        closest = values[index];
      } else {
        index = -1 - index;
        closest = Float.POSITIVE_INFINITY;
        if (index < values.length) {
          closest = values[index];
        }
        if (index - 1 >= 0) {
          if (f - values[index - 1] < closest - f) {
            closest = values[index - 1];
          } else if (f - values[index - 1] == closest - f
              && Integer.numberOfTrailingZeros(Float.floatToIntBits(values[index - 1])) > Integer.numberOfTrailingZeros(Float.floatToIntBits(closest))) {
            // in case of tie, round to even
            closest = values[index - 1];
          }
        }
      }
      assertEquals(closest, rounded, 0f);
    }
  }
}
 
Example 20
Source File: SortedIntSet.java    From lucene-solr with Apache License 2.0 2 votes vote down vote up
/**
 * Create a snapshot of this int set associated with a given state. The snapshot will not retain any frequency
 * information about the elements of this set, only existence.
 * <p>
 * It is the caller's responsibility to ensure that the hashCode and data are up to date via the {@link #computeHash()} method before calling this method.
 * @param state the state to associate with the frozen set.
 * @return A new FrozenIntSet with the same values as this set.
 */
FrozenIntSet freeze(int state) {
  final int[] c = ArrayUtil.copyOfSubArray(values, 0, upto);
  return new FrozenIntSet(c, hashCode, state);
}