Index optimization using {@link IndexWriter#optimize()} method.
170316
public void optimize() throws CorruptIndexException, IOException
   {
      for (PersistentIndex index : indexes)
      {
         IndexWriter writer = index.getIndexWriter();
         writer.forceMerge(1, true);
      }
   }
524428
private static int index(Object o) {
        Integer i = TYPE_INDICES.get(o);
        if (i == null) {
            synchronized (TYPE_INDICES) {
                // check that another thread did not already update the index
                i = TYPE_INDICES.get(o);
                if (i != null)
                    return i;
                else {
                    int j = TYPE_INDICES.size();
                    TYPE_INDICES.put(o, j);
                    TYPES.add(o);
                    return j;
                }
            }
        }
        return i;
    }
487818
public boolean optimize( DenseMatrix64F initParam ,
                             DenseMatrix64F X ,
                             DenseMatrix64F Y )
    {
        if (X.numRows == 0)
        {
            return false;
        }
        configure(initParam,X,Y);

        // save the cost of the initial parameters so that it knows if it improves or not
        initialCost = cost(param,X,Y);

        // iterate until the difference between the costs is insignificant
        // or it iterates too many times
        if( !adjustParam(X, Y, initialCost) ) {
            finalCost = Double.NaN;
            return false;
        }

        return true;
    }
960375
protected int optimize( List<Point2D_I32> contour , int c0,int c1,int c2 ) {

		double bestDistance = computeCost(contour,c0,c1,c2,0);
		int bestIndex = 0;
		for( int i = -searchRadius; i <= searchRadius; i++ ) {
			if( i == 0 ) {
				// if it found a better point in the first half stop the search since that's probably the correct
				// direction.  Could be improved by remember past search direction
				if( bestIndex != 0 )
					break;
			} else {
				double found = computeCost(contour, c0, c1, c2, i);
				if (found < bestDistance) {
					bestDistance = found;
					bestIndex = i;
				}
			}
		}
		return CircularIndex.addOffset(c1, bestIndex, contour.size());
	}
1344807
protected int advanceWriteIndex() {
        int num=0, start=write_index;
        for(;;) {
            if(buf[start] == null)
                break;
            num++;
            start=index(start+1);
            if(start == tmp_write_index.get())
                break;
        }
        write_index=start;
        return num;
    }
1145804
private int incrementIndex(int index, int maxIndex) {
		int newIndex = index + 1;
		if (newIndex >= elements.length) {
			newIndex = maxIndex;
		}
		return newIndex;
	}
878418
public DocumentOptimization getDocumentOptimization() {
        Document storage = doc.getDocument(FieldName.STORAGE);
        if (storage == null) {
            storage = Schematic.newDocument();
        }
        return new DocumentOptimization(storage.getDocument(FieldName.DOCUMENT_OPTIMIZATION));
    }
812738
private void moveIndexesToNextCombination()
    {
        for (int i = currentIndexes.length - 1, j = list.size() - 1; i >= 0; i--, j--)
        {
            if (currentIndexes[i] != j)
            {
                currentIndexes[i]++;
                for (int k = i + 1; k < currentIndexes.length; k++)
                {
                    currentIndexes[k] = currentIndexes[k - 1] + 1;
                }
                return;
            }
        }
        // otherwise, we are all done:
        currentIndexes = null;
    }
720964
protected void cleanAndReindex( boolean async ) {
        final IndexWriter writer = getIndexWriter();
        scan(async, getIndexWriter(), new Callable<Void>() {
            @SuppressWarnings( "synthetic-access" )
            @Override
            public Void call() throws Exception {
                writer.clearAllIndexes();
                reindexContent(true, writer);
                return null;
            }
        });
    }
586
protected void reallocateIndexes() {
        if (this.indexes == null || this.indexes.length != getFilteredModel().getSize()) {
            this.indexes = new int[getFilteredModel().getSize()];
        }
        applyConstraint();
    }
799622
@Override
  public MtasSpanQuery rewrite(IndexReader reader) throws IOException {
    MtasSpanQuery newQ1 = (MtasSpanQuery) q1.rewrite(reader);
    MtasSpanQuery newQ2 = (MtasSpanQuery) q2.rewrite(reader);
    if (newQ1 == null || newQ1 instanceof MtasSpanMatchNoneQuery
        || newQ2 == null || newQ2 instanceof MtasSpanMatchNoneQuery) {
      return new MtasSpanMatchNoneQuery(field);
    } else if (!newQ1.equals(q1) || !newQ2.equals(q2)) {
      return new MtasSpanFullyAlignedWithQuery(newQ1, newQ2).rewrite(reader);
    } else if (newQ1.equals(newQ2)) {
      return newQ1;
    } else {
      boolean returnNone;
      returnNone = newQ1.getMaximumWidth() != null
          && newQ1.getMaximumWidth() == 0;
      returnNone |= newQ2.getMaximumWidth() != null
          && newQ2.getMaximumWidth() == 0;
      returnNone |= newQ1.getMinimumWidth() != null
          && newQ2.getMaximumWidth() != null
          && newQ1.getMinimumWidth() > newQ2.getMaximumWidth();
      returnNone |= newQ2.getMinimumWidth() != null
          && newQ1.getMaximumWidth() != null
          && newQ2.getMinimumWidth() > newQ1.getMaximumWidth();
      returnNone |= newQ2.getMinimumWidth() != null
          && newQ1.getMaximumWidth() != null
          && newQ2.getMinimumWidth() > newQ1.getMaximumWidth();
      if (returnNone) {
        return new MtasSpanMatchNoneQuery(this.getField());
      } else {
        return super.rewrite(reader);
      }
    }
  }
996595
public function optimize()
    {
        // Commit changes if any changes have been made
        $this->commit();

        if (count($this->_segmentInfos) > 1 || $this->hasDeletions()) {
            $this->_getIndexWriter()->optimize();
            $this->_updateDocCount();
        }
    }
1265453
@Override
  public MtasSpanQuery rewrite(IndexReader reader) throws IOException {
    MtasSpanQuery newQuery = query.rewrite(reader);
    if (maximumLeft == 0 && maximumRight == 0) {
      return newQuery;
    } else if (!query.equals(newQuery)) {
      return new MtasMaximumExpandSpanQuery(newQuery, minimumLeft, maximumLeft,
          minimumRight, maximumRight);
    } else {
      return super.rewrite(reader);
    }
  }
64639
void copyIndex(final AbstractIndex index) throws IOException
   {
      // commit changes to directory on other index.
      index.commit(true);
      // simply copy over the files
      byte[] buffer = new byte[1024];
      Directory dir = index.getDirectory();
      Directory dest = getDirectory();
      String[] files = dir.listAll();
      for (int i = 0; i < files.length; i++)
      {
         IndexInput in = dir.openInput(files[i]);
         try
         {
            IndexOutput out = dest.createOutput(files[i]);
            try
            {
               long remaining = in.length();
               while (remaining > 0)
               {
                  int num = (int)Math.min(remaining, buffer.length);
                  in.readBytes(buffer, 0, num);
                  out.writeBytes(buffer, num);
                  remaining -= num;
               }
            }
            finally
            {
               out.close();
            }
         }
         finally
         {
            in.close();
         }
      }
   }
430311
public void setIndexStats(Map<String, LocalIndexStatsImpl> indexStats) {
        this.mutableIndexStats.clear();
        if (indexStats != null) {
            this.mutableIndexStats.putAll(indexStats);
        }
    }
30626
public synchronized static Index createIndex(Class<?> type) {
        Index index = indices.get(type);
        if (index == null) {
            try {
                Indexer indexer = new Indexer();
                Class<?> currentType = type;
                while ( currentType != null ) {
                    String className = currentType.getName().replace(".", "/") + ".class";
                    InputStream stream = type.getClassLoader()
                            .getResourceAsStream(className);
                    indexer.index(stream);
                    currentType = currentType.getSuperclass();
                }
                index = indexer.complete();
                indices.put(type, index);
            } catch (IOException e) {
                throw new RuntimeException("Failed to initialize Indexer", e);
            }
        }
        return index;
    }
988928
void initResultIndex() {

        int startIndex = 0;
        if ((m_pageIndex > 0) && (m_pageSize > 0)) {
            startIndex = (m_pageIndex - 1) * m_pageSize;
        }

        m_resultIndex = startIndex > m_resultSize ? m_resultSize : startIndex;
    }