Skip to content

Commit

Permalink
Update striplock.go
Browse files Browse the repository at this point in the history
  • Loading branch information
crStiv authored Jan 15, 2025
1 parent 4d3d3c2 commit b3ccdf1
Showing 1 changed file with 7 additions and 45 deletions.
52 changes: 7 additions & 45 deletions store/striplock.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,17 @@
package store

import (
"sync"

"github.com/celestiaorg/celestia-node/share"
"github.com/celestiaorg/celestia-node/utils"
)

// TODO: move to utils
type striplock struct {
heights []*sync.RWMutex
datahashes []*sync.RWMutex
}
// Deprecated: Use utils.StripLock instead
type striplock = utils.StripLock

type multiLock struct {
mu []*sync.RWMutex
}
// Deprecated: Use utils.MultiLock instead
type multiLock = utils.MultiLock

// Deprecated: Use utils.NewStripLock instead
func newStripLock(size int) *striplock {
heights := make([]*sync.RWMutex, size)
datahashes := make([]*sync.RWMutex, size)
for i := 0; i < size; i++ {
heights[i] = &sync.RWMutex{}
datahashes[i] = &sync.RWMutex{}
}
return &striplock{heights, datahashes}
}

func (l *striplock) byHeight(height uint64) *sync.RWMutex {
lkIdx := height % uint64(len(l.heights))
return l.heights[lkIdx]
}

func (l *striplock) byHash(datahash share.DataHash) *sync.RWMutex {
// Use the last 2 bytes of the hash as key to distribute the locks
last := uint16(datahash[len(datahash)-1]) | uint16(datahash[len(datahash)-2])<<8
lkIdx := last % uint16(len(l.datahashes))
return l.datahashes[lkIdx]
}

func (l *striplock) byHashAndHeight(datahash share.DataHash, height uint64) *multiLock {
return &multiLock{[]*sync.RWMutex{l.byHash(datahash), l.byHeight(height)}}
}

func (m *multiLock) lock() {
for _, lk := range m.mu {
lk.Lock()
}
}

func (m *multiLock) unlock() {
for _, lk := range m.mu {
lk.Unlock()
}
return utils.NewStripLock(size)
}

0 comments on commit b3ccdf1

Please sign in to comment.