| 1 |
package fzf |
| 2 |
|
| 3 |
import "sync" |
| 4 |
|
| 5 |
// Chunk is a list of Items whose size has the upper limit of chunkSize |
| 6 |
type Chunk struct { |
| 7 |
items [chunkSize]Item |
| 8 |
count int |
| 9 |
} |
| 10 |
|
| 11 |
// ItemBuilder is a closure type that builds Item object from byte array |
| 12 |
type ItemBuilder func(*Item, []byte) bool |
| 13 |
|
| 14 |
// ChunkList is a list of Chunks |
| 15 |
type ChunkList struct { |
| 16 |
chunks []*Chunk |
| 17 |
mutex sync.Mutex |
| 18 |
trans ItemBuilder |
| 19 |
cache *ChunkCache |
| 20 |
} |
| 21 |
|
| 22 |
// NewChunkList returns a new ChunkList |
| 23 |
func NewChunkList(cache *ChunkCache, trans ItemBuilder) *ChunkList { |
| 24 |
return &ChunkList{ |
| 25 |
chunks: []*Chunk{}, |
| 26 |
mutex: sync.Mutex{}, |
| 27 |
trans: trans, |
| 28 |
cache: cache} |
| 29 |
} |
| 30 |
|
| 31 |
func (c *Chunk) push(trans ItemBuilder, data []byte) bool { |
| 32 |
if trans(&c.items[c.count], data) { |
| 33 |
c.count++ |
| 34 |
return true |
| 35 |
} |
| 36 |
return false |
| 37 |
} |
| 38 |
|
| 39 |
// IsFull returns true if the Chunk is full |
| 40 |
func (c *Chunk) IsFull() bool { |
| 41 |
return c.count == chunkSize |
| 42 |
} |
| 43 |
|
| 44 |
func (c *Chunk) lastIndex(minValue int32) int32 { |
| 45 |
if c.count == 0 { |
| 46 |
return minValue |
| 47 |
} |
| 48 |
return c.items[c.count-1].Index() + 1 // Exclusive |
| 49 |
} |
| 50 |
|
| 51 |
func (cl *ChunkList) lastChunk() *Chunk { |
| 52 |
return cl.chunks[len(cl.chunks)-1] |
| 53 |
} |
| 54 |
|
| 55 |
// GetItems returns the first n items from the given chunks |
| 56 |
func GetItems(chunks []*Chunk, n int) []Item { |
| 57 |
items := make([]Item, 0, n) |
| 58 |
for _, chunk := range chunks { |
| 59 |
for i := 0; i < chunk.count && len(items) < n; i++ { |
| 60 |
items = append(items, chunk.items[i]) |
| 61 |
} |
| 62 |
if len(items) >= n { |
| 63 |
break |
| 64 |
} |
| 65 |
} |
| 66 |
return items |
| 67 |
} |
| 68 |
|
| 69 |
// CountItems returns the total number of Items |
| 70 |
func CountItems(cs []*Chunk) int { |
| 71 |
if len(cs) == 0 { |
| 72 |
return 0 |
| 73 |
} |
| 74 |
if len(cs) == 1 { |
| 75 |
return cs[0].count |
| 76 |
} |
| 77 |
|
| 78 |
// First chunk might not be full due to --tail=N |
| 79 |
return cs[0].count + chunkSize*(len(cs)-2) + cs[len(cs)-1].count |
| 80 |
} |
| 81 |
|
| 82 |
// Push adds the item to the list |
| 83 |
func (cl *ChunkList) Push(data []byte) bool { |
| 84 |
cl.mutex.Lock() |
| 85 |
|
| 86 |
if len(cl.chunks) == 0 || cl.lastChunk().IsFull() { |
| 87 |
cl.chunks = append(cl.chunks, &Chunk{}) |
| 88 |
} |
| 89 |
|
| 90 |
ret := cl.lastChunk().push(cl.trans, data) |
| 91 |
cl.mutex.Unlock() |
| 92 |
return ret |
| 93 |
} |
| 94 |
|
| 95 |
// Clear clears the data |
| 96 |
func (cl *ChunkList) Clear() { |
| 97 |
cl.mutex.Lock() |
| 98 |
cl.chunks = nil |
| 99 |
cl.mutex.Unlock() |
| 100 |
} |
| 101 |
|
| 102 |
// ForEachItem iterates all items and applies fn to each one. |
| 103 |
// The done callback runs under the lock to safely update shared state. |
| 104 |
func (cl *ChunkList) ForEachItem(fn func(*Item), done func()) { |
| 105 |
cl.mutex.Lock() |
| 106 |
for _, chunk := range cl.chunks { |
| 107 |
for i := 0; i < chunk.count; i++ { |
| 108 |
fn(&chunk.items[i]) |
| 109 |
} |
| 110 |
} |
| 111 |
if done != nil { |
| 112 |
done() |
| 113 |
} |
| 114 |
cl.mutex.Unlock() |
| 115 |
} |
| 116 |
|
| 117 |
// Snapshot returns immutable snapshot of the ChunkList |
| 118 |
func (cl *ChunkList) Snapshot(tail int) ([]*Chunk, int, bool) { |
| 119 |
cl.mutex.Lock() |
| 120 |
|
| 121 |
changed := false |
| 122 |
if tail > 0 && CountItems(cl.chunks) > tail { |
| 123 |
changed = true |
| 124 |
// Find the number of chunks to keep |
| 125 |
numChunks := 0 |
| 126 |
for left, i := tail, len(cl.chunks)-1; left > 0 && i >= 0; i-- { |
| 127 |
numChunks++ |
| 128 |
left -= cl.chunks[i].count |
| 129 |
} |
| 130 |
|
| 131 |
// Copy the chunks to keep |
| 132 |
ret := make([]*Chunk, numChunks) |
| 133 |
minIndex := len(cl.chunks) - numChunks |
| 134 |
cl.cache.retire(cl.chunks[:minIndex]...) |
| 135 |
copy(ret, cl.chunks[minIndex:]) |
| 136 |
|
| 137 |
for left, i := tail, len(ret)-1; i >= 0; i-- { |
| 138 |
chunk := ret[i] |
| 139 |
if chunk.count > left { |
| 140 |
newChunk := *chunk |
| 141 |
newChunk.count = left |
| 142 |
oldCount := chunk.count |
| 143 |
for i := 0; i < left; i++ { |
| 144 |
newChunk.items[i] = chunk.items[oldCount-left+i] |
| 145 |
} |
| 146 |
ret[i] = &newChunk |
| 147 |
cl.cache.retire(chunk) |
| 148 |
break |
| 149 |
} |
| 150 |
left -= chunk.count |
| 151 |
} |
| 152 |
cl.chunks = ret |
| 153 |
} |
| 154 |
|
| 155 |
ret := make([]*Chunk, len(cl.chunks)) |
| 156 |
copy(ret, cl.chunks) |
| 157 |
|
| 158 |
// Duplicate the first and the last chunk |
| 159 |
if cnt := len(ret); cnt > 0 { |
| 160 |
if tail > 0 && cnt > 1 { |
| 161 |
newChunk := *ret[0] |
| 162 |
ret[0] = &newChunk |
| 163 |
} |
| 164 |
newChunk := *ret[cnt-1] |
| 165 |
ret[cnt-1] = &newChunk |
| 166 |
} |
| 167 |
|
| 168 |
cl.mutex.Unlock() |
| 169 |
return ret, CountItems(ret), changed |
| 170 |
} |