Created
October 21, 2011 15:20
-
-
Save tcocca/1304102 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Sunspot | |
module SessionProxy | |
class NamespacedSessionProxy | |
class MultipleConnectionsError < StandardError | |
def initialize | |
super("Can not perform operations against multiple namespaced (connections)") | |
end | |
end | |
class NotImplementedError < StandardError | |
def initialize | |
super("This operation has not been implemented for multiple namespaced (connections)") | |
end | |
end | |
class <<self | |
attr_writer :connection_class | |
def connection_class | |
@connection_class ||= RSolr | |
end | |
end | |
attr_reader :config | |
def initialize(env_connection, mls_connection) | |
@env_connection = env_connection | |
@mls_connection = mls_connection | |
@env_deletes = @env_adds = 0 | |
@mls_deletes = @mls_adds = 0 | |
end | |
def new_search(*types, &block) | |
types.flatten! | |
raise MultipleConnectionsError if mixed_types?(types) | |
type_connection(types).new_search(*types, &block) | |
end | |
def search(*types, &block) | |
search = new_search(*types, &block) | |
search.execute | |
end | |
def new_more_like_this(object, *types, &block) | |
types[0] ||= object.class | |
raise MultipleConnectionsError if mixed_types?(types) | |
type_connection(types).new_more_like_this(object, *types, &block) | |
end | |
def more_like_this(object, *types, &block) | |
mlt = new_more_like_this(object, *types, &block) | |
mlt.execute | |
end | |
def index(*objects) | |
objects.flatten! | |
raise MultipleConnectionsError if mixed_objects?(objects) | |
if mls_objects?(objects) | |
@mls_adds += objects.length | |
@mls_connection.index(*objects) | |
else | |
@env_adds += objects.length | |
@env_connection.index(*objects) | |
end | |
end | |
def index!(*objects) | |
index(*objects) | |
objects.flatten! | |
if mls_objects?(objects) | |
type_commit("mls") | |
else | |
type_commit("env") | |
end | |
end | |
def commit | |
type_commit("env") | |
type_commit("mls") | |
end | |
def type_commit(namespace) | |
if namespace == "mls" | |
@mls_adds = @mls_deletes = 0 | |
@mls_connection.commit | |
elsif namespace == "env" | |
@env_adds = @env_deletes = 0 | |
@env_connection.commit | |
end | |
end | |
def optimize | |
type_optimize("env") | |
type_optimize("mls") | |
end | |
def type_optimize(namespace) | |
if namespace == "mls" | |
@mls_adds = @mls_deletes = 0 | |
@mls_connection.optimize | |
elsif namespace == "env" | |
@env_adds = @env_deletes = 0 | |
@env_connection.optimize | |
end | |
end | |
def dirty? | |
type_dirty?("mls") || type_dirty?("env") | |
end | |
def type_dirty?(namespace) | |
if namespace == "mls" | |
(@mls_deletes + @mls_adds) > 0 | |
elsif namespace == "env" | |
(@env_deletes + @env_adds) > 0 | |
end | |
end | |
def commit_if_dirty | |
type_commit("mls") if type_dirty?("mls") | |
type_commit("env") if type_dirty?("env") | |
end | |
def delete_dirty? | |
type_delete_dirty?("mls") || type_delete_dirty?("env") | |
end | |
def type_delete_dirty?(namespace) | |
if namespace == "mls" | |
@mls_deletes > 0 | |
else | |
@env_deletes > 0 | |
end | |
end | |
def commit_if_delete_dirty | |
type_commit("mls") if type_delete_dirty?("mls") | |
type_commit("env") if type_delete_dirty?("env") | |
end | |
def batch | |
raise NotImplementedError | |
end | |
def remove(*objects, &block) | |
if block | |
raise NotImplementedError | |
else | |
objects.flatten! | |
raise MultipleConnectionsError if mixed_objects?(objects) | |
if mls_objects?(objects) | |
@mls_deletes += objects.length | |
@mls_connection.remove(*objects, &block) | |
else | |
@env_deletes += objects.length | |
@env_connection.remove(*objects, &block) | |
end | |
end | |
end | |
def remove!(*objects) | |
remove(*objects) | |
objects.flatten! | |
if mls_objects?(objects) | |
type_commit("mls") | |
else | |
type_commit("env") | |
end | |
end | |
def remove_by_id(clazz, id) | |
if mls_class?(clazz) | |
@mls_connection.remove_by_id(clazz, id) | |
else | |
@env_connection.remove_by_id(clazz, id) | |
end | |
end | |
def remove_by_id!(clazz, id) | |
remove_by_id(clazz, id) | |
if mls_class?(clazz) | |
type_commit("mls") | |
else | |
type_commit("env") | |
end | |
end | |
def remove_all(*classes) | |
classes.flatten! | |
raise MultipleConnectionsError if mixed_types?(classes) | |
if classes.empty? | |
raise "Can not remove all without classes" | |
else | |
if mls_types?(classes) | |
@mls_connection.remove_all(*classes) | |
else | |
@env_connection.remove_all(*classes) | |
end | |
end | |
end | |
def remove_all!(*classes) | |
remove_all(*classes) | |
classes.flatten! | |
if mls_types?(classes) | |
type_commit("mls") | |
else | |
type_commit("env") | |
end | |
end | |
private | |
def mixed_types?(types) | |
namespaces = type_namespaces(types) | |
namespaces.size > 1 | |
end | |
def mixed_objects?(objects) | |
namespaces = object_namespaces(objects) | |
namespaces.size > 1 | |
end | |
def mls_types?(types) | |
namespaces = type_namespaces(types) | |
namespaces.first == "Mls" | |
end | |
def mls_objects?(objects) | |
namespaces = object_namespaces(objects) | |
namespaces.first == "Mls" | |
end | |
def mls_class?(clazz) | |
clazz.name.include?("Mls::") | |
end | |
def type_namespaces(types) | |
types.collect{|t| t.name.include?('::') ? t.name.split('::').first : ''}.uniq | |
end | |
def object_namespaces(objects) | |
objects.collect{|o| o.class.name.include?('::') ? o.object.name.split('::').first : ''}.uniq | |
end | |
def type_connection(types) | |
if mls_types?(types) | |
@mls_connection | |
else | |
@env_connection | |
end | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'namespaced_session_proxy' | |
env_configuration = Sunspot::Rails::Configuration.new | |
env_config = Sunspot::Configuration.build | |
env_config.solr.url = URI::HTTP.build( | |
:host => env_configuration.hostname, | |
:port => env_configuration.port, | |
:path => env_configuration.path | |
).to_s | |
env_config.pagination.default_per_page = 20 | |
env_session = Sunspot::SessionProxy::ThreadLocalSessionProxy.new(env_config) | |
mls_config = Sunspot::Configuration.build | |
mls_config.solr.url = URI::HTTP.build( | |
:host => "mls.solrdomain.com", | |
:port => 8080, | |
:path => "/solr" | |
).to_s | |
mls_config.pagination.default_per_page = 20 | |
mls_session = Sunspot::SessionProxy::ThreadLocalSessionProxy.new(mls_config) | |
Sunspot.session = Sunspot::SessionProxy::NamespacedSessionProxy.new(env_session, mls_session) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment