Debian Codegraph @

plumbing/format/idxfile/writer.go

raw
1 package idxfile
2
3 import (
4 "bytes"
5 "fmt"
6 "math"
7 "sort"
8 "sync"
9
10 "github.com/go-git/go-git/v5/plumbing"
11 "github.com/go-git/go-git/v5/utils/binary"
12 )
13
14 // objects implements sort.Interface and uses hash as sorting key.
15 type objects []Entry
16
17 // Writer implements a packfile Observer interface and is used to generate
18 // indexes.
19 type Writer struct {
20 m sync.Mutex
21
22 count uint32
23 checksum plumbing.Hash
24 objects objects
25 offset64 uint32
26 finished bool
27 index *MemoryIndex
28 added map[plumbing.Hash]struct{}
29 }
30
31 // Index returns a previously created MemoryIndex or creates a new one if
32 // needed.
33 func (w *Writer) Index() (*MemoryIndex, error) {
34 w.m.Lock()
35 defer w.m.Unlock()
36
37 if w.index == nil {
38 return w.createIndex()
39 }
40
41 return w.index, nil
42 }
43
44 // Add appends new object data.
45 func (w *Writer) Add(h plumbing.Hash, pos uint64, crc uint32) {
46 w.m.Lock()
47 defer w.m.Unlock()
48
49 if w.added == nil {
50 w.added = make(map[plumbing.Hash]struct{})
51 }
52
53 if _, ok := w.added[h]; !ok {
54 w.added[h] = struct{}{}
55 w.objects = append(w.objects, Entry{h, crc, pos})
56 }
57
58 }
59
60 func (w *Writer) Finished() bool {
61 return w.finished
62 }
63
64 // OnHeader implements packfile.Observer interface.
65 func (w *Writer) OnHeader(count uint32) error {
66 w.count = count
67 w.objects = make(objects, 0, count)
68 return nil
69 }
70
71 // OnInflatedObjectHeader implements packfile.Observer interface.
72 func (w *Writer) OnInflatedObjectHeader(t plumbing.ObjectType, objSize int64, pos int64) error {
73 return nil
74 }
75
76 // OnInflatedObjectContent implements packfile.Observer interface.
77 func (w *Writer) OnInflatedObjectContent(h plumbing.Hash, pos int64, crc uint32, _ []byte) error {
78 w.Add(h, uint64(pos), crc)
79 return nil
80 }
81
82 // OnFooter implements packfile.Observer interface.
83 func (w *Writer) OnFooter(h plumbing.Hash) error {
84 w.checksum = h
85 w.finished = true
86 _, err := w.createIndex()
87
88 return err
89 }
90
91 // creatIndex returns a filled MemoryIndex with the information filled by
92 // the observer callbacks.
93 func (w *Writer) createIndex() (*MemoryIndex, error) {
94 if !w.finished {
95 return nil, fmt.Errorf("the index still hasn't finished building")
96 }
97
98 idx := new(MemoryIndex)
99 w.index = idx
100
101 sort.Sort(w.objects)
102
103 // unmap all fans by default
104 for i := range idx.FanoutMapping {
105 idx.FanoutMapping[i] = noMapping
106 }
107
108 buf := new(bytes.Buffer)
109
110 last := -1
111 bucket := -1
112 for i, o := range w.objects {
113 fan := o.Hash[0]
114
115 // fill the gaps between fans
116 for j := last + 1; j < int(fan); j++ {
117 idx.Fanout[j] = uint32(i)
118 }
119
120 // update the number of objects for this position
121 idx.Fanout[fan] = uint32(i + 1)
122
123 // we move from one bucket to another, update counters and allocate
124 // memory
125 if last != int(fan) {
126 bucket++
127 idx.FanoutMapping[fan] = bucket
128 last = int(fan)
129
130 idx.Names = append(idx.Names, make([]byte, 0))
131 idx.Offset32 = append(idx.Offset32, make([]byte, 0))
132 idx.CRC32 = append(idx.CRC32, make([]byte, 0))
133 }
134
135 idx.Names[bucket] = append(idx.Names[bucket], o.Hash[:]...)
136
137 offset := o.Offset
138 if offset > math.MaxInt32 {
139 var err error
140 offset, err = w.addOffset64(offset)
141 if err != nil {
142 return nil, err
143 }
144 }
145
146 buf.Truncate(0)
147 if err := binary.WriteUint32(buf, uint32(offset)); err != nil {
148 return nil, err
149 }
150 idx.Offset32[bucket] = append(idx.Offset32[bucket], buf.Bytes()...)
151
152 buf.Truncate(0)
153 if err := binary.WriteUint32(buf, o.CRC32); err != nil {
154 return nil, err
155 }
156 idx.CRC32[bucket] = append(idx.CRC32[bucket], buf.Bytes()...)
157 }
158
159 for j := last + 1; j < 256; j++ {
160 idx.Fanout[j] = uint32(len(w.objects))
161 }
162
163 idx.Version = VersionSupported
164 idx.PackfileChecksum = w.checksum
165
166 return idx, nil
167 }
168
169 func (w *Writer) addOffset64(pos uint64) (uint64, error) {
170 buf := new(bytes.Buffer)
171 if err := binary.WriteUint64(buf, pos); err != nil {
172 return 0, err
173 }
174
175 w.index.Offset64 = append(w.index.Offset64, buf.Bytes()...)
176 index := uint64(w.offset64 | (1 << 31))
177 w.offset64++
178
179 return index, nil
180 }
181
182 func (o objects) Len() int {
183 return len(o)
184 }
185
186 func (o objects) Less(i int, j int) bool {
187 cmp := bytes.Compare(o[i].Hash[:], o[j].Hash[:])
188 return cmp < 0
189 }
190
191 func (o objects) Swap(i int, j int) {
192 o[i], o[j] = o[j], o[i]
193 }