Permalink
Browse files

Adding global and index-level setting - disable_range - to make life …

…easier for those using fixtures. Inspired by a patch by Johannes Kaefer (jak4).
  • Loading branch information...
pat committed Jun 1, 2009
1 parent 501b4e2 commit 9957b16b2ae8cb623f368e19f304797212fc2d25
View
@@ -130,3 +130,4 @@ Since I first released this library, there's been quite a few people who have su
* Anderson Dias
* Jerome Riga
* Tien Dung
+* Johannes Kaefer
@@ -54,6 +54,8 @@ class Configuration
min_infix_len min_prefix_len min_word_len mlock morphology ngram_chars
ngram_len phrase_boundary phrase_boundary_step preopen stopwords
wordforms )
+
+ CustomOptions = %w( disable_range )
attr_accessor :config_file, :searchd_log_file, :query_log_file,
:pid_file, :searchd_file_path, :address, :port, :allow_star,
@@ -227,6 +229,7 @@ def parse_config
set_sphinx_setting self.source_options, key, value, SourceOptions
set_sphinx_setting self.index_options, key, value, IndexOptions
+ set_sphinx_setting self.index_options, key, value, CustomOptions
set_sphinx_setting @configuration.searchd, key, value
set_sphinx_setting @configuration.indexer, key, value
end unless conf.nil?
@@ -63,7 +63,8 @@ def local_options
def options
all_index_options = ThinkingSphinx::Configuration.instance.index_options.clone
@options.keys.select { |key|
- ThinkingSphinx::Configuration::IndexOptions.include?(key.to_s)
+ ThinkingSphinx::Configuration::IndexOptions.include?(key.to_s) ||
+ ThinkingSphinx::Configuration::CustomOptions.include?(key.to_s)
}.each { |key| all_index_options[key.to_sym] = @options[key] }
all_index_options
end
@@ -17,7 +17,7 @@ def to_sql(options={})
SELECT #{ sql_select_clause options[:offset] }
FROM #{ @model.quoted_table_name }
#{ all_associations.collect { |assoc| assoc.to_sql }.join(' ') }
-WHERE #{ sql_where_clause(options) }
+#{ sql_where_clause(options) }
GROUP BY #{ sql_group_clause }
SQL
@@ -30,6 +30,8 @@ def to_sql(options={})
# so pass in :delta => true to get the delta version of the SQL.
#
def to_sql_query_range(options={})
+ return nil if @index.options[:disable_range]
+
min_statement = adapter.convert_nulls(
"MIN(#{quote_column(@model.primary_key)})", 1
)
@@ -65,18 +67,18 @@ def sql_select_clause(offset)
end
def sql_where_clause(options)
- logic = [
+ logic = []
+ logic += [
"#{@model.quoted_table_name}.#{quote_column(@model.primary_key)} >= $start",
"#{@model.quoted_table_name}.#{quote_column(@model.primary_key)} <= $end"
- ]
+ ] unless @index.options[:disable_range]
if self.delta? && !@index.delta_object.clause(@model, options[:delta]).blank?
logic << "#{@index.delta_object.clause(@model, options[:delta])}"
end
logic += (@conditions || [])
-
- logic.join(" AND ")
+ logic.empty? ? "" : "WHERE #{logic.join(' AND ')}"
end
def sql_group_clause
@@ -102,6 +102,25 @@ module Merb; end
end
end
+ describe "index options" do
+ before :each do
+ @settings = {
+ "development" => {"disable_range" => true}
+ }
+
+ open("#{RAILS_ROOT}/config/sphinx.yml", "w") do |f|
+ f.write YAML.dump(@settings)
+ end
+
+ @config = ThinkingSphinx::Configuration.instance
+ @config.send(:parse_config)
+ end
+
+ it "should collect disable_range" do
+ @config.index_options[:disable_range].should be_true
+ end
+ end
+
it "should insert set index options into the configuration file" do
config = ThinkingSphinx::Configuration.instance
ThinkingSphinx::Configuration::IndexOptions.each do |option|
@@ -174,4 +174,44 @@
end
end
end
+
+ describe "#to_riddle_for_core with range disabled" do
+ before :each do
+ ThinkingSphinx::Field.new(
+ @source, ThinkingSphinx::Index::FauxColumn.new(:first_name)
+ )
+ end
+
+ describe "set per-index" do
+ before :each do
+ @index.local_options[:disable_range] = true
+ @riddle = @source.to_riddle_for_core(1, 0)
+ end
+
+ it "should not have the range in the sql_query" do
+ @riddle.sql_query.should_not match(/`people`.`id` >= \$start/)
+ @riddle.sql_query.should_not match(/`people`.`id` <= \$end/)
+ end
+
+ it "should not have a sql_query_range" do
+ @riddle.sql_query_range.should be_nil
+ end
+ end
+
+ describe "set globally" do
+ before :each do
+ ThinkingSphinx::Configuration.instance.index_options[:disable_range] = true
+ @riddle = @source.to_riddle_for_core(1, 0)
+ end
+
+ it "should not have the range in the sql_query" do
+ @riddle.sql_query.should_not match(/`people`.`id` >= \$start/)
+ @riddle.sql_query.should_not match(/`people`.`id` <= \$end/)
+ end
+
+ it "should not have a sql_query_range" do
+ @riddle.sql_query_range.should be_nil
+ end
+ end
+ end
end

0 comments on commit 9957b16

Please sign in to comment.