aboutsummaryrefslogtreecommitdiffstats
path: root/formats/idxfile/encoder.go
blob: 0fe9ae6a1f23bca4b584c92b3c4aadddab89b1d4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package idxfile

import (
	"crypto/sha1"
	"encoding/binary"
	"fmt"
	"hash"
	"io"
	"sort"

	"gopkg.in/src-d/go-git.v4/core"
)

// An Encoder writes idx files to an output stream.
type Encoder struct {
	io.Writer
	hash hash.Hash
}

// NewEncoder returns a new encoder that writes to w.
func NewEncoder(w io.Writer) *Encoder {
	h := sha1.New()
	mw := io.MultiWriter(w, h)
	return &Encoder{mw, h}
}

// Encode writes the idx in an idx file format to the stream of the encoder.
func (e *Encoder) Encode(idx *Idxfile) (int, error) {
	idx.Entries.Sort()

	flow := []func(*Idxfile) (int, error){
		e.encodeHeader,
		e.encodeFanout,
		e.encodeHashes,
		e.encodeCRC32,
		e.encodeOffsets,
		e.encodeChecksums,
	}

	sz := 0
	for _, f := range flow {
		i, err := f(idx)
		sz += i

		if err != nil {
			return sz, err
		}
	}

	return sz, nil
}

func (e *Encoder) encodeHeader(idx *Idxfile) (int, error) {
	c, err := e.Write(idxHeader)
	if err != nil {
		return c, err
	}

	return c + 4, e.writeInt32(idx.Version)
}

func (e *Encoder) encodeFanout(idx *Idxfile) (int, error) {
	fanout := idx.calculateFanout()
	for _, c := range fanout {
		if err := e.writeInt32(c); err != nil {
			return 0, err
		}
	}

	return 1024, nil
}

func (e *Encoder) encodeHashes(idx *Idxfile) (int, error) {
	repet := make(map[core.Hash]int, 0)

	sz := 0
	for _, ent := range idx.Entries {
		i, err := e.Write(ent.Hash[:])
		sz += i

		repet[ent.Hash]++
		if err != nil {
			return sz, err
		}
	}

	for h, c := range repet {
		if c > 1 {
			fmt.Println(h, c)
		}
	}
	return sz, nil
}

func (e *Encoder) encodeCRC32(idx *Idxfile) (int, error) {
	sz := 0
	for _, ent := range idx.Entries {
		err := binary.Write(e, binary.BigEndian, ent.CRC32)
		sz += 4

		if err != nil {
			return sz, err
		}
	}

	return sz, nil
}

func (e *Encoder) encodeOffsets(idx *Idxfile) (int, error) {
	sz := 0
	for _, ent := range idx.Entries {
		if err := e.writeInt32(uint32(ent.Offset)); err != nil {
			return sz, err
		}

		sz += 4

	}

	return sz, nil
}

func (e *Encoder) encodeChecksums(idx *Idxfile) (int, error) {
	if _, err := e.Write(idx.PackfileChecksum[:]); err != nil {
		return 0, err
	}

	copy(idx.IdxChecksum[:], e.hash.Sum(nil)[:20])
	if _, err := e.Write(idx.IdxChecksum[:]); err != nil {
		return 0, err
	}

	return 40, nil
}

func (e *Encoder) writeInt32(value uint32) error {
	return binary.Write(e, binary.BigEndian, value)
}

type EntryList []Entry

func (p EntryList) Len() int           { return len(p) }
func (p EntryList) Less(i, j int) bool { return p[i].Hash.String() < p[j].Hash.String() }
func (p EntryList) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
func (p EntryList) Sort()              { sort.Sort(p) }