forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum-mapper.go
63 lines (56 loc) · 1.46 KB
/
checksum-mapper.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
package s3
import "sync"
// ChecksumMapper maintains a mapping of eTags => checksum
type ChecksumMapper interface {
// Get finds a checksum for a given eTag, returns false if not found
Get(eTag string) (string, bool)
// Set stores an eTag checksum couple if it does not already exists
Set(eTag, checksum string)
// Purge removes unknown values based on the full list of know values
Purge(knownETags []string) int
}
// MemChecksumMapper is an in-memory implementation for ChecksumMapper interface
type MemChecksumMapper struct {
data map[string]string
mux *sync.Mutex
}
// NewMemChecksumMapper instantiates a new ChecksumMapper
func NewMemChecksumMapper() *MemChecksumMapper {
return &MemChecksumMapper{
data: make(map[string]string),
mux: &sync.Mutex{},
}
}
// Get finds a checksum by eTag
func (m *MemChecksumMapper) Get(eTag string) (string, bool) {
m.mux.Lock()
defer m.mux.Unlock()
cs, ok := m.data[eTag]
return cs, ok
}
// Set stores a checksum for a given eTag
func (m *MemChecksumMapper) Set(eTag, checksum string) {
m.mux.Lock()
defer m.mux.Unlock()
m.data[eTag] = checksum
}
// Purge compares existing eTags to stored eTags and removes unnecessary ones
func (m *MemChecksumMapper) Purge(knownETags []string) int {
m.mux.Lock()
defer m.mux.Unlock()
count := 0
for e, _ := range m.data {
found := false
for _, k := range knownETags {
if k == e {
found = true
break
}
}
if !found {
delete(m.data, e)
count++
}
}
return count
}