Skip to content

Commit acc8700

Browse files
idodeclaretarzanek
authored andcommitted
Store TABSIZE with all documents
1 parent f26bd37 commit acc8700

File tree

3 files changed

+75
-11
lines changed

3 files changed

+75
-11
lines changed

src/org/opensolaris/opengrok/analysis/AnalyzerGuru.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
22-
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
22+
* Portions Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opensolaris.opengrok.analysis;
2525

@@ -52,6 +52,7 @@
5252
import org.apache.lucene.document.Field.Store;
5353
import org.apache.lucene.document.FieldType;
5454
import org.apache.lucene.document.SortedDocValuesField;
55+
import org.apache.lucene.document.StoredField;
5556
import org.apache.lucene.document.StringField;
5657
import org.apache.lucene.document.TextField;
5758
import org.apache.lucene.util.BytesRef;
@@ -470,6 +471,9 @@ public void populateDocument(Document doc, File file, String path,
470471
doc.add(npstring);
471472
}
472473

474+
doc.add(new StoredField(QueryBuilder.TABSIZE, project != null &&
475+
project.hasTabSizeSetting() ? project.getTabSize() : 0));
476+
473477
if (fa != null) {
474478
Genre g = fa.getGenre();
475479
if (g == Genre.PLAIN || g == Genre.XREFABLE || g == Genre.HTML) {

src/org/opensolaris/opengrok/index/IndexDatabase.java

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@
6161
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
6262
import org.apache.lucene.index.IndexableField;
6363
import org.apache.lucene.index.MultiFields;
64+
import org.apache.lucene.index.PostingsEnum;
6465
import org.apache.lucene.index.Term;
6566
import org.apache.lucene.index.Terms;
6667
import org.apache.lucene.index.TermsEnum;
6768
import org.apache.lucene.queryparser.classic.ParseException;
69+
import org.apache.lucene.search.DocIdSetIterator;
6870
import org.apache.lucene.search.IndexSearcher;
6971
import org.apache.lucene.search.Query;
7072
import org.apache.lucene.search.TopDocs;
@@ -106,13 +108,17 @@ public class IndexDatabase {
106108
private static final Comparator<File> FILENAME_COMPARATOR =
107109
(File p1, File p2) -> p1.getName().compareTo(p2.getName());
108110

111+
private static final Set<String> CHECK_FIELDS;
112+
109113
private final Object INSTANCE_LOCK = new Object();
110114

111115
private Project project;
112116
private FSDirectory indexDirectory;
117+
private IndexReader reader;
113118
private IndexWriter writer;
114119
private PendingFileCompleter completer;
115120
private TermsEnum uidIter;
121+
private PostingsEnum postsIter;
116122
private IgnoredNames ignoredNames;
117123
private Filter includedNames;
118124
private AnalyzerGuru analyzerGuru;
@@ -155,6 +161,11 @@ public IndexDatabase(Project project) throws IOException {
155161
initialize();
156162
}
157163

164+
static {
165+
CHECK_FIELDS = new HashSet<>();
166+
CHECK_FIELDS.add(QueryBuilder.TABSIZE);
167+
}
168+
158169
/**
159170
* Update the index database for all of the projects. Print progress to
160171
* standard out.
@@ -382,6 +393,11 @@ public void update(IndexerParallelizer parallelizer)
382393
this.parallelizer = parallelizer;
383394
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
384395

396+
reader = null;
397+
writer = null;
398+
uidIter = null;
399+
postsIter = null;
400+
385401
IOException finishingException = null;
386402
try {
387403
Analyzer analyzer = AnalyzerGuru.getAnalyzer();
@@ -422,7 +438,7 @@ public void update(IndexerParallelizer parallelizer)
422438
}
423439

424440
String startuid = Util.path2uid(dir, "");
425-
IndexReader reader = DirectoryReader.open(indexDirectory); // open existing index
441+
reader = DirectoryReader.open(indexDirectory); // open existing index
426442
Terms terms = null;
427443
int numDocs = reader.numDocs();
428444
if (numDocs > 0) {
@@ -976,14 +992,19 @@ private void indexDown(File dir, String parent, IndexDownArgs args)
976992
}
977993
}
978994

979-
// If the file was not modified, skip to the next one.
980-
if (uidIter != null && uidIter.term() != null
981-
&& uidIter.term().bytesEquals(buid)) {
982-
BytesRef next = uidIter.next(); // keep matching docs
983-
if (next == null) {
984-
uidIter = null;
985-
}
986-
continue;
995+
/**
996+
* If the file was not modified, probably skip to the
997+
* next one.
998+
*/
999+
if (uidIter != null && uidIter.term() != null &&
1000+
uidIter.term().bytesEquals(buid)) {
1001+
boolean chkres = chkFields(file, path);
1002+
if (!chkres) removeFile(false);
1003+
1004+
BytesRef next = uidIter.next();
1005+
if (next == null) uidIter = null;
1006+
1007+
if (chkres) continue; // keep matching docs
9871008
}
9881009
}
9891010

@@ -1490,6 +1511,44 @@ private void finishWriting() throws IOException {
14901511
}
14911512
}
14921513

1514+
private boolean chkFields(File file, String path) throws IOException {
1515+
int n = 0;
1516+
postsIter = uidIter.postings(postsIter);
1517+
while (postsIter.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
1518+
++n;
1519+
// Read a limited-fields version of the document.
1520+
Document doc = reader.document(postsIter.docID(), CHECK_FIELDS);
1521+
if (doc == null) {
1522+
LOGGER.log(Level.FINER, "No Document: {0}", path);
1523+
continue;
1524+
}
1525+
1526+
/**
1527+
* Verify TABSIZE, or return a value to indicate mismatch.
1528+
* For an older OpenGrok index that does not yet have TABSIZE,
1529+
* ignore the check so that no extra work is done. After a re-index,
1530+
* the TABSIZE check will be active.
1531+
*/
1532+
int reqTabSize = project != null ? project.hasTabSizeSetting() ?
1533+
project.getTabSize() : 0 : 0;
1534+
IndexableField tbsz = doc.getField(QueryBuilder.TABSIZE);
1535+
int tbszint = tbsz != null ? tbsz.numericValue().intValue(): 0;
1536+
if (tbsz != null && tbszint != reqTabSize) {
1537+
LOGGER.log(Level.FINE, "Tabsize mismatch: {0}", path);
1538+
return false;
1539+
}
1540+
1541+
break;
1542+
}
1543+
if (n < 1) {
1544+
LOGGER.log(Level.FINER, "Missing index Documents: {0}", path);
1545+
return false;
1546+
}
1547+
1548+
// Assume "true" if otherwise no discrepancies were observed.
1549+
return true;
1550+
}
1551+
14931552
private class IndexDownArgs {
14941553
boolean count_only;
14951554
int cur_count;

src/org/opensolaris/opengrok/search/QueryBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/*
2121
* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright 2011 Jens Elkner.
23-
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
23+
* Portions Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
2424
*/
2525
package org.opensolaris.opengrok.search;
2626

@@ -66,6 +66,7 @@ public class QueryBuilder {
6666
public static final String DIRPATH = "dirpath";
6767
public static final String PROJECT = "project";
6868
public static final String DATE = "date";
69+
public static final String TABSIZE = "tabsize";
6970

7071
/** Used for paths, so SHA-1 is completely sufficient */
7172
private static final String DIRPATH_HASH_ALGORITHM = "SHA-1";

0 commit comments

Comments
 (0)