2017-07-03 23:08:20 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2017-07-25 19:11:52 +00:00
|
|
|
"time"
|
|
|
|
|
2017-07-03 23:08:20 +00:00
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
"github.com/moby/buildkit/cache/metadata"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const sizeUnknown int64 = -1
|
|
|
|
const keySize = "snapshot.size"
|
2017-07-14 18:59:31 +00:00
|
|
|
const keyEqualMutable = "cache.equalMutable"
|
2017-07-19 23:39:32 +00:00
|
|
|
const keyCachePolicy = "cache.cachePolicy"
|
2017-07-25 19:11:52 +00:00
|
|
|
const keyDescription = "cache.description"
|
|
|
|
const keyCreatedAt = "cache.createdAt"
|
|
|
|
const keyLastUsedAt = "cache.lastUsedAt"
|
|
|
|
const keyUsageCount = "cache.usageCount"
|
2017-07-03 23:08:20 +00:00
|
|
|
|
2017-12-27 01:22:50 +00:00
|
|
|
const keyDeleted = "cache.deleted"
|
|
|
|
|
|
|
|
func setDeleted(si *metadata.StorageItem) error {
|
|
|
|
v, err := metadata.NewValue(true)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create size value")
|
|
|
|
}
|
|
|
|
si.Update(func(b *bolt.Bucket) error {
|
|
|
|
return si.SetValue(b, keyDeleted, v)
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDeleted(si *metadata.StorageItem) bool {
|
|
|
|
v := si.Get(keyDeleted)
|
|
|
|
if v == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var deleted bool
|
|
|
|
if err := v.Unmarshal(&deleted); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return deleted
|
|
|
|
}
|
|
|
|
|
2017-07-03 23:08:20 +00:00
|
|
|
func setSize(si *metadata.StorageItem, s int64) error {
|
|
|
|
v, err := metadata.NewValue(s)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create size value")
|
|
|
|
}
|
|
|
|
si.Queue(func(b *bolt.Bucket) error {
|
2017-07-14 18:59:31 +00:00
|
|
|
return si.SetValue(b, keySize, v)
|
2017-07-03 23:08:20 +00:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSize(si *metadata.StorageItem) int64 {
|
|
|
|
v := si.Get(keySize)
|
|
|
|
if v == nil {
|
|
|
|
return sizeUnknown
|
|
|
|
}
|
|
|
|
var size int64
|
|
|
|
if err := v.Unmarshal(&size); err != nil {
|
|
|
|
return sizeUnknown
|
|
|
|
}
|
|
|
|
return size
|
|
|
|
}
|
2017-07-14 18:59:31 +00:00
|
|
|
|
|
|
|
func getEqualMutable(si *metadata.StorageItem) string {
|
|
|
|
v := si.Get(keyEqualMutable)
|
|
|
|
if v == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var str string
|
|
|
|
if err := v.Unmarshal(&str); err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
func setEqualMutable(si *metadata.StorageItem, s string) error {
|
|
|
|
v, err := metadata.NewValue(s)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to create %s meta value", keyEqualMutable)
|
|
|
|
}
|
|
|
|
si.Queue(func(b *bolt.Bucket) error {
|
|
|
|
return si.SetValue(b, keyEqualMutable, v)
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func clearEqualMutable(si *metadata.StorageItem) error {
|
|
|
|
si.Queue(func(b *bolt.Bucket) error {
|
|
|
|
return si.SetValue(b, keyEqualMutable, nil)
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
2017-07-19 23:39:32 +00:00
|
|
|
|
2017-07-25 19:11:52 +00:00
|
|
|
func queueCachePolicy(si *metadata.StorageItem, p cachePolicy) error {
|
2017-07-19 23:39:32 +00:00
|
|
|
v, err := metadata.NewValue(p)
|
|
|
|
if err != nil {
|
2017-07-25 19:11:52 +00:00
|
|
|
return errors.Wrap(err, "failed to create cachePolicy value")
|
2017-07-19 23:39:32 +00:00
|
|
|
}
|
2017-07-25 19:11:52 +00:00
|
|
|
si.Queue(func(b *bolt.Bucket) error {
|
2017-07-19 23:39:32 +00:00
|
|
|
return si.SetValue(b, keyCachePolicy, v)
|
|
|
|
})
|
2017-07-25 19:11:52 +00:00
|
|
|
return nil
|
2017-07-19 23:39:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getCachePolicy(si *metadata.StorageItem) cachePolicy {
|
|
|
|
v := si.Get(keyCachePolicy)
|
|
|
|
if v == nil {
|
|
|
|
return cachePolicyDefault
|
|
|
|
}
|
|
|
|
var p cachePolicy
|
|
|
|
if err := v.Unmarshal(&p); err != nil {
|
|
|
|
return cachePolicyDefault
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
2017-07-25 19:11:52 +00:00
|
|
|
|
|
|
|
func queueDescription(si *metadata.StorageItem, descr string) error {
|
|
|
|
v, err := metadata.NewValue(descr)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create description value")
|
|
|
|
}
|
|
|
|
si.Queue(func(b *bolt.Bucket) error {
|
|
|
|
return si.SetValue(b, keyDescription, v)
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-08 18:53:17 +00:00
|
|
|
func GetDescription(si *metadata.StorageItem) string {
|
2017-07-25 19:11:52 +00:00
|
|
|
v := si.Get(keyDescription)
|
|
|
|
if v == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var str string
|
|
|
|
if err := v.Unmarshal(&str); err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2018-05-04 20:29:04 +00:00
|
|
|
func queueCreatedAt(si *metadata.StorageItem, tm time.Time) error {
|
|
|
|
v, err := metadata.NewValue(tm.UnixNano())
|
2017-07-25 19:11:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create createdAt value")
|
|
|
|
}
|
|
|
|
si.Queue(func(b *bolt.Bucket) error {
|
|
|
|
return si.SetValue(b, keyCreatedAt, v)
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-04 20:29:04 +00:00
|
|
|
func GetCreatedAt(si *metadata.StorageItem) time.Time {
|
2017-07-25 19:11:52 +00:00
|
|
|
v := si.Get(keyCreatedAt)
|
|
|
|
if v == nil {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
var tm int64
|
|
|
|
if err := v.Unmarshal(&tm); err != nil {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
return time.Unix(tm/1e9, tm%1e9)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLastUsed(si *metadata.StorageItem) (int, *time.Time) {
|
|
|
|
v := si.Get(keyUsageCount)
|
|
|
|
if v == nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
var usageCount int
|
|
|
|
if err := v.Unmarshal(&usageCount); err != nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
v = si.Get(keyLastUsedAt)
|
|
|
|
if v == nil {
|
|
|
|
return usageCount, nil
|
|
|
|
}
|
|
|
|
var lastUsedTs int64
|
|
|
|
if err := v.Unmarshal(&lastUsedTs); err != nil || lastUsedTs == 0 {
|
|
|
|
return usageCount, nil
|
|
|
|
}
|
|
|
|
tm := time.Unix(lastUsedTs/1e9, lastUsedTs%1e9)
|
|
|
|
return usageCount, &tm
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateLastUsed(si *metadata.StorageItem) error {
|
|
|
|
count, _ := getLastUsed(si)
|
|
|
|
count++
|
|
|
|
|
|
|
|
v, err := metadata.NewValue(count)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create usageCount value")
|
|
|
|
}
|
|
|
|
v2, err := metadata.NewValue(time.Now().UnixNano())
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create lastUsedAt value")
|
|
|
|
}
|
|
|
|
return si.Update(func(b *bolt.Bucket) error {
|
|
|
|
if err := si.SetValue(b, keyUsageCount, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return si.SetValue(b, keyLastUsedAt, v2)
|
|
|
|
})
|
|
|
|
}
|