2020-07-21 20:09:26 -04:00
# frozen_string_literal: true
module Gitlab
module Redis
class HLL
2020-10-05 08:08:47 -04:00
BATCH_SIZE = 300
2020-08-10 14:09:54 -04:00
KEY_REGEX = %r{ \ A( \ w|-|:)* \{ \ w* \} ( \ w|-|:)* \ z } . freeze
KeyFormatError = Class . new ( StandardError )
2020-07-21 20:09:26 -04:00
def self . count ( params )
2020-09-30 08:09:53 -04:00
self . new . count ( ** params )
2020-07-21 20:09:26 -04:00
end
def self . add ( params )
2020-09-30 08:09:53 -04:00
self . new . add ( ** params )
2020-07-21 20:09:26 -04:00
end
def count ( keys : )
Gitlab :: Redis :: SharedState . with do | redis |
redis . pfcount ( * keys )
end
end
2020-08-10 14:09:54 -04:00
# Check a basic format for the Redis key in order to ensure the keys are in the same hash slot
#
# Examples of keys
# project:{1}:set_a
# project:{1}:set_b
# project:{2}:set_c
# 2020-216-{project_action}
# i_{analytics}_dev_ops_score-2020-32
2020-07-21 20:09:26 -04:00
def add ( key : , value : , expiry : )
2020-10-05 08:08:47 -04:00
validate_key! ( key )
2020-08-10 14:09:54 -04:00
2020-07-21 20:09:26 -04:00
Gitlab :: Redis :: SharedState . with do | redis |
redis . multi do | multi |
2020-10-05 08:08:47 -04:00
Array . wrap ( value ) . each_slice ( BATCH_SIZE ) { | batch | multi . pfadd ( key , batch ) }
2020-07-21 20:09:26 -04:00
multi . expire ( key , expiry )
end
end
end
2020-10-05 08:08:47 -04:00
private
def validate_key! ( key )
return if KEY_REGEX . match? ( key )
raise KeyFormatError . new ( " Invalid key format. #{ key } key should have changeable parts in curly braces. See https://docs.gitlab.com/ee/development/redis.html # multi-key-commands " )
end
2020-07-21 20:09:26 -04:00
end
end
end