simplify dag add
License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
Steven Allen committed
Oct 26, 2018 at 12:57 UTC
64a517afed3bce08320715702836bfe93f21a8ff
1 file changed
+37
-60
core/commands/dag/dag.go
+37
-60
@@ -87,81 +87,58 @@ into an object of the specified format.
87
}
88
}
89
90
- outChan := make(chan interface{}, 8)
91
-
92
- addAllAndPin := func(f files.File) error {
93
- cids := cid.NewSet()
94
- b := ipld.NewBatch(req.Context, nd.DAG)
95
-
96
- for {
97
- file, err := f.NextFile()
98
- if err == io.EOF {
99
- // Finished the list of files.
100
- break
101
- } else if err != nil {
102
- return err
103
- }
104
-
105
- nds, err := coredag.ParseInputs(ienc, format, file, mhType, -1)
106
- if err != nil {
107
- return err
108
- }
109
- if len(nds) == 0 {
110
- return fmt.Errorf("no node returned from ParseInputs")
111
- }
112
-
113
- for _, nd := range nds {
114
- err := b.Add(nd)
115
- if err != nil {
116
- return err
117
- }
118
- }
119
-
120
- cid := nds[0].Cid()
121
- cids.Add(cid)
122
-
123
- select {
124
- case outChan <- &OutputObject{Cid: cid}:
125
- case <-req.Context.Done():
126
- return nil
127
- }
90
+ cids := cid.NewSet()
91
+ b := ipld.NewBatch(req.Context, nd.DAG)
92
+
93
+ for {
94
+ file, err := req.Files.NextFile()
95
+ if err == io.EOF {
96
+ // Finished the list of files.
97
+ break
98
+ } else if err != nil {
99
+ return err
100
}
101
130
- if err := b.Commit(); err != nil {
102
+ nds, err := coredag.ParseInputs(ienc, format, file, mhType, -1)
103
+ if err != nil {
104
return err
105
}
106
+ if len(nds) == 0 {
107
+ return fmt.Errorf("no node returned from ParseInputs")
108
+ }
109
134
- if dopin {
135
- defer nd.Blockstore.PinLock().Unlock()
136
-
137
- cids.ForEach(func(c cid.Cid) error {
138
- nd.Pinning.PinWithMode(c, pin.Recursive)
139
- return nil
140
- })
141
-
142
- err := nd.Pinning.Flush()
110
+ for _, nd := range nds {
111
+ err := b.Add(nd)
112
if err != nil {
113
return err
114
}
115
}
116
148
- return nil
117
+ cid := nds[0].Cid()
118
+ cids.Add(cid)
119
+ if err := res.Emit(&OutputObject{Cid: cid}); err != nil {
120
+ return err
121
+ }
122
}
123
151
- errC := make(chan error)
152
- go func() {
153
- var err error
154
- defer func() { errC <- err }()
155
- defer close(outChan)
156
- err = addAllAndPin(req.Files)
157
- }()
158
-
159
- err = res.Emit(outChan)
160
- if err != nil {
124
+ if err := b.Commit(); err != nil {
125
return err
126
}
127
164
- return <-errC
128
+ if dopin {
129
+ defer nd.Blockstore.PinLock().Unlock()
130
+
131
+ cids.ForEach(func(c cid.Cid) error {
132
+ nd.Pinning.PinWithMode(c, pin.Recursive)
133
+ return nil
134
+ })
135
+
136
+ err := nd.Pinning.Flush()
137
+ if err != nil {
138
+ return err
139
+ }
140
+ }
141
+ return nil
142
},
143
Type: OutputObject{},
144
Encoders: cmds.EncoderMap{