File: prefetch.go

package info (click to toggle)
golang-github-containerd-nydus-snapshotter 0.13.4-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,824 kB
  • sloc: sh: 470; makefile: 129
file content (60 lines) | stat: -rw-r--r-- 1,213 bytes parent folder | download
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
/*
 * Copyright (c) 2023. Nydus Developers. All rights reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

package prefetch

import (
	"encoding/json"
	"sync"

	"github.com/containerd/containerd/log"
)

type prefetchInfo struct {
	prefetchMap   map[string]string
	prefetchMutex sync.Mutex
}

var Pm prefetchInfo

func (p *prefetchInfo) SetPrefetchFiles(body []byte) error {
	p.prefetchMutex.Lock()
	defer p.prefetchMutex.Unlock()

	var prefetchMsg []map[string]string
	if err := json.Unmarshal(body, &prefetchMsg); err != nil {
		return err
	}

	if p.prefetchMap == nil {
		p.prefetchMap = make(map[string]string)
	}
	for _, item := range prefetchMsg {
		image := item["image"]
		prefetchfiles := item["prefetch"]
		p.prefetchMap[image] = prefetchfiles
	}

	log.L.Infof("received prefetch list from nri plugin: %v ", p.prefetchMap)
	return nil
}

func (p *prefetchInfo) GetPrefetchInfo(image string) string {
	p.prefetchMutex.Lock()
	defer p.prefetchMutex.Unlock()

	if prefetchfiles, ok := p.prefetchMap[image]; ok {
		return prefetchfiles
	}
	return ""
}

func (p *prefetchInfo) DeleteFromPrefetchMap(image string) {
	p.prefetchMutex.Lock()
	defer p.prefetchMutex.Unlock()

	delete(p.prefetchMap, image)
}