| 1 |
package memstream |
| 2 |
|
| 3 |
import ( |
| 4 |
"container/list" |
| 5 |
"math/rand" |
| 6 |
"time" |
| 7 |
) |
| 8 |
|
| 9 |
var random = rand.New(rand.NewSource(time.Now().UnixNano())) |
| 10 |
|
| 11 |
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") |
| 12 |
|
| 13 |
func randString(n int) string { |
| 14 |
b := make([]rune, n) |
| 15 |
for i := range b { |
| 16 |
b[i] = letters[random.Intn(len(letters))] |
| 17 |
} |
| 18 |
return string(b) |
| 19 |
} |
| 20 |
|
| 21 |
func genEpoch() string { |
| 22 |
return randString(4) |
| 23 |
} |
| 24 |
|
| 25 |
// Item to be kept inside stream. |
| 26 |
type Item struct { |
| 27 |
Offset uint64 |
| 28 |
Value interface{} |
| 29 |
} |
| 30 |
|
| 31 |
// Stream is a non-thread safe in-memory data structure that |
| 32 |
// maintains a stream of values limited by size and provides |
| 33 |
// methods to access a range of values from provided position. |
| 34 |
type Stream struct { |
| 35 |
top uint64 |
| 36 |
list *list.List |
| 37 |
index map[uint64]*list.Element |
| 38 |
epoch string |
| 39 |
} |
| 40 |
|
| 41 |
// New creates new Stream. |
| 42 |
func New() *Stream { |
| 43 |
return &Stream{ |
| 44 |
list: list.New(), |
| 45 |
index: make(map[uint64]*list.Element), |
| 46 |
epoch: genEpoch(), |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// Add item to stream. |
| 51 |
func (s *Stream) Add(v interface{}, size int) (uint64, error) { |
| 52 |
s.top++ |
| 53 |
item := Item{ |
| 54 |
Offset: s.top, |
| 55 |
Value: v, |
| 56 |
} |
| 57 |
el := s.list.PushBack(item) |
| 58 |
s.index[item.Offset] = el |
| 59 |
for s.list.Len() > size { |
| 60 |
el := s.list.Front() |
| 61 |
item := el.Value.(Item) |
| 62 |
s.list.Remove(el) |
| 63 |
delete(s.index, item.Offset) |
| 64 |
} |
| 65 |
return s.top, nil |
| 66 |
} |
| 67 |
|
| 68 |
// Top returns top of stream. |
| 69 |
func (s *Stream) Top() uint64 { |
| 70 |
return s.top |
| 71 |
} |
| 72 |
|
| 73 |
// Epoch returns epoch of stream. |
| 74 |
func (s *Stream) Epoch() string { |
| 75 |
return s.epoch |
| 76 |
} |
| 77 |
|
| 78 |
// Reset stream. |
| 79 |
func (s *Stream) Reset() { |
| 80 |
s.top = 0 |
| 81 |
s.epoch = genEpoch() |
| 82 |
s.Clear() |
| 83 |
} |
| 84 |
|
| 85 |
// Clear stream data. |
| 86 |
func (s *Stream) Clear() { |
| 87 |
s.list = list.New() |
| 88 |
s.index = make(map[uint64]*list.Element) |
| 89 |
} |
| 90 |
|
| 91 |
// Get items since provided position. |
| 92 |
// If seq is zero then elements since current first element in stream will be returned. |
| 93 |
func (s *Stream) Get(offset uint64, limit int) ([]Item, uint64, error) { |
| 94 |
if offset >= s.top+1 { |
| 95 |
return nil, s.top, nil |
| 96 |
} |
| 97 |
|
| 98 |
var el *list.Element |
| 99 |
if offset > 0 { |
| 100 |
var ok bool |
| 101 |
el, ok = s.index[offset] |
| 102 |
if !ok { |
| 103 |
el = s.list.Front() |
| 104 |
} |
| 105 |
} else { |
| 106 |
el = s.list.Front() |
| 107 |
} |
| 108 |
|
| 109 |
if el == nil { |
| 110 |
return nil, s.top, nil |
| 111 |
} |
| 112 |
|
| 113 |
var resultCap int |
| 114 |
if limit > 0 { |
| 115 |
resultCap = limit |
| 116 |
if resultCap > s.list.Len() { |
| 117 |
resultCap = s.list.Len() |
| 118 |
} |
| 119 |
} else { |
| 120 |
resultCap = s.list.Len() |
| 121 |
} |
| 122 |
|
| 123 |
result := make([]Item, 0, resultCap) |
| 124 |
|
| 125 |
item := el.Value.(Item) |
| 126 |
result = append(result, item) |
| 127 |
i := 1 |
| 128 |
for e := el.Next(); e != nil; e = e.Next() { |
| 129 |
if limit >= 0 && i >= limit { |
| 130 |
break |
| 131 |
} |
| 132 |
i++ |
| 133 |
item := e.Value.(Item) |
| 134 |
result = append(result, item) |
| 135 |
} |
| 136 |
return result, s.top, nil |
| 137 |
} |