Permalink
Browse files

+ complete index/backend rewrite, - configuration cludge

  • Loading branch information...
1 parent 50883fe commit 9fe8c8646454bdf97a9ad830685785f1d71d507c @floere committed Apr 13, 2011
@@ -1,67 +0,0 @@
-module Configuration # :nodoc:all
-
- # Holds the configuration for a
- # index/category combination.
- #
- class Index
-
- attr_reader :index, :category
-
- def initialize index, category
- @index = index
- @category = category
- end
-
- def index_name
- @index_name ||= index.name
- end
- def category_name
- @category_name ||= category.name
- end
-
- #
- #
- def index_path bundle_name, name
- "#{index_directory}/#{category_name}_#{bundle_name}_#{name}"
- end
-
- # Was: search_index_file_name
- #
- def prepared_index_path
- @prepared_index_path ||= "#{index_directory}/prepared_#{category_name}_index"
- end
- def prepared_index_file &block
- @prepared_index_file ||= Internals::Index::File::Text.new prepared_index_path
- @prepared_index_file.open_for_indexing &block
- end
-
- # Identifier for internal use.
- #
- def identifier
- @identifier ||= "#{index_name}:#{category_name}"
- end
-
- def to_s
- "#{index_name} #{category_name}"
- end
-
- def self.index_root
- @index_root ||= "#{PICKY_ROOT}/index"
- end
- def index_root
- self.class.index_root
- end
- # Was: cache_directory
- #
- def index_directory
- @index_directory ||= "#{index_root}/#{PICKY_ENVIRONMENT}/#{index_name}"
- end
- # Was: prepare_cache_directory
- #
- def prepare_index_directory
- FileUtils.mkdir_p index_directory
- end
-
- end
-
-end
@@ -1,29 +1,29 @@
module Internals
-
+
module Index
-
+
class Backend
-
+
attr_reader :bundle_name
attr_reader :prepared, :index, :weights, :similarity, :configuration
-
- delegate :index_name, :category_name, :to => :@config
-
- def initialize bundle_name, config
+
+ delegate :index_name, :category_name, :to => :@category
+
+ def initialize bundle_name, category
@bundle_name = bundle_name
- @config = config
- @prepared = File::Text.new config.prepared_index_path
+ @category = category
+ @prepared = File::Text.new category.prepared_index_path
end
-
+
# Delegators.
#
-
+
# Retrieving data.
#
def retrieve &block
prepared.retrieve &block
end
-
+
# Dumping.
#
def dump_index index_hash
@@ -38,7 +38,7 @@ def dump_similarity similarity_hash
def dump_configuration configuration_hash
configuration.dump configuration_hash
end
-
+
# Loading.
#
def load_index
@@ -53,7 +53,7 @@ def load_weights
def load_configuration
configuration.load
end
-
+
# Cache ok?
#
def index_cache_ok?
@@ -65,7 +65,7 @@ def similarity_cache_ok?
def weights_cache_ok?
weights.cache_ok?
end
-
+
# Cache small?
#
def index_cache_small?
@@ -77,7 +77,7 @@ def similarity_cache_small?
def weights_cache_small?
weights.cache_small?
end
-
+
# Copies the indexes to the "backup" directory.
#
def backup
@@ -86,7 +86,7 @@ def backup
similarity.backup
configuration.backup
end
-
+
# Restores the indexes from the "backup" directory.
#
def restore
@@ -95,7 +95,7 @@ def restore
similarity.restore
configuration.restore
end
-
+
# Delete all index files.
#
def delete
@@ -104,9 +104,9 @@ def delete
similarity.delete
configuration.delete
end
-
+
end
-
+
end
-
+
end
@@ -4,17 +4,17 @@ module Index
class Files < Backend
- def initialize bundle_name, config
- super bundle_name, config
+ def initialize bundle_name, category
+ super bundle_name, category
# Note: We marshal the similarity, as the
# Yajl json lib cannot load symbolized
# values, just keys.
#
- @index = File::JSON.new config.index_path(bundle_name, :index)
- @weights = File::JSON.new config.index_path(bundle_name, :weights)
- @similarity = File::Marshal.new config.index_path(bundle_name, :similarity)
- @configuration = File::JSON.new config.index_path(bundle_name, :configuration)
+ @index = File::JSON.new category.index_path(bundle_name, :index)
+ @weights = File::JSON.new category.index_path(bundle_name, :weights)
+ @similarity = File::Marshal.new category.index_path(bundle_name, :similarity)
+ @configuration = File::JSON.new category.index_path(bundle_name, :configuration)
end
def to_s
@@ -1,44 +1,44 @@
module Internals
module Index
-
+
# TODO Needs a reconnect to be run after forking.
#
class Redis < Backend
-
- def initialize bundle_name, config
- super bundle_name, config
+
+ def initialize bundle_name, category
+ super bundle_name, category
# Refine a few Redis "types".
#
- @index = Redis::ListHash.new "#{config.identifier}:#{bundle_name}:index"
- @weights = Redis::StringHash.new "#{config.identifier}:#{bundle_name}:weights"
- @similarity = Redis::ListHash.new "#{config.identifier}:#{bundle_name}:similarity"
- @configuration = Redis::StringHash.new "#{config.identifier}:#{bundle_name}:configuration"
+ @index = Redis::ListHash.new "#{category.identifier}:#{bundle_name}:index"
+ @weights = Redis::StringHash.new "#{category.identifier}:#{bundle_name}:weights"
+ @similarity = Redis::ListHash.new "#{category.identifier}:#{bundle_name}:similarity"
+ @configuration = Redis::StringHash.new "#{category.identifier}:#{bundle_name}:configuration"
end
-
+
# Delegate to the right collection.
#
def ids sym
index.collection sym
end
-
+
# Delegate to the right member value.
#
# Note: Converts to float.
#
def weight sym
weights.member(sym).to_f
end
-
+
# Delegate to a member value.
#
def setting sym
configuration.member sym
end
-
+
end
-
+
end
-
+
end
@@ -35,8 +35,8 @@ class Base
delegate :[], :to => :configuration
delegate :size, :to => :index
- def initialize name, configuration, similarity_strategy
- @identifier = "#{configuration.identifier}:#{name}"
+ def initialize name, category, similarity_strategy
+ @identifier = "#{category.identifier}:#{name}"
@index = {}
@weights = {}
@@ -14,10 +14,10 @@ module Bundle
#
class Redis < Base
- def initialize name, configuration, *args
- super name, configuration, *args
+ def initialize name, category, *args
+ super name, category, *args
- @backend = Internals::Index::Redis.new name, configuration
+ @backend = Internals::Index::Redis.new name, category
end
# Get the ids for the given symbol.
@@ -9,34 +9,33 @@ module Indexed
#
class Category
+ include Internals::Shared::Category
+
attr_accessor :exact
- attr_reader :identifier, :name
+ attr_reader :name, :index
attr_writer :partial
#
#
def initialize name, index, options = {}
- @name = name
-
- configuration = Configuration::Index.new index, self
-
- @identifier = configuration.identifier
+ @name = name
+ @index = index
# TODO Push the defaults out into the index.
#
@partial_strategy = options[:partial] || Internals::Generators::Partial::Default
similarity = options[:similarity] || Internals::Generators::Similarity::Default
bundle_class = options[:indexed_bundle_class] || Bundle::Memory
- @exact = bundle_class.new :exact, configuration, similarity
- @partial = bundle_class.new :partial, configuration, similarity
+ @exact = bundle_class.new :exact, self, similarity
+ @partial = bundle_class.new :partial, self, similarity
# @exact = exact_lambda.call(@exact, @partial) if exact_lambda = options[:exact_lambda]
# @partial = partial_lambda.call(@exact, @partial) if partial_lambda = options[:partial_lambda]
# TODO Extract?
#
- Query::Qualifiers.add(configuration.category_name, generate_qualifiers_from(options) || [name])
+ Query::Qualifiers.add(name, generate_qualifiers_from(options) || [name])
end
def to_s
@@ -16,8 +16,8 @@ class Base < SuperBase
# Path is in which directory the cache is located.
#
- def initialize name, configuration, similarity_strategy, partial_strategy, weights_strategy
- super name, configuration, similarity_strategy
+ def initialize name, category, similarity_strategy, partial_strategy, weights_strategy
+ super name, category, similarity_strategy
@partial_strategy = partial_strategy
@weights_strategy = weights_strategy
@@ -33,9 +33,9 @@ class SuperBase
delegate :clear, :to => :index
delegate :[], :[]=, :to => :configuration
- def initialize name, configuration, similarity_strategy
- @identifier = "#{configuration.identifier}:#{name}"
- @files = Internals::Index::Files.new name, configuration
+ def initialize name, category, similarity_strategy
+ @identifier = "#{category.identifier}:#{name}"
+ @files = Internals::Index::Files.new name, category
@index = {}
@weights = {}
Oops, something went wrong.

0 comments on commit 9fe8c86

Please sign in to comment.