Sha256: be74372afc2fd931ca311498babd9f372cefbef352cb067cdde85e1a8960e6f2

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

require "concurrent/atomics"

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

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

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

    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

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

    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

    def initialized?
      @initialized.value
    end

    def stop
      # nothing to do
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ldclient-rb-2.5.0 lib/ldclient-rb/feature_store.rb
ldclient-rb-2.4.1 lib/ldclient-rb/feature_store.rb
ldclient-rb-2.4.0 lib/ldclient-rb/feature_store.rb
ldclient-rb-2.3.2 lib/ldclient-rb/feature_store.rb
ldclient-rb-2.3.1 lib/ldclient-rb/feature_store.rb