Debian Codegraph @

plumbing/format/packfile/delta_selector_test.go

raw
1 package packfile
2
3 import (
4 "github.com/go-git/go-git/v5/plumbing"
5 "github.com/go-git/go-git/v5/storage/memory"
6
7 . "gopkg.in/check.v1"
8 )
9
10 type DeltaSelectorSuite struct {
11 ds *deltaSelector
12 store *memory.Storage
13 hashes map[string]plumbing.Hash
14 }
15
16 var _ = Suite(&DeltaSelectorSuite{})
17
18 func (s *DeltaSelectorSuite) SetUpTest(c *C) {
19 s.store = memory.NewStorage()
20 s.createTestObjects()
21 s.ds = newDeltaSelector(s.store)
22 }
23
24 func (s *DeltaSelectorSuite) TestSort(c *C) {
25 var o1 = newObjectToPack(newObject(plumbing.BlobObject, []byte("00000")))
26 var o4 = newObjectToPack(newObject(plumbing.BlobObject, []byte("0000")))
27 var o6 = newObjectToPack(newObject(plumbing.BlobObject, []byte("00")))
28 var o9 = newObjectToPack(newObject(plumbing.BlobObject, []byte("0")))
29 var o8 = newObjectToPack(newObject(plumbing.TreeObject, []byte("000")))
30 var o2 = newObjectToPack(newObject(plumbing.TreeObject, []byte("00")))
31 var o3 = newObjectToPack(newObject(plumbing.TreeObject, []byte("0")))
32 var o5 = newObjectToPack(newObject(plumbing.CommitObject, []byte("0000")))
33 var o7 = newObjectToPack(newObject(plumbing.CommitObject, []byte("00")))
34
35 toSort := []*ObjectToPack{o1, o2, o3, o4, o5, o6, o7, o8, o9}
36 s.ds.sort(toSort)
37 expected := []*ObjectToPack{o1, o4, o6, o9, o8, o2, o3, o5, o7}
38 c.Assert(toSort, DeepEquals, expected)
39 }
40
41 type testObject struct {
42 id string
43 object plumbing.EncodedObject
44 }
45
46 var testObjects []*testObject = []*testObject{{
47 id: "base",
48 object: newObject(plumbing.BlobObject,
49 genBytes([]piece{{
50 times: 1000,
51 val: "a",
52 }, {
53 times: 1000,
54 val: "b",
55 }})),
56 }, {
57 id: "smallBase",
58 object: newObject(plumbing.BlobObject,
59 genBytes([]piece{{
60 times: 1,
61 val: "a",
62 }, {
63 times: 1,
64 val: "b",
65 }, {
66 times: 6,
67 val: "c",
68 }})),
69 }, {
70 id: "smallTarget",
71 object: newObject(plumbing.BlobObject,
72 genBytes([]piece{{
73 times: 1,
74 val: "a",
75 }, {
76 times: 1,
77 val: "c",
78 }})),
79 }, {
80 id: "target",
81 object: newObject(plumbing.BlobObject,
82 genBytes([]piece{{
83 times: 1000,
84 val: "a",
85 }, {
86 times: 1000,
87 val: "b",
88 }, {
89 times: 1000,
90 val: "c",
91 }})),
92 }, {
93 id: "o1",
94 object: newObject(plumbing.BlobObject,
95 genBytes([]piece{{
96 times: 1000,
97 val: "a",
98 }, {
99 times: 1000,
100 val: "b",
101 }})),
102 }, {
103 id: "o2",
104 object: newObject(plumbing.BlobObject,
105 genBytes([]piece{{
106 times: 1000,
107 val: "a",
108 }, {
109 times: 500,
110 val: "b",
111 }})),
112 }, {
113 id: "o3",
114 object: newObject(plumbing.BlobObject,
115 genBytes([]piece{{
116 times: 1000,
117 val: "a",
118 }, {
119 times: 499,
120 val: "b",
121 }})),
122 }, {
123 id: "bigBase",
124 object: newObject(plumbing.BlobObject,
125 genBytes([]piece{{
126 times: 1000000,
127 val: "a",
128 }})),
129 }, {
130 id: "treeType",
131 object: newObject(plumbing.TreeObject,
132 []byte("I am a tree!")),
133 }}
134
135 func (s *DeltaSelectorSuite) createTestObjects() {
136 s.hashes = make(map[string]plumbing.Hash)
137 for _, o := range testObjects {
138 h, err := s.store.SetEncodedObject(o.object)
139 if err != nil {
140 panic(err)
141 }
142 s.hashes[o.id] = h
143 }
144 }
145
146 func (s *DeltaSelectorSuite) TestObjectsToPack(c *C) {
147 // Different type
148 hashes := []plumbing.Hash{s.hashes["base"], s.hashes["treeType"]}
149 deltaWindowSize := uint(10)
150 otp, err := s.ds.ObjectsToPack(hashes, deltaWindowSize)
151 c.Assert(err, IsNil)
152 c.Assert(len(otp), Equals, 2)
153 c.Assert(otp[0].Object, Equals, s.store.Objects[s.hashes["base"]])
154 c.Assert(otp[1].Object, Equals, s.store.Objects[s.hashes["treeType"]])
155
156 // Size radically different
157 hashes = []plumbing.Hash{s.hashes["bigBase"], s.hashes["target"]}
158 otp, err = s.ds.ObjectsToPack(hashes, deltaWindowSize)
159 c.Assert(err, IsNil)
160 c.Assert(len(otp), Equals, 2)
161 c.Assert(otp[0].Object, Equals, s.store.Objects[s.hashes["bigBase"]])
162 c.Assert(otp[1].Object, Equals, s.store.Objects[s.hashes["target"]])
163
164 // Delta Size Limit with no best delta yet
165 hashes = []plumbing.Hash{s.hashes["smallBase"], s.hashes["smallTarget"]}
166 otp, err = s.ds.ObjectsToPack(hashes, deltaWindowSize)
167 c.Assert(err, IsNil)
168 c.Assert(len(otp), Equals, 2)
169 c.Assert(otp[0].Object, Equals, s.store.Objects[s.hashes["smallBase"]])
170 c.Assert(otp[1].Object, Equals, s.store.Objects[s.hashes["smallTarget"]])
171
172 // It will create the delta
173 hashes = []plumbing.Hash{s.hashes["base"], s.hashes["target"]}
174 otp, err = s.ds.ObjectsToPack(hashes, deltaWindowSize)
175 c.Assert(err, IsNil)
176 c.Assert(len(otp), Equals, 2)
177 c.Assert(otp[0].Object, Equals, s.store.Objects[s.hashes["target"]])
178 c.Assert(otp[0].IsDelta(), Equals, false)
179 c.Assert(otp[1].Original, Equals, s.store.Objects[s.hashes["base"]])
180 c.Assert(otp[1].IsDelta(), Equals, true)
181 c.Assert(otp[1].Depth, Equals, 1)
182
183 // If our base is another delta, the depth will increase by one
184 hashes = []plumbing.Hash{
185 s.hashes["o1"],
186 s.hashes["o2"],
187 s.hashes["o3"],
188 }
189 otp, err = s.ds.ObjectsToPack(hashes, deltaWindowSize)
190 c.Assert(err, IsNil)
191 c.Assert(len(otp), Equals, 3)
192 c.Assert(otp[0].Object, Equals, s.store.Objects[s.hashes["o1"]])
193 c.Assert(otp[0].IsDelta(), Equals, false)
194 c.Assert(otp[1].Original, Equals, s.store.Objects[s.hashes["o2"]])
195 c.Assert(otp[1].IsDelta(), Equals, true)
196 c.Assert(otp[1].Depth, Equals, 1)
197 c.Assert(otp[2].Original, Equals, s.store.Objects[s.hashes["o3"]])
198 c.Assert(otp[2].IsDelta(), Equals, true)
199 c.Assert(otp[2].Depth, Equals, 2)
200
201 // Check that objects outside of the sliding window don't produce
202 // a delta.
203 hashes = make([]plumbing.Hash, 0, deltaWindowSize+2)
204 hashes = append(hashes, s.hashes["base"])
205 for i := uint(0); i < deltaWindowSize; i++ {
206 hashes = append(hashes, s.hashes["smallTarget"])
207 }
208 hashes = append(hashes, s.hashes["target"])
209
210 // Don't sort so we can easily check the sliding window without
211 // creating a bunch of new objects.
212 otp, err = s.ds.objectsToPack(hashes, deltaWindowSize)
213 c.Assert(err, IsNil)
214 err = s.ds.walk(otp, deltaWindowSize)
215 c.Assert(err, IsNil)
216 c.Assert(len(otp), Equals, int(deltaWindowSize)+2)
217 targetIdx := len(otp) - 1
218 c.Assert(otp[targetIdx].IsDelta(), Equals, false)
219
220 // Check that no deltas are created, and the objects are unsorted,
221 // if compression is off.
222 hashes = []plumbing.Hash{s.hashes["base"], s.hashes["target"]}
223 otp, err = s.ds.ObjectsToPack(hashes, 0)
224 c.Assert(err, IsNil)
225 c.Assert(len(otp), Equals, 2)
226 c.Assert(otp[0].Object, Equals, s.store.Objects[s.hashes["base"]])
227 c.Assert(otp[0].IsDelta(), Equals, false)
228 c.Assert(otp[1].Original, Equals, s.store.Objects[s.hashes["target"]])
229 c.Assert(otp[1].IsDelta(), Equals, false)
230 c.Assert(otp[1].Depth, Equals, 0)
231 }
232
233 func (s *DeltaSelectorSuite) TestMaxDepth(c *C) {
234 dsl := s.ds.deltaSizeLimit(0, 0, int(maxDepth), true)
235 c.Assert(dsl, Equals, int64(0))
236 }