]>
Commit | Line | Data |
---|---|---|
3443546f LT |
1 | /* |
2 | * LibXDiff by Davide Libenzi ( File Differential Library ) | |
3 | * Copyright (C) 2003 Davide Libenzi | |
4 | * | |
5 | * This library is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU Lesser General Public | |
7 | * License as published by the Free Software Foundation; either | |
8 | * version 2.1 of the License, or (at your option) any later version. | |
9 | * | |
10 | * This library is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this library; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | * | |
19 | * Davide Libenzi <davidel@xmailserver.org> | |
20 | * | |
21 | */ | |
22 | ||
23 | #include "xinclude.h" | |
24 | ||
25 | ||
3443546f | 26 | #define XDL_KPDIS_RUN 4 |
ca557aff | 27 | #define XDL_MAX_EQLIMIT 1024 |
9b28d554 | 28 | #define XDL_SIMSCAN_WINDOW 100 |
3443546f LT |
29 | |
30 | ||
31 | typedef struct s_xdlclass { | |
32 | struct s_xdlclass *next; | |
33 | unsigned long ha; | |
34 | char const *line; | |
35 | long size; | |
36 | long idx; | |
37 | } xdlclass_t; | |
38 | ||
39 | typedef struct s_xdlclassifier { | |
40 | unsigned int hbits; | |
41 | long hsize; | |
42 | xdlclass_t **rchash; | |
43 | chastore_t ncha; | |
44 | long count; | |
0d21efa5 | 45 | long flags; |
3443546f LT |
46 | } xdlclassifier_t; |
47 | ||
48 | ||
49 | ||
50 | ||
0d21efa5 | 51 | static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags); |
3443546f LT |
52 | static void xdl_free_classifier(xdlclassifier_t *cf); |
53 | static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits, | |
54 | xrecord_t *rec); | |
55 | static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp, | |
56 | xdlclassifier_t *cf, xdfile_t *xdf); | |
57 | static void xdl_free_ctx(xdfile_t *xdf); | |
58 | static int xdl_clean_mmatch(char const *dis, long i, long s, long e); | |
59 | static int xdl_cleanup_records(xdfile_t *xdf1, xdfile_t *xdf2); | |
60 | static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2); | |
61 | static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2); | |
62 | ||
63 | ||
64 | ||
65 | ||
0d21efa5 | 66 | static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) { |
0d21efa5 JS |
67 | cf->flags = flags; |
68 | ||
3443546f LT |
69 | cf->hbits = xdl_hashbits((unsigned int) size); |
70 | cf->hsize = 1 << cf->hbits; | |
71 | ||
72 | if (xdl_cha_init(&cf->ncha, sizeof(xdlclass_t), size / 4 + 1) < 0) { | |
73 | ||
74 | return -1; | |
75 | } | |
76 | if (!(cf->rchash = (xdlclass_t **) xdl_malloc(cf->hsize * sizeof(xdlclass_t *)))) { | |
77 | ||
78 | xdl_cha_free(&cf->ncha); | |
79 | return -1; | |
80 | } | |
452f4fa5 | 81 | memset(cf->rchash, 0, cf->hsize * sizeof(xdlclass_t *)); |
3443546f LT |
82 | |
83 | cf->count = 0; | |
84 | ||
85 | return 0; | |
86 | } | |
87 | ||
88 | ||
89 | static void xdl_free_classifier(xdlclassifier_t *cf) { | |
90 | ||
91 | xdl_free(cf->rchash); | |
92 | xdl_cha_free(&cf->ncha); | |
93 | } | |
94 | ||
95 | ||
96 | static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits, | |
97 | xrecord_t *rec) { | |
98 | long hi; | |
99 | char const *line; | |
100 | xdlclass_t *rcrec; | |
101 | ||
102 | line = rec->ptr; | |
103 | hi = (long) XDL_HASHLONG(rec->ha, cf->hbits); | |
104 | for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next) | |
0d21efa5 JS |
105 | if (rcrec->ha == rec->ha && |
106 | xdl_recmatch(rcrec->line, rcrec->size, | |
107 | rec->ptr, rec->size, cf->flags)) | |
3443546f LT |
108 | break; |
109 | ||
110 | if (!rcrec) { | |
111 | if (!(rcrec = xdl_cha_alloc(&cf->ncha))) { | |
112 | ||
113 | return -1; | |
114 | } | |
115 | rcrec->idx = cf->count++; | |
116 | rcrec->line = line; | |
117 | rcrec->size = rec->size; | |
118 | rcrec->ha = rec->ha; | |
119 | rcrec->next = cf->rchash[hi]; | |
120 | cf->rchash[hi] = rcrec; | |
121 | } | |
122 | ||
123 | rec->ha = (unsigned long) rcrec->idx; | |
124 | ||
125 | hi = (long) XDL_HASHLONG(rec->ha, hbits); | |
126 | rec->next = rhash[hi]; | |
127 | rhash[hi] = rec; | |
128 | ||
129 | return 0; | |
130 | } | |
131 | ||
132 | ||
133 | static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp, | |
134 | xdlclassifier_t *cf, xdfile_t *xdf) { | |
135 | unsigned int hbits; | |
452f4fa5 | 136 | long nrec, hsize, bsize; |
3443546f LT |
137 | unsigned long hav; |
138 | char const *blk, *cur, *top, *prev; | |
139 | xrecord_t *crec; | |
140 | xrecord_t **recs, **rrecs; | |
141 | xrecord_t **rhash; | |
142 | unsigned long *ha; | |
143 | char *rchg; | |
144 | long *rindex; | |
145 | ||
159607a8 TRC |
146 | ha = NULL; |
147 | rindex = NULL; | |
148 | rchg = NULL; | |
149 | rhash = NULL; | |
150 | recs = NULL; | |
3443546f | 151 | |
159607a8 TRC |
152 | if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) |
153 | goto abort; | |
154 | if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) | |
155 | goto abort; | |
3443546f | 156 | |
9f37c275 TRC |
157 | if (xpp->flags & XDF_HISTOGRAM_DIFF) |
158 | hbits = hsize = 0; | |
159 | else { | |
160 | hbits = xdl_hashbits((unsigned int) narec); | |
161 | hsize = 1 << hbits; | |
162 | if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) | |
163 | goto abort; | |
164 | memset(rhash, 0, hsize * sizeof(xrecord_t *)); | |
165 | } | |
3443546f LT |
166 | |
167 | nrec = 0; | |
168 | if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) { | |
169 | for (top = blk + bsize;;) { | |
170 | if (cur >= top) { | |
171 | if (!(cur = blk = xdl_mmfile_next(mf, &bsize))) | |
172 | break; | |
173 | top = blk + bsize; | |
174 | } | |
175 | prev = cur; | |
0d21efa5 | 176 | hav = xdl_hash_record(&cur, top, xpp->flags); |
3443546f LT |
177 | if (nrec >= narec) { |
178 | narec *= 2; | |
159607a8 TRC |
179 | if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) |
180 | goto abort; | |
3443546f LT |
181 | recs = rrecs; |
182 | } | |
159607a8 TRC |
183 | if (!(crec = xdl_cha_alloc(&xdf->rcha))) |
184 | goto abort; | |
3443546f LT |
185 | crec->ptr = prev; |
186 | crec->size = (long) (cur - prev); | |
187 | crec->ha = hav; | |
188 | recs[nrec++] = crec; | |
189 | ||
9f37c275 TRC |
190 | if (!(xpp->flags & XDF_HISTOGRAM_DIFF) && |
191 | xdl_classify_record(cf, rhash, hbits, crec) < 0) | |
159607a8 | 192 | goto abort; |
3443546f LT |
193 | } |
194 | } | |
195 | ||
159607a8 TRC |
196 | if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char)))) |
197 | goto abort; | |
3443546f LT |
198 | memset(rchg, 0, (nrec + 2) * sizeof(char)); |
199 | ||
159607a8 TRC |
200 | if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) |
201 | goto abort; | |
202 | if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) | |
203 | goto abort; | |
3443546f LT |
204 | |
205 | xdf->nrec = nrec; | |
206 | xdf->recs = recs; | |
207 | xdf->hbits = hbits; | |
208 | xdf->rhash = rhash; | |
209 | xdf->rchg = rchg + 1; | |
210 | xdf->rindex = rindex; | |
211 | xdf->nreff = 0; | |
212 | xdf->ha = ha; | |
213 | xdf->dstart = 0; | |
214 | xdf->dend = nrec - 1; | |
215 | ||
216 | return 0; | |
159607a8 TRC |
217 | |
218 | abort: | |
219 | xdl_free(ha); | |
220 | xdl_free(rindex); | |
221 | xdl_free(rchg); | |
222 | xdl_free(rhash); | |
223 | xdl_free(recs); | |
224 | xdl_cha_free(&xdf->rcha); | |
225 | return -1; | |
3443546f LT |
226 | } |
227 | ||
228 | ||
229 | static void xdl_free_ctx(xdfile_t *xdf) { | |
230 | ||
231 | xdl_free(xdf->rhash); | |
232 | xdl_free(xdf->rindex); | |
233 | xdl_free(xdf->rchg - 1); | |
234 | xdl_free(xdf->ha); | |
235 | xdl_free(xdf->recs); | |
236 | xdl_cha_free(&xdf->rcha); | |
237 | } | |
238 | ||
239 | ||
240 | int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, | |
241 | xdfenv_t *xe) { | |
242 | long enl1, enl2; | |
243 | xdlclassifier_t cf; | |
244 | ||
245 | enl1 = xdl_guess_lines(mf1) + 1; | |
246 | enl2 = xdl_guess_lines(mf2) + 1; | |
247 | ||
9f37c275 TRC |
248 | if (!(xpp->flags & XDF_HISTOGRAM_DIFF) && |
249 | xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0) { | |
3443546f LT |
250 | |
251 | return -1; | |
252 | } | |
253 | ||
254 | if (xdl_prepare_ctx(mf1, enl1, xpp, &cf, &xe->xdf1) < 0) { | |
255 | ||
256 | xdl_free_classifier(&cf); | |
257 | return -1; | |
258 | } | |
259 | if (xdl_prepare_ctx(mf2, enl2, xpp, &cf, &xe->xdf2) < 0) { | |
260 | ||
261 | xdl_free_ctx(&xe->xdf1); | |
262 | xdl_free_classifier(&cf); | |
263 | return -1; | |
264 | } | |
265 | ||
9f37c275 TRC |
266 | if (!(xpp->flags & XDF_HISTOGRAM_DIFF)) |
267 | xdl_free_classifier(&cf); | |
3443546f | 268 | |
92b7de93 | 269 | if (!(xpp->flags & XDF_PATIENCE_DIFF) && |
9f37c275 | 270 | !(xpp->flags & XDF_HISTOGRAM_DIFF) && |
92b7de93 | 271 | xdl_optimize_ctxs(&xe->xdf1, &xe->xdf2) < 0) { |
3443546f LT |
272 | |
273 | xdl_free_ctx(&xe->xdf2); | |
274 | xdl_free_ctx(&xe->xdf1); | |
275 | return -1; | |
276 | } | |
277 | ||
278 | return 0; | |
279 | } | |
280 | ||
281 | ||
282 | void xdl_free_env(xdfenv_t *xe) { | |
283 | ||
284 | xdl_free_ctx(&xe->xdf2); | |
285 | xdl_free_ctx(&xe->xdf1); | |
286 | } | |
287 | ||
288 | ||
289 | static int xdl_clean_mmatch(char const *dis, long i, long s, long e) { | |
ca557aff DL |
290 | long r, rdis0, rpdis0, rdis1, rpdis1; |
291 | ||
9b28d554 DL |
292 | /* |
293 | * Limits the window the is examined during the similar-lines | |
294 | * scan. The loops below stops when dis[i - r] == 1 (line that | |
295 | * has no match), but there are corner cases where the loop | |
296 | * proceed all the way to the extremities by causing huge | |
297 | * performance penalties in case of big files. | |
298 | */ | |
299 | if (i - s > XDL_SIMSCAN_WINDOW) | |
300 | s = i - XDL_SIMSCAN_WINDOW; | |
301 | if (e - i > XDL_SIMSCAN_WINDOW) | |
302 | e = i + XDL_SIMSCAN_WINDOW; | |
303 | ||
ca557aff DL |
304 | /* |
305 | * Scans the lines before 'i' to find a run of lines that either | |
306 | * have no match (dis[j] == 0) or have multiple matches (dis[j] > 1). | |
307 | * Note that we always call this function with dis[i] > 1, so the | |
308 | * current line (i) is already a multimatch line. | |
309 | */ | |
310 | for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) { | |
3443546f | 311 | if (!dis[i - r]) |
ca557aff | 312 | rdis0++; |
3443546f | 313 | else if (dis[i - r] == 2) |
ca557aff | 314 | rpdis0++; |
3443546f LT |
315 | else |
316 | break; | |
317 | } | |
ca557aff DL |
318 | /* |
319 | * If the run before the line 'i' found only multimatch lines, we | |
320 | * return 0 and hence we don't make the current line (i) discarded. | |
321 | * We want to discard multimatch lines only when they appear in the | |
322 | * middle of runs with nomatch lines (dis[j] == 0). | |
323 | */ | |
324 | if (rdis0 == 0) | |
325 | return 0; | |
326 | for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) { | |
3443546f | 327 | if (!dis[i + r]) |
ca557aff | 328 | rdis1++; |
3443546f | 329 | else if (dis[i + r] == 2) |
ca557aff | 330 | rpdis1++; |
3443546f LT |
331 | else |
332 | break; | |
333 | } | |
ca557aff DL |
334 | /* |
335 | * If the run after the line 'i' found only multimatch lines, we | |
336 | * return 0 and hence we don't make the current line (i) discarded. | |
337 | */ | |
338 | if (rdis1 == 0) | |
339 | return 0; | |
340 | rdis1 += rdis0; | |
341 | rpdis1 += rpdis0; | |
342 | ||
343 | return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1); | |
3443546f LT |
344 | } |
345 | ||
346 | ||
347 | /* | |
348 | * Try to reduce the problem complexity, discard records that have no | |
349 | * matches on the other file. Also, lines that have multiple matches | |
350 | * might be potentially discarded if they happear in a run of discardable. | |
351 | */ | |
352 | static int xdl_cleanup_records(xdfile_t *xdf1, xdfile_t *xdf2) { | |
ca557aff | 353 | long i, nm, rhi, nreff, mlim; |
3443546f LT |
354 | unsigned long hav; |
355 | xrecord_t **recs; | |
356 | xrecord_t *rec; | |
357 | char *dis, *dis1, *dis2; | |
358 | ||
ca557aff | 359 | if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) { |
3443546f LT |
360 | |
361 | return -1; | |
362 | } | |
ca557aff | 363 | memset(dis, 0, xdf1->nrec + xdf2->nrec + 2); |
3443546f LT |
364 | dis1 = dis; |
365 | dis2 = dis1 + xdf1->nrec + 1; | |
366 | ||
ca557aff DL |
367 | if ((mlim = xdl_bogosqrt(xdf1->nrec)) > XDL_MAX_EQLIMIT) |
368 | mlim = XDL_MAX_EQLIMIT; | |
3443546f LT |
369 | for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) { |
370 | hav = (*recs)->ha; | |
371 | rhi = (long) XDL_HASHLONG(hav, xdf2->hbits); | |
ca557aff DL |
372 | for (nm = 0, rec = xdf2->rhash[rhi]; rec; rec = rec->next) |
373 | if (rec->ha == hav && ++nm == mlim) | |
3443546f | 374 | break; |
ca557aff | 375 | dis1[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1; |
3443546f LT |
376 | } |
377 | ||
ca557aff DL |
378 | if ((mlim = xdl_bogosqrt(xdf2->nrec)) > XDL_MAX_EQLIMIT) |
379 | mlim = XDL_MAX_EQLIMIT; | |
3443546f LT |
380 | for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) { |
381 | hav = (*recs)->ha; | |
382 | rhi = (long) XDL_HASHLONG(hav, xdf1->hbits); | |
ca557aff DL |
383 | for (nm = 0, rec = xdf1->rhash[rhi]; rec; rec = rec->next) |
384 | if (rec->ha == hav && ++nm == mlim) | |
3443546f | 385 | break; |
ca557aff | 386 | dis2[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1; |
3443546f LT |
387 | } |
388 | ||
389 | for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; | |
390 | i <= xdf1->dend; i++, recs++) { | |
391 | if (dis1[i] == 1 || | |
392 | (dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) { | |
393 | xdf1->rindex[nreff] = i; | |
394 | xdf1->ha[nreff] = (*recs)->ha; | |
395 | nreff++; | |
396 | } else | |
397 | xdf1->rchg[i] = 1; | |
398 | } | |
399 | xdf1->nreff = nreff; | |
400 | ||
401 | for (nreff = 0, i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; | |
402 | i <= xdf2->dend; i++, recs++) { | |
403 | if (dis2[i] == 1 || | |
404 | (dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) { | |
405 | xdf2->rindex[nreff] = i; | |
406 | xdf2->ha[nreff] = (*recs)->ha; | |
407 | nreff++; | |
408 | } else | |
409 | xdf2->rchg[i] = 1; | |
410 | } | |
411 | xdf2->nreff = nreff; | |
412 | ||
413 | xdl_free(dis); | |
414 | ||
415 | return 0; | |
416 | } | |
417 | ||
418 | ||
419 | /* | |
420 | * Early trim initial and terminal matching records. | |
421 | */ | |
422 | static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) { | |
423 | long i, lim; | |
424 | xrecord_t **recs1, **recs2; | |
425 | ||
426 | recs1 = xdf1->recs; | |
427 | recs2 = xdf2->recs; | |
428 | for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim; | |
429 | i++, recs1++, recs2++) | |
430 | if ((*recs1)->ha != (*recs2)->ha) | |
431 | break; | |
432 | ||
433 | xdf1->dstart = xdf2->dstart = i; | |
434 | ||
435 | recs1 = xdf1->recs + xdf1->nrec - 1; | |
436 | recs2 = xdf2->recs + xdf2->nrec - 1; | |
437 | for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--) | |
438 | if ((*recs1)->ha != (*recs2)->ha) | |
439 | break; | |
440 | ||
441 | xdf1->dend = xdf1->nrec - i - 1; | |
442 | xdf2->dend = xdf2->nrec - i - 1; | |
443 | ||
444 | return 0; | |
445 | } | |
446 | ||
447 | ||
448 | static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2) { | |
449 | ||
450 | if (xdl_trim_ends(xdf1, xdf2) < 0 || | |
451 | xdl_cleanup_records(xdf1, xdf2) < 0) { | |
452 | ||
453 | return -1; | |
454 | } | |
455 | ||
456 | return 0; | |
457 | } |