Skip to content

Commit

Permalink
feature(unique_values_list): introduce has_value? method
Browse files Browse the repository at this point in the history
  • Loading branch information
Napolskih committed Oct 17, 2014
1 parent cccc2be commit e1206b7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
14 changes: 12 additions & 2 deletions lib/redis_counters/unique_values_lists/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ class Base < RedisCounters::BaseCounter

alias_method :add, :process

# Public: Проверяет существует ли заданное значение.
#
# value_params - Hash - параметры значения.
#
# Returns Boolean.
#
def has_value?(value_params)
raise NotImplementedError
end

protected

def value
value_params = value_keys.map { |key| params.fetch(key) }
def value(value_params = params)
value_params = value_keys.map { |key| value_params.fetch(key) }
value_params.join(value_delimiter)
end

Expand Down
20 changes: 15 additions & 5 deletions lib/redis_counters/unique_values_lists/blocking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ module UniqueValuesLists
class Blocking < Base
PARTITIONS_LIST_POSTFIX = :partitions

# Public: Проверяет существует ли заданное значение.
#
# value_params - Hash - параметры значения.
#
# Returns Boolean.
#
def has_value?(value_params)
all_partitions.reverse.any? do |partition|
redis.sismember(key(partition), value(value_params))
end
end

# Public: Нетранзакционно удаляет данные конкретной конечной партиции.
#
# params - Hash - хеш параметров, определяющий кластер и партицию.
Expand Down Expand Up @@ -55,7 +67,7 @@ def process_value
watch_partitions_list
watch_all_partitions

if value_already_exists?
if current_value_already_exists?
redis.unwatch
return false
end
Expand Down Expand Up @@ -85,10 +97,8 @@ def watch_all_partitions
end
end

def value_already_exists?
all_partitions.reverse.any? do |partition|
redis.sismember(key(partition), value)
end
def current_value_already_exists?
has_value?(params)
end

def add_value
Expand Down
12 changes: 11 additions & 1 deletion lib/redis_counters/unique_values_lists/non_blocking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@ module UniqueValuesLists
# или воспользоваться методами delete_all! или delete_all_direct!,
# для удаления всех партиций кластера включая основную.

class NonBlocking < UniqueValuesLists::Base
class NonBlocking < Base

# Public: Проверяет существует ли заданное значение.
#
# value_params - Hash - параметры значения.
#
# Returns Boolean.
#
def has_value?(value_params)
redis.sismember(main_partition_key, value(value_params))
end

# Public: Нетранзакционно удаляет все данные счетчика в кластере, включая основную партицию.
# Если кластеризация не используется, то удаляет все данные.
Expand Down
18 changes: 18 additions & 0 deletions spec/support/unique_values_lists/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@
end
end

context '#has_value?' do
let(:options) { {
:counter_name => :test_counter,
:value_keys => [:param0]
} }

context 'when item not exists' do
it { expect(counter.has_value?(:param0 => 1)).to be_false }
end

context 'when item exists' do
before { counter.add(:param0 => 1) }

it { expect(counter.has_value?(:param0 => 1)).to be_true }
it { expect(counter.has_value?(:param0 => 2)).to be_false }
end
end

context '#partitions' do
let(:cluster1_subcluster1) { {:cluster => :cluster1, :subcluster => :subcluster1} }
let(:cluster1_subcluster2) { {:cluster => :cluster1, :subcluster => :subcluster2} }
Expand Down

0 comments on commit e1206b7

Please sign in to comment.