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

all_sessions[R]

The shard sessions encapsulated by this class.

sessions[R]

The shard sessions encapsulated by this class.

Public Class Methods

new(search_session, shard_sessions) click to toggle source

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.

Calls superclass method
# 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

remove_by_id(clazz, *ids) click to toggle source

See Sunspot.remove_by_id

# 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
remove_by_id!(clazz, *ids) click to toggle source

See Sunspot.remove_by_id!

# 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

id_hash(id) click to toggle source
# 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
ids_by_session(clazz, ids) click to toggle source
# 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
session_for_index_id(index_id) click to toggle source
# 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