Skip to content
Myr

chunklist.go

This file

Path

src/chunklist.go

170 lines · 3.6 KiB · Go

Last changed by

Not known.

History of this file

Take it

1package fzf23import "sync"45// Chunk is a list of Items whose size has the upper limit of chunkSize6type Chunk struct {7	items [chunkSize]Item8	count int9}1011// ItemBuilder is a closure type that builds Item object from byte array12type ItemBuilder func(*Item, []byte) bool1314// ChunkList is a list of Chunks15type ChunkList struct {16	chunks []*Chunk17	mutex  sync.Mutex18	trans  ItemBuilder19	cache  *ChunkCache20}2122// NewChunkList returns a new ChunkList23func NewChunkList(cache *ChunkCache, trans ItemBuilder) *ChunkList {24	return &ChunkList{25		chunks: []*Chunk{},26		mutex:  sync.Mutex{},27		trans:  trans,28		cache:  cache}29}3031func (c *Chunk) push(trans ItemBuilder, data []byte) bool {32	if trans(&c.items[c.count], data) {33		c.count++34		return true35	}36	return false37}3839// IsFull returns true if the Chunk is full40func (c *Chunk) IsFull() bool {41	return c.count == chunkSize42}4344func (c *Chunk) lastIndex(minValue int32) int32 {45	if c.count == 0 {46		return minValue47	}48	return c.items[c.count-1].Index() + 1 // Exclusive49}5051func (cl *ChunkList) lastChunk() *Chunk {52	return cl.chunks[len(cl.chunks)-1]53}5455// GetItems returns the first n items from the given chunks56func 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			break64		}65	}66	return items67}6869// CountItems returns the total number of Items70func CountItems(cs []*Chunk) int {71	if len(cs) == 0 {72		return 073	}74	if len(cs) == 1 {75		return cs[0].count76	}7778	// First chunk might not be full due to --tail=N79	return cs[0].count + chunkSize*(len(cs)-2) + cs[len(cs)-1].count80}8182// Push adds the item to the list83func (cl *ChunkList) Push(data []byte) bool {84	cl.mutex.Lock()8586	if len(cl.chunks) == 0 || cl.lastChunk().IsFull() {87		cl.chunks = append(cl.chunks, &Chunk{})88	}8990	ret := cl.lastChunk().push(cl.trans, data)91	cl.mutex.Unlock()92	return ret93}9495// Clear clears the data96func (cl *ChunkList) Clear() {97	cl.mutex.Lock()98	cl.chunks = nil99	cl.mutex.Unlock()100}101102// ForEachItem iterates all items and applies fn to each one.103// The done callback runs under the lock to safely update shared state.104func (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}116117// Snapshot returns immutable snapshot of the ChunkList118func (cl *ChunkList) Snapshot(tail int) ([]*Chunk, int, bool) {119	cl.mutex.Lock()120121	changed := false122	if tail > 0 && CountItems(cl.chunks) > tail {123		changed = true124		// Find the number of chunks to keep125		numChunks := 0126		for left, i := tail, len(cl.chunks)-1; left > 0 && i >= 0; i-- {127			numChunks++128			left -= cl.chunks[i].count129		}130131		// Copy the chunks to keep132		ret := make([]*Chunk, numChunks)133		minIndex := len(cl.chunks) - numChunks134		cl.cache.retire(cl.chunks[:minIndex]...)135		copy(ret, cl.chunks[minIndex:])136137		for left, i := tail, len(ret)-1; i >= 0; i-- {138			chunk := ret[i]139			if chunk.count > left {140				newChunk := *chunk141				newChunk.count = left142				oldCount := chunk.count143				for i := 0; i < left; i++ {144					newChunk.items[i] = chunk.items[oldCount-left+i]145				}146				ret[i] = &newChunk147				cl.cache.retire(chunk)148				break149			}150			left -= chunk.count151		}152		cl.chunks = ret153	}154155	ret := make([]*Chunk, len(cl.chunks))156	copy(ret, cl.chunks)157158	// Duplicate the first and the last chunk159	if cnt := len(ret); cnt > 0 {160		if tail > 0 && cnt > 1 {161			newChunk := *ret[0]162			ret[0] = &newChunk163		}164		newChunk := *ret[cnt-1]165		ret[cnt-1] = &newChunk166	}167168	cl.mutex.Unlock()169	return ret, CountItems(ret), changed170}