From 7e707748a5d500c34b2d6350c0093c05c3d2775c Mon Sep 17 00:00:00 2001
From: Mat Booth <mat.booth@redhat.com>
Date: Mon, 8 Aug 2016 19:48:08 +0100
Subject: [PATCH 2/2] Update to lucene 5

---
 h2/pom.xml                                      | 13 ++++++++++++-
 h2/src/main/META-INF/MANIFEST.MF                | 16 ++++++++--------
 h2/src/main/org/h2/fulltext/FullTextLucene.java | 18 +++++++++---------
 h2/src/tools/org/h2/build/Build.java            |  8 ++++++--
 4 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/h2/src/main/META-INF/MANIFEST.MF b/h2/src/main/META-INF/MANIFEST.MF
index 9f219d1..49dc47d 100644
--- a/h2/src/main/META-INF/MANIFEST.MF
+++ b/h2/src/main/META-INF/MANIFEST.MF
@@ -23,14 +23,14 @@ Import-Package: javax.management,
  javax.sql,
  javax.tools;resolution:=optional,
  javax.transaction.xa;resolution:=optional,
- org.apache.lucene.analysis;version="[3.6.2,4.0.0)";resolution:=optional,
- org.apache.lucene.analysis.standard;version="[3.6.2,4.0.0)";resolution:=optional,
- org.apache.lucene.document;version="[3.6.2,4.0.0)";resolution:=optional,
- org.apache.lucene.index;version="[3.6.2,4.0.0)";resolution:=optional,
- org.apache.lucene.queryParser;version="[3.6.2,4.0.0)";resolution:=optional,
- org.apache.lucene.search;version="[3.6.2,4.0.0)";resolution:=optional,
- org.apache.lucene.store;version="[3.6.2,4.0.0)";resolution:=optional,
- org.apache.lucene.util;version="[3.6.2,4.0.0)";resolution:=optional,
+ org.apache.lucene.analysis;version="[6.1.0,8.0.0)";resolution:=optional,
+ org.apache.lucene.analysis.standard;version="[6.1.0,8.0.0)";resolution:=optional,
+ org.apache.lucene.document;version="[6.1.0,8.0.0)";resolution:=optional,
+ org.apache.lucene.index;version="[6.1.0,8.0.0)";resolution:=optional,
+ org.apache.lucene.queryParser;version="[6.1.0,8.0.0)";resolution:=optional,
+ org.apache.lucene.search;version="[6.1.0,8.0.0)";resolution:=optional,
+ org.apache.lucene.store;version="[6.1.0,8.0.0)";resolution:=optional,
+ org.apache.lucene.util;version="[6.1.0,8.0.0)";resolution:=optional,
  com.vividsolutions.jts.geom;version="1.14.0";resolution:=optional,
  com.vividsolutions.jts.io;version="1.14.0";resolution:=optional,
  org.h2;version="[${version},1.5.0)",
diff --git a/h2/src/main/org/h2/fulltext/FullTextLucene.java b/h2/src/main/org/h2/fulltext/FullTextLucene.java
index f24bcab..d9c5b92 100644
--- a/h2/src/main/org/h2/fulltext/FullTextLucene.java
+++ b/h2/src/main/org/h2/fulltext/FullTextLucene.java
@@ -19,10 +19,13 @@
 import org.apache.lucene.document.DateTools;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.Field;
+import org.apache.lucene.document.StringField;
+import org.apache.lucene.document.TextField;
+import org.apache.lucene.index.DirectoryReader;
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.index.IndexWriterConfig;
 import org.apache.lucene.index.Term;
-import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.queryparser.classic.QueryParser;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
 import org.h2.api.Trigger;
@@ -303,13 +306,14 @@
             if (access == null) {
                 try {
                     Directory indexDir = path.startsWith(IN_MEMORY_PREFIX) ?
-                            new RAMDirectory() : FSDirectory.open(new File(path));
-                    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
-                    IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_30, analyzer);
+                            new RAMDirectory() : FSDirectory.open(new File(path).toPath());
+                    Analyzer analyzer = new StandardAnalyzer();
+                    analyzer.setVersion(Version.LUCENE_6_1_0);
+                    IndexWriterConfig conf = new IndexWriterConfig(analyzer);
                     conf.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
                     IndexWriter writer = new IndexWriter(indexDir, conf);
                     //see http://wiki.apache.org/lucene-java/NearRealtimeSearch
-                    IndexReader reader = IndexReader.open(writer, true);
+                    IndexReader reader = DirectoryReader.open(writer, true, true);
                     access = new IndexAccess();
                     access.writer = writer;
                     access.reader = reader;
@@ -394,7 +398,6 @@
         synchronized (INDEX_ACCESS) {
             try {
                 INDEX_ACCESS.remove(indexPath);
-                access.searcher.close();
                 access.reader.close();
                 access.writer.close();
             } catch (Exception e) {
@@ -430,7 +433,7 @@
             // reuse the same analyzer; it's thread-safe;
             // also allows subclasses to control the analyzer used.
             Analyzer analyzer = access.writer.getAnalyzer();
-            QueryParser parser = new QueryParser(Version.LUCENE_30,
+            QueryParser parser = new QueryParser(
                     LUCENE_FIELD_DATA, analyzer);
             Query query = parser.parse(text);
             // Lucene 3 insists on a hard limit and will not provide
@@ -440,10 +443,10 @@
             int maxResults = (limit == 0 ? 100 : limit) + offset;
             TopDocs docs = searcher.search(query, maxResults);
             if (limit == 0) {
-                limit = docs.totalHits;
+                limit = (int)docs.totalHits;
             }
             for (int i = 0, len = docs.scoreDocs.length;
-                    i < limit && i + offset < docs.totalHits
+                    i < limit && i + offset < (int)docs.totalHits
                     && i + offset < len; i++) {
                 ScoreDoc sd = docs.scoreDocs[i + offset];
                 Document doc = searcher.doc(sd.doc);
@@ -604,9 +607,8 @@
             try {
                 indexAccess.writer.commit();
                 // recreate Searcher with the IndexWriter's reader.
-                indexAccess.searcher.close();
                 indexAccess.reader.close();
-                indexAccess.reader = IndexReader.open(indexAccess.writer, true);
+                indexAccess.reader = DirectoryReader.open(indexAccess.writer, true, true);
                 indexAccess.searcher = new IndexSearcher(indexAccess.reader);
             } catch (IOException e) {
                 throw convertException(e);
@@ -622,12 +624,12 @@
         protected void insert(Object[] row, boolean commitIndex) throws SQLException {
             String query = getQuery(row);
             Document doc = new Document();
-            doc.add(new Field(LUCENE_FIELD_QUERY, query,
-                    Field.Store.YES, Field.Index.NOT_ANALYZED));
+            doc.add(new StringField(LUCENE_FIELD_QUERY, query,
+                    Field.Store.YES));
             long time = System.currentTimeMillis();
-            doc.add(new Field(LUCENE_FIELD_MODIFIED,
+            doc.add(new StringField(LUCENE_FIELD_MODIFIED,
                     DateTools.timeToString(time, DateTools.Resolution.SECOND),
-                    Field.Store.YES, Field.Index.NOT_ANALYZED));
+                    Field.Store.YES));
             StatementBuilder buff = new StatementBuilder();
             for (int index : indexColumns) {
                 String columnName = columns[index];
@@ -638,15 +640,14 @@
                 if (columnName.startsWith(LUCENE_FIELD_COLUMN_PREFIX)) {
                     columnName = LUCENE_FIELD_COLUMN_PREFIX + columnName;
                 }
-                doc.add(new Field(columnName, data,
-                        Field.Store.NO, Field.Index.ANALYZED));
+                doc.add(new TextField(columnName, data,
+                        Field.Store.NO));
                 buff.appendExceptFirst(" ");
                 buff.append(data);
             }
             Field.Store storeText = STORE_DOCUMENT_TEXT_IN_INDEX ?
                     Field.Store.YES : Field.Store.NO;
-            doc.add(new Field(LUCENE_FIELD_DATA, buff.toString(), storeText,
-                    Field.Index.ANALYZED));
+            doc.add(new TextField(LUCENE_FIELD_DATA, buff.toString(), storeText));
             try {
                 indexAccess.writer.addDocument(doc);
                 if (commitIndex) {
diff --git a/h2/src/tools/org/h2/build/Build.java b/h2/src/tools/org/h2/build/Build.java
index a599656..24d97c6 100644
--- a/h2/src/tools/org/h2/build/Build.java
+++ b/h2/src/tools/org/h2/build/Build.java
@@ -204,7 +204,9 @@ public class Build extends BuildBase {
         download();
         String classpath = "temp" +
                 File.pathSeparator + "ext/servlet-api-3.1.0.jar" +
-                File.pathSeparator + "ext/lucene-core-3.6.2.jar" +
+                File.pathSeparator + "ext/lucene-core-6.1.0.jar" +
+                File.pathSeparator + "ext/lucene-analyzers-common-6.1.0.jar" +
+                File.pathSeparator + "ext/lucene-queryparser-6.1.0.jar" +
                 File.pathSeparator + "ext/slf4j-api-1.6.0.jar" +
                 File.pathSeparator + "ext/org.osgi.core-4.2.0.jar" +
                 File.pathSeparator + "ext/org.osgi.enterprise-4.2.0.jar" +
@@ -595,7 +597,9 @@ public class Build extends BuildBase {
         javadoc("-sourcepath", "src/main", "org.h2.jdbc", "org.h2.jdbcx",
                 "org.h2.tools", "org.h2.api", "org.h2.engine", "org.h2.fulltext",
                 "-classpath",
-                "ext/lucene-core-3.6.2.jar" +
+                "ext/lucene-core-6.1.0.jar" + File.pathSeparator +
+                "ext/lucene-analyzers-common-6.1.0.jar" + File.pathSeparator +
+                "ext/lucene-queryparser-6.1.0.jar" +
                 File.pathSeparator + "ext/jts-core-1.14.0.jar",
                 "-docletpath", "bin" + File.pathSeparator + "temp",
                 "-doclet", "org.h2.build.doclet.Doclet");
-- 
2.5.5
