class Sunspot::SessionProxy::IdShardingSessionProxy
A concrete implementation of ShardingSessionProxy
that determines the shard for a given object based on the hash of its class and ID.
<strong>If you change the number of shard sessions that this proxy encapsulates, all objects will point to a different shard.</strong> If you plan on adding more shards over time, consider your own ShardingSessionProxy
implementation that does not determine the session using modular arithmetic (e.g., IDs 1-10000 go to shard 1, 10001-20000 go to shard 2, etc.)
This implementation will, on average, yield an even distribution of objects across shards.
Unlike the abstract ShardingSessionProxy
, this proxy supports the remove_by_id
method.
Attributes
The shard sessions encapsulated by this class.
The shard sessions encapsulated by this class.
Public Class Methods
Initialize with a search session (see ShardingSessionProxy.new
) and a collection of one or more shard sessions. See note about changing the number of shard sessions in the documentation for this class.
# File lib/sunspot/session_proxy/id_sharding_session_proxy.rb, line 32 def initialize(search_session, shard_sessions) super(search_session) @sessions = shard_sessions end
Public Instance Methods
# File lib/sunspot/session_proxy/id_sharding_session_proxy.rb, line 48 def remove_by_id(clazz, *ids) ids.flatten! ids_by_session(clazz, ids).each do |session, ids| session.remove_by_id(clazz, ids) end end
# File lib/sunspot/session_proxy/id_sharding_session_proxy.rb, line 58 def remove_by_id!(clazz, *ids) ids.flatten! ids_by_session(clazz, ids).each do |session, ids| session.remove_by_id!(clazz, ids) end end
Private Instance Methods
# File lib/sunspot/session_proxy/id_sharding_session_proxy.rb, line 82 def id_hash(id) id.bytes.inject { |hash, byte| hash * 31 + byte } end
# File lib/sunspot/session_proxy/id_sharding_session_proxy.rb, line 67 def ids_by_session(clazz, ids) ids.group_by do |id| session_for_index_id(Adapters::InstanceAdapter.index_id_for(clazz, id)) end end
# File lib/sunspot/session_proxy/id_sharding_session_proxy.rb, line 73 def session_for_index_id(index_id) @sessions[id_hash(index_id) % @sessions.length] end