Class: Sunspot::SessionProxy::IdShardingSessionProxy

Inherits:
ShardingSessionProxy show all
Defined in:
lib/sunspot/session_proxy/id_sharding_session_proxy.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ShardingSessionProxy

#commit, #commit_if_delete_dirty, #commit_if_dirty, #delete_dirty?, #dirty?, #index, #index!, #more_like_this, #new_more_like_this, #new_search, #optimize, #remove, #remove!, #remove_all, #remove_all!, #search

Methods inherited from AbstractSessionProxy

delegate, not_supported

Constructor Details

#initialize(search_session, shard_sessions) ⇒ IdShardingSessionProxy

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.



32
33
34
35
# 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

Instance Attribute Details

#sessionsObject (readonly) Also known as: all_sessions

The shard sessions encapsulated by this class.



24
25
26
# File 'lib/sunspot/session_proxy/id_sharding_session_proxy.rb', line 24

def sessions
  @sessions
end

Instance Method Details

#remove_by_id(clazz, *ids) ⇒ Object

See Sunspot.remove_by_id



48
49
50
51
52
53
# 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) ⇒ Object

See Sunspot.remove_by_id!



58
59
60
61
62
63
# 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

#session_for(object) ⇒ Object

Return a session based on the hash of the class and ID, modulo the number of shard sessions.



41
42
43
# File 'lib/sunspot/session_proxy/id_sharding_session_proxy.rb', line 41

def session_for(object) #:nodoc:
  session_for_index_id(Adapters::InstanceAdapter.adapt(object).index_id)
end