Fossil

Check-in [b4800dc5]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Optimize REGEXP matching by folding all regular expressions into one
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | andygoth-timeline-ms
Files: files | file ages | folders
SHA1: b4800dc53d075bb81eafd392732af1e8c72ce040
User & Date: andygoth 2016-10-23 21:50:41
Context
2016-10-24
15:44
Push to 1.37 check-in: 4d1bee54 user: andygoth tags: andygoth-timeline-ms
2016-10-23
21:50
Optimize REGEXP matching by folding all regular expressions into one check-in: b4800dc5 user: andygoth tags: andygoth-timeline-ms
15:42
Improve comment check-in: c1afe56f user: andygoth tags: andygoth-timeline-ms
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/timeline.c.

1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265

1266
1267
1268



1269



1270
1271
1272



1273
1274
1275
1276
1277




1278
1279
1280
1281
1282
1283
1284
....
1306
1307
1308
1309
1310
1311
1312
1313
1314


1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
*/
static const char *tagMatchExpression(
  MatchStyle matchStyle,  /* Match style code */
  const char *zTag,       /* Tag name, match pattern, or list of patterns */
  int *pCount             /* Pointer to match pattern count variable */
){
  Blob blob = BLOB_INITIALIZER;
  const char *zSep = "(", *zPre, *zSuf;
  char cDel;
  int i, dummy;

  /* Protect against NULL count pointer. */
  if( !pCount ){
    pCount = &dummy;
  }

  /* Decide pattern prefix and suffix strings according to match style. */
  if( matchStyle==MS_EXACT ){
    /* Optimize exact matches by looking up the numeric ID in advance.  Bypass
     * the remainder of this function. */

    *pCount = 1;
    return mprintf("(tagid=%d)", db_int(-1,
        "SELECT tagid FROM tag WHERE tagname='sym-%q'", zTag));



  }else if( matchStyle==MS_LIKE ){



    zPre = "LIKE 'sym-";
    zSuf = "'";
  }else if( matchStyle==MS_GLOB ){



    zPre = "GLOB 'sym-";
    zSuf = "'";
  }else/* if( matchStyle==MS_REGEXP )*/{
    zPre = "REGEXP '^sym-";
    zSuf = "$'";




  }

  /* Convert the list of matches into an SQL expression. */
  *pCount = 0;
  blob_zero(&blob);
  while( 1 ){
    /* Skip leading delimiters. */
................................................................................

      /* In regexp mode, ignore delimiters following backslashes. */
      if( matchStyle==MS_REGEXP && zTag[i]=='\\' && zTag[i+1] ){
        ++i;
      }
    }

    /* Incorporate the match word into the final expression. */
    blob_appendf(&blob, "%stagname %s%#q%s", zSep, zPre, i, zTag, zSuf);



    /* Keep track of the number of match expressions. */
    ++*pCount;

    /* Prepare for the next match word. */
    zTag += i;
    if( cDel!=',' && *zTag==cDel ){
      ++zTag;
    }
    zSep = " OR ";
  }

  /* Finalize and extract the SQL expression. */
  if( *pCount ){
    blob_append(&blob, ")", 1);
    return blob_str(&blob);
  }

  /* If execution reaches this point, the pattern was empty.  Return NULL. */
  return 0;
}








|

|

<
<
<
<
<
<
<
|
|
>



>
>
>
|
>
>
>
|
|

>
>
>
|
|

|
<
>
>
>
>







 







|
|
>
>




|




<




|







1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256







1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279

1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
....
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331

1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
*/
static const char *tagMatchExpression(
  MatchStyle matchStyle,  /* Match style code */
  const char *zTag,       /* Tag name, match pattern, or list of patterns */
  int *pCount             /* Pointer to match pattern count variable */
){
  Blob blob = BLOB_INITIALIZER;
  const char *zStart, *zDelimiter, *zEnd, *zPrefix, *zSuffix;
  char cDel;
  int i;








  /* Optimize exact matches by looking up the ID in advance to create a simple
   * numeric comparison.  Bypass the remainder of this function. */
  if( matchStyle==MS_EXACT ){
    *pCount = 1;
    return mprintf("(tagid=%d)", db_int(-1,
        "SELECT tagid FROM tag WHERE tagname='sym-%q'", zTag));
  }
  
  /* Decide pattern prefix and suffix strings according to match style. */
  if( matchStyle==MS_LIKE ){
    zStart = "(";
    zDelimiter = " OR ";
    zEnd = ")";
    zPrefix = "tagname LIKE 'sym-";
    zSuffix = "'";
  }else if( matchStyle==MS_GLOB ){
    zStart = "(";
    zDelimiter = " OR ";
    zEnd = ")";
    zPrefix = "tagname GLOB 'sym-";
    zSuffix = "'";
  }else/* if( matchStyle==MS_REGEXP )*/{
    zStart = "(tagname REGEXP '^sym-(";

    zDelimiter = "|";
    zEnd = ")$')";
    zPrefix = "";
    zSuffix = "";
  }

  /* Convert the list of matches into an SQL expression. */
  *pCount = 0;
  blob_zero(&blob);
  while( 1 ){
    /* Skip leading delimiters. */
................................................................................

      /* In regexp mode, ignore delimiters following backslashes. */
      if( matchStyle==MS_REGEXP && zTag[i]=='\\' && zTag[i+1] ){
        ++i;
      }
    }

    /* Incorporate the match word into the output expression.  The %q format is
     * used to protect against SQL injection attacks by replacing ' with ''. */
    blob_appendf(&blob, "%s%s%#q%s", *pCount ? zDelimiter : zStart,
        zPrefix, i, zTag, zSuffix);

    /* Keep track of the number of match expressions. */
    ++*pCount;

    /* Advance past all consumed input characters. */
    zTag += i;
    if( cDel!=',' && *zTag==cDel ){
      ++zTag;
    }

  }

  /* Finalize and extract the SQL expression. */
  if( *pCount ){
    blob_append(&blob, zEnd, -1);
    return blob_str(&blob);
  }

  /* If execution reaches this point, the pattern was empty.  Return NULL. */
  return 0;
}