aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--project.clj2
-rw-r--r--src/clucy/core.clj63
2 files changed, 40 insertions, 25 deletions
diff --git a/project.clj b/project.clj
index 52f4e07..a9df710 100644
--- a/project.clj
+++ b/project.clj
@@ -1,7 +1,7 @@
(defproject me.arrdem/clucy "0.4.2-SNAPSHOT"
:description "A Clojure interface to the Lucene search engine"
:url "http://github/weavejester/clucy"
- :dependencies [[org.clojure/clojure "1.4.0"]
+ :dependencies [[org.clojure/clojure "1.7.0"]
[org.apache.lucene/lucene-core "4.10.4"]
[org.apache.lucene/lucene-queryparser "4.10.4"]
[org.apache.lucene/lucene-analyzers-common "4.10.4"]
diff --git a/src/clucy/core.clj b/src/clucy/core.clj
index 8ddd1fe..e52d16e 100644
--- a/src/clucy/core.clj
+++ b/src/clucy/core.clj
@@ -28,6 +28,13 @@
(name x)
(str x)))
+(defn as-dir ^File [x]
+ (if-not (instance? File x)
+ (if (string? x)
+ (File. x)
+ (throw (Exception. "Expects a java.{io.File,lang.String}")))
+ x))
+
;; flag to indicate a default "_content" field should be maintained
(def ^{:dynamic true} *content*
true)
@@ -38,22 +45,22 @@
(RAMDirectory.))
(defn disk-index
- "Create a new index in a directory on disk."
- [^String dir-path]
- (NIOFSDirectory. (File. dir-path)))
+ "Create a new index in a directory on disk. Dir should be a File or
+ a String, either must name a directory which exists."
+ [dir]
+ (let [^File d (as-dir dir)]
+ (NIOFSDirectory. d)))
-(defn- index-writer
+(defn index-writer
"Create an IndexWriter."
- ^IndexWriter
- [index]
+ ^IndexWriter [index]
(let [iwcfg (IndexWriterConfig. *version* *analyzer*)]
(.setOpenMode iwcfg IndexWriterConfig$OpenMode/CREATE_OR_APPEND)
(IndexWriter. index iwcfg)))
-(defn- index-reader
+(defn index-reader
"Create an IndexReader."
- ^IndexReader
- [index]
+ ^IndexReader [index]
(DirectoryReader/open ^Directory index))
@@ -66,8 +73,8 @@
;; given Index, and maintain a cache mapping from Index instance to a
;; (Reader, Writer, Index) structure which may be thread shared.
-(defonce ^{:private true} index-cache
- (ref {}))
+(defonce index-cache
+ (atom {}))
;; We also want some common abstraction for working with either
;; a "real" open index or something we know how to open into either a
@@ -98,15 +105,18 @@
(let [key (if (instance? NIOFSDirectory ind)
(.getDirectory ind)
ind)]
- (dosync
- (if-let [ai (get @index-cache key)]
- ai
- (let [ai (AnIndex.
- (index-reader ind)
- (index-writer ind)
- ind)]
- (alter index-cache assoc key ai)
- ai)))))
+ (if-let [ai (get @index-cache key)]
+ ai
+ (let [iw (index-writer ind)]
+ (. iw (flush true true))
+ (try
+ (let [ir (index-reader ind)
+ ai (AnIndex. ir iw ind)]
+ (swap! index-cache assoc key ai)
+ ai)
+ (catch Exception e
+ (.close iw)
+ (throw e)))))))
;; Close them invalidating the cache
@@ -115,10 +125,9 @@
(let [key (if (instance? NIOFSDirectory ind)
(.getDirectory ind)
ind)]
- (dosync
- (.close (.reader ind))
- (.close (.writer ind))
- (alter index-cache dissoc key))
+ (.close (.reader ind))
+ (.close (.writer ind))
+ (swap! index-cache dissoc key)
nil))
;; And extend this abstraction to the other interesting types
@@ -131,6 +140,12 @@
(as-writer (open-index d)))
(as-index [d] d)
+ IndexWriter
+ (as-writer [d] d)
+
+ IndexReader
+ (as-reader [d] d)
+
RAMDirectory
(as-reader [d]
(as-reader (open-index d)))