Class: AmplitudeExperiment::InMemoryCohortStorage

Inherits:
CohortStorage
  • Object
show all
Defined in:
lib/experiment/cohort/cohort_storage.rb

Instance Method Summary collapse

Constructor Details

#initializeInMemoryCohortStorage



34
35
36
37
38
39
# File 'lib/experiment/cohort/cohort_storage.rb', line 34

def initialize
  super
  @lock = Mutex.new
  @group_to_cohort_store = {}
  @cohort_store = {}
end

Instance Method Details

#cohort(cohort_id) ⇒ Object



41
42
43
44
45
# File 'lib/experiment/cohort/cohort_storage.rb', line 41

def cohort(cohort_id)
  @lock.synchronize do
    @cohort_store[cohort_id]
  end
end

#cohort_idsObject



85
86
87
88
89
# File 'lib/experiment/cohort/cohort_storage.rb', line 85

def cohort_ids
  @lock.synchronize do
    @cohort_store.keys.to_set
  end
end

#cohortsObject



47
48
49
50
51
# File 'lib/experiment/cohort/cohort_storage.rb', line 47

def cohorts
  @lock.synchronize do
    @cohort_store.dup
  end
end

#delete_cohort(group_type, cohort_id) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/experiment/cohort/cohort_storage.rb', line 77

def delete_cohort(group_type, cohort_id)
  @lock.synchronize do
    group_cohorts = @group_to_cohort_store[group_type] || Set.new
    group_cohorts.delete(cohort_id)
    @cohort_store.delete(cohort_id)
  end
end

#get_cohorts_for_group(group_type, group_name, cohort_ids) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/experiment/cohort/cohort_storage.rb', line 57

def get_cohorts_for_group(group_type, group_name, cohort_ids)
  result = Set.new
  @lock.synchronize do
    group_type_cohorts = @group_to_cohort_store[group_type] || Set.new
    group_type_cohorts.each do |cohort_id|
      members = @cohort_store[cohort_id]&.member_ids || Set.new
      result.add(cohort_id) if cohort_ids.include?(cohort_id) && members.include?(group_name)
    end
  end
  result
end

#get_cohorts_for_user(user_id, cohort_ids) ⇒ Object



53
54
55
# File 'lib/experiment/cohort/cohort_storage.rb', line 53

def get_cohorts_for_user(user_id, cohort_ids)
  get_cohorts_for_group(USER_GROUP_TYPE, user_id, cohort_ids)
end

#put_cohort(cohort) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/experiment/cohort/cohort_storage.rb', line 69

def put_cohort(cohort)
  @lock.synchronize do
    @group_to_cohort_store[cohort.group_type] ||= Set.new
    @group_to_cohort_store[cohort.group_type].add(cohort.id)
    @cohort_store[cohort.id] = cohort
  end
end