-
Notifications
You must be signed in to change notification settings - Fork 1
/
mdstore.go
154 lines (124 loc) · 2.49 KB
/
mdstore.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package qfs
import (
"io"
"io/fs"
"io/ioutil"
"sort"
cid "github.com/ipfs/go-cid"
format "github.com/ipfs/go-ipld-format"
)
// MerkleDagStore is a store of Content-Addressed block data indexed by merkle
// proofs. It's an abstraction over IPFS that defines the required feature set
// to run wnfs
type MerkleDagStore interface {
Type() string
// linked data nodes
GetNode(id cid.Cid, path ...string) (DagNode, error)
PutNode(links Links) (PutResult, error)
GetBlock(id cid.Cid) (r io.Reader, err error)
PutBlock(d []byte) (id cid.Cid, err error)
// files
PutFile(f fs.File) (PutResult, error)
GetFile(root cid.Cid, path ...string) (io.ReadCloser, error)
}
func GetBlockBytes(store MerkleDagStore, id cid.Cid) ([]byte, error) {
r, err := store.GetBlock(id)
if err != nil {
return nil, err
}
return ioutil.ReadAll(r)
}
type DagNode interface {
Size() int64
Cid() cid.Cid
Links() Links
}
type Links struct {
l map[string]Link
}
func NewLinks(links ...Link) Links {
lks := Links{
l: map[string]Link{},
}
for _, link := range links {
lks.Add(link)
}
return lks
}
func (ls Links) Len() int {
return len(ls.l)
}
func (ls Links) Add(link Link) {
ls.l[link.Name] = link
}
func (ls Links) Remove(name string) bool {
_, existed := ls.l[name]
delete(ls.l, name)
return existed
}
func (ls Links) Get(name string) *Link {
lk, ok := ls.l[name]
if !ok {
return nil
}
return &lk
}
func (ls Links) Slice() []Link {
links := make([]Link, 0, len(ls.l))
for _, link := range ls.l {
links = append(links, link)
}
return links
}
func (ls Links) SortedSlice() []Link {
names := make([]string, 0, len(ls.l))
for name := range ls.l {
names = append(names, name)
}
sort.Strings(names)
links := make([]Link, 0, len(ls.l))
for _, name := range names {
links = append(links, ls.l[name])
}
return links
}
func (ls Links) Map() map[string]Link {
return ls.l
}
type Link struct {
Name string
Size int64
Cid cid.Cid
IsFile bool
Mtime int64
}
func LinkFromNode(node DagNode, name string, isFile bool) Link {
return Link{
Name: name,
IsFile: isFile,
Cid: node.Cid(),
Size: node.Size(),
}
}
func (l Link) IsEmpty() bool {
return l.Cid.String() == ""
}
func (l Link) IPLD() *format.Link {
return &format.Link{
Name: l.Name,
Cid: l.Cid,
Size: uint64(l.Size),
}
}
type PutResult struct {
Cid cid.Cid
Size int64
}
func (pr *PutResult) ToLink(name string, isFile bool) Link {
return Link{
Name: name,
IsFile: isFile,
Cid: pr.Cid,
Size: pr.Size,
}
}