76
76
import org .apache .lucene .index .TermsEnum ;
77
77
import org .apache .lucene .queryparser .classic .ParseException ;
78
78
import org .apache .lucene .search .DocIdSetIterator ;
79
- import org .apache .lucene .search .IndexSearcher ;
80
79
import org .apache .lucene .search .Query ;
81
80
import org .apache .lucene .search .TopDocs ;
82
81
import org .apache .lucene .store .AlreadyClosedException ;
99
98
import org .opengrok .indexer .configuration .PathAccepter ;
100
99
import org .opengrok .indexer .configuration .Project ;
101
100
import org .opengrok .indexer .configuration .RuntimeEnvironment ;
101
+ import org .opengrok .indexer .configuration .SuperIndexSearcher ;
102
102
import org .opengrok .indexer .history .FileCollector ;
103
103
import org .opengrok .indexer .history .HistoryGuru ;
104
104
import org .opengrok .indexer .history .Repository ;
@@ -1848,45 +1848,11 @@ public int getNumFiles() throws IOException {
1848
1848
}
1849
1849
}
1850
1850
1851
- /**
1852
- * Get an indexReader for the Index database where a given file.
1853
- *
1854
- * @param path the file to get the database for
1855
- * @return The index database where the file should be located or {@code null} if it cannot be located.
1856
- */
1857
- @ SuppressWarnings ("java:S2095" )
1858
- @ Nullable
1859
- public static IndexReader getIndexReader (String path ) {
1860
- IndexReader ret = null ;
1861
-
1862
- RuntimeEnvironment env = RuntimeEnvironment .getInstance ();
1863
- File indexDir = new File (env .getDataRootFile (), INDEX_DIR );
1864
-
1865
- if (env .hasProjects ()) {
1866
- Project p = Project .getProject (path );
1867
- if (p == null ) {
1868
- return null ;
1869
- }
1870
- indexDir = new File (indexDir , p .getPath ());
1871
- }
1872
- try {
1873
- FSDirectory fdir = FSDirectory .open (indexDir .toPath (), NoLockFactory .INSTANCE );
1874
- if (indexDir .exists () && DirectoryReader .indexExists (fdir )) {
1875
- ret = DirectoryReader .open (fdir );
1876
- }
1877
- } catch (Exception ex ) {
1878
- LOGGER .log (Level .SEVERE , "Failed to open index: {0}" , indexDir .getAbsolutePath ());
1879
- LOGGER .log (Level .FINE , "Stack Trace: " , ex );
1880
- }
1881
- return ret ;
1882
- }
1883
-
1884
1851
/**
1885
1852
* Get the latest definitions for a file from the index.
1886
1853
*
1887
1854
* @param file the file whose definitions to find
1888
- * @return definitions for the file, or {@code null} if they could not be
1889
- * found
1855
+ * @return definitions for the file, or {@code null} if they could not be found
1890
1856
* @throws IOException if an error happens when accessing the index
1891
1857
* @throws ParseException if an error happens when building the Lucene query
1892
1858
* @throws ClassNotFoundException if the class for the stored definitions
@@ -1909,11 +1875,13 @@ public static Definitions getDefinitions(File file) throws ParseException, IOExc
1909
1875
1910
1876
/**
1911
1877
* @param file File object for a file under source root
1912
- * @return Document object for the file or {@code null}
1878
+ * @return Document object for the file or {@code null} if no document was found
1913
1879
* @throws IOException on I/O error
1914
1880
* @throws ParseException on problem with building Query
1915
1881
*/
1916
- public static Document getDocument (File file ) throws IOException , ParseException {
1882
+ @ Nullable
1883
+ public static Document getDocument (File file ) throws ParseException , IOException {
1884
+
1917
1885
RuntimeEnvironment env = RuntimeEnvironment .getInstance ();
1918
1886
String path ;
1919
1887
try {
@@ -1925,36 +1893,28 @@ public static Document getDocument(File file) throws IOException, ParseException
1925
1893
// Sanitize Windows path delimiters in order not to conflict with Lucene escape character.
1926
1894
path = path .replace ("\\ " , "/" );
1927
1895
1928
- try (IndexReader indexReader = getIndexReader (path )) {
1929
- return getDocument (path , indexReader );
1930
- }
1931
- }
1932
-
1933
- @ Nullable
1934
- private static Document getDocument (String path , IndexReader indexReader ) throws ParseException , IOException {
1935
- if (indexReader == null ) {
1936
- // No index, no document..
1937
- return null ;
1938
- }
1939
-
1940
1896
Document doc ;
1941
1897
Query q = new QueryBuilder ().setPath (path ).build ();
1942
- IndexSearcher searcher = RuntimeEnvironment .getInstance ().getIndexSearcherFactory ().newSearcher (indexReader );
1943
- Statistics stat = new Statistics ();
1944
- TopDocs top = searcher .search (q , 1 );
1945
- stat .report (LOGGER , Level .FINEST , "search via getDocument() done" ,
1946
- "search.latency" , new String []{"category" , "getdocument" ,
1947
- "outcome" , top .totalHits .value == 0 ? "empty" : "success" });
1948
- if (top .totalHits .value == 0 ) {
1949
- // No hits, no document...
1950
- return null ;
1951
- }
1952
- doc = searcher .doc (top .scoreDocs [0 ].doc );
1953
- String foundPath = doc .get (QueryBuilder .PATH );
1898
+ SuperIndexSearcher searcher = env .getSuperIndexSearcher (file );
1899
+ try {
1900
+ Statistics stat = new Statistics ();
1901
+ TopDocs top = searcher .search (q , 1 );
1902
+ stat .report (LOGGER , Level .FINEST , "search via getDocument() done" ,
1903
+ "search.latency" , new String []{"category" , "getdocument" ,
1904
+ "outcome" , top .totalHits .value == 0 ? "empty" : "success" });
1905
+ if (top .totalHits .value == 0 ) {
1906
+ // No hits, no document...
1907
+ return null ;
1908
+ }
1909
+ doc = searcher .doc (top .scoreDocs [0 ].doc );
1910
+ String foundPath = doc .get (QueryBuilder .PATH );
1954
1911
1955
- // Only use the document if we found an exact match.
1956
- if (!path .equals (foundPath )) {
1957
- return null ;
1912
+ // Only use the document if we found an exact match.
1913
+ if (!path .equals (foundPath )) {
1914
+ return null ;
1915
+ }
1916
+ } finally {
1917
+ searcher .release ();
1958
1918
}
1959
1919
1960
1920
return doc ;
0 commit comments