lib/sup/index.rb in sup-0.9.1 vs lib/sup/index.rb in sup-0.10
- old
+ new
@@ -4,11 +4,11 @@
begin
require 'chronic'
$have_chronic = true
rescue LoadError => e
- debug "optional 'chronic' library not found; date-time query restrictions disabled"
+ debug "No 'chronic' gem detected. Install it for date/time query restrictions."
$have_chronic = false
end
module Redwood
@@ -21,15 +21,19 @@
end
def method_missing m; @h[m.to_s] end
end
+ def is_a_deprecated_ferret_index?; false end
+
include Singleton
def initialize dir=BASE_DIR
@dir = dir
@lock = Lockfile.new lockfile, :retries => 0, :max_age => nil
+ @sync_worker = nil
+ @sync_queue = Queue.new
end
def lockfile; File.join @dir, "lock" end
def lock
@@ -170,17 +174,67 @@
##
## raises a ParseError if something went wrong.
def parse_query s
unimplemented
end
+
+ def save_thread t
+ t.each_dirty_message do |m|
+ if @sync_worker
+ @sync_queue << m
+ else
+ update_message_state m
+ end
+ m.clear_dirty
+ end
+ end
+
+ def start_sync_worker
+ @sync_worker = Redwood::reporting_thread('index sync') { run_sync_worker }
+ end
+
+ def stop_sync_worker
+ return unless worker = @sync_worker
+ @sync_worker = nil
+ @sync_queue << :die
+ worker.join
+ end
+
+ def run_sync_worker
+ while m = @sync_queue.deq
+ return if m == :die
+ update_message_state m
+ # Necessary to keep Xapian calls from lagging the UI too much.
+ sleep 0.03
+ end
+ end
end
-index_name = ENV['SUP_INDEX'] || $config[:index] || DEFAULT_INDEX
-case index_name
- when "xapian"; require "sup/xapian_index"
- when "ferret"; require "sup/ferret_index"
- else fail "unknown index type #{index_name.inspect}"
+## just to make the backtraces even more insane, here we engage in yet more
+## method_missing metaprogramming so that Index.init(index_type_name) will
+## magically make Index act like the correct Index class.
+class Index
+ def self.init type=nil
+ ## determine the index type from the many possible ways of setting it
+ type = (type == "auto" ? nil : type) ||
+ ENV['SUP_INDEX'] ||
+ $config[:index] ||
+ (File.exist?(File.join(BASE_DIR, "xapian")) && "xapian") || ## PRIORITIZE THIS
+ (File.exist?(File.join(BASE_DIR, "ferret")) && "ferret") || ## deprioritize this
+ DEFAULT_NEW_INDEX_TYPE
+ begin
+ require "sup/#{type}_index"
+ @klass = Redwood.const_get "#{type.capitalize}Index"
+ @obj = @klass.init
+ rescue LoadError, NameError => e
+ raise "unknown index type #{type.inspect}: #{e.message}"
+ end
+ debug "using #{type} index"
+ @obj
+ end
+
+ def self.instance; @obj end
+ def self.method_missing m, *a, &b; @obj.send(m, *a, &b) end
+ def self.const_missing x; @obj.class.const_get(x) end
end
-Index = Redwood.const_get "#{index_name.capitalize}Index"
-debug "using index #{Index.name}"
end