discourse/migrations/lib/common/set_store/key_value_set.rb
Gerhard Schlager 53d7a756d6
DEV: Add Migrations::SetStore to work with nested sets of data (#33593)
There are concrete implementations for a simple set, a key-value store,
and nested sets with 2 or 3 keys. The API stays the same for all
implementations and the performances is more or less the same as without
the wrapper (at least with YJIT enabled).
2025-07-24 12:11:41 +02:00

46 lines
786 B
Ruby
Vendored

# frozen_string_literal: true
module Migrations::SetStore
class KeyValueSet
include Interface
def initialize
@store = {}
end
def add(key, value)
(@store[key] ||= Set.new).add(value)
self
end
def add?(key, value)
!!(@store[key] ||= Set.new).add?(value)
end
def include?(key, value)
set = @store[key] or return false
set.include?(value)
end
def bulk_add(records)
current_key = nil
current_set = nil
records.each do |record|
key, value = record
if key != current_key
current_key = key
current_set = @store[key] ||= Set.new
end
current_set.add(value)
end
nil
end
def empty?
@store.empty?
end
end
end