MCPcopy Create free account
hub / github.com/AdRoll/baker / flushBlocks

Method flushBlocks

pkg/buffercache/cache.go:200–239  ·  view source on GitHub ↗

flushBlocks decompresses and flushes consecutive blocks in buf, interleaving each of them with a `\n` in the decompressed buffer.

(buf []byte)

Source from the content-addressed store, hash-verified

198// flushBlocks decompresses and flushes consecutive blocks in buf, interleaving each of
199// them with a `\n` in the decompressed buffer.
200func (c *BufferCache) flushBlocks(buf []byte) {
201 var (
202 pos uint64
203 zpos int // indices on buf and zbuf
204 n int // length of last decompressed buffer
205 )
206
207 // Decompress consecutive blocks. Each block is prepended with an uint32
208 // representing the block length. The outer loop moves pos and zpos.
209 // The inner loop handles failed decompression due to not enough space in
210 // the destination buffer to hold the currently decompressed block.
211 for pos != uint64(len(buf)) {
212 // Extract block length from the first 4 bytes.
213 prefix := uint64(binary.LittleEndian.Uint32(buf[pos : pos+4]))
214 blocklen, compressed := prefix&0x7fffffff, (prefix&0x80000000) != 0
215 pos += 4
216
217 if !compressed { // just copy it
218 for {
219 if uint64(len(c.decomp.buf[zpos:])) < blocklen {
220 c.decomp.grow()
221 continue
222 }
223 n = copy(c.decomp.buf[zpos:], buf[pos:pos+blocklen])
224 break
225 }
226 } else {
227 n = c.decomp.uncompress(zpos, buf[pos:pos+blocklen])
228 }
229
230 pos += blocklen
231 zpos += n
232
233 c.decomp.copyByte(zpos, '\n')
234 zpos++
235 }
236
237 // Return the effective part of c.zbuf
238 c.onFlush(c.decomp.bytes(zpos))
239}
240
241func (c *BufferCache) flushCold() {
242 for b := range c.cold.buckets {

Callers 2

flushHotMethod · 0.95
putInHotMethod · 0.95

Calls 4

uncompressMethod · 0.80
copyByteMethod · 0.80
bytesMethod · 0.80
growMethod · 0.45

Tested by

no test coverage detected