Permalink
Browse files

+ backend rewrite, + specs

  • Loading branch information...
1 parent c10bdf5 commit bcf042a8cbf67120a3b2a3e6ecd838dbde1cc15d @floere committed Aug 25, 2011
@@ -4,25 +4,18 @@ module Backends
class Backend
- attr_reader :bundle,
- :inverted,
+ attr_reader :inverted,
:weights,
:similarity,
:configuration
- delegate :identifier,
- :to => :bundle
-
- def configure_with bundle
- @bundle = bundle
+ def configure bundle
+ create_inverted bundle
+ create_weights bundle
+ create_similarity bundle
+ create_configuration bundle
end
- # Delegators.
- #
-
- # TODO Should the bundle pass in itself into the load methods?
- #
-
# Load the indexes.
#
# Each returns an object responding to [].
@@ -34,18 +27,18 @@ def load_inverted
inverted.load
end
- # returned_object[sym] # => [:similar1, :similar2, ...]
- #
- def load_similarity
- similarity.load
- end
-
# returned_object[sym] # => 1.23 (float)
#
def load_weights
weights.load
end
+ # returned_object[sym] # => [:similar1, :similar2, ...]
+ #
+ def load_similarity
+ similarity.load
+ end
+
# returned_object[:option] # => value for option
#
def load_configuration
@@ -55,22 +48,22 @@ def load_configuration
# Dumping.
#
def dump_inverted inverted_hash
- timed_exclaim %Q{"#{identifier}": => #{inverted}.}
+ # timed_exclaim %Q{"#{identifier}": => #{inverted}.}
inverted.dump inverted_hash
end
def dump_weights weights_hash
- timed_exclaim %Q{"#{identifier}": => #{weights}.}
+ # timed_exclaim %Q{"#{identifier}": => #{weights}.}
weights.dump weights_hash
end
def dump_similarity similarity_hash
- timed_exclaim %Q{"#{identifier}": => #{similarity}.}
+ # timed_exclaim %Q{"#{identifier}": => #{similarity}.}
similarity.dump similarity_hash
end
def dump_configuration configuration_hash
- timed_exclaim %Q{"#{identifier}": => #{configuration}.}
+ # timed_exclaim %Q{"#{identifier}": => #{configuration}.}
configuration.dump configuration_hash
end
@@ -4,12 +4,17 @@ module Backends
class Memory < Backend
- def configure_with bundle
- super bundle
- @inverted = File::JSON.new bundle.index_path(:inverted)
- @weights = File::JSON.new bundle.index_path(:weights)
- @similarity = File::Marshal.new bundle.index_path(:similarity)
- @configuration = File::JSON.new bundle.index_path(:configuration)
+ def create_inverted bundle
+ @inverted ||= File::JSON.new bundle.index_path(:inverted)
+ end
+ def create_weights bundle
+ @weights ||= File::JSON.new bundle.index_path(:weights)
+ end
+ def create_similarity bundle
+ @similarity ||= File::Marshal.new bundle.index_path(:similarity)
+ end
+ def create_configuration bundle
+ @configuration ||= File::JSON.new bundle.index_path(:configuration)
end
# Returns the result ids for the allocation.
@@ -9,15 +9,21 @@ class Redis < Backend
attr_reader :actual_backend
def initialize options = {}
- @actual_backend = ::Redis.new :db => (options[:db] || 15)
+ db = options[:db] || 15
+ @actual_backend = ::Redis.new :db => db
end
- def configure_with bundle
- super bundle
- @inverted = Redis::ListHash.new "#{bundle.identifier}:inverted", actual_backend
- @weights = Redis::FloatHash.new "#{bundle.identifier}:weights", actual_backend
- @similarity = Redis::ListHash.new "#{bundle.identifier}:similarity", actual_backend
- @configuration = Redis::StringHash.new "#{bundle.identifier}:configuration", actual_backend
+ def create_inverted bundle
+ @inverted ||= Redis::ListHash.new "#{bundle.identifier}:inverted", actual_backend
+ end
+ def create_weights bundle
+ @weights ||= Redis::FloatHash.new "#{bundle.identifier}:weights", actual_backend
+ end
+ def create_similarity bundle
+ @similarity ||= Redis::ListHash.new "#{bundle.identifier}:similarity", actual_backend
+ end
+ def create_configuration bundle
+ @configuration ||= Redis::StringHash.new "#{bundle.identifier}:configuration", actual_backend
end
# Returns the result ids for the allocation.
@@ -36,13 +36,10 @@ class Bundle
delegate :index_directory, :to => :category
def initialize name, category, backend, similarity_strategy, options = {}
- @name = name
- @category = category
-
- # This allows initialization.
- #
- backend.configure_with self
- @backend = backend
+ @name = name
+ @category = category
+ @backend = backend
+ @backend.configure self
# Default backend values.
#
@@ -1,43 +1,47 @@
module Picky
- # TODO Remove.
- #
- class IndexesCheck
-
- class << self
-
- # Returns the right combinations strategy for
- # a number of query indexes.
- #
- # Currently it isn't possible using Memory and Redis etc.
- # indexes in the same query index group.
- #
- # Picky will raise a Query::Indexes::DifferentTypesError.
- #
- def check_backend_types index_definitions_ary # :nodoc:
- backend_types = index_definitions_ary.map(&:backend).map(&:class)
- backend_types.uniq!
- raise_different backend_types if backend_types.size > 1
- backend_types
- end
- def raise_different backend_types # :nodoc:
- raise DifferentTypesError.new(backend_types)
- end
+ module Query
+
+ # TODO Remove.
+ #
+ class IndexesCheck
- # Currently it isn't possible using Memory and Redis etc.
- # indexes in the same query index group.
- #
- class DifferentTypesError < StandardError # :nodoc:all
- def initialize types
- @types = types
+ class << self
+
+ # Returns the right combinations strategy for
+ # a number of query indexes.
+ #
+ # Currently it isn't possible using Memory and Redis etc.
+ # indexes in the same query index group.
+ #
+ # Picky will raise a Query::Indexes::DifferentTypesError.
+ #
+ def check_backend_types index_definitions_ary # :nodoc:
+ backend_types = index_definitions_ary.map(&:backend).map(&:class)
+ backend_types.uniq!
+ raise_different backend_types if backend_types.size > 1
+ backend_types
end
- def to_s
- "Currently it isn't possible to mix Indexes with backends #{@types.join(" and ")} in the same Search instance."
+ def raise_different backend_types # :nodoc:
+ raise DifferentTypesError.new(backend_types)
end
+
end
end
+ # Currently it isn't possible using Memory and Redis etc.
+ # indexes in the same query index group.
+ #
+ class DifferentTypesError < StandardError # :nodoc:all
+ def initialize types
+ @types = types
+ end
+ def to_s
+ "Currently it isn't possible to mix Indexes with backends #{@types.join(" and ")} in the same Search instance."
+ end
+ end
+
end
end
Oops, something went wrong.

0 comments on commit bcf042a

Please sign in to comment.