Class: LaunchDarkly::InMemoryFeatureStore

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/feature_store.rb

Instance Method Summary collapse

Constructor Details

#initializeInMemoryFeatureStore

Returns a new instance of InMemoryFeatureStore.



6
7
8
9
10
# File 'lib/ldclient-rb/feature_store.rb', line 6

def initialize
  @features = Hash.new
  @lock = Concurrent::ReadWriteLock.new
  @initialized = Concurrent::AtomicBoolean.new(false)
end

Instance Method Details

#allObject



19
20
21
22
23
# File 'lib/ldclient-rb/feature_store.rb', line 19

def all
  @lock.with_read_lock do
    @features.select { |_k, f| not f[:deleted] }
  end
end

#delete(key, version) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ldclient-rb/feature_store.rb', line 25

def delete(key, version)
  @lock.with_write_lock do
    old = @features[key.to_sym]

    if !old.nil? && old[:version] < version
      old[:deleted] = true
      old[:version] = version
      @features[key.to_sym] = old
    elsif old.nil?
      @features[key.to_sym] = { deleted: true, version: version }
    end
  end
end

#get(key) ⇒ Object



12
13
14
15
16
17
# File 'lib/ldclient-rb/feature_store.rb', line 12

def get(key)
  @lock.with_read_lock do
    f = @features[key.to_sym]
    (f.nil? || f[:deleted]) ? nil : f
  end
end

#init(fs) ⇒ Object



39
40
41
42
43
44
# File 'lib/ldclient-rb/feature_store.rb', line 39

def init(fs)
  @lock.with_write_lock do
    @features.replace(fs)
    @initialized.make_true
  end
end

#initialized?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/ldclient-rb/feature_store.rb', line 56

def initialized?
  @initialized.value
end

#upsert(key, feature) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/ldclient-rb/feature_store.rb', line 46

def upsert(key, feature)
  @lock.with_write_lock do
    old = @features[key.to_sym]

    if old.nil? || old[:version] < feature[:version]
      @features[key.to_sym] = feature
    end
  end
end