MCPcopy
hub / github.com/coocood/freecache / view

Method view

segment.go:259–274  ·  view source on GitHub ↗

view provides zero-copy access to the element's value, without copying to an intermediate buffer.

(key []byte, fn func([]byte) error, hashVal uint64, peek bool)

Source from the content-addressed store, hash-verified

257// view provides zero-copy access to the element's value, without copying to
258// an intermediate buffer.
259func (seg *segment) view(key []byte, fn func([]byte) error, hashVal uint64, peek bool) (err error) {
260 hdr, ptrOffset, err := seg.locate(key, hashVal, peek)
261 if err != nil {
262 return
263 }
264 start := ptrOffset + ENTRY_HDR_SIZE + int64(hdr.keyLen)
265 val, err := seg.rb.Slice(start, int64(hdr.valLen))
266 if err != nil {
267 return err
268 }
269 err = fn(val)
270 if !peek {
271 atomic.AddInt64(&seg.hitCount, 1)
272 }
273 return
274}
275
276func (seg *segment) locate(key []byte, hashVal uint64, peek bool) (hdrEntry entryHdr, ptrOffset int64, err error) {
277 slotId := uint8(hashVal >> 8)

Callers 2

GetFnMethod · 0.80
PeekFnMethod · 0.80

Calls 2

locateMethod · 0.95
SliceMethod · 0.80

Tested by

no test coverage detected