Line data Source code
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 2 : func (v *Value) Buf() []byte { 20 2 : if v == nil { 21 0 : return nil 22 0 : } 23 2 : 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 2 : func (v *Value) Truncate(n int) { 31 2 : v.buf = v.buf[:n] 32 2 : } 33 : 34 2 : func (v *Value) refs() int32 { 35 2 : return v.ref.refs() 36 2 : } 37 : 38 2 : func (v *Value) acquire() { 39 2 : v.ref.acquire() 40 2 : } 41 : 42 2 : func (v *Value) release() { 43 2 : if v != nil && v.ref.release() { 44 2 : v.free() 45 2 : } 46 : }