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

The following examples show how to use org.apache.lucene.util.ArrayUtil#grow() . 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: SortedSetDocValuesWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private long[][] sortDocValues(int maxDoc, Sorter.DocMap sortMap, SortedSetDocValues oldValues) throws IOException {
  long[][] ords = new long[maxDoc][];
  int docID;
  while ((docID = oldValues.nextDoc()) != NO_MORE_DOCS) {
    int newDocID = sortMap.oldToNew(docID);
    long[] docOrds = new long[1];
    int upto = 0;
    while (true) {
      long ord = oldValues.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);
  }
  return ords;
}
 
Example 2
Source File: BinaryDictionaryWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
void addMapping(int sourceId, int wordId) {
  if (wordId <= lastWordId) {
    throw new IllegalStateException("words out of order: " + wordId + " vs lastID: " + lastWordId);
  }

  if (sourceId > lastSourceId) {
    targetMapOffsets = ArrayUtil.grow(targetMapOffsets, sourceId + 1);
    for (int i = lastSourceId + 1; i <= sourceId; i++) {
      targetMapOffsets[i] = targetMapEndOffset;
    }
  } else if (sourceId != lastSourceId) {
    throw new IllegalStateException("source ids not in increasing order: lastSourceId=" + lastSourceId + " vs sourceId=" + sourceId);
  }

  targetMap = ArrayUtil.grow(targetMap, targetMapEndOffset + 1);
  targetMap[targetMapEndOffset] = wordId;
  targetMapEndOffset++;

  lastSourceId = sourceId;
  lastWordId = wordId;
}
 
Example 3
Source File: Stats.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
void startBlock(SegmentTermsEnumFrame frame, boolean isFloor) {
  totalBlockCount++;
  if (isFloor) {
    if (frame.fp == frame.fpOrig) {
      floorBlockCount++;
    }
    floorSubBlockCount++;
  } else {
    nonFloorBlockCount++;
  }

  if (blockCountByPrefixLen.length <= frame.prefix) {
    blockCountByPrefixLen = ArrayUtil.grow(blockCountByPrefixLen, 1+frame.prefix);
  }
  blockCountByPrefixLen[frame.prefix]++;
  startBlockCount++;
  totalBlockSuffixBytes += frame.totalSuffixBytes;
  totalUncompressedBlockSuffixBytes += frame.suffixesReader.length();
  if (frame.suffixesReader != frame.suffixLengthsReader) {
    totalUncompressedBlockSuffixBytes += frame.suffixLengthsReader.length();
  }
  totalBlockStatsBytes += frame.statsReader.length();
  compressionAlgorithms[frame.compressionAlg.code]++;
}
 
Example 4
Source File: TermsWithScoreCollector.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void collect(int doc) throws IOException {
  if (docValues.advanceExact(doc)) {
    long ord;
    while ((ord = docValues.nextOrd()) != SortedSetDocValues.NO_MORE_ORDS) {
      int termID = collectedTerms.add(docValues.lookupOrd(ord));
      if (termID < 0) {
        termID = -termID - 1;
      } else {
        if (termID >= scoreSums.length) {
          scoreSums = ArrayUtil.grow(scoreSums);
          scoreCounts = ArrayUtil.grow(scoreCounts);
        }
      }
    
      scoreSums[termID] += scorer.score();
      scoreCounts[termID]++;
    }
  }
}
 
Example 5
Source File: BufferedInputIterator.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Creates a new iterator, buffering entries from the specified iterator */
public BufferedInputIterator(InputIterator source) throws IOException {
  BytesRef spare;
  int freqIndex = 0;
  hasPayloads = source.hasPayloads();
  hasContexts = source.hasContexts();
  while((spare = source.next()) != null) {
    entries.append(spare);
    if (hasPayloads) {
      payloads.append(source.payload());
    }
    if (hasContexts) {
      contextSets.add(source.contexts());
    }
    if (freqIndex >= freqs.length) {
      freqs = ArrayUtil.grow(freqs, freqs.length+1);
    }
    freqs[freqIndex++] = source.weight();
  }
 
}
 
Example 6
Source File: TermsWithScoreCollector.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void collect(int doc) throws IOException {
  if (docValues.advanceExact(doc)) {
    long ord;
    while ((ord = docValues.nextOrd()) != SortedSetDocValues.NO_MORE_ORDS) {
      int termID = collectedTerms.add(docValues.lookupOrd(ord));
      if (termID < 0) {
        termID = -termID - 1;
      } else {
        if (termID >= scoreSums.length) {
          int begin = scoreSums.length;
          scoreSums = ArrayUtil.grow(scoreSums);
          if (scoreMode == ScoreMode.Min) {
            Arrays.fill(scoreSums, begin, scoreSums.length, Float.POSITIVE_INFINITY);
          } else if (scoreMode == ScoreMode.Max) {
            Arrays.fill(scoreSums, begin, scoreSums.length, Float.NEGATIVE_INFINITY);
          }
        }
      }
    
      switch (scoreMode) {
      case Total:
        scoreSums[termID] += scorer.score();
        break;
      case Min:
        scoreSums[termID] = Math.min(scoreSums[termID], scorer.score());
        break;
      case Max:
        scoreSums[termID] = Math.max(scoreSums[termID], scorer.score());
        break;
      default:
        throw new AssertionError("unexpected: " + scoreMode);
      }
    }
  }
}
 
Example 7
Source File: WFSTCompletionLookup.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected void encode(ByteSequencesWriter writer, ByteArrayDataOutput output, byte[] buffer, BytesRef spare, BytesRef payload, Set<BytesRef> contexts, long weight) throws IOException {
  if (spare.length + 4 >= buffer.length) {
    buffer = ArrayUtil.grow(buffer, spare.length + 4);
  }
  output.reset(buffer);
  output.writeBytes(spare.bytes, spare.offset, spare.length);
  output.writeInt(encodeWeight(weight));
  writer.write(buffer, 0, output.getPosition());
}
 
Example 8
Source File: Automaton.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Copies over all states/transitions from other.  The states numbers
 *  are sequentially assigned (appended). */
public void copy(Automaton other) {

  // Bulk copy and then fixup the state pointers:
  int stateOffset = getNumStates();
  states = ArrayUtil.grow(states, nextState + other.nextState);
  System.arraycopy(other.states, 0, states, nextState, other.nextState);
  for(int i=0;i<other.nextState;i += 2) {
    if (states[nextState+i] != -1) {
      states[nextState+i] += nextTransition;
    }
  }
  nextState += other.nextState;
  int otherNumStates = other.getNumStates();
  BitSet otherAcceptStates = other.getAcceptStates();
  int state = 0;
  while (state < otherNumStates && (state = otherAcceptStates.nextSetBit(state)) != -1) {
    setAccept(stateOffset + state, true);
    state++;
  }

  // Bulk copy and then fixup dest for each transition:
  transitions = ArrayUtil.grow(transitions, nextTransition + other.nextTransition);
  System.arraycopy(other.transitions, 0, transitions, nextTransition, other.nextTransition);
  for(int i=0;i<other.nextTransition;i += 3) {
    transitions[nextTransition+i] += stateOffset;
  }
  nextTransition += other.nextTransition;

  if (other.deterministic == false) {
    deterministic = false;
  }
}
 
Example 9
Source File: BlockReader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected BytesRef decodeBlockBytesIfNeeded(int numBlockBytes) throws IOException {
  scratchBlockBytes.bytes = ArrayUtil.grow(scratchBlockBytes.bytes, numBlockBytes);
  blockInput.readBytes(scratchBlockBytes.bytes, 0, numBlockBytes);
  scratchBlockBytes.length = numBlockBytes;
  if (blockDecoder == null) {
    return scratchBlockBytes;
  }
  blockReadBuffer.reset(scratchBlockBytes.bytes, 0, numBlockBytes);
  return blockDecoder.decode(blockReadBuffer, numBlockBytes);
}
 
Example 10
Source File: PorterStemmer.java    From hanlp-lucene-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Add a character to the word being stemmed. When you are finished adding
 * characters, you can call stem(void) to process the word.
 */
public void add(char ch)
{
    if (b.length <= i)
    {
        b = ArrayUtil.grow(b, i + 1);
    }
    b[i++] = ch;
}
 
Example 11
Source File: PorterStemmer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Add a character to the word being stemmed.  When you are finished
 * adding characters, you can call stem(void) to process the word.
 */
public void add(char ch) {
  if (b.length <= i) {
    b = ArrayUtil.grow(b, i+1);
  }
  b[i++] = ch;
}
 
Example 12
Source File: Lucene80DocValuesConsumer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private long writeValuesMultipleBlocks(SortedNumericDocValues values, long gcd) throws IOException {
  long[] offsets = new long[ArrayUtil.oversize(1, Long.BYTES)];
  int offsetsIndex = 0;
  final long[] buffer = new long[NUMERIC_BLOCK_SIZE];
  final ByteBuffersDataOutput encodeBuffer = ByteBuffersDataOutput.newResettableInstance();
  int upTo = 0;
  for (int doc = values.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = values.nextDoc()) {
    for (int i = 0, count = values.docValueCount(); i < count; ++i) {
      buffer[upTo++] = values.nextValue();
      if (upTo == NUMERIC_BLOCK_SIZE) {
        offsets = ArrayUtil.grow(offsets, offsetsIndex+1);
        offsets[offsetsIndex++] = data.getFilePointer();
        writeBlock(buffer, NUMERIC_BLOCK_SIZE, gcd, encodeBuffer);
        upTo = 0;
      }
    }
  }
  if (upTo > 0) {
    offsets = ArrayUtil.grow(offsets, offsetsIndex+1);
    offsets[offsetsIndex++] = data.getFilePointer();
    writeBlock(buffer, upTo, gcd, encodeBuffer);
  }

  // All blocks has been written. Flush the offset jump-table
  final long offsetsOrigo = data.getFilePointer();
  for (int i = 0 ; i < offsetsIndex ; i++) {
    data.writeLong(offsets[i]);
  }
  data.writeLong(offsetsOrigo);
  return offsetsOrigo;
}
 
Example 13
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 14
Source File: MaxScoreCache.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void ensureCacheSize(int size) {
  if (maxScoreCache.length < size) {
    int oldLength = maxScoreCache.length;
    maxScoreCache = ArrayUtil.grow(maxScoreCache, size);
    maxScoreCacheUpTo = ArrayUtil.growExact(maxScoreCacheUpTo, maxScoreCache.length);
    Arrays.fill(maxScoreCacheUpTo, oldLength, maxScoreCacheUpTo.length, -1);
  }
}
 
Example 15
Source File: MemoryIndex.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void storeDocValues(Info info, DocValuesType docValuesType, Object docValuesValue) {
  String fieldName = info.fieldInfo.name;
  DocValuesType existingDocValuesType = info.fieldInfo.getDocValuesType();
  if (existingDocValuesType == DocValuesType.NONE) {
    // first time we add doc values for this field:
    info.fieldInfo = new FieldInfo(
        info.fieldInfo.name, info.fieldInfo.number, info.fieldInfo.hasVectors(), info.fieldInfo.hasPayloads(),
        info.fieldInfo.hasPayloads(), info.fieldInfo.getIndexOptions(), docValuesType, -1, info.fieldInfo.attributes(),
        info.fieldInfo.getPointDimensionCount(), info.fieldInfo.getPointIndexDimensionCount(), info.fieldInfo.getPointNumBytes(),
        info.fieldInfo.isSoftDeletesField()
    );
  } else if (existingDocValuesType != docValuesType) {
    throw new IllegalArgumentException("Can't add [" + docValuesType + "] doc values field [" + fieldName + "], because [" + existingDocValuesType + "] doc values field already exists");
  }
  switch (docValuesType) {
    case NUMERIC:
      if (info.numericProducer.dvLongValues != null) {
        throw new IllegalArgumentException("Only one value per field allowed for [" + docValuesType + "] doc values field [" + fieldName + "]");
      }
      info.numericProducer.dvLongValues = new long[]{(long) docValuesValue};
      info.numericProducer.count++;
      break;
    case SORTED_NUMERIC:
      if (info.numericProducer.dvLongValues == null) {
        info.numericProducer.dvLongValues = new long[4];
      }
      info.numericProducer.dvLongValues = ArrayUtil.grow(info.numericProducer.dvLongValues, info.numericProducer.count + 1);
      info.numericProducer.dvLongValues[info.numericProducer.count++] = (long) docValuesValue;
      break;
    case BINARY:
      if (info.binaryProducer.dvBytesValuesSet != null) {
        throw new IllegalArgumentException("Only one value per field allowed for [" + docValuesType + "] doc values field [" + fieldName + "]");
      }
      info.binaryProducer.dvBytesValuesSet = new BytesRefHash(byteBlockPool);
      info.binaryProducer.dvBytesValuesSet.add((BytesRef) docValuesValue);
      break;
    case SORTED:
      if (info.binaryProducer.dvBytesValuesSet != null) {
        throw new IllegalArgumentException("Only one value per field allowed for [" + docValuesType + "] doc values field [" + fieldName + "]");
      }
      info.binaryProducer.dvBytesValuesSet = new BytesRefHash(byteBlockPool);
      info.binaryProducer.dvBytesValuesSet.add((BytesRef) docValuesValue);
      break;
    case SORTED_SET:
      if (info.binaryProducer.dvBytesValuesSet == null) {
        info.binaryProducer.dvBytesValuesSet = new BytesRefHash(byteBlockPool);
      }
      info.binaryProducer.dvBytesValuesSet.add((BytesRef) docValuesValue);
      break;
    default:
      throw new UnsupportedOperationException("unknown doc values type [" + docValuesType + "]");
  }
}
 
Example 16
Source File: DirectPostingsFormat.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void add(int value) {
  if (ints.length == upto) {
    ints = ArrayUtil.grow(ints);
  }
  ints[upto++] = value;
}
 
Example 17
Source File: CJKBigramFilter.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * refills buffers with new data from the current token.
 */
private void refill() {
  // compact buffers to keep them smallish if they become large
  // just a safety check, but technically we only need the last codepoint
  if (bufferLen > 64) {
    int last = bufferLen - 1;
    buffer[0] = buffer[last];
    startOffset[0] = startOffset[last];
    endOffset[0] = endOffset[last];
    bufferLen = 1;
    index -= last;
  }

  char termBuffer[] = termAtt.buffer();
  int len = termAtt.length();
  int start = offsetAtt.startOffset();
  int end = offsetAtt.endOffset();
  
  int newSize = bufferLen + len;
  buffer = ArrayUtil.grow(buffer, newSize);
  startOffset = ArrayUtil.grow(startOffset, newSize);
  endOffset = ArrayUtil.grow(endOffset, newSize);
  lastEndOffset = end;

  if (end - start != len) {
    // crazy offsets (modified by synonym or charfilter): just preserve
    for (int i = 0, cp = 0; i < len; i += Character.charCount(cp)) {
      cp = buffer[bufferLen] = Character.codePointAt(termBuffer, i, len);
      startOffset[bufferLen] = start;
      endOffset[bufferLen] = end;
      bufferLen++;
    }
  } else {
    // normal offsets
    for (int i = 0, cp = 0, cpLen = 0; i < len; i += cpLen) {
      cp = buffer[bufferLen] = Character.codePointAt(termBuffer, i, len);
      cpLen = Character.charCount(cp);
      startOffset[bufferLen] = start;
      start = endOffset[bufferLen] = start + cpLen;
      bufferLen++;
    }
  }
}
 
Example 18
Source File: Automaton.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void growTransitions() {
  if (nextTransition+3 > transitions.length) {
    transitions = ArrayUtil.grow(transitions, nextTransition+3);
  }
}
 
Example 19
Source File: TaskSequence.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private int doSerialTasksWithRate() throws Exception {
  initTasksArray();
  long delayStep = (perMin ? 60000 : 1000) /rate;
  long nextStartTime = System.currentTimeMillis();
  int count = 0;
  final long t0 = System.currentTimeMillis();
  for (int k=0; (repetitions==REPEAT_EXHAUST && !exhausted) || k<repetitions; k++) {
    if (stopNow) {
      break;
    }
    for (int l=0;l<tasksArray.length;l++) {
      final PerfTask task = tasksArray[l];
      while(!stopNow) {
        long waitMore = nextStartTime - System.currentTimeMillis();
        if (waitMore > 0) {
          // TODO: better to use condition to notify
          Thread.sleep(1);
        } else {
          break;
        }
      }
      if (stopNow) {
        break;
      }
      nextStartTime += delayStep; // this aims at avarage rate. 
      try {
        final int inc = task.runAndMaybeStats(letChildReport);
        count += inc;
        if (countsByTime != null) {
          final int slot = (int) ((System.currentTimeMillis()-t0)/logByTimeMsec);
          if (slot >= countsByTime.length) {
            countsByTime = ArrayUtil.grow(countsByTime, 1+slot);
          }
          countsByTime[slot] += inc;
        }

        if (anyExhaustibleTasks)
          updateExhausted(task);
      } catch (NoMoreDataException e) {
        exhausted = true;
      }
    }
  }
  stopNow = false;
  return count;
}
 
Example 20
Source File: GraphTokenStreamFiniteStrings.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Build an automaton from the provided {@link TokenStream}.
 */
private Automaton build(final TokenStream in) throws IOException {
  Automaton.Builder builder = new Automaton.Builder();

  final PositionIncrementAttribute posIncAtt = in.addAttribute(PositionIncrementAttribute.class);
  final PositionLengthAttribute posLengthAtt = in.addAttribute(PositionLengthAttribute.class);

  in.reset();

  int pos = -1;
  int prevIncr = 1;
  int state = -1;
  int id = -1;
  int gap = 0;
  while (in.incrementToken()) {
    int currentIncr = posIncAtt.getPositionIncrement();
    if (pos == -1 && currentIncr < 1) {
      throw new IllegalStateException("Malformed TokenStream, start token can't have increment less than 1");
    }

    if (currentIncr == 0) {
      if (gap > 0) {
        pos -= gap;
      }
    }
    else {
      pos++;
      gap = currentIncr - 1;
    }

    int endPos = pos + posLengthAtt.getPositionLength() + gap;
    while (state < endPos) {
      state = builder.createState();
    }

    id++;
    if (tokens.length < id + 1) {
      tokens = ArrayUtil.grow(tokens, id + 1);
    }

    tokens[id] = in.cloneAttributes();
    builder.addTransition(pos, endPos, id);
    pos += gap;

    // we always produce linear token graphs from getFiniteStrings(), so we need to adjust
    // posLength and posIncrement accordingly
    tokens[id].addAttribute(PositionLengthAttribute.class).setPositionLength(1);
    if (currentIncr == 0) {
      // stacked token should have the same increment as original token at this position
      tokens[id].addAttribute(PositionIncrementAttribute.class).setPositionIncrement(prevIncr);
    }

    // only save last increment on non-zero increment in case we have multiple stacked tokens
    if (currentIncr > 0) {
      prevIncr = currentIncr;
    }
  }

  in.end();
  if (state != -1) {
    builder.setAccept(state, true);
  }
  return builder.finish();
}