aboutsummaryrefslogtreecommitdiff
path: root/shards.go
diff options
context:
space:
mode:
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)
+}