Show More
@@ -1,367 +1,367 | |||
|
1 | 1 | use byteorder::{BigEndian, ByteOrder}; |
|
2 | 2 | |
|
3 | 3 | /// A chunk of data to insert, delete or replace in a patch |
|
4 | 4 | /// |
|
5 | 5 | /// A chunk is: |
|
6 | 6 | /// - an insertion when `!data.is_empty() && start == end` |
|
7 | 7 | /// - an deletion when `data.is_empty() && start < end` |
|
8 | 8 | /// - a replacement when `!data.is_empty() && start < end` |
|
9 | 9 | /// - not doing anything when `data.is_empty() && start == end` |
|
10 | 10 | #[derive(Debug, Clone)] |
|
11 | 11 | struct PatchFrag<'a> { |
|
12 | 12 | /// The start position of the chunk of data to replace |
|
13 | 13 | start: i32, |
|
14 | 14 | /// The end position of the chunk of data to replace (open end interval) |
|
15 | 15 | end: i32, |
|
16 | 16 | /// The data replacing the chunk |
|
17 | 17 | data: &'a [u8], |
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | impl<'a> PatchFrag<'a> { |
|
21 | 21 | /// Adjusted start of the chunk to replace. |
|
22 | 22 | /// |
|
23 | 23 | /// Offset allow to take into account the growth/shrinkage of data |
|
24 | 24 | /// induced by previously applied chunks. |
|
25 | 25 | fn start_offseted_by(&self, offset: i32) -> i32 { |
|
26 | 26 | self.start + offset |
|
27 | 27 | } |
|
28 | 28 | |
|
29 | 29 | /// Adjusted end of the chunk to replace. |
|
30 | 30 | /// |
|
31 | 31 | /// Offset allow to take into account the growth/shrinkage of data |
|
32 | 32 | /// induced by previously applied chunks. |
|
33 | 33 | fn end_offseted_by(&self, offset: i32) -> i32 { |
|
34 | 34 | self.start_offseted_by(offset) + (self.data.len() as i32) |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | /// Length of the replaced chunk. |
|
38 | 38 | fn replaced_len(&self) -> i32 { |
|
39 | 39 | self.end - self.start |
|
40 | 40 | } |
|
41 | 41 | |
|
42 | 42 | /// Length difference between the replacing data and the replaced data. |
|
43 | 43 | fn len_diff(&self) -> i32 { |
|
44 | 44 | (self.data.len() as i32) - self.replaced_len() |
|
45 | 45 | } |
|
46 | 46 | } |
|
47 | 47 | |
|
48 | 48 | /// The delta between two revisions data. |
|
49 | 49 | #[derive(Debug, Clone)] |
|
50 | 50 | pub struct PatchList<'a> { |
|
51 | 51 | /// A collection of chunks to apply. |
|
52 | 52 | /// |
|
53 | 53 | /// Those chunks are: |
|
54 | 54 | /// - ordered from the left-most replacement to the right-most replacement |
|
55 | 55 | /// - non-overlapping, meaning that two chucks can not change the same |
|
56 | 56 | /// chunk of the patched data |
|
57 | 57 | frags: Vec<PatchFrag<'a>>, |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | impl<'a> PatchList<'a> { |
|
61 | 61 | /// Create a `PatchList` from bytes. |
|
62 | 62 | pub fn new(data: &'a [u8]) -> Self { |
|
63 | 63 | let mut frags = vec![]; |
|
64 | 64 | let mut data = data; |
|
65 | 65 | while !data.is_empty() { |
|
66 | 66 | let start = BigEndian::read_i32(&data[0..]); |
|
67 | 67 | let end = BigEndian::read_i32(&data[4..]); |
|
68 | 68 | let len = BigEndian::read_i32(&data[8..]); |
|
69 | 69 | assert!(0 <= start && start <= end && len >= 0); |
|
70 | 70 | frags.push(PatchFrag { |
|
71 | 71 | start, |
|
72 | 72 | end, |
|
73 | 73 | data: &data[12..12 + (len as usize)], |
|
74 | 74 | }); |
|
75 | 75 | data = &data[12 + (len as usize)..]; |
|
76 | 76 | } |
|
77 | 77 | PatchList { frags } |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | /// Return the final length of data after patching |
|
81 | 81 | /// given its initial length . |
|
82 | 82 | fn size(&self, initial_size: i32) -> i32 { |
|
83 | 83 | self.frags |
|
84 | 84 | .iter() |
|
85 | 85 | .fold(initial_size, |acc, frag| acc + frag.len_diff()) |
|
86 | 86 | } |
|
87 | 87 | |
|
88 | 88 | /// Apply the patch to some data. |
|
89 | 89 | pub fn apply(&self, initial: &[u8]) -> Vec<u8> { |
|
90 | 90 | let mut last: usize = 0; |
|
91 | 91 | let mut vec = |
|
92 | 92 | Vec::with_capacity(self.size(initial.len() as i32) as usize); |
|
93 | 93 | for PatchFrag { start, end, data } in self.frags.iter() { |
|
94 | 94 | vec.extend(&initial[last..(*start as usize)]); |
|
95 | 95 | vec.extend(data.iter()); |
|
96 | 96 | last = *end as usize; |
|
97 | 97 | } |
|
98 | 98 | vec.extend(&initial[last..]); |
|
99 | 99 | vec |
|
100 | 100 | } |
|
101 | 101 | |
|
102 | 102 | /// Combine two patch lists into a single patch list. |
|
103 | 103 | /// |
|
104 | 104 | /// Applying consecutive patches can lead to waste of time and memory |
|
105 | 105 | /// as the changes introduced by one patch can be overridden by the next. |
|
106 | 106 | /// Combining patches optimizes the whole patching sequence. |
|
107 | 107 | fn combine(&mut self, other: &mut Self) -> Self { |
|
108 | 108 | let mut frags = vec![]; |
|
109 | 109 | |
|
110 | 110 | // Keep track of each growth/shrinkage resulting from applying a chunk |
|
111 | 111 | // in order to adjust the start/end of subsequent chunks. |
|
112 | 112 | let mut offset = 0i32; |
|
113 | 113 | |
|
114 | 114 | // Keep track of the chunk of self.chunks to process. |
|
115 | 115 | let mut pos = 0; |
|
116 | 116 | |
|
117 | 117 | // For each chunk of `other`, chunks of `self` are processed |
|
118 | 118 | // until they start after the end of the current chunk. |
|
119 | 119 | for PatchFrag { start, end, data } in other.frags.iter() { |
|
120 | 120 | // Add chunks of `self` that start before this chunk of `other` |
|
121 | 121 | // without overlap. |
|
122 | 122 | while pos < self.frags.len() |
|
123 | 123 | && self.frags[pos].end_offseted_by(offset) <= *start |
|
124 | 124 | { |
|
125 | 125 | let first = self.frags[pos].clone(); |
|
126 | 126 | offset += first.len_diff(); |
|
127 | 127 | frags.push(first); |
|
128 | 128 | pos += 1; |
|
129 | 129 | } |
|
130 | 130 | |
|
131 | 131 | // The current chunk of `self` starts before this chunk of `other` |
|
132 | 132 | // with overlap. |
|
133 | 133 | // The left-most part of data is added as an insertion chunk. |
|
134 | 134 | // The right-most part data is kept in the chunk. |
|
135 | 135 | if pos < self.frags.len() |
|
136 | 136 | && self.frags[pos].start_offseted_by(offset) < *start |
|
137 | 137 | { |
|
138 | 138 | let first = &mut self.frags[pos]; |
|
139 | 139 | |
|
140 | 140 | let (data_left, data_right) = first.data.split_at( |
|
141 | 141 | (*start - first.start_offseted_by(offset)) as usize, |
|
142 | 142 | ); |
|
143 | 143 | let left = PatchFrag { |
|
144 | 144 | start: first.start, |
|
145 | 145 | end: first.start, |
|
146 | 146 | data: data_left, |
|
147 | 147 | }; |
|
148 | 148 | |
|
149 | 149 | first.data = data_right; |
|
150 | 150 | |
|
151 | 151 | offset += left.len_diff(); |
|
152 | 152 | |
|
153 | 153 | frags.push(left); |
|
154 | 154 | |
|
155 | 155 | // There is no index incrementation because the right-most part |
|
156 | 156 | // needs further examination. |
|
157 | 157 | } |
|
158 | 158 | |
|
159 | 159 | // At this point remaining chunks of `self` starts after |
|
160 | 160 | // the current chunk of `other`. |
|
161 | 161 | |
|
162 | 162 | // `start_offset` will be used to adjust the start of the current |
|
163 | 163 | // chunk of `other`. |
|
164 | 164 | // Offset tracking continues with `end_offset` to adjust the end |
|
165 | 165 | // of the current chunk of `other`. |
|
166 | 166 | let mut next_offset = offset; |
|
167 | 167 | |
|
168 | 168 | // Discard the chunks of `self` that are totally overridden |
|
169 | 169 | // by the current chunk of `other` |
|
170 | 170 | while pos < self.frags.len() |
|
171 | 171 | && self.frags[pos].end_offseted_by(next_offset) <= *end |
|
172 | 172 | { |
|
173 | 173 | let first = &self.frags[pos]; |
|
174 | 174 | next_offset += first.len_diff(); |
|
175 | 175 | pos += 1; |
|
176 | 176 | } |
|
177 | 177 | |
|
178 | 178 | // Truncate the left-most part of chunk of `self` that overlaps |
|
179 | 179 | // the current chunk of `other`. |
|
180 | 180 | if pos < self.frags.len() |
|
181 | 181 | && self.frags[pos].start_offseted_by(next_offset) < *end |
|
182 | 182 | { |
|
183 | 183 | let first = &mut self.frags[pos]; |
|
184 | 184 | |
|
185 | 185 | let how_much_to_discard = |
|
186 | 186 | *end - first.start_offseted_by(next_offset); |
|
187 | 187 | |
|
188 | 188 | first.data = &first.data[(how_much_to_discard as usize)..]; |
|
189 | 189 | |
|
190 | 190 | next_offset += how_much_to_discard; |
|
191 | 191 | } |
|
192 | 192 | |
|
193 | 193 | // Add the chunk of `other` with adjusted position. |
|
194 | 194 | frags.push(PatchFrag { |
|
195 | 195 | start: *start - offset, |
|
196 | 196 | end: *end - next_offset, |
|
197 | 197 | data, |
|
198 | 198 | }); |
|
199 | 199 | |
|
200 | 200 | // Go back to normal offset tracking for the next `o` chunk |
|
201 | 201 | offset = next_offset; |
|
202 | 202 | } |
|
203 | 203 | |
|
204 | 204 | // Add remaining chunks of `self`. |
|
205 | 205 | for elt in &self.frags[pos..] { |
|
206 | 206 | frags.push(elt.clone()); |
|
207 | 207 | } |
|
208 | 208 | PatchList { frags } |
|
209 | 209 | } |
|
210 | 210 | } |
|
211 | 211 | |
|
212 | 212 | /// Combine a list of patch list into a single patch optimized patch list. |
|
213 | 213 | pub fn fold_patch_lists<'a>(lists: &[PatchList<'a>]) -> PatchList<'a> { |
|
214 | 214 | if lists.len() <= 1 { |
|
215 | 215 | if lists.is_empty() { |
|
216 | 216 | PatchList { frags: vec![] } |
|
217 | 217 | } else { |
|
218 | 218 | lists[0].clone() |
|
219 | 219 | } |
|
220 | 220 | } else { |
|
221 | 221 | let (left, right) = lists.split_at(lists.len() / 2); |
|
222 | 222 | let mut left_res = fold_patch_lists(left); |
|
223 | 223 | let mut right_res = fold_patch_lists(right); |
|
224 | 224 | left_res.combine(&mut right_res) |
|
225 | 225 | } |
|
226 | 226 | } |
|
227 | 227 | |
|
228 | 228 | #[cfg(test)] |
|
229 | 229 | mod tests { |
|
230 | 230 | use super::*; |
|
231 | 231 | |
|
232 | 232 | struct PatchDataBuilder { |
|
233 | 233 | data: Vec<u8>, |
|
234 | 234 | } |
|
235 | 235 | |
|
236 | 236 | impl PatchDataBuilder { |
|
237 | 237 | pub fn new() -> Self { |
|
238 | 238 | Self { data: vec![] } |
|
239 | 239 | } |
|
240 | 240 | |
|
241 | 241 | pub fn replace( |
|
242 | 242 | &mut self, |
|
243 | 243 | start: usize, |
|
244 | 244 | end: usize, |
|
245 | 245 | data: &[u8], |
|
246 | 246 | ) -> &mut Self { |
|
247 | 247 | assert!(start <= end); |
|
248 | 248 | self.data.extend(&(start as i32).to_be_bytes()); |
|
249 | 249 | self.data.extend(&(end as i32).to_be_bytes()); |
|
250 | 250 | self.data.extend(&(data.len() as i32).to_be_bytes()); |
|
251 | 251 | self.data.extend(data.iter()); |
|
252 | 252 | self |
|
253 | 253 | } |
|
254 | 254 | |
|
255 | 255 | pub fn get(&mut self) -> &[u8] { |
|
256 |
&self.data |
|
|
256 | &self.data | |
|
257 | 257 | } |
|
258 | 258 | } |
|
259 | 259 | |
|
260 | 260 | #[test] |
|
261 | 261 | fn test_ends_before() { |
|
262 | 262 | let data = vec![0u8, 0u8, 0u8]; |
|
263 | 263 | let mut patch1_data = PatchDataBuilder::new(); |
|
264 | 264 | patch1_data.replace(0, 1, &[1, 2]); |
|
265 | 265 | let mut patch1 = PatchList::new(patch1_data.get()); |
|
266 | 266 | |
|
267 | 267 | let mut patch2_data = PatchDataBuilder::new(); |
|
268 | 268 | patch2_data.replace(2, 4, &[3, 4]); |
|
269 | 269 | let mut patch2 = PatchList::new(patch2_data.get()); |
|
270 | 270 | |
|
271 | 271 | let patch = patch1.combine(&mut patch2); |
|
272 | 272 | |
|
273 | 273 | let result = patch.apply(&data); |
|
274 | 274 | |
|
275 | 275 | assert_eq!(result, vec![1u8, 2, 3, 4]); |
|
276 | 276 | } |
|
277 | 277 | |
|
278 | 278 | #[test] |
|
279 | 279 | fn test_starts_after() { |
|
280 | 280 | let data = vec![0u8, 0u8, 0u8]; |
|
281 | 281 | let mut patch1_data = PatchDataBuilder::new(); |
|
282 | 282 | patch1_data.replace(2, 3, &[3]); |
|
283 | 283 | let mut patch1 = PatchList::new(patch1_data.get()); |
|
284 | 284 | |
|
285 | 285 | let mut patch2_data = PatchDataBuilder::new(); |
|
286 | 286 | patch2_data.replace(1, 2, &[1, 2]); |
|
287 | 287 | let mut patch2 = PatchList::new(patch2_data.get()); |
|
288 | 288 | |
|
289 | 289 | let patch = patch1.combine(&mut patch2); |
|
290 | 290 | |
|
291 | 291 | let result = patch.apply(&data); |
|
292 | 292 | |
|
293 | 293 | assert_eq!(result, vec![0u8, 1, 2, 3]); |
|
294 | 294 | } |
|
295 | 295 | |
|
296 | 296 | #[test] |
|
297 | 297 | fn test_overridden() { |
|
298 | 298 | let data = vec![0u8, 0, 0]; |
|
299 | 299 | let mut patch1_data = PatchDataBuilder::new(); |
|
300 | 300 | patch1_data.replace(1, 2, &[3, 4]); |
|
301 | 301 | let mut patch1 = PatchList::new(patch1_data.get()); |
|
302 | 302 | |
|
303 | 303 | let mut patch2_data = PatchDataBuilder::new(); |
|
304 | 304 | patch2_data.replace(1, 4, &[1, 2, 3]); |
|
305 | 305 | let mut patch2 = PatchList::new(patch2_data.get()); |
|
306 | 306 | |
|
307 | 307 | let patch = patch1.combine(&mut patch2); |
|
308 | 308 | |
|
309 | 309 | let result = patch.apply(&data); |
|
310 | 310 | |
|
311 | 311 | assert_eq!(result, vec![0u8, 1, 2, 3]); |
|
312 | 312 | } |
|
313 | 313 | |
|
314 | 314 | #[test] |
|
315 | 315 | fn test_right_most_part_is_overridden() { |
|
316 | 316 | let data = vec![0u8, 0, 0]; |
|
317 | 317 | let mut patch1_data = PatchDataBuilder::new(); |
|
318 | 318 | patch1_data.replace(0, 1, &[1, 3]); |
|
319 | 319 | let mut patch1 = PatchList::new(patch1_data.get()); |
|
320 | 320 | |
|
321 | 321 | let mut patch2_data = PatchDataBuilder::new(); |
|
322 | 322 | patch2_data.replace(1, 4, &[2, 3, 4]); |
|
323 | 323 | let mut patch2 = PatchList::new(patch2_data.get()); |
|
324 | 324 | |
|
325 | 325 | let patch = patch1.combine(&mut patch2); |
|
326 | 326 | |
|
327 | 327 | let result = patch.apply(&data); |
|
328 | 328 | |
|
329 | 329 | assert_eq!(result, vec![1u8, 2, 3, 4]); |
|
330 | 330 | } |
|
331 | 331 | |
|
332 | 332 | #[test] |
|
333 | 333 | fn test_left_most_part_is_overridden() { |
|
334 | 334 | let data = vec![0u8, 0, 0]; |
|
335 | 335 | let mut patch1_data = PatchDataBuilder::new(); |
|
336 | 336 | patch1_data.replace(1, 3, &[1, 3, 4]); |
|
337 | 337 | let mut patch1 = PatchList::new(patch1_data.get()); |
|
338 | 338 | |
|
339 | 339 | let mut patch2_data = PatchDataBuilder::new(); |
|
340 | 340 | patch2_data.replace(0, 2, &[1, 2]); |
|
341 | 341 | let mut patch2 = PatchList::new(patch2_data.get()); |
|
342 | 342 | |
|
343 | 343 | let patch = patch1.combine(&mut patch2); |
|
344 | 344 | |
|
345 | 345 | let result = patch.apply(&data); |
|
346 | 346 | |
|
347 | 347 | assert_eq!(result, vec![1u8, 2, 3, 4]); |
|
348 | 348 | } |
|
349 | 349 | |
|
350 | 350 | #[test] |
|
351 | 351 | fn test_mid_is_overridden() { |
|
352 | 352 | let data = vec![0u8, 0, 0]; |
|
353 | 353 | let mut patch1_data = PatchDataBuilder::new(); |
|
354 | 354 | patch1_data.replace(0, 3, &[1, 3, 3, 4]); |
|
355 | 355 | let mut patch1 = PatchList::new(patch1_data.get()); |
|
356 | 356 | |
|
357 | 357 | let mut patch2_data = PatchDataBuilder::new(); |
|
358 | 358 | patch2_data.replace(1, 3, &[2, 3]); |
|
359 | 359 | let mut patch2 = PatchList::new(patch2_data.get()); |
|
360 | 360 | |
|
361 | 361 | let patch = patch1.combine(&mut patch2); |
|
362 | 362 | |
|
363 | 363 | let result = patch.apply(&data); |
|
364 | 364 | |
|
365 | 365 | assert_eq!(result, vec![1u8, 2, 3, 4]); |
|
366 | 366 | } |
|
367 | 367 | } |
General Comments 0
You need to be logged in to leave comments.
Login now