-
Notifications
You must be signed in to change notification settings - Fork 5
/
reification.go
89 lines (77 loc) · 3.24 KB
/
reification.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 unixfsnode
import (
"context"
"fmt"
"github.com/ipfs/go-unixfsnode/data"
"github.com/ipfs/go-unixfsnode/directory"
"github.com/ipfs/go-unixfsnode/file"
"github.com/ipfs/go-unixfsnode/hamt"
dagpb "github.com/ipld/go-codec-dagpb"
"github.com/ipld/go-ipld-prime"
)
// Reify looks at an ipld Node and tries to interpret it as a UnixFSNode
// if successful, it returns the UnixFSNode
func Reify(lnkCtx ipld.LinkContext, maybePBNodeRoot ipld.Node, lsys *ipld.LinkSystem) (ipld.Node, error) {
return doReify(lnkCtx, maybePBNodeRoot, lsys, true)
}
// nonLazyReify works like reify but will load all of a directory or file as it reaches them.
func nonLazyReify(lnkCtx ipld.LinkContext, maybePBNodeRoot ipld.Node, lsys *ipld.LinkSystem) (ipld.Node, error) {
return doReify(lnkCtx, maybePBNodeRoot, lsys, false)
}
func doReify(lnkCtx ipld.LinkContext, maybePBNodeRoot ipld.Node, lsys *ipld.LinkSystem, lazy bool) (ipld.Node, error) {
pbNode, ok := maybePBNodeRoot.(dagpb.PBNode)
if !ok {
return maybePBNodeRoot, nil
}
if !pbNode.FieldData().Exists() {
// no data field, therefore, not UnixFS
return defaultReifier(lnkCtx.Ctx, pbNode, lsys)
}
data, err := data.DecodeUnixFSData(pbNode.Data.Must().Bytes())
if err != nil {
// we could not decode the UnixFS data, therefore, not UnixFS
return defaultReifier(lnkCtx.Ctx, pbNode, lsys)
}
var builder reifyTypeFunc
if lazy {
builder, ok = lazyReifyFuncs[data.FieldDataType().Int()]
} else {
builder, ok = reifyFuncs[data.FieldDataType().Int()]
}
if !ok {
return nil, fmt.Errorf("no reification for this UnixFS node type")
}
return builder(lnkCtx.Ctx, pbNode, data, lsys)
}
type reifyTypeFunc func(context.Context, dagpb.PBNode, data.UnixFSData, *ipld.LinkSystem) (ipld.Node, error)
var reifyFuncs = map[int64]reifyTypeFunc{
data.Data_File: unixFSFileReifierWithPreload,
data.Data_Metadata: defaultUnixFSReifier,
data.Data_Raw: unixFSFileReifier,
data.Data_Symlink: defaultUnixFSReifier,
data.Data_Directory: directory.NewUnixFSBasicDir,
data.Data_HAMTShard: hamt.NewUnixFSHAMTShardWithPreload,
}
var lazyReifyFuncs = map[int64]reifyTypeFunc{
data.Data_File: unixFSFileReifier,
data.Data_Metadata: defaultUnixFSReifier,
data.Data_Raw: unixFSFileReifier,
data.Data_Symlink: defaultUnixFSReifier,
data.Data_Directory: directory.NewUnixFSBasicDir,
data.Data_HAMTShard: hamt.NewUnixFSHAMTShard,
}
// treat non-unixFS nodes like directories -- allow them to lookup by link
// TODO: Make this a separate node as directories gain more functionality
func defaultReifier(_ context.Context, substrate dagpb.PBNode, _ *ipld.LinkSystem) (ipld.Node, error) {
return &_PathedPBNode{_substrate: substrate}, nil
}
func unixFSFileReifier(ctx context.Context, substrate dagpb.PBNode, _ data.UnixFSData, ls *ipld.LinkSystem) (ipld.Node, error) {
return file.NewUnixFSFile(ctx, substrate, ls)
}
func unixFSFileReifierWithPreload(ctx context.Context, substrate dagpb.PBNode, _ data.UnixFSData, ls *ipld.LinkSystem) (ipld.Node, error) {
return file.NewUnixFSFileWithPreload(ctx, substrate, ls)
}
func defaultUnixFSReifier(ctx context.Context, substrate dagpb.PBNode, _ data.UnixFSData, ls *ipld.LinkSystem) (ipld.Node, error) {
return defaultReifier(ctx, substrate, ls)
}
var _ ipld.NodeReifier = Reify