mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-26 14:00:42 +08:00
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).
46 lines
786 B
Ruby
Vendored
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
|