Commit 4616c214 authored by Matthew Sykes's avatar Matthew Sykes
Browse files

[FAB-11684] serialization in ram ledger simpleList



Change-Id: I79355d46daa9d2495edc1779dc3aed816cd6a284
Signed-off-by: default avatarMatthew Sykes <sykesmat@us.ibm.com>
parent c454413a
......@@ -26,11 +26,24 @@ type cursor struct {
}
type simpleList struct {
lock sync.RWMutex
next *simpleList
signal chan struct{}
block *cb.Block
}
func (s *simpleList) getNext() *simpleList {
s.lock.RLock()
defer s.lock.RUnlock()
return s.next
}
func (s *simpleList) setNext(n *simpleList) {
s.lock.Lock()
defer s.lock.Unlock()
s.next = n
}
type ramLedger struct {
lock sync.RWMutex
maxSize int
......@@ -44,8 +57,9 @@ type ramLedger struct {
func (cu *cursor) Next() (*cb.Block, cb.Status) {
// This only loops once, as signal reading indicates non-nil next
for {
if cu.list.next != nil {
cu.list = cu.list.next
next := cu.list.getNext()
if next != nil {
cu.list = next
return cu.list.block, cb.Status_SUCCESS
}
<-cu.list.signal
......@@ -106,7 +120,7 @@ func (rl *ramLedger) Iterator(startPosition *ab.SeekPosition) (blockledger.Itera
if list.block.Header.Number == specified-1 {
break
}
list = list.next // No need for nil check, because of range check above
list = list.getNext() // No need for nil check, because of range check above
}
}
cursor := &cursor{list: list}
......@@ -150,14 +164,15 @@ func (rl *ramLedger) Append(block *cb.Block) error {
}
func (rl *ramLedger) appendBlock(block *cb.Block) {
rl.newest.next = &simpleList{
next := &simpleList{
signal: make(chan struct{}),
block: block,
}
rl.newest.setNext(next)
lastSignal := rl.newest.signal
logger.Debugf("Sending signal that block %d has a successor", rl.newest.block.Header.Number)
rl.newest = rl.newest.next
rl.newest = rl.newest.getNext()
close(lastSignal)
rl.size++
......@@ -165,7 +180,7 @@ func (rl *ramLedger) appendBlock(block *cb.Block) {
if rl.size > rl.maxSize {
logger.Debugf("RAM ledger max size about to be exceeded, removing oldest item: %d",
rl.oldest.block.Header.Number)
rl.oldest = rl.oldest.next
rl.oldest = rl.oldest.getNext()
rl.size--
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment