Dragonfly2
148 строк · 4.7 Кб
1/*
2* Copyright 2023 The Dragonfly Authors
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17//go:generate mockgen -destination mocks/storage_mock.go -source storage.go -package mocks
18
19package storage
20
21import (
22"errors"
23"fmt"
24"os"
25"path/filepath"
26
27"github.com/gocarina/gocsv"
28
29schedulerstorage "d7y.io/dragonfly/v2/scheduler/storage"
30)
31
32const (
33// DownloadFilePrefix is prefix of download file name.
34DownloadFilePrefix = "download"
35
36// NetworkTopologyFilePrefix is prefix of network topology file name.
37NetworkTopologyFilePrefix = "networktopology"
38
39// CSVFileExt is extension of file name.
40CSVFileExt = "csv"
41)
42
43// Storage is the interface used for storage.
44type Storage interface {
45// ListDownload returns downloads in csv files based on the given model key.
46ListDownload(string) ([]schedulerstorage.Download, error)
47
48// ListNetworkTopology returns network topologies in csv files based on the given model key.
49ListNetworkTopology(string) ([]schedulerstorage.NetworkTopology, error)
50
51// OpenDownload opens download files for read based on the given model key, it returns io.ReadCloser of download files.
52OpenDownload(string) (*os.File, error)
53
54// OpenNetworkTopology opens network topology files for read based on the given model key, it returns io.ReadCloser of network topology files.
55OpenNetworkTopology(string) (*os.File, error)
56
57// ClearDownload removes all downloads based on the given model key.
58ClearDownload(string) error
59
60// ClearNetworkTopology removes network topologies based on the given model key.
61ClearNetworkTopology(string) error
62
63// Clear removes all files.
64Clear() error
65}
66
67// storage provides storage function.
68type storage struct {
69baseDir string
70}
71
72// New returns a new Storage instance.
73func New(baseDir string) Storage {
74return &storage{baseDir: baseDir}
75}
76
77// ListDownload returns downloads in csv files based on the given model key.
78func (s *storage) ListDownload(key string) (downloads []schedulerstorage.Download, err error) {
79file, err := s.OpenDownload(key)
80if err != nil {
81return nil, err
82}
83defer func() {
84if cerr := file.Close(); cerr != nil {
85err = errors.Join(err, cerr)
86}
87}()
88
89if err = gocsv.UnmarshalWithoutHeaders(file, &downloads); err != nil {
90return nil, err
91}
92
93return downloads, nil
94}
95
96// ListNetworkTopology returns network topologies in csv files based on the given model key.
97func (s *storage) ListNetworkTopology(key string) (networkTopologies []schedulerstorage.NetworkTopology, err error) {
98file, err := s.OpenNetworkTopology(key)
99if err != nil {
100return nil, err
101}
102defer func() {
103if cerr := file.Close(); cerr != nil {
104err = errors.Join(err, cerr)
105}
106}()
107
108if err = gocsv.UnmarshalWithoutHeaders(file, &networkTopologies); err != nil {
109return nil, err
110}
111
112return networkTopologies, nil
113}
114
115// OpenDownload opens download files for read based on the given model key, it returns io.ReadCloser of download files.
116func (s *storage) OpenDownload(key string) (*os.File, error) {
117return os.OpenFile(s.downloadFilename(key), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
118}
119
120// OpenNetworkTopology opens network topology files for read based on the given model key, it returns io.ReadCloser of network topology files.
121func (s *storage) OpenNetworkTopology(key string) (*os.File, error) {
122return os.OpenFile(s.networkTopologyFilename(key), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
123}
124
125// ClearDownload removes downloads based on the given model key.
126func (s *storage) ClearDownload(key string) error {
127return os.Remove(s.downloadFilename(key))
128}
129
130// ClearNetworkTopology removes network topologies based on the given model key.
131func (s *storage) ClearNetworkTopology(key string) error {
132return os.Remove(s.networkTopologyFilename(key))
133}
134
135// Clear removes all files.
136func (s *storage) Clear() error {
137return os.RemoveAll(s.baseDir)
138}
139
140// downloadFilename generates download file name based on the given model key.
141func (s *storage) downloadFilename(key string) string {
142return filepath.Join(s.baseDir, fmt.Sprintf("%s_%s.%s", DownloadFilePrefix, key, CSVFileExt))
143}
144
145// networkTopologyFilename generates network topology file name based on the given model key.
146func (s *storage) networkTopologyFilename(key string) string {
147return filepath.Join(s.baseDir, fmt.Sprintf("%s_%s.%s", NetworkTopologyFilePrefix, key, CSVFileExt))
148}
149