2022-04-20 20:29:37 +02:00
|
|
|
package stack
|
2022-02-28 21:23:06 +01:00
|
|
|
|
2022-04-11 03:40:29 +02:00
|
|
|
// The db_stack package contains the implementation of a generic slice backed stack
|
|
|
|
// used for tracking various states in the hub, i.e. headers and txcounts
|
|
|
|
|
2022-03-08 19:29:24 +01:00
|
|
|
import (
|
|
|
|
"sync"
|
2022-04-11 03:40:29 +02:00
|
|
|
|
|
|
|
"github.com/lbryio/hub/internal"
|
2022-03-08 19:29:24 +01:00
|
|
|
)
|
2022-03-07 20:28:36 +01:00
|
|
|
|
2022-04-20 20:29:37 +02:00
|
|
|
type SliceBacked struct {
|
2022-02-28 21:23:06 +01:00
|
|
|
slice []interface{}
|
|
|
|
len uint32
|
2022-03-07 20:28:36 +01:00
|
|
|
mut sync.RWMutex
|
2022-02-28 21:23:06 +01:00
|
|
|
}
|
|
|
|
|
2022-04-20 20:29:37 +02:00
|
|
|
func NewSliceBacked(size int) *SliceBacked {
|
|
|
|
return &SliceBacked{
|
2022-02-28 21:23:06 +01:00
|
|
|
slice: make([]interface{}, size),
|
|
|
|
len: 0,
|
2022-03-07 20:28:36 +01:00
|
|
|
mut: sync.RWMutex{},
|
2022-02-28 21:23:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 20:29:37 +02:00
|
|
|
func (s *SliceBacked) Push(v interface{}) {
|
2022-03-07 20:28:36 +01:00
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
2022-02-28 21:23:06 +01:00
|
|
|
if s.len == uint32(len(s.slice)) {
|
|
|
|
s.slice = append(s.slice, v)
|
|
|
|
} else {
|
|
|
|
s.slice[s.len] = v
|
|
|
|
}
|
|
|
|
s.len++
|
|
|
|
}
|
|
|
|
|
2022-04-20 20:29:37 +02:00
|
|
|
func (s *SliceBacked) Pop() interface{} {
|
2022-03-07 20:28:36 +01:00
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
2022-02-28 21:23:06 +01:00
|
|
|
if s.len == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s.len--
|
|
|
|
return s.slice[s.len]
|
|
|
|
}
|
|
|
|
|
2022-04-20 20:29:37 +02:00
|
|
|
func (s *SliceBacked) Get(i uint32) interface{} {
|
2022-03-07 20:28:36 +01:00
|
|
|
s.mut.RLock()
|
|
|
|
defer s.mut.RUnlock()
|
|
|
|
|
2022-02-28 21:23:06 +01:00
|
|
|
if i >= s.len {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return s.slice[i]
|
|
|
|
}
|
|
|
|
|
2022-04-20 20:29:37 +02:00
|
|
|
func (s *SliceBacked) GetTip() interface{} {
|
2022-03-07 20:28:36 +01:00
|
|
|
s.mut.RLock()
|
|
|
|
defer s.mut.RUnlock()
|
|
|
|
|
2022-02-28 21:23:06 +01:00
|
|
|
if s.len == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return s.slice[s.len-1]
|
|
|
|
}
|
|
|
|
|
2022-04-20 20:29:37 +02:00
|
|
|
func (s *SliceBacked) Len() uint32 {
|
2022-03-07 20:28:36 +01:00
|
|
|
s.mut.RLock()
|
|
|
|
defer s.mut.RUnlock()
|
|
|
|
|
2022-02-28 21:23:06 +01:00
|
|
|
return s.len
|
|
|
|
}
|
|
|
|
|
2022-04-20 20:29:37 +02:00
|
|
|
func (s *SliceBacked) Cap() int {
|
2022-03-07 20:28:36 +01:00
|
|
|
s.mut.RLock()
|
|
|
|
defer s.mut.RUnlock()
|
|
|
|
|
2022-03-05 01:51:36 +01:00
|
|
|
return cap(s.slice)
|
2022-02-28 21:23:06 +01:00
|
|
|
}
|
|
|
|
|
2022-04-20 20:29:37 +02:00
|
|
|
func (s *SliceBacked) GetSlice() []interface{} {
|
2022-03-07 20:28:36 +01:00
|
|
|
// This is not thread safe so I won't bother with locking
|
2022-02-28 21:23:06 +01:00
|
|
|
return s.slice
|
|
|
|
}
|
2022-03-08 19:29:24 +01:00
|
|
|
|
|
|
|
// This function is dangerous because it assumes underlying types
|
2022-04-20 20:29:37 +02:00
|
|
|
func (s *SliceBacked) TxCountsBisectRight(txNum, rootTxNum uint32) (uint32, uint32) {
|
2022-03-08 19:29:24 +01:00
|
|
|
s.mut.RLock()
|
|
|
|
defer s.mut.RUnlock()
|
|
|
|
|
|
|
|
txCounts := s.slice[:s.Len()]
|
2022-04-11 03:40:29 +02:00
|
|
|
height := internal.BisectRight(txCounts, txNum)
|
|
|
|
createdHeight := internal.BisectRight(txCounts, rootTxNum)
|
2022-03-08 19:29:24 +01:00
|
|
|
|
|
|
|
return height, createdHeight
|
|
|
|
}
|