Skip to content

Commit

Permalink
+ backend rewrite, towards making it more exchangeable
Browse files Browse the repository at this point in the history
  • Loading branch information
floere committed Aug 25, 2011
1 parent dbe241f commit 536a852
Show file tree
Hide file tree
Showing 43 changed files with 458 additions and 905 deletions.
@@ -1,6 +1,6 @@
module Picky

module Backend
module Backends

class Base

Expand Down
@@ -1,6 +1,6 @@
module Picky

module Backend
module Backends

# Handles all aspects of index files, such as dumping/loading.
#
Expand Down
@@ -1,6 +1,6 @@
module Picky

module Backend
module Backends

module File

Expand Down
@@ -1,6 +1,6 @@
module Picky

module Backend
module Backends

module File

Expand Down
@@ -1,6 +1,6 @@
module Picky

module Backend
module Backends

module File

Expand Down
@@ -1,8 +1,8 @@
module Picky

module Backend
module Backends

class Files < Base
class Memory < Base

def initialize bundle
super bundle
Expand Down
@@ -1,6 +1,6 @@
module Picky

module Backend
module Backends

#
#
Expand Down
@@ -1,6 +1,6 @@
module Picky

module Backend
module Backends

class Redis

Expand Down
@@ -1,6 +1,6 @@
module Picky

module Backend
module Backends

class Redis

Expand Down
@@ -1,6 +1,6 @@
module Picky

module Backend
module Backends

class Redis

Expand Down
@@ -1,6 +1,6 @@
module Picky

module Backend
module Backends

class Redis

Expand Down
12 changes: 9 additions & 3 deletions server/lib/picky/bundle.rb
Expand Up @@ -35,16 +35,22 @@ class Bundle
delegate :[], :[]=, :to => :configuration
delegate :index_directory, :to => :category

def initialize name, category, similarity_strategy, options = {}
def initialize name, category, backend_class, similarity_strategy, options = {}
@name = name
@category = category

# TODO Still needed?
# TODO Refactor further.
#
@backend = backend_class.new self

# Default backend values.
#
# TODO Use a default (memory) backend and load instantly.
#
@inverted = {}
@weights = {}
@similarity = {}
@configuration = {} # A hash with config options.
@configuration = {}

@similarity_strategy = similarity_strategy
end
Expand Down
10 changes: 5 additions & 5 deletions server/lib/picky/category.rb
Expand Up @@ -35,16 +35,16 @@ def initialize name, index, options = {}
partial = options[:partial] || Generators::Partial::Default
similarity = options[:similarity] || Generators::Similarity::Default

@indexing_exact = index.indexing_bundle_class.new :exact, self, weights, Generators::Partial::None.new, similarity, options
@indexing_partial = index.indexing_bundle_class.new :partial, self, weights, partial, Generators::Similarity::None.new, options
@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

# Indexed.
#
@indexed_exact = index.indexed_bundle_class.new :exact, self, similarity
@indexed_exact = Indexed::Bundle.new :exact, self, index.backend_class, similarity
if partial.use_exact_for_partial?
@indexed_partial = @indexed_exact
else
@indexed_partial = index.indexed_bundle_class.new :partial, self, similarity
@indexed_partial = Indexed::Bundle.new :partial, self, index.backend_class, similarity
end

# @exact = exact_lambda.call(@exact, @partial) if exact_lambda = options[:exact_lambda]
Expand Down Expand Up @@ -92,7 +92,7 @@ def prepared_index_path
# Note: If you don't use it with the block, do not forget to close it.
#
def prepared_index_file &block
@prepared_index_file ||= Backend::File::Text.new prepared_index_path
@prepared_index_file ||= Backends::File::Text.new prepared_index_path
@prepared_index_file.open &block
end
# Creates the index directory including all necessary paths above it.
Expand Down
112 changes: 112 additions & 0 deletions server/lib/picky/indexed/bundle.rb
@@ -0,0 +1,112 @@
module Picky

module Indexed # :nodoc:all

# An indexed bundle is a number of memory/redis
# indexes that compose the indexes for a single category:
# * core (inverted) index
# * weights index
# * similarity index
# * index configuration
#
# Indexed refers to them being indexed.
# This class notably offers the methods:
# * load
# * clear
#
# To (re)load or clear the current indexes.
#
class Bundle < Picky::Bundle

# Get the ids for the given symbol.
#
# Returns a (potentially empty) array of ids.
#
def ids sym
@inverted[sym] || []
end

# Get a weight for the given symbol.
#
# Returns a number, or nil.
#
def weight sym
@weights[sym]
end

# Get settings for this bundle.
#
# Returns an object.
#
def [] sym
@configuration[sym]
end

# Loads all indexes.
#
# Loading loads index objects from the backend.
# They should each respond to [].
#
def load
load_inverted
load_weights
load_similarity
load_configuration
end

# Loads the core index.
#
def load_inverted
self.inverted = @backend.load_inverted
end
# Loads the weights index.
#
def load_weights
self.weights = @backend.load_weights
end
# Loads the similarity index.
#
def load_similarity
self.similarity = @backend.load_similarity
end
# Loads the configuration.
#
def load_configuration
self.configuration = @backend.load_configuration
end

# Clears all indexes.
#
def clear
clear_inverted
clear_weights
clear_similarity
clear_configuration
end

# Clears the core index.
#
def clear_inverted
inverted.clear
end
# Clears the weights index.
#
def clear_weights
weights.clear
end
# Clears the similarity index.
#
def clear_similarity
similarity.clear
end
# Clears the configuration.
#
def clear_configuration
configuration.clear
end

end

end

end
116 changes: 0 additions & 116 deletions server/lib/picky/indexed/bundle/base.rb

This file was deleted.

0 comments on commit 536a852

Please sign in to comment.