github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libkbfs/disk_block_cache_helper.go (about) 1 package libkbfs 2 3 import ( 4 "time" 5 6 "github.com/keybase/client/go/kbfs/kbfsblock" 7 "github.com/keybase/client/go/kbfs/kbfscrypto" 8 "github.com/keybase/client/go/kbfs/tlf" 9 ) 10 11 // diskBlockCacheEntry packages an encoded block and serverHalf into one data 12 // structure, allowing us to encode it as one set of bytes. 13 type diskBlockCacheEntry struct { 14 Buf []byte 15 ServerHalf kbfscrypto.BlockCryptKeyServerHalf 16 } 17 18 // Wrap time.Time so that go-codec falls back to using 19 // time.Time.MarshalBinary instead of its new msgpack timestamp 20 // extension encoding. 21 type legacyEncodedTime struct { 22 time.Time 23 } 24 25 // DiskBlockCacheMetadata packages the metadata needed to make decisions on 26 // cache eviction. 27 type DiskBlockCacheMetadata struct { 28 // the TLF ID for the block 29 TlfID tlf.ID 30 // the last time the block was used 31 LRUTime legacyEncodedTime 32 // the size of the block 33 BlockSize uint32 34 // whether the block has triggered prefetches 35 // This used to be called "HasPrefetched" so to maintain compatibility with 36 // existing disk caches, we have to name it that in the codec tag. 37 TriggeredPrefetch bool `codec:"HasPrefetched"` 38 // whether the block's triggered prefetches are complete 39 FinishedPrefetch bool 40 // the last tag with which the block was marked 41 Tag string 42 } 43 44 // PrefetchStatus returns the overall prefetch status corresponding to 45 // this metadata. 46 func (dbcm DiskBlockCacheMetadata) PrefetchStatus() PrefetchStatus { 47 if dbcm.FinishedPrefetch { 48 return FinishedPrefetch 49 } else if dbcm.TriggeredPrefetch { 50 return TriggeredPrefetch 51 } 52 return NoPrefetch 53 } 54 55 // lruEntry is an entry for sorting LRU times 56 type lruEntry struct { 57 BlockID kbfsblock.ID 58 Time time.Time 59 } 60 61 type blockIDsByTime []lruEntry 62 63 func (b blockIDsByTime) Len() int { return len(b) } 64 func (b blockIDsByTime) Swap(i, j int) { b[i], b[j] = b[j], b[i] } 65 func (b blockIDsByTime) Less(i, j int) bool { return b[i].Time.Before(b[j].Time) } 66 67 func (b blockIDsByTime) ToBlockIDSlice(numBlocks int) []kbfsblock.ID { 68 ids := make([]kbfsblock.ID, 0, numBlocks) 69 for _, entry := range b { 70 if len(ids) == numBlocks { 71 return ids 72 } 73 ids = append(ids, entry.BlockID) 74 } 75 return ids 76 }