Skip to content

Commit b058860

Browse files
committed
Precompute Blob CID, fixes #21
1 parent 479420c commit b058860

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

blob.go

+22-27
Original file line numberDiff line numberDiff line change
@@ -5,67 +5,62 @@ import (
55

66
cid "github.com/ipfs/go-cid"
77
node "github.com/ipfs/go-ipld-format"
8-
mh "github.com/multiformats/go-multihash"
98
)
109

11-
type Blob []byte
10+
type Blob struct {
11+
rawData []byte
12+
cid cid.Cid
13+
}
1214

13-
func (b Blob) Cid() cid.Cid {
14-
c, _ := cid.Prefix{
15-
MhType: mh.SHA1,
16-
MhLength: -1,
17-
Codec: cid.GitRaw,
18-
Version: 1,
19-
}.Sum([]byte(b))
20-
return c
15+
func (b *Blob) Cid() cid.Cid {
16+
return b.cid
2117
}
2218

23-
func (b Blob) Copy() node.Node {
24-
out := make([]byte, len(b))
25-
copy(out, b)
26-
return Blob(out)
19+
func (b *Blob) Copy() node.Node {
20+
nb := *b
21+
return &nb
2722
}
2823

29-
func (b Blob) Links() []*node.Link {
24+
func (b *Blob) Links() []*node.Link {
3025
return nil
3126
}
3227

33-
func (b Blob) Resolve(_ []string) (interface{}, []string, error) {
28+
func (b *Blob) Resolve(_ []string) (interface{}, []string, error) {
3429
return nil, nil, errors.New("no such link")
3530
}
3631

37-
func (b Blob) ResolveLink(_ []string) (*node.Link, []string, error) {
32+
func (b *Blob) ResolveLink(_ []string) (*node.Link, []string, error) {
3833
return nil, nil, errors.New("no such link")
3934
}
4035

41-
func (b Blob) Loggable() map[string]interface{} {
36+
func (b *Blob) Loggable() map[string]interface{} {
4237
return map[string]interface{}{
4338
"type": "git_blob",
4439
}
4540
}
4641

47-
func (b Blob) RawData() []byte {
48-
return []byte(b)
42+
func (b *Blob) RawData() []byte {
43+
return []byte(b.rawData)
4944
}
5045

51-
func (b Blob) Size() (uint64, error) {
52-
return uint64(len(b)), nil
46+
func (b *Blob) Size() (uint64, error) {
47+
return uint64(len(b.rawData)), nil
5348
}
5449

55-
func (b Blob) Stat() (*node.NodeStat, error) {
50+
func (b *Blob) Stat() (*node.NodeStat, error) {
5651
return &node.NodeStat{}, nil
5752
}
5853

59-
func (b Blob) String() string {
54+
func (b *Blob) String() string {
6055
return "[git blob]"
6156
}
6257

63-
func (b Blob) Tree(p string, depth int) []string {
58+
func (b *Blob) Tree(p string, depth int) []string {
6459
return nil
6560
}
6661

67-
func (b Blob) GitSha() []byte {
62+
func (b *Blob) GitSha() []byte {
6863
return cidToSha(b.Cid())
6964
}
7065

71-
var _ node.Node = (Blob)(nil)
66+
var _ node.Node = (*Blob)(nil)

git.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func ParseObject(r io.Reader) (node.Node, error) {
6767
}
6868
}
6969

70-
func ReadBlob(rd *bufio.Reader) (Blob, error) {
70+
func ReadBlob(rd *bufio.Reader) (*Blob, error) {
7171
size, err := rd.ReadString(0)
7272
if err != nil {
7373
return nil, err
@@ -90,7 +90,11 @@ func ReadBlob(rd *bufio.Reader) (Blob, error) {
9090
return nil, fmt.Errorf("blob size was not accurate")
9191
}
9292

93-
return Blob(buf.Bytes()), nil
93+
out := &Blob{}
94+
out.rawData = buf.Bytes()
95+
out.cid = hashObject(out.RawData())
96+
97+
return out, nil
9498
}
9599

96100
func ReadCommit(rd *bufio.Reader) (*Commit, error) {

git_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func TestArchiveObjectParse(t *testing.T) {
152152
func testNode(t *testing.T, nd node.Node) error {
153153
switch nd.String() {
154154
case "[git blob]":
155-
blob, ok := nd.(Blob)
155+
blob, ok := nd.(*Blob)
156156
if !ok {
157157
t.Fatalf("Blob is not a blob")
158158
}

0 commit comments

Comments
 (0)