Class: FastExists::MultiTenant::Strategies::AdaptiveStrategy
- Inherits:
-
BaseStrategy
- Object
- BaseStrategy
- FastExists::MultiTenant::Strategies::AdaptiveStrategy
- Defined in:
- lib/fast_exists/multi_tenant/strategies/adaptive_strategy.rb
Instance Attribute Summary collapse
-
#per_tenant_strategy ⇒ Object
readonly
Returns the value of attribute per_tenant_strategy.
-
#pools ⇒ Object
readonly
Returns the value of attribute pools.
Attributes inherited from BaseStrategy
Instance Method Summary collapse
- #add(tenant_id, attribute, value) ⇒ Object
- #classify_tenant(_tenant_id, record_count) ⇒ Object
- #contains?(tenant_id, attribute, value) ⇒ Boolean
-
#initialize(options = {}) ⇒ AdaptiveStrategy
constructor
A new instance of AdaptiveStrategy.
- #set_tenant_category(tenant_id, category) ⇒ Object
- #stats ⇒ Object
- #tenant_category(tenant_id) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ AdaptiveStrategy
Returns a new instance of AdaptiveStrategy.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/fast_exists/multi_tenant/strategies/adaptive_strategy.rb', line 9 def initialize( = {}) super @thresholds = [:thresholds] || FastExists.configuration.tenant_thresholds @pools = { tiny: FastExists::MultiTenant::Pool.new(name: "tiny", expected_elements: 200_000), small: FastExists::MultiTenant::Pool.new(name: "small", expected_elements: 1_000_000), medium: FastExists::MultiTenant::Pool.new(name: "medium", expected_elements: 5_000_000) } @per_tenant_strategy = FastExists::MultiTenant::Strategies::PerTenantStrategy.new() @tenant_classification_cache = {} @mutex = Mutex.new end |
Instance Attribute Details
#per_tenant_strategy ⇒ Object (readonly)
Returns the value of attribute per_tenant_strategy.
7 8 9 |
# File 'lib/fast_exists/multi_tenant/strategies/adaptive_strategy.rb', line 7 def per_tenant_strategy @per_tenant_strategy end |
#pools ⇒ Object (readonly)
Returns the value of attribute pools.
7 8 9 |
# File 'lib/fast_exists/multi_tenant/strategies/adaptive_strategy.rb', line 7 def pools @pools end |
Instance Method Details
#add(tenant_id, attribute, value) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/fast_exists/multi_tenant/strategies/adaptive_strategy.rb', line 22 def add(tenant_id, attribute, value) category = tenant_category(tenant_id) if category == :large @per_tenant_strategy.add(tenant_id, attribute, value) else pool = @pools[category] || @pools[:tiny] pool.add(tenant_id, attribute, value) end end |
#classify_tenant(_tenant_id, record_count) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/fast_exists/multi_tenant/strategies/adaptive_strategy.rb', line 42 def classify_tenant(_tenant_id, record_count) tiny_max = @thresholds[:tiny] || 10_000 small_max = @thresholds[:small] || 100_000 medium_max = @thresholds[:medium] || 1_000_000 case record_count when 0...tiny_max then :tiny when tiny_max...small_max then :small when small_max...medium_max then :medium else :large end end |
#contains?(tenant_id, attribute, value) ⇒ Boolean
32 33 34 35 36 37 38 39 40 |
# File 'lib/fast_exists/multi_tenant/strategies/adaptive_strategy.rb', line 32 def contains?(tenant_id, attribute, value) category = tenant_category(tenant_id) if category == :large @per_tenant_strategy.contains?(tenant_id, attribute, value) else pool = @pools[category] || @pools[:tiny] pool.contains?(tenant_id, attribute, value) end end |
#set_tenant_category(tenant_id, category) ⇒ Object
55 56 57 58 59 |
# File 'lib/fast_exists/multi_tenant/strategies/adaptive_strategy.rb', line 55 def set_tenant_category(tenant_id, category) @mutex.synchronize do @tenant_classification_cache[tenant_id.to_s] = category.to_sym end end |
#stats ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/fast_exists/multi_tenant/strategies/adaptive_strategy.rb', line 67 def stats @mutex.synchronize do { strategy: :adaptive, classified_tenants: @tenant_classification_cache.size, distribution: @tenant_classification_cache.values.tally, pools: @pools.transform_values(&:stats), dedicated_filters: @per_tenant_strategy.stats } end end |
#tenant_category(tenant_id) ⇒ Object
61 62 63 64 65 |
# File 'lib/fast_exists/multi_tenant/strategies/adaptive_strategy.rb', line 61 def tenant_category(tenant_id) @mutex.synchronize do @tenant_classification_cache[tenant_id.to_s] || :tiny end end |