Class: Sunspot::SessionProxy::IdShardingSessionProxy

Inherits:
ShardingSessionProxy show all
Defined in:
sunspot/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.

If you change the number of shard sessions that this proxy encapsulates, all objects will point to a different shard. 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

- (IdShardingSessionProxy) initialize(search_session, shard_sessions)

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 'sunspot/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

- (Object) sessions (readonly) Also known as: all_sessions

The shard sessions encapsulated by this class.



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

def sessions
  @sessions
end

Instance Method Details

- (Object) id_hash(id)



74
75
76
77
78
79
80
81
# File 'sunspot/lib/sunspot/session_proxy/id_sharding_session_proxy.rb', line 74

def id_hash(id)
  hash, i, len = 0, 0, id.length
  while i < len
    hash = hash * 31 + id[i]
    i += 1
  end
  hash
end

- (Object) remove_by_id(clazz, id)

See Sunspot.remove_by_id



48
49
50
51
52
# File 'sunspot/lib/sunspot/session_proxy/id_sharding_session_proxy.rb', line 48

def remove_by_id(clazz, id)
  session_for_index_id(
    Adapters::InstanceAdapter.index_id_for(clazz, id)
  ).remove_by_id(clazz, id)
end

- (Object) remove_by_id!(clazz, id)

See Sunspot.remove_by_id!



57
58
59
60
61
# File 'sunspot/lib/sunspot/session_proxy/id_sharding_session_proxy.rb', line 57

def remove_by_id!(clazz, id)
  session_for_index_id(
    Adapters::InstanceAdapter.index_id_for(clazz, id)
  ).remove_by_id!(clazz, id)
end

- (Object) session_for(object)

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



41
42
43
# File 'sunspot/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