You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
584 B
38 lines
584 B
3 years ago
|
package leveldb_shard
|
||
|
|
||
|
import (
|
||
|
"hash/crc32"
|
||
|
"sync"
|
||
|
"sync/atomic"
|
||
|
)
|
||
|
|
||
|
func mapDBIndex(key []byte, dbCount uint32) uint32 {
|
||
|
return crc32.ChecksumIEEE(key) % dbCount
|
||
|
}
|
||
|
|
||
|
func parallelRunAndReturnErr(parallelNum int, cb func(index int) error) error {
|
||
|
wg := sync.WaitGroup{}
|
||
|
errAtomic := atomic.Value{}
|
||
|
|
||
|
for i := 0; i < parallelNum; i++ {
|
||
|
wg.Add(1)
|
||
|
|
||
|
go func(i int) {
|
||
|
defer wg.Done()
|
||
|
|
||
|
err := cb(i)
|
||
|
if err != nil {
|
||
|
errAtomic.Store(err)
|
||
|
}
|
||
|
}(i)
|
||
|
}
|
||
|
|
||
|
wg.Wait()
|
||
|
|
||
|
if err := errAtomic.Load(); err != nil {
|
||
|
return errAtomic.Load().(error)
|
||
|
} else {
|
||
|
return nil
|
||
|
}
|
||
|
}
|