forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk_block_cache_helper.go
56 lines (48 loc) · 1.55 KB
/
disk_block_cache_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package libkbfs
import (
"time"
"github.com/keybase/kbfs/kbfsblock"
"github.com/keybase/kbfs/kbfscrypto"
"github.com/keybase/kbfs/tlf"
)
// diskBlockCacheEntry packages an encoded block and serverHalf into one data
// structure, allowing us to encode it as one set of bytes.
type diskBlockCacheEntry struct {
Buf []byte
ServerHalf kbfscrypto.BlockCryptKeyServerHalf
}
// DiskBlockCacheMetadata packages the metadata needed to make decisions on
// cache eviction.
type DiskBlockCacheMetadata struct {
// the TLF ID for the block
TlfID tlf.ID
// the last time the block was used
LRUTime time.Time
// the size of the block
BlockSize uint32
// whether the block has triggered prefetches
// This used to be called "HasPrefetched" so to maintain compatibility with
// existing disk caches, we have to name it that in the codec tag.
TriggeredPrefetch bool `codec:"HasPrefetched"`
// whether the block's triggered prefetches are complete
FinishedPrefetch bool
}
// lruEntry is an entry for sorting LRU times
type lruEntry struct {
BlockID kbfsblock.ID
Time time.Time
}
type blockIDsByTime []lruEntry
func (b blockIDsByTime) Len() int { return len(b) }
func (b blockIDsByTime) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b blockIDsByTime) Less(i, j int) bool { return b[i].Time.Before(b[j].Time) }
func (b blockIDsByTime) ToBlockIDSlice(numBlocks int) []kbfsblock.ID {
ids := make([]kbfsblock.ID, 0, numBlocks)
for _, entry := range b {
if len(ids) == numBlocks {
return ids
}
ids = append(ids, entry.BlockID)
}
return ids
}