aboutsummaryrefslogtreecommitdiff
path: root/shards.go
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-08 21:04:46 +0200
committeralex <[email protected]>2026-07-08 21:04:46 +0200
commitaac1dd50b61090461cefc0263f065140be4bf50b (patch)
tree9bdb01936379a83c5a5651c7713e55c5c95c3972 /shards.go
parent2d2b8102ecc5b6a786a97c6c0fc49737c9088e63 (diff)
downloadredis-clone-aac1dd50b61090461cefc0263f065140be4bf50b.tar.xz
redis-clone-aac1dd50b61090461cefc0263f065140be4bf50b.zip
added ExecuteMulti method and refactored del to work with shards
Diffstat (limited to 'shards.go')
-rw-r--r--shards.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/shards.go b/shards.go
index c69f920..ae6e069 100644
--- a/shards.go
+++ b/shards.go
@@ -2,6 +2,7 @@ package main
import (
"hash/fnv"
+ "sort"
"sync"
)
type Shard struct{
@@ -24,3 +25,30 @@ func (db *RedisDB) Execute(key string, fn func(*Shard) interface{}) interface{}
defer shard.mu.Unlock()
return fn(shard)
}
+
+func ( db *RedisDB) ExecuteMulti(keys []string, fn func([]*Shard) interface{})interface{}{
+ shardMap := make(map[int]*Shard)
+ for _, k := range keys {
+ shard := db.getShard(k)
+ shardMap[shard.id] = shard
+ }
+ var sortedIDs []int
+ for id := range shardMap {
+ sortedIDs = append(sortedIDs, id)
+ }
+ sort.Ints(sortedIDs)
+ for _, id := range sortedIDs{
+ shardMap[id].mu.Lock()
+ }
+ defer func(){
+ for i := len(sortedIDs)-1; i >= 0; i --{
+ shardMap[sortedIDs[i]].mu.Unlock()
+ }
+ }()
+
+ shards := make([]*Shard, 0, len(shardMap))
+ for _, id := range sortedIDs {
+ shards = append(shards, shardMap[id])
+ }
+ return fn(shards)
+}