aboutsummaryrefslogtreecommitdiff
path: root/format/pack/ingest/idx_write.go
blob: 730548aaeeaae376628a07a8e28c2a085eefaf78 (about) (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package ingest

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"hash"
	"io"
	"slices"

	"codeberg.org/lindenii/furgit/internal/intconv"
)

const (
	idxMagicV2   = 0xff744f63
	idxVersionV2 = 2
)

// writeIdx writes idx v2 for resolved records.
func writeIdx(state *ingestState) error {
	order := buildIdxOrder(state)

	hashImpl, err := state.algo.New()
	if err != nil {
		return err
	}

	write := func(src []byte) error {
		_, err := state.idxFile.Write(src)
		if err != nil {
			return err
		}

		_, err = hashImpl.Write(src)
		if err != nil {
			return err
		}

		return nil
	}

	var scratch [8]byte
	binary.BigEndian.PutUint32(scratch[:4], idxMagicV2)
	binary.BigEndian.PutUint32(scratch[4:8], idxVersionV2)

	err = write(scratch[:8])
	if err != nil {
		return err
	}

	var fanout [256]uint32

	for _, recordIdx := range order {
		idRaw := state.records[recordIdx].objectID.Bytes()
		fanout[idRaw[0]]++
	}

	var cumulative uint32
	for i := range fanout {
		cumulative += fanout[i]
		binary.BigEndian.PutUint32(scratch[:4], cumulative)

		err := write(scratch[:4])
		if err != nil {
			return err
		}
	}

	for _, recordIdx := range order {
		idRaw := state.records[recordIdx].objectID.Bytes()

		err := write(idRaw)
		if err != nil {
			return err
		}
	}

	for _, recordIdx := range order {
		binary.BigEndian.PutUint32(scratch[:4], state.records[recordIdx].crc32)

		err := write(scratch[:4])
		if err != nil {
			return err
		}
	}

	largeOffsets := make([]uint64, 0)

	for _, recordIdx := range order {
		offset := state.records[recordIdx].offset
		if offset >= 0x80000000 {
			largeOffsetIdx, err := intconv.IntToUint32(len(largeOffsets))
			if err != nil {
				return err
			}

			word := 0x80000000 | largeOffsetIdx

			largeOffsets = append(largeOffsets, offset)

			binary.BigEndian.PutUint32(scratch[:4], word)
		} else {
			binary.BigEndian.PutUint32(scratch[:4], uint32(offset))
		}

		err := write(scratch[:4])
		if err != nil {
			return err
		}
	}

	for _, off := range largeOffsets {
		binary.BigEndian.PutUint64(scratch[:8], off)

		err := write(scratch[:8])
		if err != nil {
			return err
		}
	}

	err = write(state.packHash.Bytes())
	if err != nil {
		return err
	}

	idxHash := hashImpl.Sum(nil)

	_, err = state.idxFile.Write(idxHash)
	if err != nil {
		return err
	}

	return state.idxFile.Sync()
}

// buildIdxOrder returns record indexes sorted by ObjectID.
func buildIdxOrder(state *ingestState) []int {
	out := make([]int, 0, len(state.records))
	for idx := range state.records {
		out = append(out, idx)
	}

	slices.SortFunc(out, func(a, b int) int {
		return bytes.Compare(state.records[a].objectID.Bytes(), state.records[b].objectID.Bytes())
	})

	return out
}

// verifyResolvedRecords checks that all records are fully resolved before index writing.
func verifyResolvedRecords(state *ingestState) error {
	for idx, record := range state.records {
		if !record.resolved {
			return fmt.Errorf("format/pack/ingest: unresolved record %d at offset %d", idx, record.offset)
		}
	}

	return nil
}

// writeAndHash writes src to dst and updates hash.
func writeAndHash(dst io.Writer, hashImpl hash.Hash, src []byte) error {
	_, err := dst.Write(src)
	if err != nil {
		return err
	}

	_, err = hashImpl.Write(src)
	if err != nil {
		return err
	}

	return nil
}