~james/core

core/spindle/mill/executor/cache.go -rw-r--r-- 2.4 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 14 hours ago
                                                                                
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package executor

import (
	"fmt"

	millv1 "tangled.org/core/spindle/mill/proto/gen"
	"tangled.org/core/spindle/models"
)

func cacheBindingsFromProto(repoDid string, entries []models.CacheEntry, bindings []*millv1.CacheBinding) ([]models.CacheBinding, error) {
	result := make([]models.CacheBinding, 0, len(bindings))
	seen := make(map[int]struct{}, len(bindings))
	for _, binding := range bindings {
		entryIndex := int(binding.GetEntryIndex())
		if entryIndex < 0 || entryIndex >= len(entries) {
			return nil, fmt.Errorf("cache binding index %d is out of range", entryIndex)
		}
		if _, ok := seen[entryIndex]; ok {
			return nil, fmt.Errorf("duplicate cache binding index %d", entryIndex)
		}
		seen[entryIndex] = struct{}{}

		for _, object := range []struct {
			name string
			id   string
			key  string
		}{
			{name: "restore", id: binding.GetRestoreId(), key: binding.GetRestoreKey()},
			{name: "save", id: binding.GetSaveId(), key: binding.GetSaveKey()},
		} {
			if (object.id == "") != (object.key == "") {
				return nil, fmt.Errorf("%s cache id and key must both be set", object.name)
			}
			if object.id == "" {
				continue
			}
			want, err := models.CacheObjectKey(repoDid, object.id)
			if err != nil {
				return nil, err
			}
			if object.key != want {
				return nil, fmt.Errorf("%s cache key %q does not match object id", object.name, object.key)
			}
		}

		entry := entries[entryIndex]
		result = append(result, models.CacheBinding{
			EntryIndex:       entryIndex,
			Paths:            entry.Paths,
			Key:              entry.Key,
			SaveID:           binding.GetSaveId(),
			SaveKey:          binding.GetSaveKey(),
			RestoreID:        binding.GetRestoreId(),
			RestoreKey:       binding.GetRestoreKey(),
			RestoreName:      binding.GetRestoreName(),
			Checksum:         binding.GetRestoreChecksum(),
			SizeBytes:        binding.GetRestoreSizeBytes(),
			CompressionLevel: entry.CompressionLevel,
			When:             entry.When,
		})
	}
	return result, nil
}

// an untrusted run never keeps a push token, even if the mill sent one
func nixCacheFromProto(grant *millv1.NixCacheGrant, trusted bool) *models.NixCacheGrant {
	if grant == nil {
		return nil
	}
	result := &models.NixCacheGrant{
		Cache:          grant.GetCache(),
		PublicKey:      grant.GetPublicKey(),
		PullToken:      grant.GetPullToken(),
		MaxUploadBytes: grant.GetMaxUploadBytes(),
	}
	if trusted {
		result.PushToken = grant.GetPushToken()
	}
	return result
}