This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
/
util.go
73 lines (61 loc) · 1.42 KB
/
util.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
package nv15
import (
"sync"
"testing"
"github.com/filecoin-project/go-state-types/rt"
"github.com/ipfs/go-cid"
"golang.org/x/xerrors"
)
type MemMigrationCache struct {
MigrationMap sync.Map
}
func NewMemMigrationCache() *MemMigrationCache {
return new(MemMigrationCache)
}
func (m *MemMigrationCache) Write(key string, c cid.Cid) error {
m.MigrationMap.Store(key, c)
return nil
}
func (m *MemMigrationCache) Read(key string) (bool, cid.Cid, error) {
val, found := m.MigrationMap.Load(key)
if !found {
return false, cid.Undef, nil
}
c, ok := val.(cid.Cid)
if !ok {
return false, cid.Undef, xerrors.Errorf("non cid value in cache")
}
return true, c, nil
}
func (m *MemMigrationCache) Load(key string, loadFunc func() (cid.Cid, error)) (cid.Cid, error) {
found, c, err := m.Read(key)
if err != nil {
return cid.Undef, err
}
if found {
return c, nil
}
c, err = loadFunc()
if err != nil {
return cid.Undef, err
}
m.MigrationMap.Store(key, c)
return c, nil
}
func (m *MemMigrationCache) Clone() *MemMigrationCache {
newCache := NewMemMigrationCache()
newCache.Update(m)
return newCache
}
func (m *MemMigrationCache) Update(other *MemMigrationCache) {
other.MigrationMap.Range(func(key, value interface{}) bool {
m.MigrationMap.Store(key, value)
return true
})
}
type TestLogger struct {
TB testing.TB
}
func (t TestLogger) Log(_ rt.LogLevel, msg string, args ...interface{}) {
t.TB.Logf(msg, args...)
}