@@ -10,9 +10,9 @@ import (
10
10
11
11
// EmptyBlock constructs an empty Block with type in the given CID.
12
12
func EmptyBlock (cid cid.Cid ) (Block , error ) {
13
- spec , ok := specRegistry [ cid . Prefix (). MhType ]
14
- if ! ok {
15
- return nil , fmt . Errorf ( "unsupported Block type: %v" , cid . Prefix (). MhType )
13
+ spec , err := getSpec ( cid )
14
+ if err != nil {
15
+ return nil , err
16
16
}
17
17
18
18
blk , err := spec .builder (cid )
@@ -25,9 +25,9 @@ func EmptyBlock(cid cid.Cid) (Block, error) {
25
25
26
26
// maxBlockSize returns the maximum size of the Block type in the given CID.
27
27
func maxBlockSize (cid cid.Cid ) (int , error ) {
28
- spec , ok := specRegistry [ cid . Prefix (). MhType ]
29
- if ! ok {
30
- return - 1 , fmt . Errorf ( "unsupported Block type: %v" , cid . Prefix (). MhType )
28
+ spec , err := getSpec ( cid )
29
+ if err != nil {
30
+ return 0 , err
31
31
}
32
32
33
33
return spec .maxSize , nil
@@ -38,24 +38,34 @@ func registerBlock(mhcode, codec uint64, maxSize, idSize int, bldrFn func(cid.Ci
38
38
mh .Register (mhcode , func () hash.Hash {
39
39
return & hasher {IDSize : idSize }
40
40
})
41
- specRegistry [mhcode ] = blockSpec {
41
+ specRegistry [codec ] = blockSpec {
42
42
idSize : idSize ,
43
43
maxSize : maxSize ,
44
- codec : codec ,
44
+ mhCode : mhcode ,
45
45
builder : bldrFn ,
46
46
}
47
47
}
48
48
49
+ // getSpec returns the blockSpec for the given CID.
50
+ func getSpec (cid cid.Cid ) (blockSpec , error ) {
51
+ spec , ok := specRegistry [cid .Type ()]
52
+ if ! ok {
53
+ return blockSpec {}, fmt .Errorf ("unsupported codec %d" , cid .Type ())
54
+ }
55
+
56
+ return spec , nil
57
+ }
58
+
49
59
// blockSpec holds constant metadata about particular Block types.
50
60
type blockSpec struct {
51
61
idSize int
52
62
maxSize int
53
- codec uint64
63
+ mhCode uint64
54
64
builder func (cid.Cid ) (Block , error )
55
65
}
56
66
57
67
func (spec * blockSpec ) String () string {
58
- return fmt .Sprintf ("BlockSpec{IDSize: %d, Codec : %d}" , spec .idSize , spec .codec )
68
+ return fmt .Sprintf ("BlockSpec{IDSize: %d, MHCode : %d}" , spec .idSize , spec .mhCode )
59
69
}
60
70
61
71
var specRegistry = make (map [uint64 ]blockSpec )
0 commit comments