Merge pull request #7232 from trevor211/handleHashTagWhenComputingHashSlot

Tcl client support hash tagged keys.
This commit is contained in:
Salvatore Sanfilippo 2020-05-19 09:23:44 +02:00 committed by GitHub
commit 9fba05f758
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 3 deletions

View File

@ -41,4 +41,10 @@ test "client do not break when cluster slot" {
if { [catch {R 0 cluster slots}] } { if { [catch {R 0 cluster slots}] } {
fail "output overflow when cluster slots" fail "output overflow when cluster slots"
} }
} }
test "client can handle keys with hash tag" {
set cluster [redis_cluster 127.0.0.1:[get_instance_attrib redis 0 port]]
$cluster set foo{tag} bar
$cluster close
}

View File

@ -286,8 +286,29 @@ proc ::redis_cluster::crc16 {s} {
# Hash a single key returning the slot it belongs to, Implemented hash # Hash a single key returning the slot it belongs to, Implemented hash
# tags as described in the Redis Cluster specification. # tags as described in the Redis Cluster specification.
proc ::redis_cluster::hash {key} { proc ::redis_cluster::hash {key} {
# TODO: Handle hash slots. set keylen [string length $key]
expr {[::redis_cluster::crc16 $key] & 16383} set s {}
set e {}
for {set s 0} {$s < $keylen} {incr s} {
if {[string index $key $s] eq "\{"} break
}
if {[expr {$s == $keylen}]} {
set res [expr {[crc16 $key] & 16383}]
return $res
}
for {set e [expr {$s+1}]} {$e < $keylen} {incr e} {
if {[string index $key $e] == "\}"} break
}
if {$e == $keylen || $e == [expr {$s+1}]} {
set res [expr {[crc16 $key] & 16383}]
return $res
}
set key_sub [string range $key [expr {$s+1}] [expr {$e-1}]]
return [expr {[crc16 $key_sub] & 16383}]
} }
# Return the slot the specified keys hash to. # Return the slot the specified keys hash to.