/
memory_dao.go
89 lines (71 loc) · 1.74 KB
/
memory_dao.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package gdrive
import (
"context"
"errors"
"sync"
"time"
"gopkg.in/typ.v4/slices"
)
type Memory struct {
mut sync.Mutex
data []FileInfo
}
func NewMemoryDao() *Memory {
return &Memory{
mut: sync.Mutex{},
data: []FileInfo{},
}
}
func (m *Memory) InsertOrUpdate(ctx context.Context, fileInfo *FileInfo) error {
m.mut.Lock()
defer m.mut.Unlock()
idx := slices.IndexFunc(m.data, func(data FileInfo) bool { return data.Filepath == fileInfo.Filepath })
if idx >= 0 {
fileInfo.LastAccess = time.Now()
m.data[idx] = *fileInfo
} else {
m.data = append(m.data, *fileInfo)
}
return nil
}
func (m *Memory) Touch(ctx context.Context, filepathName string, date time.Time) error {
m.mut.Lock()
defer m.mut.Unlock()
idx := slices.IndexFunc(m.data, func(data FileInfo) bool { return data.Filepath == filepathName })
if idx >= 0 {
m.data[idx].LastAccess = date
}
return nil
}
func (m *Memory) Delete(ctx context.Context, filepathName string) error {
m.mut.Lock()
defer m.mut.Unlock()
idx := slices.IndexFunc(m.data, func(data FileInfo) bool { return data.Filepath == filepathName })
if idx < 0 {
return errors.New("file not found")
}
slices.Remove(&m.data, idx)
return nil
}
func (m *Memory) TotalSize(ctx context.Context) (int64, error) {
m.mut.Lock()
defer m.mut.Unlock()
var total int64
for i := range m.data {
total += m.data[i].Size
}
return total, nil
}
func (m *Memory) QueryOldest(ctx context.Context, limit int) ([]FileInfo, error) {
m.mut.Lock()
defer m.mut.Unlock()
slices.SortFunc(m.data, func(a, b FileInfo) bool { return a.LastAccess.Before(b.LastAccess) })
retVal := []FileInfo{}
for i := 0; i < limit; i++ {
if i >= len(m.data) {
break
}
retVal = append(retVal, m.data[i])
}
return retVal, nil
}