forked from bitmark-inc/bitmarkd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringreader.go
43 lines (37 loc) · 788 Bytes
/
ringreader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright (c) 2014-2017 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package blockring
// to iterate though the ring
type RingReader struct {
stop int
current int
}
// start of ring iterator
func NewRingReader() *RingReader {
globalData.Lock()
i := globalData.ringIndex
globalData.Unlock()
c := i - 1
if c < 0 {
c = len(globalData.ring) - 1
}
r := &RingReader{
stop: i,
current: c,
}
return r
}
// fetch item from ring
// works in reverse, fetching older items
func (r *RingReader) Get() (uint64, bool) {
if r.stop == r.current {
return 0, false
}
crc := globalData.ring[r.current].crc
r.current -= 1
if r.current < 0 {
r.current = len(globalData.ring) - 1
}
return crc, true
}