Skip to content

Commit 95daef4

Browse files
Merge pull request #4664 from ipfs/doc/golint-unixfs
Golint-ify unixfs module
2 parents 12354cc + f4f450d commit 95daef4

File tree

15 files changed

+174
-103
lines changed

15 files changed

+174
-103
lines changed

unixfs/archive/archive.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package archive provides utilities to archive and compress a [Unixfs] DAG.
12
package archive
23

34
import (

unixfs/archive/tar/writer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Package tar provides functionality to write a unixfs merkledag
2+
// as a tar archive.
13
package tar
24

35
import (
@@ -69,6 +71,7 @@ func (w *Writer) writeFile(nd *mdag.ProtoNode, pb *upb.Data, fpath string) error
6971
return nil
7072
}
7173

74+
// WriteNode adds a node to the archive.
7275
func (w *Writer) WriteNode(nd ipld.Node, fpath string) error {
7376
switch nd := nd.(type) {
7477
case *mdag.ProtoNode:
@@ -106,6 +109,7 @@ func (w *Writer) WriteNode(nd ipld.Node, fpath string) error {
106109
}
107110
}
108111

112+
// Close closes the tar writer.
109113
func (w *Writer) Close() error {
110114
return w.TarW.Close()
111115
}

unixfs/hamt/hamt.go

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ import (
3838
)
3939

4040
const (
41+
// HashMurmur3 is the multiformats identifier for Murmur3
4142
HashMurmur3 uint64 = 0x22
4243
)
4344

44-
type HamtShard struct {
45+
// A Shard represents the HAMT. It should be initialized with NewShard().
46+
type Shard struct {
4547
nd *dag.ProtoNode
4648

4749
bitfield *big.Int
@@ -66,9 +68,9 @@ type child interface {
6668
Label() string
6769
}
6870

69-
// NewHamtShard creates a new, empty HAMT shard with the given size.
70-
func NewHamtShard(dserv ipld.DAGService, size int) (*HamtShard, error) {
71-
ds, err := makeHamtShard(dserv, size)
71+
// NewShard creates a new, empty HAMT shard with the given size.
72+
func NewShard(dserv ipld.DAGService, size int) (*Shard, error) {
73+
ds, err := makeShard(dserv, size)
7274
if err != nil {
7375
return nil, err
7476
}
@@ -79,13 +81,13 @@ func NewHamtShard(dserv ipld.DAGService, size int) (*HamtShard, error) {
7981
return ds, nil
8082
}
8183

82-
func makeHamtShard(ds ipld.DAGService, size int) (*HamtShard, error) {
84+
func makeShard(ds ipld.DAGService, size int) (*Shard, error) {
8385
lg2s := int(math.Log2(float64(size)))
8486
if 1<<uint(lg2s) != size {
8587
return nil, fmt.Errorf("hamt size should be a power of two")
8688
}
8789
maxpadding := fmt.Sprintf("%X", size-1)
88-
return &HamtShard{
90+
return &Shard{
8991
tableSizeLg2: lg2s,
9092
prefixPadStr: fmt.Sprintf("%%0%dX", len(maxpadding)),
9193
maxpadlen: len(maxpadding),
@@ -95,7 +97,7 @@ func makeHamtShard(ds ipld.DAGService, size int) (*HamtShard, error) {
9597
}
9698

9799
// NewHamtFromDag creates new a HAMT shard from the given DAG.
98-
func NewHamtFromDag(dserv ipld.DAGService, nd ipld.Node) (*HamtShard, error) {
100+
func NewHamtFromDag(dserv ipld.DAGService, nd ipld.Node) (*Shard, error) {
99101
pbnd, ok := nd.(*dag.ProtoNode)
100102
if !ok {
101103
return nil, dag.ErrLinkNotFound
@@ -114,7 +116,7 @@ func NewHamtFromDag(dserv ipld.DAGService, nd ipld.Node) (*HamtShard, error) {
114116
return nil, fmt.Errorf("only murmur3 supported as hash function")
115117
}
116118

117-
ds, err := makeHamtShard(dserv, int(pbd.GetFanout()))
119+
ds, err := makeShard(dserv, int(pbd.GetFanout()))
118120
if err != nil {
119121
return nil, err
120122
}
@@ -129,17 +131,17 @@ func NewHamtFromDag(dserv ipld.DAGService, nd ipld.Node) (*HamtShard, error) {
129131
}
130132

131133
// SetPrefix sets the CID Prefix
132-
func (ds *HamtShard) SetPrefix(prefix *cid.Prefix) {
134+
func (ds *Shard) SetPrefix(prefix *cid.Prefix) {
133135
ds.prefix = prefix
134136
}
135137

136138
// Prefix gets the CID Prefix, may be nil if unset
137-
func (ds *HamtShard) Prefix() *cid.Prefix {
139+
func (ds *Shard) Prefix() *cid.Prefix {
138140
return ds.prefix
139141
}
140142

141143
// Node serializes the HAMT structure into a merkledag node with unixfs formatting
142-
func (ds *HamtShard) Node() (ipld.Node, error) {
144+
func (ds *Shard) Node() (ipld.Node, error) {
143145
out := new(dag.ProtoNode)
144146
out.SetPrefix(ds.prefix)
145147

@@ -214,14 +216,14 @@ func hash(val []byte) []byte {
214216
return h.Sum(nil)
215217
}
216218

217-
// Label for HamtShards is the empty string, this is used to differentiate them from
219+
// Label for Shards is the empty string, this is used to differentiate them from
218220
// value entries
219-
func (ds *HamtShard) Label() string {
221+
func (ds *Shard) Label() string {
220222
return ""
221223
}
222224

223225
// Set sets 'name' = nd in the HAMT
224-
func (ds *HamtShard) Set(ctx context.Context, name string, nd ipld.Node) error {
226+
func (ds *Shard) Set(ctx context.Context, name string, nd ipld.Node) error {
225227
hv := &hashBits{b: hash([]byte(name))}
226228
err := ds.dserv.Add(ctx, nd)
227229
if err != nil {
@@ -238,13 +240,13 @@ func (ds *HamtShard) Set(ctx context.Context, name string, nd ipld.Node) error {
238240
}
239241

240242
// Remove deletes the named entry if it exists, this operation is idempotent.
241-
func (ds *HamtShard) Remove(ctx context.Context, name string) error {
243+
func (ds *Shard) Remove(ctx context.Context, name string) error {
242244
hv := &hashBits{b: hash([]byte(name))}
243245
return ds.modifyValue(ctx, hv, name, nil)
244246
}
245247

246248
// Find searches for a child node by 'name' within this hamt
247-
func (ds *HamtShard) Find(ctx context.Context, name string) (*ipld.Link, error) {
249+
func (ds *Shard) Find(ctx context.Context, name string) (*ipld.Link, error) {
248250
hv := &hashBits{b: hash([]byte(name))}
249251

250252
var out *ipld.Link
@@ -262,7 +264,7 @@ func (ds *HamtShard) Find(ctx context.Context, name string) (*ipld.Link, error)
262264
// getChild returns the i'th child of this shard. If it is cached in the
263265
// children array, it will return it from there. Otherwise, it loads the child
264266
// node from disk.
265-
func (ds *HamtShard) getChild(ctx context.Context, i int) (child, error) {
267+
func (ds *Shard) getChild(ctx context.Context, i int) (child, error) {
266268
if i >= len(ds.children) || i < 0 {
267269
return nil, fmt.Errorf("invalid index passed to getChild (likely corrupt bitfield)")
268270
}
@@ -281,7 +283,7 @@ func (ds *HamtShard) getChild(ctx context.Context, i int) (child, error) {
281283

282284
// loadChild reads the i'th child node of this shard from disk and returns it
283285
// as a 'child' interface
284-
func (ds *HamtShard) loadChild(ctx context.Context, i int) (child, error) {
286+
func (ds *Shard) loadChild(ctx context.Context, i int) (child, error) {
285287
lnk := ds.nd.Links()[i]
286288
if len(lnk.Name) < ds.maxpadlen {
287289
return nil, fmt.Errorf("invalid link name '%s'", lnk.Name)
@@ -326,12 +328,12 @@ func (ds *HamtShard) loadChild(ctx context.Context, i int) (child, error) {
326328
return c, nil
327329
}
328330

329-
func (ds *HamtShard) setChild(i int, c child) {
331+
func (ds *Shard) setChild(i int, c child) {
330332
ds.children[i] = c
331333
}
332334

333335
// Link returns a merklelink to this shard node
334-
func (ds *HamtShard) Link() (*ipld.Link, error) {
336+
func (ds *Shard) Link() (*ipld.Link, error) {
335337
nd, err := ds.Node()
336338
if err != nil {
337339
return nil, err
@@ -345,7 +347,7 @@ func (ds *HamtShard) Link() (*ipld.Link, error) {
345347
return ipld.MakeLink(nd)
346348
}
347349

348-
func (ds *HamtShard) insertChild(idx int, key string, lnk *ipld.Link) error {
350+
func (ds *Shard) insertChild(idx int, key string, lnk *ipld.Link) error {
349351
if lnk == nil {
350352
return os.ErrNotExist
351353
}
@@ -364,7 +366,7 @@ func (ds *HamtShard) insertChild(idx int, key string, lnk *ipld.Link) error {
364366
return nil
365367
}
366368

367-
func (ds *HamtShard) rmChild(i int) error {
369+
func (ds *Shard) rmChild(i int) error {
368370
if i < 0 || i >= len(ds.children) || i >= len(ds.nd.Links()) {
369371
return fmt.Errorf("hamt: attempted to remove child with out of range index")
370372
}
@@ -378,7 +380,7 @@ func (ds *HamtShard) rmChild(i int) error {
378380
return nil
379381
}
380382

381-
func (ds *HamtShard) getValue(ctx context.Context, hv *hashBits, key string, cb func(*shardValue) error) error {
383+
func (ds *Shard) getValue(ctx context.Context, hv *hashBits, key string, cb func(*shardValue) error) error {
382384
idx := hv.Next(ds.tableSizeLg2)
383385
if ds.bitfield.Bit(int(idx)) == 1 {
384386
cindex := ds.indexForBitPos(idx)
@@ -389,7 +391,7 @@ func (ds *HamtShard) getValue(ctx context.Context, hv *hashBits, key string, cb
389391
}
390392

391393
switch child := child.(type) {
392-
case *HamtShard:
394+
case *Shard:
393395
return child.getValue(ctx, hv, key, cb)
394396
case *shardValue:
395397
if child.key == key {
@@ -401,7 +403,8 @@ func (ds *HamtShard) getValue(ctx context.Context, hv *hashBits, key string, cb
401403
return os.ErrNotExist
402404
}
403405

404-
func (ds *HamtShard) EnumLinks(ctx context.Context) ([]*ipld.Link, error) {
406+
// EnumLinks collects all links in the Shard.
407+
func (ds *Shard) EnumLinks(ctx context.Context) ([]*ipld.Link, error) {
405408
var links []*ipld.Link
406409
err := ds.ForEachLink(ctx, func(l *ipld.Link) error {
407410
links = append(links, l)
@@ -410,7 +413,8 @@ func (ds *HamtShard) EnumLinks(ctx context.Context) ([]*ipld.Link, error) {
410413
return links, err
411414
}
412415

413-
func (ds *HamtShard) ForEachLink(ctx context.Context, f func(*ipld.Link) error) error {
416+
// ForEachLink walks the Shard and calls the given function.
417+
func (ds *Shard) ForEachLink(ctx context.Context, f func(*ipld.Link) error) error {
414418
return ds.walkTrie(ctx, func(sv *shardValue) error {
415419
lnk := sv.val
416420
lnk.Name = sv.key
@@ -419,7 +423,7 @@ func (ds *HamtShard) ForEachLink(ctx context.Context, f func(*ipld.Link) error)
419423
})
420424
}
421425

422-
func (ds *HamtShard) walkTrie(ctx context.Context, cb func(*shardValue) error) error {
426+
func (ds *Shard) walkTrie(ctx context.Context, cb func(*shardValue) error) error {
423427
for i := 0; i < ds.tableSize; i++ {
424428
if ds.bitfield.Bit(i) == 0 {
425429
continue
@@ -440,7 +444,7 @@ func (ds *HamtShard) walkTrie(ctx context.Context, cb func(*shardValue) error) e
440444
return err
441445
}
442446

443-
case *HamtShard:
447+
case *Shard:
444448
err := c.walkTrie(ctx, cb)
445449
if err != nil {
446450
return err
@@ -452,7 +456,7 @@ func (ds *HamtShard) walkTrie(ctx context.Context, cb func(*shardValue) error) e
452456
return nil
453457
}
454458

455-
func (ds *HamtShard) modifyValue(ctx context.Context, hv *hashBits, key string, val *ipld.Link) error {
459+
func (ds *Shard) modifyValue(ctx context.Context, hv *hashBits, key string, val *ipld.Link) error {
456460
idx := hv.Next(ds.tableSizeLg2)
457461

458462
if ds.bitfield.Bit(idx) != 1 {
@@ -467,7 +471,7 @@ func (ds *HamtShard) modifyValue(ctx context.Context, hv *hashBits, key string,
467471
}
468472

469473
switch child := child.(type) {
470-
case *HamtShard:
474+
case *Shard:
471475
err := child.modifyValue(ctx, hv, key, val)
472476
if err != nil {
473477
return err
@@ -510,7 +514,7 @@ func (ds *HamtShard) modifyValue(ctx context.Context, hv *hashBits, key string,
510514
}
511515

512516
// replace value with another shard, one level deeper
513-
ns, err := NewHamtShard(ds.dserv, ds.tableSize)
517+
ns, err := NewShard(ds.dserv, ds.tableSize)
514518
if err != nil {
515519
return err
516520
}
@@ -540,7 +544,7 @@ func (ds *HamtShard) modifyValue(ctx context.Context, hv *hashBits, key string,
540544
// indexForBitPos returns the index within the collapsed array corresponding to
541545
// the given bit in the bitset. The collapsed array contains only one entry
542546
// per bit set in the bitfield, and this function is used to map the indices.
543-
func (ds *HamtShard) indexForBitPos(bp int) int {
547+
func (ds *Shard) indexForBitPos(bp int) int {
544548
// TODO: an optimization could reuse the same 'mask' here and change the size
545549
// as needed. This isnt yet done as the bitset package doesnt make it easy
546550
// to do.
@@ -553,6 +557,6 @@ func (ds *HamtShard) indexForBitPos(bp int) int {
553557
}
554558

555559
// linkNamePrefix takes in the bitfield index of an entry and returns its hex prefix
556-
func (ds *HamtShard) linkNamePrefix(idx int) string {
560+
func (ds *Shard) linkNamePrefix(idx int) string {
557561
return fmt.Sprintf(ds.prefixPadStr, idx)
558562
}

unixfs/hamt/hamt_stress_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestOrderConsistency(t *testing.T) {
9494
}
9595
}
9696

97-
func validateOpSetCompletion(t *testing.T, s *HamtShard, keep, temp []string) error {
97+
func validateOpSetCompletion(t *testing.T, s *Shard, keep, temp []string) error {
9898
ctx := context.TODO()
9999
for _, n := range keep {
100100
_, err := s.Find(ctx, n)
@@ -113,9 +113,9 @@ func validateOpSetCompletion(t *testing.T, s *HamtShard, keep, temp []string) er
113113
return nil
114114
}
115115

116-
func executeOpSet(t *testing.T, ds ipld.DAGService, width int, ops []testOp) (*HamtShard, error) {
116+
func executeOpSet(t *testing.T, ds ipld.DAGService, width int, ops []testOp) (*Shard, error) {
117117
ctx := context.TODO()
118-
s, err := NewHamtShard(ds, width)
118+
s, err := NewShard(ds, width)
119119
if err != nil {
120120
return nil, err
121121
}
@@ -189,9 +189,9 @@ func genOpSet(seed int64, keep, temp []string) []testOp {
189189
}
190190

191191
// executes the given op set with a repl to allow easier debugging
192-
/*func debugExecuteOpSet(ds node.DAGService, width int, ops []testOp) (*HamtShard, error) {
192+
/*func debugExecuteOpSet(ds node.DAGService, width int, ops []testOp) (*Shard, error) {
193193
194-
s, err := NewHamtShard(ds, width)
194+
s, err := NewShard(ds, width)
195195
if err != nil {
196196
return nil, err
197197
}
@@ -244,7 +244,7 @@ mainloop:
244244
}
245245
case "restart":
246246
var err error
247-
s, err = NewHamtShard(ds, width)
247+
s, err = NewShard(ds, width)
248248
if err != nil {
249249
panic(err)
250250
}

0 commit comments

Comments
 (0)