Skip to content

Commit

Permalink
fix: fix hash counter with delimeters containing non-utf8 data
Browse files Browse the repository at this point in the history
  • Loading branch information
ipagbox committed May 5, 2022
1 parent e10e51d commit fb05645
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true
source 'https://rubygems.org'

gemspec

gem 'nokogiri', '< 1.11.0', require: false
gem 'parallel', '< 1.21.0'
gem 'racc', ' < 1.6.0', require: false
2 changes: 2 additions & 0 deletions lib/redis_counters/hash_counter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'redis_counters/base_counter'
require 'redis_counters/clusterize_and_partitionize'
require 'string_tools'

module RedisCounters
# Счетчик на основе redis-hash, с возможностью партиционирования и кластеризации значений.
Expand Down Expand Up @@ -53,6 +54,7 @@ def partition_data(cluster, partition)
end

redis.hgetall(key(partition, cluster)).inject(Array.new) do |result, (key, value)|
key = key.dup.to_utf8
values = if delimiter_is_ary
if key.include?(new_delim)
key.split(new_delim, -1)
Expand Down
1 change: 1 addition & 0 deletions redis_counters.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']

spec.add_runtime_dependency 'activesupport', '>= 4.0', '< 5'
spec.add_runtime_dependency 'string_tools'

spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake'
Expand Down
21 changes: 21 additions & 0 deletions spec/redis_counters/hash_counter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,27 @@
end
end

context 'when non-utf8 data contains delimeters' do
let(:options) do
{
counter_name: :test_counter,
group_keys: [:title, :url],
partition_keys: [:date],
key_delimiter: '&',
value_delimiter: '|'
}
end
let(:partition) { {title: "Erg\xE4nzendes", url: 'http://example.com', date: '2022-05-04'} }

before do
redis.hincrbyfloat('test_counter&2022-05-04', "Erg\xE4nzendes|http://example.com", 1.0)
end

it 'successfully splits data' do
expect(counter.data).to eq([{'value' => 1, 'title' => 'Ergänzendes', 'url' => 'http://example.com'}])
end
end

context 'when check custom increment' do
let(:options) { {
:counter_name => :test_counter,
Expand Down

0 comments on commit fb05645

Please sign in to comment.