putInHot places buf in the hot cache, copying it into a new hot cache buffer if that's the first with that key, or appending to the previous buffer with that key, with a separator in between them..
(key string, buf []byte, compressed bool)
| 274 | // if that's the first with that key, or appending to the previous buffer with |
| 275 | // that key, with a separator in between them.. |
| 276 | func (c *BufferCache) putInHot(key string, buf []byte, compressed bool) { |
| 277 | blen := len(buf) |
| 278 | if blen+4 > c.hot.maxbuflen { |
| 279 | panic(fmt.Sprintf("BufferCache: can't add buffers bigger than %d bytes", c.hot.maxbuflen)) |
| 280 | } |
| 281 | |
| 282 | if c.hot.cap+blen+4 > c.hot.maxcap { |
| 283 | // Adding this would exceed maximum hot cache capacity so we flush |
| 284 | // the whole cache. |
| 285 | c.flushHot() |
| 286 | // After flushing, this key would now be unique in the whole cache so |
| 287 | // it goes by definition in the cold cache. |
| 288 | c.putInCold(key, buf, compressed) |
| 289 | return |
| 290 | } |
| 291 | |
| 292 | bbuf, ok := c.hot.m[key] |
| 293 | if !ok { |
| 294 | // Create a hot cache entry |
| 295 | bbuf := make([]byte, blen+4) |
| 296 | |
| 297 | // Prefix the buffer with its size and the compressed bit. |
| 298 | prefix := uint32(blen) |
| 299 | if compressed { |
| 300 | // set the compressed bit |
| 301 | prefix |= 0x80000000 |
| 302 | } |
| 303 | |
| 304 | binary.LittleEndian.PutUint32(bbuf[:4], prefix) |
| 305 | copy(bbuf[4:], buf) |
| 306 | c.hot.m[key] = bbuf |
| 307 | c.hot.cap += blen + 4 |
| 308 | return |
| 309 | } |
| 310 | |
| 311 | bblen := len(bbuf) |
| 312 | if blen+bblen+4 > c.hot.maxbuflen { |
| 313 | // Adding this would exceed maximum hot buffer length so we flush this |
| 314 | // entry, removing both its location from the location map and the |
| 315 | // actual buffer from the hot cache. |
| 316 | delete(c.m.m, key) |
| 317 | delete(c.hot.m, key) |
| 318 | c.flushBlocks(bbuf) |
| 319 | c.hot.cap -= bblen |
| 320 | |
| 321 | // After flushing, the new key would now be unique in the whole cache, |
| 322 | // so by definition, its destination is the cold cache. |
| 323 | c.putInCold(key, buf, compressed) |
| 324 | return |
| 325 | } |
| 326 | |
| 327 | // Grow current entry before appending to it: |
| 328 | // - 4 bytes representing the prefix of the new buffer (length + compressed bit) |
| 329 | // - the new buffer itself |
| 330 | bbuf = append(bbuf, make([]byte, 4+blen)...) |
| 331 | prefix := uint32(blen) |
| 332 | if compressed { |
| 333 | // set the compressed bit |
no test coverage detected