Skip to content
This repository was archived by the owner on Oct 23, 2018. It is now read-only.
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: TV4/sunspot_mongoid
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: bdmac/sunspot_mongoid
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 7 commits
  • 4 files changed
  • 1 contributor

Commits on Jan 2, 2012

  1. Copy the full SHA
    6ace9a7 View commit details

Commits on Jan 4, 2012

  1. Copy the full SHA
    db8ff74 View commit details
  2. Copy the full SHA
    4c56078 View commit details
  3. Copy the full SHA
    8f5574d View commit details

Commits on Jan 31, 2012

  1. Rescue websolr http errors.

    bdmac committed Jan 31, 2012
    Copy the full SHA
    ad7d6ee View commit details
  2. Copy the full SHA
    372d279 View commit details

Commits on May 24, 2012

  1. Copy the full SHA
    a171d3a View commit details
Showing with 384 additions and 1 deletion.
  1. +329 −0 lib/sunspot/configuration.rb
  2. +53 −1 lib/sunspot/mongoid.rb
  3. +1 −0 lib/sunspot_mongoid.rb
  4. +1 −0 sunspot_mongoid.gemspec
329 changes: 329 additions & 0 deletions lib/sunspot/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,329 @@
require 'erb'
# copied from sunspot_rails gem Copyright (c) 2009 Mat Brown (MIT)

module Sunspot #:nodoc:
module Mongoid #:nodoc:
#
# Sunspot::Rails is configured via the config/sunspot.yml file, which
# contains properties keyed by environment name. A sample sunspot.yml file
# would look like:
#
# development:
# solr:
# hostname: localhost
# port: 8982
# min_memory: 512M
# max_memory: 1G
# solr_jar: /some/path/solr15/start.jar
# test:
# solr:
# hostname: localhost
# port: 8983
# log_level: OFF
# production:
# solr:
# hostname: localhost
# port: 8983
# path: /solr/myindex
# log_level: WARNING
# solr_home: /some/path
# master_solr:
# hostname: localhost
# port: 8982
# path: /solr
# auto_commit_after_request: true
#
# Sunspot::Rails uses the configuration to set up the Solr connection, as
# well as for starting Solr with the appropriate port using the
# <code>rake sunspot:solr:start</code> task.
#
# If the <code>master_solr</code> configuration is present, Sunspot will use
# the Solr instance specified here for all write operations, and the Solr
# configured under <code>solr</code> for all read operations.
#
class Configuration
attr_writer :user_configuration
def initialize(root_path = nil)
@root = root_path || "."

end
#
# The host name at which to connect to Solr. Default 'localhost'.
#
# ==== Returns
#
# String:: host name
#
def hostname
unless defined?(@hostname)
@hostname = solr_url.host if solr_url
@hostname ||= user_configuration_from_key('solr', 'hostname')
@hostname ||= default_hostname
end
@hostname
end

#
# The port at which to connect to Solr.
# Defaults to 8981 in test, 8982 in development and 8983 in production.
#
# ==== Returns
#
# Integer:: port
#
def port
unless defined?(@port)
@port = solr_url.port if solr_url
@port ||= user_configuration_from_key('solr', 'port')
@port ||= default_port
@port = @port.to_i
end
@port
end

#
# The url path to the Solr servlet (useful if you are running multicore).
# Default '/solr'.
#
# ==== Returns
#
# String:: path
#
def path
unless defined?(@path)
@path = solr_url.path if solr_url
@path ||= user_configuration_from_key('solr', 'path')
@path ||= default_path
end
@path
end

#
# The host name at which to connect to the master Solr instance. Defaults
# to the 'hostname' configuration option.
#
# ==== Returns
#
# String:: host name
#
def master_hostname
@master_hostname ||= (user_configuration_from_key('master_solr', 'hostname') || hostname)
end

#
# The port at which to connect to the master Solr instance. Defaults to
# the 'port' configuration option.
#
# ==== Returns
#
# Integer:: port
#
def master_port
@master_port ||= (user_configuration_from_key('master_solr', 'port') || port).to_i
end

#
# The path to the master Solr servlet (useful if you are running multicore).
# Defaults to the value of the 'path' configuration option.
#
# ==== Returns
#
# String:: path
#
def master_path
@master_path ||= (user_configuration_from_key('master_solr', 'path') || path)
end

#
# True if there is a master Solr instance configured, otherwise false.
#
# ==== Returns
#
# Boolean:: bool
#
def has_master?
@has_master = !!user_configuration_from_key('master_solr')
end

#
# The default log_level that should be passed to solr. You can
# change the individual log_levels in the solr admin interface.
# Default 'INFO'.
#
# ==== Returns
#
# String:: log_level
#
def log_level
@log_level ||= (user_configuration_from_key('solr', 'log_level') || 'INFO')
end

#
# Should the solr index receive a commit after each http-request.
# Default true
#
# ==== Returns
#
# Boolean: auto_commit_after_request?
#
def auto_commit_after_request?
@auto_commit_after_request ||=
user_configuration_from_key('auto_commit_after_request') != false
end

#
# As for #auto_commit_after_request? but only for deletes
# Default false
#
# ==== Returns
#
# Boolean: auto_commit_after_delete_request?
#
def auto_commit_after_delete_request?
@auto_commit_after_delete_request ||=
(user_configuration_from_key('auto_commit_after_delete_request') || false)
end


#
# The log directory for solr logfiles
#
# ==== Returns
#
# String:: log_dir
#
def log_file
@log_file ||= (user_configuration_from_key('solr', 'log_file') || default_log_file_location )
end

def data_path
@data_path ||= user_configuration_from_key('solr', 'data_path') || File.join(@root, 'solr', 'data', @root)
end

def pid_dir
@pid_dir ||= user_configuration_from_key('solr', 'pid_dir') || File.join(@root, 'solr', 'pids', @root)
end


#
# The solr home directory. Sunspot::Rails expects this directory
# to contain a config, data and pids directory. See
# Sunspot::Rails::Server.bootstrap for more information.
#
# ==== Returns
#
# String:: solr_home
#
def solr_home
@solr_home ||=
if user_configuration_from_key('solr', 'solr_home')
user_configuration_from_key('solr', 'solr_home')
else
File.join(@root, 'solr')
end
end

#
# Solr start jar
#
def solr_jar
@solr_jar ||= user_configuration_from_key('solr', 'solr_jar')
end

#
# Minimum java heap size for Solr instance
#
def min_memory
@min_memory ||= user_configuration_from_key('solr', 'min_memory')
end

#
# Maximum java heap size for Solr instance
#
def max_memory
@max_memory ||= user_configuration_from_key('solr', 'max_memory')
end

private

#
# Logging in rails_root/log as solr_<environment>.log as a
# default.
#
# ===== Returns
#
# String:: default_log_file_location
#
def default_log_file_location
File.join(".", 'log', "solr_" + (ENV["RACK_ENV"] || ENV["RAILS_ENV"]) + ".log")
end

#
# return a specific key from the user configuration in config/sunspot.yml
#
# ==== Returns
#
# Mixed:: requested_key or nil
#
def user_configuration_from_key( *keys )
keys.inject(user_configuration) do |hash, key|
hash[key] if hash
end
end

#
# Memoized hash of configuration options for the current Rails environment
# as specified in config/sunspot.yml
#
# ==== Returns
#
# Hash:: configuration options for current environment
#
def user_configuration
@user_configuration ||=
begin
path = File.join(".", 'config', 'sunspot.yml')
p path
if File.exist?(path)
File.open(path) do |file|
processed = ERB.new(file.read).result
YAML.load(processed)[(ENV["RACK_ENV"] || ENV["RAILS_ENV"])]
end
else
{}
end
end
end

protected

#
# When a specific hostname, port and path aren't provided in the
# sunspot.yml file, look for a key named 'url', then check the
# environment, then fall back to a sensible localhost default.
#

def solr_url
if ENV['SOLR_URL'] || ENV['WEBSOLR_URL']
URI.parse(ENV['SOLR_URL'] || ENV['WEBSOLR_URL'])
end
end

def default_hostname
'localhost'
end

def default_port
{ 'test' => 8981,
'development' => 8982,
'production' => 8983
}[(ENV["RACK_ENV"] || ENV["RAILS_ENV"])] || 8983
end

def default_path
'/solr'
end

end
end
end
54 changes: 53 additions & 1 deletion lib/sunspot/mongoid.rb
Original file line number Diff line number Diff line change
@@ -40,7 +40,14 @@ module ClassMethods
# The sunspot solr_index method is very dependent on ActiveRecord, so
# we'll change it to work more efficiently with Mongoid.
def solr_index(opt={})
Sunspot.index!(all)
0.step(count, 5000) do |offset|
records = []
limit(5000).skip(offset).each do |r|
records << r
end
Sunspot.index(records.select { |model| model.indexable? })
end
Sunspot.commit
end
end

@@ -71,6 +78,51 @@ def _remove_index
end
def _update_index
Sunspot.index! self
rescue => e
puts " Error updating Sunspot index: #{e}"
end
end

class <<self
attr_writer :configuration

def configuration(path = nil)
@configuration ||= Sunspot::Mongoid::Configuration.new(path)
end

def reset
@configuration = nil
end
def build_session(configuration = self.configuration)
if configuration.has_master?
SessionProxy::MasterSlaveSessionProxy.new(
SessionProxy::ThreadLocalSessionProxy.new(master_config(configuration)),
SessionProxy::ThreadLocalSessionProxy.new(slave_config(configuration))
)
else
SessionProxy::ThreadLocalSessionProxy.new(slave_config(configuration))
end
end
private

def master_config(sunspot_mongoid_configuration)
config = Sunspot::Configuration.build
config.solr.url = URI::HTTP.build(
:host => sunspot_mongoid_configuration.master_hostname,
:port => sunspot_mongoid_configuration.master_port,
:path => sunspot_mongoid_configuration.master_path
).to_s
config
end

def slave_config(sunspot_mongoid_configuration)
config = Sunspot::Configuration.build
config.solr.url = URI::HTTP.build(
:host => sunspot_mongoid_configuration.hostname,
:port => sunspot_mongoid_configuration.port,
:path => sunspot_mongoid_configuration.path
).to_s
config
end
end
end
Loading