2  *  LibXDiff by Davide Libenzi ( File Differential Library )
 
   3  *  Copyright (C) 2003  Davide Libenzi
 
   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.
 
  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.
 
  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
 
  19  *  Davide Libenzi <davidel@xmailserver.org>
 
  26 #define XDL_KPDIS_RUN 4
 
  27 #define XDL_MAX_EQLIMIT 1024
 
  28 #define XDL_SIMSCAN_WINDOW 100
 
  29 #define XDL_GUESS_NLINES1 256
 
  30 #define XDL_GUESS_NLINES2 20
 
  33 typedef struct s_xdlclass {
 
  34         struct s_xdlclass *next;
 
  42 typedef struct s_xdlclassifier {
 
  56 static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags);
 
  57 static void xdl_free_classifier(xdlclassifier_t *cf);
 
  58 static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash,
 
  59                                unsigned int hbits, xrecord_t *rec);
 
  60 static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
 
  61                            xdlclassifier_t *cf, xdfile_t *xdf);
 
  62 static void xdl_free_ctx(xdfile_t *xdf);
 
  63 static int xdl_clean_mmatch(char const *dis, long i, long s, long e);
 
  64 static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2);
 
  65 static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2);
 
  66 static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2);
 
  71 static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
 
  74         cf->hbits = xdl_hashbits((unsigned int) size);
 
  75         cf->hsize = 1 << cf->hbits;
 
  77         if (xdl_cha_init(&cf->ncha, sizeof(xdlclass_t), size / 4 + 1) < 0) {
 
  81         if (!(cf->rchash = (xdlclass_t **) xdl_malloc(cf->hsize * sizeof(xdlclass_t *)))) {
 
  83                 xdl_cha_free(&cf->ncha);
 
  86         memset(cf->rchash, 0, cf->hsize * sizeof(xdlclass_t *));
 
  89         if (!(cf->rcrecs = (xdlclass_t **) xdl_malloc(cf->alloc * sizeof(xdlclass_t *)))) {
 
  92                 xdl_cha_free(&cf->ncha);
 
 102 static void xdl_free_classifier(xdlclassifier_t *cf) {
 
 104         xdl_free(cf->rcrecs);
 
 105         xdl_free(cf->rchash);
 
 106         xdl_cha_free(&cf->ncha);
 
 110 static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash,
 
 111                                unsigned int hbits, xrecord_t *rec) {
 
 118         hi = (long) XDL_HASHLONG(rec->ha, cf->hbits);
 
 119         for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
 
 120                 if (rcrec->ha == rec->ha &&
 
 121                                 xdl_recmatch(rcrec->line, rcrec->size,
 
 122                                         rec->ptr, rec->size, cf->flags))
 
 126                 if (!(rcrec = xdl_cha_alloc(&cf->ncha))) {
 
 130                 rcrec->idx = cf->count++;
 
 131                 if (cf->count > cf->alloc) {
 
 133                         if (!(rcrecs = (xdlclass_t **) xdl_realloc(cf->rcrecs, cf->alloc * sizeof(xdlclass_t *)))) {
 
 139                 cf->rcrecs[rcrec->idx] = rcrec;
 
 141                 rcrec->size = rec->size;
 
 143                 rcrec->len1 = rcrec->len2 = 0;
 
 144                 rcrec->next = cf->rchash[hi];
 
 145                 cf->rchash[hi] = rcrec;
 
 148         (pass == 1) ? rcrec->len1++ : rcrec->len2++;
 
 150         rec->ha = (unsigned long) rcrec->idx;
 
 152         hi = (long) XDL_HASHLONG(rec->ha, hbits);
 
 153         rec->next = rhash[hi];
 
 160 static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
 
 161                            xdlclassifier_t *cf, xdfile_t *xdf) {
 
 163         long nrec, hsize, bsize;
 
 165         char const *blk, *cur, *top, *prev;
 
 167         xrecord_t **recs, **rrecs;
 
 179         if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0)
 
 181         if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *))))
 
 184         if (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF)
 
 187                 hbits = xdl_hashbits((unsigned int) narec);
 
 189                 if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *))))
 
 191                 memset(rhash, 0, hsize * sizeof(xrecord_t *));
 
 195         if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) {
 
 196                 for (top = blk + bsize; cur < top; ) {
 
 198                         hav = xdl_hash_record(&cur, top, xpp->flags);
 
 201                                 if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *))))
 
 205                         if (!(crec = xdl_cha_alloc(&xdf->rcha)))
 
 208                         crec->size = (long) (cur - prev);
 
 212                         if ((XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF) &&
 
 213                             xdl_classify_record(pass, cf, rhash, hbits, crec) < 0)
 
 218         if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char))))
 
 220         memset(rchg, 0, (nrec + 2) * sizeof(char));
 
 222         if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long))))
 
 224         if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long))))
 
 231         xdf->rchg = rchg + 1;
 
 232         xdf->rindex = rindex;
 
 236         xdf->dend = nrec - 1;
 
 246         xdl_cha_free(&xdf->rcha);
 
 251 static void xdl_free_ctx(xdfile_t *xdf) {
 
 253         xdl_free(xdf->rhash);
 
 254         xdl_free(xdf->rindex);
 
 255         xdl_free(xdf->rchg - 1);
 
 258         xdl_cha_free(&xdf->rcha);
 
 262 int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
 
 264         long enl1, enl2, sample;
 
 267         memset(&cf, 0, sizeof(cf));
 
 270          * For histogram diff, we can afford a smaller sample size and
 
 271          * thus a poorer estimate of the number of lines, as the hash
 
 272          * table (rhash) won't be filled up/grown. The number of lines
 
 273          * (nrecs) will be updated correctly anyway by
 
 276         sample = (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF
 
 277                   ? XDL_GUESS_NLINES2 : XDL_GUESS_NLINES1);
 
 279         enl1 = xdl_guess_lines(mf1, sample) + 1;
 
 280         enl2 = xdl_guess_lines(mf2, sample) + 1;
 
 282         if (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF &&
 
 283             xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0)
 
 286         if (xdl_prepare_ctx(1, mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
 
 288                 xdl_free_classifier(&cf);
 
 291         if (xdl_prepare_ctx(2, mf2, enl2, xpp, &cf, &xe->xdf2) < 0) {
 
 293                 xdl_free_ctx(&xe->xdf1);
 
 294                 xdl_free_classifier(&cf);
 
 298         if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
 
 299             (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF) &&
 
 300             xdl_optimize_ctxs(&cf, &xe->xdf1, &xe->xdf2) < 0) {
 
 302                 xdl_free_ctx(&xe->xdf2);
 
 303                 xdl_free_ctx(&xe->xdf1);
 
 307         if (!(xpp->flags & XDF_HISTOGRAM_DIFF))
 
 308                 xdl_free_classifier(&cf);
 
 314 void xdl_free_env(xdfenv_t *xe) {
 
 316         xdl_free_ctx(&xe->xdf2);
 
 317         xdl_free_ctx(&xe->xdf1);
 
 321 static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
 
 322         long r, rdis0, rpdis0, rdis1, rpdis1;
 
 325          * Limits the window the is examined during the similar-lines
 
 326          * scan. The loops below stops when dis[i - r] == 1 (line that
 
 327          * has no match), but there are corner cases where the loop
 
 328          * proceed all the way to the extremities by causing huge
 
 329          * performance penalties in case of big files.
 
 331         if (i - s > XDL_SIMSCAN_WINDOW)
 
 332                 s = i - XDL_SIMSCAN_WINDOW;
 
 333         if (e - i > XDL_SIMSCAN_WINDOW)
 
 334                 e = i + XDL_SIMSCAN_WINDOW;
 
 337          * Scans the lines before 'i' to find a run of lines that either
 
 338          * have no match (dis[j] == 0) or have multiple matches (dis[j] > 1).
 
 339          * Note that we always call this function with dis[i] > 1, so the
 
 340          * current line (i) is already a multimatch line.
 
 342         for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) {
 
 345                 else if (dis[i - r] == 2)
 
 351          * If the run before the line 'i' found only multimatch lines, we
 
 352          * return 0 and hence we don't make the current line (i) discarded.
 
 353          * We want to discard multimatch lines only when they appear in the
 
 354          * middle of runs with nomatch lines (dis[j] == 0).
 
 358         for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) {
 
 361                 else if (dis[i + r] == 2)
 
 367          * If the run after the line 'i' found only multimatch lines, we
 
 368          * return 0 and hence we don't make the current line (i) discarded.
 
 375         return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1);
 
 380  * Try to reduce the problem complexity, discard records that have no
 
 381  * matches on the other file. Also, lines that have multiple matches
 
 382  * might be potentially discarded if they happear in a run of discardable.
 
 384 static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
 
 385         long i, nm, nreff, mlim;
 
 388         char *dis, *dis1, *dis2;
 
 390         if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) {
 
 394         memset(dis, 0, xdf1->nrec + xdf2->nrec + 2);
 
 396         dis2 = dis1 + xdf1->nrec + 1;
 
 398         if ((mlim = xdl_bogosqrt(xdf1->nrec)) > XDL_MAX_EQLIMIT)
 
 399                 mlim = XDL_MAX_EQLIMIT;
 
 400         for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) {
 
 401                 rcrec = cf->rcrecs[(*recs)->ha];
 
 402                 nm = rcrec ? rcrec->len2 : 0;
 
 403                 dis1[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
 
 406         if ((mlim = xdl_bogosqrt(xdf2->nrec)) > XDL_MAX_EQLIMIT)
 
 407                 mlim = XDL_MAX_EQLIMIT;
 
 408         for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) {
 
 409                 rcrec = cf->rcrecs[(*recs)->ha];
 
 410                 nm = rcrec ? rcrec->len1 : 0;
 
 411                 dis2[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
 
 414         for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart];
 
 415              i <= xdf1->dend; i++, recs++) {
 
 417                     (dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) {
 
 418                         xdf1->rindex[nreff] = i;
 
 419                         xdf1->ha[nreff] = (*recs)->ha;
 
 426         for (nreff = 0, i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart];
 
 427              i <= xdf2->dend; i++, recs++) {
 
 429                     (dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) {
 
 430                         xdf2->rindex[nreff] = i;
 
 431                         xdf2->ha[nreff] = (*recs)->ha;
 
 445  * Early trim initial and terminal matching records.
 
 447 static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
 
 449         xrecord_t **recs1, **recs2;
 
 453         for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim;
 
 454              i++, recs1++, recs2++)
 
 455                 if ((*recs1)->ha != (*recs2)->ha)
 
 458         xdf1->dstart = xdf2->dstart = i;
 
 460         recs1 = xdf1->recs + xdf1->nrec - 1;
 
 461         recs2 = xdf2->recs + xdf2->nrec - 1;
 
 462         for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--)
 
 463                 if ((*recs1)->ha != (*recs2)->ha)
 
 466         xdf1->dend = xdf1->nrec - i - 1;
 
 467         xdf2->dend = xdf2->nrec - i - 1;
 
 473 static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
 
 475         if (xdl_trim_ends(xdf1, xdf2) < 0 ||
 
 476             xdl_cleanup_records(cf, xdf1, xdf2) < 0) {