Permalink
Browse files

+ Do not use the backend class, but the backend instance (enables pas…

…sing parameters, like the db to use by Redis)
  • Loading branch information...
1 parent 4eafaa7 commit bda840ebf5ccaca585f1b37260d37bed2677358a @floere committed Aug 25, 2011
Showing with 150 additions and 160 deletions.
  1. +6 −3 server/lib/picky/backends/{base.rb → backend.rb}
  2. +2 −11 server/lib/picky/backends/memory.rb
  3. +10 −11 server/lib/picky/backends/redis.rb
  4. +2 −8 server/lib/picky/backends/redis/basic.rb
  5. +4 −3 server/lib/picky/bundle.rb
  6. +4 −4 server/lib/picky/category.rb
  7. +9 −10 server/lib/picky/index.rb
  8. +2 −2 server/lib/picky/indexing/bundle.rb
  9. +1 −1 server/lib/picky/loader.rb
  10. +1 −1 server/lib/picky/search/index_combinations_type.rb
  11. +0 −41 server/spec/lib/backend/redis/float_hash_spec.rb
  12. +0 −41 server/spec/lib/backend/redis/string_hash_spec.rb
  13. 0 server/spec/lib/{backend → backends}/file/basic_spec.rb
  14. 0 server/spec/lib/{backend → backends}/file/json_spec.rb
  15. 0 server/spec/lib/{backend → backends}/file/marshal_spec.rb
  16. 0 server/spec/lib/{backend → backends}/file/text_spec.rb
  17. +9 −8 server/spec/lib/{backend → backends}/memory_spec.rb
  18. +1 −1 server/spec/lib/{backend → backends}/redis/basic_spec.rb
  19. +42 −0 server/spec/lib/backends/redis/float_hash_spec.rb
  20. +1 −1 server/spec/lib/{backend → backends}/redis/list_hash_spec.rb
  21. +42 −0 server/spec/lib/backends/redis/string_hash_spec.rb
  22. 0 server/spec/lib/{backend → backends}/redis_spec.rb
  23. +2 −2 server/spec/lib/indexed/memory_spec.rb
  24. +1 −1 server/spec/lib/indexing/bundle_partial_generation_speed_spec.rb
  25. +2 −2 server/spec/lib/indexing/bundle_spec.rb
  26. +5 −5 server/spec/lib/search_spec.rb
  27. +2 −2 server/test_project/app/application.rb
  28. +2 −2 server/test_project_sinatra/app.rb
@@ -2,7 +2,7 @@ module Picky
module Backends
- class Base
+ class Backend
attr_reader :bundle,
:inverted,
@@ -13,13 +13,16 @@ class Base
delegate :identifier,
:to => :bundle
- def initialize bundle
+ def configure_with bundle
@bundle = bundle
end
# Delegators.
#
+ # TODO Should the bundle pass in itself into the load methods?
+ #
+
# Load the indexes.
#
# Each returns an object responding to [].
@@ -126,7 +129,7 @@ def delete
#
#
def to_s
- "#{self.class}(#{bundle.identifier})"
+ "#{self.class}(#{[inverted, weights, similarity, configuration].join(', ')})"
end
end
@@ -2,25 +2,16 @@ module Picky
module Backends
- class Memory < Base
+ class Memory < Backend
- def initialize bundle
+ def configure_with bundle
super bundle
-
- # Note: We marshal the similarity, as the
- # Yajl json lib cannot load symbolized
- # values, just keys.
- #
@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)
end
- def to_s
- "#{self.class}(#{[@inverted, @weights, @similarity, @configuration].join(', ')})"
- end
-
end
end
@@ -4,21 +4,20 @@ module Backends
#
#
- class Redis < Base
+ class Redis < Backend
- def initialize bundle
- super bundle
+ attr_reader :actual_backend
- # Refine a few Redis "types".
- #
- @inverted = Redis::ListHash.new "#{bundle.identifier}:inverted"
- @weights = Redis::FloatHash.new "#{bundle.identifier}:weights"
- @similarity = Redis::ListHash.new "#{bundle.identifier}:similarity"
- @configuration = Redis::StringHash.new "#{bundle.identifier}:configuration"
+ def initialize options = {}
+ @actual_backend = ::Redis.new :db => (options[:db] || 15)
end
- def to_s
- "#{self.class}(#{[@inverted, @weights, @similarity, @configuration].join(', ')})"
+ 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
end
end
@@ -18,15 +18,9 @@ class Basic
# An index cache takes a path, without file extension,
# which will be provided by the subclasses.
#
- def initialize namespace
+ def initialize namespace, backend
@namespace = namespace
-
- # TODO Turn this inside out such that people can pass in
- # their own Redis instance.
- #
- # TODO Make the :db a real option.
- #
- @backend = ::Redis.new :db => 15
+ @backend = backend
end
# Returns itself.
@@ -35,13 +35,14 @@ class Bundle
delegate :[], :[]=, :to => :configuration
delegate :index_directory, :to => :category
- def initialize name, category, backend_class, similarity_strategy, options = {}
+ def initialize name, category, backend, similarity_strategy, options = {}
@name = name
@category = category
- # TODO Refactor further.
+ # This allows initialization.
#
- @backend = backend_class.new self
+ backend.configure_with self
+ @backend = backend
# Default backend values.
#
@@ -35,16 +35,16 @@ def initialize name, index, options = {}
partial = options[:partial] || Generators::Partial::Default
similarity = options[:similarity] || Generators::Similarity::Default
- @indexing_exact = Indexing::Bundle.new :exact, self, index.backend_class, weights, Generators::Partial::None.new, similarity, options
- @indexing_partial = Indexing::Bundle.new :partial, self, index.backend_class, weights, partial, Generators::Similarity::None.new, options
+ @indexing_exact = Indexing::Bundle.new :exact, self, index.backend, weights, Generators::Partial::None.new, similarity, options
+ @indexing_partial = Indexing::Bundle.new :partial, self, index.backend, weights, partial, Generators::Similarity::None.new, options
# Indexed.
#
- @indexed_exact = Indexed::Bundle.new :exact, self, index.backend_class, similarity
+ @indexed_exact = Indexed::Bundle.new :exact, self, index.backend, similarity
if partial.use_exact_for_partial?
@indexed_partial = @indexed_exact
else
- @indexed_partial = Indexed::Bundle.new :partial, self, index.backend_class, similarity
+ @indexed_partial = Indexed::Bundle.new :partial, self, index.backend, similarity
end
# @exact = exact_lambda.call(@exact, @partial) if exact_lambda = options[:exact_lambda]
View
@@ -129,16 +129,15 @@ def initialize name, options = {}
# API method.
#
- # Sets the backend used.
- # Default is @Backends::Memory@.
- #
- def backend backend_class
- @backend_class = backend_class
- end
- # Default backend.
- #
- def backend_class
- @backend_class || Backends::Memory
+ # Sets/returns the backend used.
+ # Default is @Backends::Memory.new@.
+ #
+ def backend backend = nil
+ if backend
+ @backend = backend
+ else
+ @backend || Backends::Memory.new
+ end
end
# Defines a searchable category on the index.
@@ -36,8 +36,8 @@ class Bundle < Picky::Bundle
attr_accessor :partial_strategy,
:weights_strategy
- def initialize name, category, backend_class, weights_strategy, partial_strategy, similarity_strategy, options = {}
- super name, category, backend_class, similarity_strategy, options
+ def initialize name, category, backend, weights_strategy, partial_strategy, similarity_strategy, options = {}
+ super name, category, backend, similarity_strategy, options
@weights_strategy = weights_strategy
@partial_strategy = partial_strategy
@@ -110,7 +110,7 @@ def self.load_framework_internals
# Index store handling.
#
- load_relative 'backends/base'
+ load_relative 'backends/backend'
load_relative 'backends/redis'
load_relative 'backends/redis/basic'
@@ -19,7 +19,7 @@ def combinations_type_for index_definitions_ary # :nodoc:
!backend_types.empty? && @@mapping[*backend_types] || Query::Combinations::Memory
end
def extract_backend_types index_definitions_ary # :nodoc:
- backend_types = index_definitions_ary.map(&:backend_class)
+ backend_types = index_definitions_ary.map(&:backend).map(&:class)
backend_types.uniq!
check_backend_types backend_types
backend_types
@@ -1,41 +0,0 @@
-require 'spec_helper'
-
-describe Picky::Backends::Redis::FloatHash do
-
- let(:index) { described_class.new :some_namespace }
-
- describe 'dump' do
- let(:backend) { index.backend }
- it 'dumps correctly' do
- backend.should_receive(:hset).once.ordered.with :some_namespace, :a, 1
- backend.should_receive(:hset).once.ordered.with :some_namespace, :b, 2
- backend.should_receive(:hset).once.ordered.with :some_namespace, :c, 3
-
- index.dump a: 1, b: 2, c: 3
- end
- end
-
- describe '[]' do
- before(:each) do
- @backend = stub :backend
- index.stub! :backend => @backend
- end
- it 'delegates to the backend' do
- @backend.should_receive(:hget).once.with :some_namespace, :some_symbol
-
- index[:some_symbol]
- end
- it 'returns whatever it gets from the backend' do
- @backend.should_receive(:hget).any_number_of_times.and_return '1.23'
-
- index[:anything].should == 1.23
- end
- end
-
- describe 'to_s' do
- it 'returns the cache path with the default file extension' do
- index.to_s.should == 'Picky::Backends::Redis::FloatHash(some_namespace:*)'
- end
- end
-
-end
@@ -1,41 +0,0 @@
-require 'spec_helper'
-
-describe Picky::Backends::Redis::StringHash do
-
- let(:index) { described_class.new :some_namespace }
-
- describe 'dump' do
- let(:backend) { index.backend }
- it 'dumps correctly' do
- backend.should_receive(:hset).once.ordered.with :some_namespace, :a, 1
- backend.should_receive(:hset).once.ordered.with :some_namespace, :b, 2
- backend.should_receive(:hset).once.ordered.with :some_namespace, :c, 3
-
- index.dump a: 1, b: 2, c: 3
- end
- end
-
- describe 'member' do
- before(:each) do
- @backend = stub :backend
- index.stub! :backend => @backend
- end
- it 'delegates to the backend' do
- @backend.should_receive(:hget).once.with :some_namespace, :some_symbol
-
- index[:some_symbol]
- end
- it 'returns whatever it gets from the backend' do
- @backend.should_receive(:hget).any_number_of_times.and_return :some_result
-
- index[:anything].should == :some_result
- end
- end
-
- describe 'to_s' do
- it 'returns the cache path with the default file extension' do
- index.to_s.should == 'Picky::Backends::Redis::StringHash(some_namespace:*)'
- end
- end
-
-end
@@ -6,10 +6,9 @@
index = Picky::Index.new :some_index
category = Picky::Category.new :some_category, index
- # This is just wrong.
- #
- bundle = Picky::Indexing::Bundle.new :some_bundle, category, described_class, nil, nil, nil
- @files = described_class.new bundle
+ @files = described_class.new
+ bundle = Picky::Indexing::Bundle.new :some_bundle, category, @files, nil, nil, nil
+
@index = @files.inverted
@weights = @files.weights
@@ -180,11 +179,13 @@
describe 'to_s' do
it 'returns the right value' do
- bundle = stub :bundle,
- :index_path => 'index/path',
- :prepared_index_path => 'prepared/index/path'
+ bundle = stub :bundle,
+ :index_path => 'index/path',
+ :prepared_index_path => 'prepared/index/path'
- described_class.new(bundle).to_s.should == "Picky::Backends::Memory(Picky::Backends::File::JSON(index/path.json), Picky::Backends::File::JSON(index/path.json), Picky::Backends::File::Marshal(index/path.dump), Picky::Backends::File::JSON(index/path.json))"
+ memory = described_class.new
+ memory.configure_with bundle
+ memory.to_s.should == "Picky::Backends::Memory(Picky::Backends::File::JSON(index/path.json), Picky::Backends::File::JSON(index/path.json), Picky::Backends::File::Marshal(index/path.dump), Picky::Backends::File::JSON(index/path.json))"
end
end
@@ -2,7 +2,7 @@
describe Picky::Backends::Redis::Basic do
- let(:redis) { described_class.new "some_namespace" }
+ let(:redis) { described_class.new :some_namespace, :some_backend }
describe 'load, retrieve, backup, delete' do
it 'is nothing they do (at least on the backend)' do
@@ -0,0 +1,42 @@
+require 'spec_helper'
+
+describe Picky::Backends::Redis::FloatHash do
+
+ let(:redis_backend) { stub :redis_backend }
+ let(:backend) { described_class.new :some_namespace, redis_backend }
+
+ describe 'dump' do
+ it 'dumps correctly' do
+ redis_backend.should_receive(:del).once.ordered.with :some_namespace
+ redis_backend.should_receive(:hset).once.ordered.with :some_namespace, :a, 1
+ redis_backend.should_receive(:hset).once.ordered.with :some_namespace, :b, 2
+ redis_backend.should_receive(:hset).once.ordered.with :some_namespace, :c, 3
+
+ backend.dump a: 1, b: 2, c: 3
+ end
+ end
+
+ describe 'member' do
+ before(:each) do
+ @actual_backend = stub :actual_backend
+ backend.stub! :backend => @actual_backend
+ end
+ it 'delegates to the backend' do
+ @actual_backend.should_receive(:hget).once.with :some_namespace, :some_symbol
+
+ backend[:some_symbol]
+ end
+ it 'returns whatever it gets from the backend' do
+ @actual_backend.should_receive(:hget).any_number_of_times.and_return '1.23'
+
+ backend[:anything].should == 1.23
+ end
+ end
+
+ describe 'to_s' do
+ it 'returns the cache path with the default file extension' do
+ backend.to_s.should == 'Picky::Backends::Redis::FloatHash(some_namespace:*)'
+ end
+ end
+
+end
@@ -2,7 +2,7 @@
describe Picky::Backends::Redis::ListHash do
- let(:index) { described_class.new :some_namespace }
+ let(:index) { described_class.new :some_namespace, :some_backend }
describe '[]' do
it 'returns whatever comes back from the backend' do
Oops, something went wrong.

0 comments on commit bda840e

Please sign in to comment.