@@ -56,7 +56,9 @@ func BuildUnixFSShardedDirectory(size int, hasher uint64, entries []dagpb.PBLink
56
56
hamtEntries := make ([]hamtLink , 0 , len (entries ))
57
57
for _ , e := range entries {
58
58
name := e .Name .Must ().String ()
59
- sum := h .Sum ([]byte (name ))
59
+ h .Reset ()
60
+ h .Write ([]byte (name ))
61
+ sum := h .Sum (nil )
60
62
hamtEntries = append (hamtEntries , hamtLink {
61
63
sum ,
62
64
e ,
@@ -97,9 +99,11 @@ func (s *shard) add(lnk hamtLink) error {
97
99
98
100
current , ok := s .children [bucket ]
99
101
if ! ok {
102
+ // no bucket, make one with this entry
100
103
s .children [bucket ] = entry {nil , & lnk }
101
104
return nil
102
105
} else if current .shard != nil {
106
+ // existing shard, add this link to the shard
103
107
return current .shard .add (lnk )
104
108
}
105
109
// make a shard for current and lnk
@@ -114,15 +118,18 @@ func (s *shard) add(lnk hamtLink) error {
114
118
},
115
119
nil ,
116
120
}
121
+ // add existing link from this bucket to the new shard
117
122
if err := newShard .add (* current .hamtLink ); err != nil {
118
123
return err
119
124
}
125
+ // replace bucket with shard
120
126
s .children [bucket ] = newShard
127
+ // add new link to the new shard
121
128
return newShard .add (lnk )
122
129
}
123
130
124
131
func (s * shard ) formatLinkName (name string , idx int ) string {
125
- return fmt .Sprintf ("%*X%s" , s .width , idx , name )
132
+ return fmt .Sprintf ("%0 *X%s" , s .width , idx , name )
126
133
}
127
134
128
135
// bitmap calculates the bitmap of which links in the shard are set.
@@ -169,21 +176,25 @@ func (s *shard) serialize(ls *ipld.LinkSystem) (ipld.Link, uint64, error) {
169
176
return nil , 0 , err
170
177
}
171
178
// sorting happens in codec-dagpb
179
+ var totalSize uint64
172
180
for idx , e := range s .children {
173
181
var lnk dagpb.PBLink
174
182
if e .shard != nil {
175
183
ipldLnk , sz , err := e .shard .serialize (ls )
176
184
if err != nil {
177
185
return nil , 0 , err
178
186
}
187
+ totalSize += sz
179
188
fullName := s .formatLinkName ("" , idx )
180
189
lnk , err = BuildUnixFSDirectoryEntry (fullName , int64 (sz ), ipldLnk )
181
190
if err != nil {
182
191
return nil , 0 , err
183
192
}
184
193
} else {
185
194
fullName := s .formatLinkName (e .Name .Must ().String (), idx )
186
- lnk , err = BuildUnixFSDirectoryEntry (fullName , e .Tsize .Must ().Int (), e .Hash .Link ())
195
+ sz := e .Tsize .Must ().Int ()
196
+ totalSize += uint64 (sz )
197
+ lnk , err = BuildUnixFSDirectoryEntry (fullName , sz , e .Hash .Link ())
187
198
}
188
199
if err != nil {
189
200
return nil , 0 , err
@@ -200,5 +211,9 @@ func (s *shard) serialize(ls *ipld.LinkSystem) (ipld.Link, uint64, error) {
200
211
return nil , 0 , err
201
212
}
202
213
node := pbb .Build ()
203
- return sizedStore (ls , fileLinkProto , node )
214
+ lnk , sz , err := sizedStore (ls , fileLinkProto , node )
215
+ if err != nil {
216
+ return nil , 0 , err
217
+ }
218
+ return lnk , totalSize + sz , nil
204
219
}
0 commit comments