           (log-close-err (.getDirectory taxo) e)
           (DirectoryTaxonomyWriter/unlock (.getDirectory taxo)))))))
 
-(defn try-close-manager-taxo [^SearcherManager manager ^DirectoryTaxonomyReader taxo]
-  (try
-    (do
-      (try
-        (.close taxo)
-        (catch Exception e
-          (log/warn (as-str e))))
-      (.close manager))
+(defn try-close-manager [^SearcherTaxonomyManager manager]
+  (try
+    (.close manager)
     (catch Exception e
       (log/warn (as-str e)))))
 

  (try
    (doseq [dir (sub-directories)]
      (try
        (get-smanager-taxo (.getName ^File dir) false)
        (catch Exception e
          (log/warn (ex-str e)))))
    (catch Exception e
      (log/warn (ex-str e)))))

(defn get-smanager-taxo [index refresh]
  (locking name->smanager-taxo*
    (when (nil? (@name->smanager-taxo* index))
      (do
        (swap! name->smanager-taxo* assoc index [(new-searcher-manager index)
                                                 (try-new-taxo-reader index)])
        ;; to avoid a race in case multiple threads
        ;; decide to use the index in the very beginning
        ;; before the statistic keys are ready, so we just create them under the lock here
        ;; not doing the same for writers, because of the exclusive write lock there
        (stat/get-statistics index)))
    (let [[manager taxo] (@name->smanager-taxo* index)]
      (when refresh
        (refresh-searcher-locked index manager taxo))
      [manager taxo])))

(defn refresh-searcher-locked [index ^SearcherManager manager ^DirectoryTaxonomyReader taxo]
  (log/debug "refreshing: " index " " manager " " taxo)
  (let [t0 (time-ms)]
    (try
      (do
        (.maybeRefresh manager)
        (if-let [changed (DirectoryTaxonomyReader/openIfChanged taxo)]
          (do
            (.close taxo)
            (swap! name->smanager-taxo* assoc-in [index 1] changed)))
        (stat/update-took-count index "refresh-search-managers" (time-took t0)))
      (catch Throwable e
        (do
          (log/info (str index " refresh exception, closing it. Exception: " (ex-str e)))
          (try-close-manager-taxo manager taxo)
          (swap! name->smanager-taxo* dissoc index))))))

(defn refresh-search-managers []
  (let [t0 (time-ms)]
    (bootstrap-indexes)
    (locking name->smanager-taxo*
      (doseq [[index [^SearcherManager manager
                      ^DirectoryTaxonomyReader taxo]] @name->smanager-taxo*]
        (refresh-searcher-locked index manager taxo)))
    (stat/update-took-count stat/total "refresh-search-managers" (time-took t0))
    (log/debug "refreshing took" (time-took t0))))

(defn shutdown []
  (locking name->smanager-taxo*
    (log/info "executing shutdown hook, current mapping: " @name->smanager-taxo*)
    (reset-search-managers)
    (log/info "mapping after cleanup: " @name->smanager-taxo*)))

(defn index-stat []
  (into {} (for [[name [manager taxo]] @name->smanager-taxo*]
             [name (use-searcher name
                                 false
                                 (fn [^IndexSearcher searcher ^DirectoryTaxonomyReader taxo-reader]

           (redis.clients.jedis JedisPool)
           (org.apache.lucene.analysis Analyzer)
           (org.apache.lucene.facet.taxonomy.directory DirectoryTaxonomyWriter DirectoryTaxonomyReader)
           (org.apache.lucene.facet.taxonomy SearcherTaxonomyManager SearcherTaxonomyManager$SearcherAndTaxonomy)
           (org.apache.lucene.index IndexWriter IndexReader IndexWriterConfig DirectoryReader)
           (org.apache.lucene.search Query ScoreDoc SearcherManager IndexSearcher)
           (org.apache.lucene.store NIOFSDirectory Directory NoSuchDirectoryException)))
