8080public class JBrowseLuceneSearch
8181{
8282 private static final Logger _log = LogHelper .getLogger (JBrowseLuceneSearch .class , "Logger related to JBrowse/Lucene indexing and queries" );
83- private static final ExecutorService SEARCH_EXECUTOR = Executors .newFixedThreadPool (JBrowseServiceImpl .get ().getCoresForLuceneSearches ());
84- private final JBrowseSession _session ;
85- private final JsonFile _jsonFile ;
86- private final User _user ;
87- private final String [] specialStartPatterns = {"*:* -" , "+" , "-" };
8883 private static final String ALL_DOCS = "all" ;
8984 private static final String GENOMIC_POSITION = "genomicPosition" ;
9085 private static final int maxCachedQueries = 1000 ;
9186 private static final long maxRamBytesUsed = 250 * 1024 * 1024L ;
9287
9388 private static final Cache <String , CacheEntry > _cache = new LuceneIndexCache ();
9489
90+ private static ExecutorService _executor = null ;
91+
92+ private final JBrowseSession _session ;
93+ private final JsonFile _jsonFile ;
94+ private final User _user ;
95+ private final String [] specialStartPatterns = {"*:* -" , "+" , "-" };
96+
9597 private JBrowseLuceneSearch (final JBrowseSession session , final JsonFile jsonFile , User u )
9698 {
9799 _session = session ;
98100 _jsonFile = jsonFile ;
99101 _user = u ;
100102 }
101103
104+ private static synchronized ExecutorService getSearchExecutor ()
105+ {
106+ if (_executor == null )
107+ {
108+ _executor = Executors .newFixedThreadPool (JBrowseServiceImpl .get ().getCoresForLuceneSearches ());
109+ }
110+
111+ return _executor ;
112+ }
113+
102114 private Container getContainer ()
103115 {
104116 return ContainerManager .getForId (_session .getContainer ());
@@ -114,7 +126,7 @@ public static JBrowseLuceneSearch create(String sessionId, String trackId, User
114126 private static synchronized CacheEntry getCacheEntryForSession (String trackObjectId , File indexPath ) throws IOException {
115127 CacheEntry cacheEntry = _cache .get (trackObjectId );
116128
117- if (SEARCH_EXECUTOR .isShutdown () || SEARCH_EXECUTOR .isTerminated ())
129+ if (getSearchExecutor () .isShutdown () || getSearchExecutor () .isTerminated ())
118130 {
119131 throw new IllegalStateException ("The server is shutting down!" );
120132 }
@@ -127,7 +139,7 @@ private static synchronized CacheEntry getCacheEntryForSession(String trackObjec
127139 Directory indexDirectory = FSDirectory .open (indexPath .toPath ());
128140 LRUQueryCache queryCache = new LRUQueryCache (maxCachedQueries , maxRamBytesUsed );
129141 IndexReader indexReader = DirectoryReader .open (indexDirectory );
130- IndexSearcher indexSearcher = new IndexSearcher (indexReader , SEARCH_EXECUTOR );
142+ IndexSearcher indexSearcher = new IndexSearcher (indexReader , getSearchExecutor () );
131143 indexSearcher .setQueryCache (queryCache );
132144 indexSearcher .setQueryCachingPolicy (new ForceMatchAllDocsCachingPolicy ());
133145 cacheEntry = new CacheEntry (queryCache , indexSearcher , indexPath );
@@ -697,7 +709,10 @@ public void shutdownStarted()
697709
698710 try
699711 {
700- SEARCH_EXECUTOR .shutdown ();
712+ if (_executor != null )
713+ {
714+ _executor .shutdown ();
715+ }
701716 }
702717 catch (Exception e )
703718 {
@@ -706,15 +721,17 @@ public void shutdownStarted()
706721 }
707722 }
708723
709- private class SearchConfig {
724+ private static class SearchConfig
725+ {
710726 CacheEntry cacheEntry ;
711727 Query query ;
712728 int pageSize ;
713729 int offset ;
714730 Sort sort ;
715731 List <String > fields ;
716732
717- public SearchConfig (CacheEntry cacheEntry , Query query , int pageSize , int offset , Sort sort , List <String > fields ) {
733+ public SearchConfig (CacheEntry cacheEntry , Query query , int pageSize , int offset , Sort sort , List <String > fields )
734+ {
718735 this .cacheEntry = cacheEntry ;
719736 this .query = query ;
720737 this .pageSize = pageSize ;
0 commit comments