Files
gerrit/gerrit-lucene/src/main/java/com/google/gerrit/lucene/AutoCommitWriter.java
Dave Borowitz 70aaf7eaa7 Update Lucene to 5.0.0
Use this version starting at the existing schema version 15 (which has
been in master for only a week or so).

Notable changes:

 - Can use the new IndexWriter#setCommitOnClose(boolean) method to
   simplify closing an index.

 - This means we no longer need to pass Version into the
   IndexWriterConstructor. According to [1], this was _only_ used to
   determine whether or not the index should be committed on close, as
   this behavior differed between versions. No more mapping schema
   versions to Lucene versions!

 - IndexWriters are now forced to use their configured Analyzer,
   removing the methods taking an Analyzer (which we weren't using
   anyway). This saves some code in AutoCommitWriter.

 - Lucene 5 cannot read indexes created by older versions without an
   additional jar in the classpath, so we need to add that.

The most annoying change is that sorting cannot be done on normal
numeric fields by default anymore[2]. This was inefficient anyway, as
Lucene had to seek and read all index field values before doing the
sorting. Switch to the newer DocValues API for strongly-typed sortable
fields. This introduces some medium-term ugliness as the sort spec
changes depending on the schema version.

Unfortunately we can only use DocValues on new index versions; older
versions need to use the new UninvertingReader API, which provides
FieldCache based sorting without a reindex. An overzealous check in a
static method in Lucene[3] means we need to temporarily fork
SearcherManager.java from Lucene in order to get this to work with the
NRT machinery.

Since we have to jump through significant hoops to get older index
versions readable by this version of Lucene, add a test specifically
for schema v14.

[1] https://issues.apache.org/jira/browse/LUCENE-5871
[2] https://issues.apache.org/jira/browse/LUCENE-5666
[3] https://issues.apache.org/jira/browse/LUCENE-6370

Change-Id: I843be2fb697779fc741e25459a2716280b2bd0b6
2015-03-25 12:21:59 -07:00

108 lines
2.8 KiB
Java

// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.lucene;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import java.io.IOException;
/** Writer that optionally flushes/commits after every write. */
class AutoCommitWriter extends IndexWriter {
private boolean autoCommit;
AutoCommitWriter(Directory dir, IndexWriterConfig config, boolean autoCommit)
throws IOException {
super(dir, config);
this.autoCommit = autoCommit;
}
@Override
public void addDocument(Iterable<? extends IndexableField> doc)
throws IOException {
super.addDocument(doc);
autoFlush();
}
@Override
public void addDocuments(
Iterable<? extends Iterable<? extends IndexableField>> docs)
throws IOException {
super.addDocuments(docs);
autoFlush();
}
@Override
public void updateDocuments(Term delTerm,
Iterable<? extends Iterable<? extends IndexableField>> docs)
throws IOException {
super.updateDocuments(delTerm, docs);
autoFlush();
}
@Override
public void deleteDocuments(Term... term) throws IOException {
super.deleteDocuments(term);
autoFlush();
}
@Override
public synchronized boolean tryDeleteDocument(IndexReader readerIn, int docID)
throws IOException {
boolean ret = super.tryDeleteDocument(readerIn, docID);
if (ret) {
autoFlush();
}
return ret;
}
@Override
public void deleteDocuments(Query... queries) throws IOException {
super.deleteDocuments(queries);
autoFlush();
}
@Override
public void updateDocument(Term term, Iterable<? extends IndexableField> doc)
throws IOException {
super.updateDocument(term, doc);
autoFlush();
}
@Override
public void deleteAll() throws IOException {
super.deleteAll();
autoFlush();
}
void manualFlush() throws IOException {
flush(true, true);
if (autoCommit) {
commit();
}
}
private void autoFlush() throws IOException {
if (autoCommit) {
manualFlush();
}
}
}