Browse files

adding LocalDatabase wrapper so we can make a remote database wrapper

  • Loading branch information...
1 parent 245ccc0 commit b2140ae1488f03eccbc2a2c034f00fb057c684af @ryanb committed Aug 27, 2010
View
3 .autotest
@@ -0,0 +1,3 @@
+Autotest.add_hook(:initialize) do |at|
+ at.add_exception %r{^./tmp} # ignore temp files, lest autotest will run again, and again...
+end
View
1 lib/xapit.rb
@@ -59,6 +59,7 @@ def self.serialize_value(value)
require File.dirname(__FILE__) + '/xapit/facet_blueprint'
require File.dirname(__FILE__) + '/xapit/facet'
require File.dirname(__FILE__) + '/xapit/facet_option'
+require File.dirname(__FILE__) + '/xapit/local_database'
require File.dirname(__FILE__) + '/xapit/query'
require File.dirname(__FILE__) + '/xapit/query_parsers/abstract_query_parser'
require File.dirname(__FILE__) + '/xapit/query_parsers/simple_query_parser'
View
24 lib/xapit/config.rb
@@ -6,10 +6,7 @@ class << self
# See Xapit#setup
def setup(options = {})
- if @options && options[:database_path] != @options[:database_path]
- @database = nil
- @writable_database = nil
- end
+ @database = nil
@options = options.reverse_merge(default_options)
end
@@ -59,22 +56,14 @@ def breadcrumb_facets?
@options[:breadcrumb_facets]
end
- # Fetch Xapian::Database object at configured path. Database is stored in memory.
def database
- @writable_database || (@database ||= Xapian::Database.new(path))
- end
-
- # Fetch Xapian::WritableDatabase object at configured path. Database is stored in memory.
- # Creates the database directory if needed.
- def writable_database
- @writable_database ||= generate_database
+ @database ||= Xapit::LocalDatabase.new(path, template_path)
end
# Removes the configured database file and clears the stored one in memory.
def remove_database
FileUtils.rm_rf(path) if File.exist? File.join(path, "record.DB")
@database = nil
- @writable_database = nil
end
# Clear the current database from memory. Unfortunately this is a hack because
@@ -83,17 +72,8 @@ def remove_database
# TODO looks like it does in 1.2, I should investigate and switch to that.
def close_database
@database = nil
- @writable_database = nil
GC.start
end
-
- private
-
- def generate_database
- FileUtils.mkdir_p(File.dirname(path)) unless File.exist?(File.dirname(path))
- FileUtils.cp_r(template_path, path) if template_path && !File.exist?(path)
- Xapian::WritableDatabase.new(path, Xapian::DB_CREATE_OR_OPEN)
- end
end
end
end
View
13 lib/xapit/document.rb
@@ -0,0 +1,13 @@
+module Xapit
+ class Document
+ attr_accessor :id, :terms, :term_weights, :values, :value_indexes, :spellings
+
+ def initialize
+ @terms = []
+ @term_weights = []
+ @values = []
+ @value_indexes = []
+ @spellings = []
+ end
+ end
+end
View
2 lib/xapit/facet_option.rb
@@ -34,7 +34,7 @@ def save
doc = Xapian::Document.new
doc.data = [facet.member_class.name, facet.attribute, name].join("|||")
doc.add_term("Q#{self.class.name}-#{identifier}")
- Xapit::Config.writable_database.add_document(doc)
+ Xapit::Config.database.add_document(doc)
end
end
View
2 lib/xapit/index_blueprint.rb
@@ -112,7 +112,7 @@ def update_record(member_id)
# Remove a single record from the index.
def destroy_record(member_id)
- Xapit::Config.writable_database.delete_document("Q#{@member_class}-#{member_id}")
+ Xapit::Config.database.delete_document("Q#{@member_class}-#{member_id}")
end
private
View
2 lib/xapit/indexers/abstract_indexer.rb
@@ -110,7 +110,7 @@ def save_facet_options_for(member)
private
def database
- Config.writable_database
+ Config.database
end
end
end
View
2 lib/xapit/indexers/classic_indexer.rb
@@ -21,7 +21,7 @@ def term_generator
def create_term_generator
term_generator = Xapian::TermGenerator.new
term_generator.set_flags(Xapian::TermGenerator::FLAG_SPELLING, 0) if Config.spelling?
- term_generator.database = database
+ term_generator.database = Config.database.writable_database
term_generator.stemmer = Xapian::Stem.new(Config.stemming)
term_generator
end
View
50 lib/xapit/local_database.rb
@@ -0,0 +1,50 @@
+module Xapit
+ class LocalDatabase
+ def initialize(path, template_path)
+ @path = path
+ @template_path = template_path
+ end
+
+ def readable_database
+ writable_database
+ end
+
+ def writable_database
+ @writable_database ||= generate_database
+ end
+
+ def add_document(document)
+ writable_database.add_document(document)
+ end
+
+ def delete_document(id)
+ writable_database.delete_document(id)
+ end
+
+ def replace_document(id, document)
+ writable_database.replace_document(id, document)
+ end
+
+ def get_spelling_suggestion(term)
+ readable_database.get_spelling_suggestion(term)
+ end
+
+ def add_spelling(term)
+ writable_database.add_spelling(term)
+ end
+
+ def doccount
+ readable_database.doccount
+ end
+
+ private
+
+ def generate_database
+ FileUtils.mkdir_p(File.dirname(@path)) unless File.exist?(File.dirname(@path))
+ if @template_path && !File.exist?(@path)
+ FileUtils.cp_r(@template_path, @path)
+ end
+ Xapian::WritableDatabase.new(@path, Xapian::DB_CREATE_OR_OPEN)
+ end
+ end
+end
View
4 lib/xapit/query.rb
@@ -23,7 +23,7 @@ def not_query(*args)
def matchset(options = {})
options.reverse_merge! :offset => 0, :sort_descending => false
- enquire = Xapian::Enquire.new(Config.database)
+ enquire = Xapian::Enquire.new(Config.database.readable_database)
if options[:sort_by_values]
sorter = Xapian::MultiValueSorter.new
options[:sort_by_values].each do |sort_value|
@@ -42,7 +42,7 @@ def matches(options = {})
def count
# a bit of a hack to get more accurate count estimate
- @count ||= matchset(:limit => Config.database.doccount).matches_estimated
+ @count ||= matchset(:limit => Config.database.readable_database.doccount).matches_estimated
end
private
View
2 lib/xapit/query_parsers/abstract_query_parser.rb
@@ -167,7 +167,7 @@ def condition_term(name, value)
def wildcard_query(term, prefix = "")
full_term = (prefix + term.downcase).sub(/\*$/, '') # remove asterisk at end if it exists
parser = Xapian::QueryParser.new
- parser.database = Xapit::Config.database
+ parser.database = Xapit::Config.database.readable_database
parser.parse_query(full_term[-1..-1], Xapian::QueryParser::FLAG_PARTIAL, full_term[0..-2])
end
end
View
2 lib/xapit/query_parsers/classic_query_parser.rb
@@ -14,7 +14,7 @@ def cleanup_text(text)
def build_xapian_parser
parser = Xapian::QueryParser.new
- parser.database = Config.database
+ parser.database = Config.database.readable_database
parser.stemmer = Xapian::Stem.new(Config.stemming)
parser.stemming_strategy = Xapian::QueryParser::STEM_SOME
parser.default_op = Xapian::Query::OP_AND
View
1 spec/spec.opts
@@ -1,2 +1,3 @@
--colour
--loadby mtime
+--backtrace
View
5 spec/xapit/config_spec.rb
@@ -2,8 +2,8 @@
describe Xapit::Config do
it "should be able to set database path and fetch writable or readable" do
- Xapit::Config.writable_database.should be_kind_of(Xapian::WritableDatabase)
- Xapit::Config.database.should be_kind_of(Xapian::Database)
+ Xapit::Config.database.writable_database.should be_kind_of(Xapian::WritableDatabase)
+ Xapit::Config.database.readable_database.should be_kind_of(Xapian::Database)
end
it "should default query parser to SimpleQueryParser" do
@@ -47,7 +47,6 @@
end
it "should remove the database if it is a true xapian database" do
- Xapit::Config.writable_database # load the database
Xapit::Config.remove_database
File.exist?(Xapit::Config.path).should be_false
end
View
10 spec/xapit/facet_option_spec.rb
@@ -41,14 +41,14 @@
doc = Xapian::Document.new
doc.data = "XapitMember|||age|||17"
doc.add_term("QXapit::FacetOption-abc123")
- Xapit::Config.writable_database.add_document(doc)
+ Xapit::Config.database.add_document(doc)
option = Xapit::FacetOption.find("abc123")
option.name.should == "17"
option.facet.name.should == "Person Age"
end
it "should save facet to database" do
- Xapit::Config.writable_database # make sure there's a database setup in case we try to read from it
+ Xapit::Config.database.writable_database # make sure there's a database setup in case we try to read from it
option = Xapit::FacetOption.new(nil, nil, nil)
option.facet = XapitMember.xapit_facet_blueprint("age")
option.name = "23"
@@ -60,8 +60,8 @@
doc = Xapian::Document.new
doc.data = "XapitMember|||age|||17"
doc.add_term("QXapit::FacetOption-abc123")
- Xapit::Config.writable_database.add_document(doc)
- stub(Xapit::Config.writable_database).add_document { raise "should not add doc" }
+ Xapit::Config.database.add_document(doc)
+ stub(Xapit::Config.database).add_document { raise "should not add doc" }
option = Xapit::FacetOption.new(XapitMember, nil, nil)
stub(option).identifier { "abc123" }
option.save
@@ -71,7 +71,7 @@
doc = Xapian::Document.new
doc.data = "XapitMember|||age|||"
doc.add_term("QXapit::FacetOption-abc123")
- Xapit::Config.writable_database.add_document(doc)
+ Xapit::Config.database.add_document(doc)
option = Xapit::FacetOption.find("abc123")
option.name.should == ""
option.facet.name.should == "Person Age"
View
8 spec/xapit/index_blueprint_spec.rb
@@ -50,12 +50,12 @@
it "should index member document into database" do
XapitMember.new
@index.index_all
- Xapit::Config.writable_database.doccount.should >= 1
- Xapit::Config.writable_database.flush
+ Xapit::Config.database.doccount.should >= 1
+ Xapit::Config.database.writable_database.flush
end
it "should remember all blueprints and index each of them" do
- stub(Xapit::Config.writable_database).add_document
+ stub(Xapit::Config.database).add_document
mock(@index).index_all
Xapit::IndexBlueprint.index_all
end
@@ -91,7 +91,7 @@
end
it "should not create record index if member isn't found" do
- Xapit::Config.writable_database # make sure the database is built
+ Xapit::Config.database.writable_database # make sure the database is built
member = XapitMember.new(:name => "New Record!")
stub(XapitMember).find { nil }
@index.text :name
View
2 spec/xapit/indexers/abstract_indexer_spec.rb
@@ -28,7 +28,7 @@
end
it "should add terms, values and options for facets" do
- Xapit::Config.writable_database # force xapit to use a writable database at the beginning
+ Xapit::Config.database.writable_database # force xapit to use a writable database at the beginning
stub(XapitMember).xapit_index_blueprint { @index }
member = XapitMember.new(:foo => ["ABC", "DEF"])
ids = Xapit::FacetBlueprint.new(XapitMember, 0, :foo).identifiers_for(member)
View
4 spec/xapit/local_database_spec.rb
@@ -0,0 +1,4 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe Xapit::LocalDatabase do
+end
View
2 spec/xapit/query_parsers/abstract_query_parser_spec.rb
@@ -22,7 +22,7 @@
end
it "should give spelling suggestion on full term" do
- Xapit::Config.writable_database.add_spelling("foo bar")
+ Xapit::Config.database.add_spelling("foo bar")
parser = Xapit::AbstractQueryParser.new(nil, "foo barr")
parser.spelling_suggestion.should == "foo bar"
end
View
2 spec/xapit/query_parsers/classic_query_parser_spec.rb
@@ -6,7 +6,7 @@
end
it "should have an initial xapian parser with stemming and default operator support" do
- Xapit::Config.writable_database # just so a database exists
+ Xapit::Config.database.writable_database # just so a database exists
expected = Xapian::QueryParser.new
expected.stemmer = Xapian::Stem.new("english")
expected.stemming_strategy = Xapian::QueryParser::STEM_SOME

0 comments on commit b2140ae

Please sign in to comment.