@@ -132,12 +132,7 @@ func storeItems(ctx context.Context, dag merkledag.DAGService, estimatedLen uint
132
132
sort .Stable (s )
133
133
}
134
134
135
- // wasteful but simple
136
- type item struct {
137
- c * cid.Cid
138
- data []byte
139
- }
140
- hashed := make (map [uint32 ][]item )
135
+ hashed := make ([][]* cid.Cid , defaultFanout )
141
136
for {
142
137
// This loop essentially enumerates every single item in the set
143
138
// and maps them all into a set of buckets. Each bucket will be recursively
@@ -152,41 +147,49 @@ func storeItems(ctx context.Context, dag merkledag.DAGService, estimatedLen uint
152
147
// and losing pins. The fix (a few lines down from this comment), is to
153
148
// map the hash value down to the 8 bit keyspace here while creating the
154
149
// buckets. This way, we avoid any overlapping later on.
155
- k , data , ok := iter ()
150
+ k , _ , ok := iter ()
156
151
if ! ok {
157
152
break
158
153
}
159
154
h := hash (seed , k ) % defaultFanout
160
- hashed [h ] = append (hashed [h ], item { k , data } )
155
+ hashed [h ] = append (hashed [h ], k )
161
156
}
157
+
162
158
for h , items := range hashed {
159
+ if len (items ) == 0 {
160
+ // recursion base case
161
+ continue
162
+ }
163
+
163
164
childIter := func () (c * cid.Cid , data []byte , ok bool ) {
164
165
if len (items ) == 0 {
165
166
return nil , nil , false
166
167
}
167
168
first := items [0 ]
168
169
items = items [1 :]
169
- return first . c , first . data , true
170
+ return first , nil , true
170
171
}
172
+
171
173
child , err := storeItems (ctx , dag , uint64 (len (items )), childIter , internalKeys )
172
174
if err != nil {
173
175
return nil , err
174
176
}
177
+
175
178
size , err := child .Size ()
176
179
if err != nil {
177
180
return nil , err
178
181
}
182
+
179
183
childKey , err := dag .Add (child )
180
184
if err != nil {
181
185
return nil , err
182
186
}
187
+
183
188
internalKeys (childKey )
184
- l := & merkledag.Link {
185
- Name : "" ,
189
+ n .Links [int (h )] = & merkledag.Link {
186
190
Hash : childKey .Hash (),
187
191
Size : size ,
188
192
}
189
- n .Links [int (h % defaultFanout )] = l
190
193
}
191
194
return n , nil
192
195
}
0 commit comments