1
0
mirror of https://github.com/meineerde/rackstash.git synced 2025-12-19 15:01:12 +00:00

Use thread-safe raw objects for field hashes and arrays

This commit is contained in:
Holger Just 2017-02-14 22:47:55 +01:00
parent 3ec21e7646
commit bd376af884
5 changed files with 17 additions and 13 deletions

View File

@ -7,6 +7,8 @@ require 'bigdecimal'
require 'pathname' require 'pathname'
require 'uri' require 'uri'
require 'concurrent'
module Rackstash module Rackstash
module Fields module Fields
class AbstractCollection class AbstractCollection
@ -101,7 +103,7 @@ module Rackstash
when Rackstash::Fields::AbstractCollection when Rackstash::Fields::AbstractCollection
return wrap ? value : value.raw return wrap ? value : value.raw
when ::Hash when ::Hash
hash = value.each_with_object({}) do |(k, v), memo| hash = value.each_with_object(Concurrent::Hash.new) do |(k, v), memo|
memo[utf8_encode(k)] = normalize(v, scope: scope) memo[utf8_encode(k)] = normalize(v, scope: scope)
end end
hash = Rackstash::Fields::Hash.new.tap do |hash_field| hash = Rackstash::Fields::Hash.new.tap do |hash_field|
@ -109,7 +111,9 @@ module Rackstash
end if wrap end if wrap
return hash return hash
when ::Array, ::Set, ::Enumerator when ::Array, ::Set, ::Enumerator
array = value.map { |e| normalize(e, scope: scope) } array = value.each_with_object(Concurrent::Array.new) do |e, memo|
memo << normalize(e, scope: scope)
end
array = Rackstash::Fields::Array.new.tap do |array_field| array = Rackstash::Fields::Array.new.tap do |array_field|
array_field.raw = array array_field.raw = array
end if wrap end if wrap

View File

@ -9,7 +9,7 @@ module Rackstash
module Fields module Fields
class Array < AbstractCollection class Array < AbstractCollection
def initialize def initialize
@raw = [] @raw = Concurrent::Array.new
end end
# Retrieve a stored value from a given `index` # Retrieve a stored value from a given `index`

View File

@ -11,7 +11,7 @@ module Rackstash
# @param forbidden_keys [Set<String>,Array<String>] a list of strings # @param forbidden_keys [Set<String>,Array<String>] a list of strings
# which are not allowed to be used as keys in this hash # which are not allowed to be used as keys in this hash
def initialize(forbidden_keys: EMPTY_SET) def initialize(forbidden_keys: EMPTY_SET)
@raw = {} @raw = Concurrent::Hash.new
if forbidden_keys.is_a?(Set) if forbidden_keys.is_a?(Set)
forbidden_keys = forbidden_keys.dup.freeze unless forbidden_keys.frozen? forbidden_keys = forbidden_keys.dup.freeze unless forbidden_keys.frozen?

View File

@ -56,7 +56,7 @@ describe Rackstash::Fields::Array do
end end
it 'returns a simple array' do it 'returns a simple array' do
expect(array.as_json).to be_a ::Array expect(array.as_json).to be_instance_of ::Array
expect(array.as_json.length).to eql 3 expect(array.as_json.length).to eql 3
end end
@ -70,7 +70,7 @@ describe Rackstash::Fields::Array do
it 'returns a nested array' do it 'returns a nested array' do
expect(array[2]).to be_a Rackstash::Fields::Array expect(array[2]).to be_a Rackstash::Fields::Array
expect(array.as_json[2]).to be_an ::Array expect(array.as_json[2]).to be_instance_of ::Array
expect(array.as_json[2]).to eql %w[v1 v2] expect(array.as_json[2]).to eql %w[v1 v2]
end end

View File

@ -100,21 +100,21 @@ describe Rackstash::Fields::Hash do
end end
it 'returns a simple hash' do it 'returns a simple hash' do
expect(hash.as_json).to be_a ::Hash expect(hash.as_json).to be_instance_of ::Hash
expect(hash.as_json.keys).to eql %w[simple hash array] expect(hash.as_json.keys).to eql %w[simple hash array]
end end
it 'returns a nested hash' do it 'returns a nested hash' do
expect(hash['hash']).to be_a Rackstash::Fields::Hash expect(hash['hash']).to be_instance_of Rackstash::Fields::Hash
expect(hash.as_json['hash']).to be_a Hash expect(hash.as_json['hash']).to be_instance_of Hash
expect(hash.as_json['hash']).to eql 'key' => 'nested value', 'number' => 42 expect(hash.as_json['hash']).to eql 'key' => 'nested value', 'number' => 42
end end
it 'returns a nested array' do it 'returns a nested array' do
expect(hash['array']).to be_a Rackstash::Fields::Array expect(hash['array']).to be_instance_of Rackstash::Fields::Array
expect(hash.as_json['array']).to be_an ::Array expect(hash.as_json['array']).to be_instance_of ::Array
expect(hash.as_json['array']).to eql %w[v1 v2] expect(hash.as_json['array']).to eql %w[v1 v2]
end end
@ -291,7 +291,7 @@ describe Rackstash::Fields::Hash do
it 'returns a new object' do it 'returns a new object' do
new_hash = hash.merge(foo: :bar) new_hash = hash.merge(foo: :bar)
expect(new_hash).to be_a Rackstash::Fields::Hash expect(new_hash).to be_instance_of Rackstash::Fields::Hash
expect(new_hash).not_to equal hash expect(new_hash).not_to equal hash
# The origiginal hash is not changed # The origiginal hash is not changed
@ -370,7 +370,7 @@ describe Rackstash::Fields::Hash do
raw = { :time => Time.now, 'string' => 'foo' } raw = { :time => Time.now, 'string' => 'foo' }
hash = Rackstash::Fields::Hash(raw) hash = Rackstash::Fields::Hash(raw)
expect(hash).to be_a Rackstash::Fields::Hash expect(hash).to be_instance_of Rackstash::Fields::Hash
expect(hash['time']).to be_a String expect(hash['time']).to be_a String
expect(hash['string']).to eql 'foo' expect(hash['string']).to eql 'foo'
end end