github.com/cockroachdb/pebble@v1.1.2/internal/cache/value.go (about) 1 // Copyright 2020 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 // of this source code is governed by a BSD-style license that can be found in 3 // the LICENSE file. 4 5 package cache 6 7 // Value holds a reference counted immutable value. 8 type Value struct { 9 buf []byte 10 // Reference count for the value. The value is freed when the reference count 11 // drops to zero. 12 ref refcnt 13 } 14 15 // Buf returns the buffer associated with the value. The contents of the buffer 16 // should not be changed once the value has been added to the cache. Instead, a 17 // new Value should be created and added to the cache to replace the existing 18 // value. 19 func (v *Value) Buf() []byte { 20 if v == nil { 21 return nil 22 } 23 return v.buf 24 } 25 26 // Truncate the buffer to the specified length. The buffer length should not be 27 // changed once the value has been added to the cache as there may be 28 // concurrent readers of the Value. Instead, a new Value should be created and 29 // added to the cache to replace the existing value. 30 func (v *Value) Truncate(n int) { 31 v.buf = v.buf[:n] 32 } 33 34 func (v *Value) refs() int32 { 35 return v.ref.refs() 36 } 37 38 func (v *Value) acquire() { 39 v.ref.acquire() 40 } 41 42 func (v *Value) release() { 43 if v != nil && v.ref.release() { 44 v.free() 45 } 46 }