From f4e85f701343358d4fbab469e743507887eee60f Mon Sep 17 00:00:00 2001 From: Holger Just Date: Tue, 6 Jun 2017 22:30:11 +0200 Subject: [PATCH] Use a Concurrent::Hash instead of a Set for Fields::Tags This makes the Rackstash::Fields::Tags class thread safe while still mantaining its existing semantincs, given that Ruby's set is also build on top of a simple Hash. --- lib/rackstash/fields/tags.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/rackstash/fields/tags.rb b/lib/rackstash/fields/tags.rb index 10cb1c1..b6758e8 100644 --- a/lib/rackstash/fields/tags.rb +++ b/lib/rackstash/fields/tags.rb @@ -11,18 +11,18 @@ module Rackstash module Fields class Tags < AbstractCollection def initialize - @raw = Set.new + @raw = Concurrent::Hash.new end def <<(tag) tag = resolve_value(tag) tag = utf8_encode(tag).freeze - @raw << tag unless tag.empty? + @raw[tag] = true unless tag.empty? self end def as_json(*) - @raw.to_a + @raw.keys end alias to_ary as_json alias to_a as_json @@ -42,18 +42,18 @@ module Rackstash def merge!(tags, scope: nil) tags = normalize_tags(tags, scope: scope) - tags.reject!(&:empty?) - - @raw.merge tags + Array(tags).each do |tag| + @raw[tag] = true unless tag.empty? + end self end def tagged?(tag) - @raw.include? utf8_encode(tag) + @raw.key? utf8_encode(tag) end def to_set - @raw.dup + Set.new to_a end protected