Class: Featureflip::Store::FlagStore

Inherits:
Object
  • Object
show all
Defined in:
lib/featureflip/store/flag_store.rb

Instance Method Summary collapse

Constructor Details

#initializeFlagStore

Returns a new instance of FlagStore.



4
5
6
7
8
# File 'lib/featureflip/store/flag_store.rb', line 4

def initialize
  @flags = {}
  @segments = {}
  @mutex = Mutex.new
end

Instance Method Details

#all_flagsObject



27
28
29
# File 'lib/featureflip/store/flag_store.rb', line 27

def all_flags
  @mutex.synchronize { @flags.values }
end

#all_flags_mapObject



31
32
33
# File 'lib/featureflip/store/flag_store.rb', line 31

def all_flags_map
  @mutex.synchronize { @flags.dup }
end

#get_flag(key) ⇒ Object



19
20
21
# File 'lib/featureflip/store/flag_store.rb', line 19

def get_flag(key)
  @mutex.synchronize { @flags[key] }
end

#get_segment(key) ⇒ Object



23
24
25
# File 'lib/featureflip/store/flag_store.rb', line 23

def get_segment(key)
  @mutex.synchronize { @segments[key] }
end

#init(flags, segments) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/featureflip/store/flag_store.rb', line 10

def init(flags, segments)
  @mutex.synchronize do
    @flags.clear
    @segments.clear
    flags.each { |f| @flags[f.key] = f }
    segments.each { |s| @segments[s.key] = s }
  end
end

#remove_flag(key) ⇒ Object



53
54
55
# File 'lib/featureflip/store/flag_store.rb', line 53

def remove_flag(key)
  @mutex.synchronize { @flags.delete(key) }
end

#upsert(flag) ⇒ Object

Apply a single flag delta (SSE flag.created / flag.updated).

Rejects only a strictly older config. Equal versions must be applied: the wire version is second-granular, so two edits to one flag inside the same wall-clock second carry an identical version, and treating equal as stale discarded the second edit outright. With streaming on there is no polling snapshot to correct it, so the store evaluated against the pre-edit config until an SSE sync or reconnect.

Re-applying an identical config is harmless; dropping a real one is not.



45
46
47
48
49
50
51
# File 'lib/featureflip/store/flag_store.rb', line 45

def upsert(flag)
  @mutex.synchronize do
    existing = @flags[flag.key]
    return if existing && existing.version > flag.version
    @flags[flag.key] = flag
  end
end