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:
parent
3ec21e7646
commit
bd376af884
@ -7,6 +7,8 @@ require 'bigdecimal'
|
||||
require 'pathname'
|
||||
require 'uri'
|
||||
|
||||
require 'concurrent'
|
||||
|
||||
module Rackstash
|
||||
module Fields
|
||||
class AbstractCollection
|
||||
@ -101,7 +103,7 @@ module Rackstash
|
||||
when Rackstash::Fields::AbstractCollection
|
||||
return wrap ? value : value.raw
|
||||
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)
|
||||
end
|
||||
hash = Rackstash::Fields::Hash.new.tap do |hash_field|
|
||||
@ -109,7 +111,9 @@ module Rackstash
|
||||
end if wrap
|
||||
return hash
|
||||
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_field.raw = array
|
||||
end if wrap
|
||||
|
||||
@ -9,7 +9,7 @@ module Rackstash
|
||||
module Fields
|
||||
class Array < AbstractCollection
|
||||
def initialize
|
||||
@raw = []
|
||||
@raw = Concurrent::Array.new
|
||||
end
|
||||
|
||||
# Retrieve a stored value from a given `index`
|
||||
|
||||
@ -11,7 +11,7 @@ module Rackstash
|
||||
# @param forbidden_keys [Set<String>,Array<String>] a list of strings
|
||||
# which are not allowed to be used as keys in this hash
|
||||
def initialize(forbidden_keys: EMPTY_SET)
|
||||
@raw = {}
|
||||
@raw = Concurrent::Hash.new
|
||||
|
||||
if forbidden_keys.is_a?(Set)
|
||||
forbidden_keys = forbidden_keys.dup.freeze unless forbidden_keys.frozen?
|
||||
|
||||
@ -56,7 +56,7 @@ describe Rackstash::Fields::Array do
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
@ -70,7 +70,7 @@ describe Rackstash::Fields::Array do
|
||||
it 'returns a nested array' do
|
||||
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]
|
||||
end
|
||||
|
||||
|
||||
@ -100,21 +100,21 @@ describe Rackstash::Fields::Hash do
|
||||
end
|
||||
|
||||
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]
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
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]
|
||||
end
|
||||
|
||||
@ -291,7 +291,7 @@ describe Rackstash::Fields::Hash do
|
||||
it 'returns a new object' do
|
||||
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
|
||||
|
||||
# The origiginal hash is not changed
|
||||
@ -370,7 +370,7 @@ describe Rackstash::Fields::Hash do
|
||||
raw = { :time => Time.now, 'string' => 'foo' }
|
||||
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['string']).to eql 'foo'
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user