-
Notifications
You must be signed in to change notification settings - Fork 972
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
7 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |