pgbackrest.git / summary / log / commit / refs

commit    df200bee2acf614edf9d008cccde67f02d89f103
Author:   David Steele <david@pgmasters.net>
Date:     Fri Nov 09 21:50:22 2018 +0000

    Add regExpPrefix() to aid in static prefix searches.
    
    The storageList() command accepts a regular expression as a filter.  This works fine for local filesystems where it is relatively cheap to get a complete list of files and filter them in code.  However, for remote filesystems like S3 it can be expensive to fetch a complete list of files only to discard the bulk of them locally.
    
    S3 does not filter on regular expressions but it can accept a static prefix so this function extracts a prefix from a regular expression when possible.
    
    Even a few characters can drastically reduce the amount of data that must be fetched remotely so the function does not try to be too clever.  It requires a ^ anchor and stops scanning when the first special character is found.


doc/xml/release.xml | 4 ++++ src/common/regExp.c | 41 +++++++++++++++++++++++++++++++++++++ src/common/regExp.h | 1 + test/define.yaml | 2 +- test/src/module/common/regExpTest.c | 24 ++++++++++++++++++++++ 5 files changed, 71 insertions(+), 1 deletion(-) diff --git a/doc/xml/release.xml b/doc/xml/release.xml index f8ea7e887..7aba77baf 100644 --- a/doc/xml/release.xml +++ b/doc/xml/release.xml @@ -78,6 +78,10 @@ <p>Add <code>strCatChr()</code> to <code>String</code> object.</p> </release-item> + <release-item> + <p>Add <code>regExpPrefix()</code> to aid in static prefix searches.</p> + </release-item> + <release-item> <release-item-contributor-list> <release-item-contributor id="cynthia.shang"/> diff --git a/src/common/regExp.c b/src/common/regExp.c index 849618e96..23b68209f 100644 --- a/src/common/regExp.c +++ b/src/common/regExp.c @@ -144,3 +144,44 @@ regExpMatchOne(const String *expression, const String *string) FUNCTION_TEST_RESULT(BOOL, result); } + +/*********************************************************************************************************************************** +Return the constant first part of the regular expression if it has a beginning anchor + +This works by scanning the string until the first special regex character is found so escaped characters will not be included. +***********************************************************************************************************************************/ +String * +regExpPrefix(const String *expression) +{ + FUNCTION_TEST_BEGIN(); + FUNCTION_TEST_PARAM(STRING, expression); + FUNCTION_TEST_END(); + + String *result = NULL; + + // Only generate prefix if expression is defined and has a beginning anchor + if (expression != NULL && strPtr(expression)[0] == '^') + { + unsigned int expressionIdx = 1; + + for (; expressionIdx < strSize(expression); expressionIdx++) + { + char expressionChr = strPtr(expression)[expressionIdx]; + + // Search for characters that will end the prefix + if (expressionChr == '.' || expressionChr == '^' || expressionChr == '$' || expressionChr == '*' || + expressionChr == '+' || expressionChr == '-' || expressionChr == '?' || expressionChr == '(' || + expressionChr == '[' || expressionChr == '{' || expressionChr == ' ' || expressionChr == '|' || + expressionChr == '\\') + { + break; + } + } + + // Will there be any characters in the prefix? + if (expressionIdx > 1) + result = strSubN(expression, 1, expressionIdx - 1); + } + + FUNCTION_TEST_RESULT(STRING, result); +} diff --git a/src/common/regExp.h b/src/common/regExp.h index a9b4b1bc6..debab18db 100644 --- a/src/common/regExp.h +++ b/src/common/regExp.h @@ -19,6 +19,7 @@ bool regExpMatch(RegExp *this, const String *string); void regExpFree(RegExp *this); bool regExpMatchOne(const String *expression, const String *string); +String *regExpPrefix(const String *expression); /*********************************************************************************************************************************** Macros for function logging diff --git a/test/define.yaml b/test/define.yaml index 8f32f0a33..718269348 100644 --- a/test/define.yaml +++ b/test/define.yaml @@ -239,7 +239,7 @@ unit: # ---------------------------------------------------------------------------------------------------------------------------- - name: reg-exp - total: 2 + total: 3 coverage: common/regExp: full diff --git a/test/src/module/common/regExpTest.c b/test/src/module/common/regExpTest.c index 13b518135..73f4eb8f6 100644 --- a/test/src/module/common/regExpTest.c +++ b/test/src/module/common/regExpTest.c @@ -24,6 +24,30 @@ testRun(void) TEST_RESULT_VOID(regExpFree(NULL), "free NULL regexp"); } + // ***************************************************************************************************************************** + if (testBegin("regExpPrefix()")) + { + TEST_RESULT_PTR(regExpPrefix(NULL), NULL, "null expression has no prefix"); + TEST_RESULT_PTR(regExpPrefix(strNew("")), NULL, "empty expression has no prefix"); + TEST_RESULT_PTR(regExpPrefix(strNew("ABC")), NULL, "expression without begin anchor has no prefix"); + TEST_RESULT_PTR(regExpPrefix(strNew("^.")), NULL, "expression with no regular character has no prefix"); + + TEST_RESULT_STR(strPtr(regExpPrefix(strNew("^ABC^"))), "ABC", "prefix stops at special character"); + TEST_RESULT_STR(strPtr(regExpPrefix(strNew("^ABC$"))), "ABC", "prefix stops at special character"); + TEST_RESULT_STR(strPtr(regExpPrefix(strNew("^ABC*"))), "ABC", "prefix stops at special character"); + TEST_RESULT_STR(strPtr(regExpPrefix(strNew("^ABC+"))), "ABC", "prefix stops at special character"); + TEST_RESULT_STR(strPtr(regExpPrefix(strNew("^ABC-"))), "ABC", "prefix stops at special character"); + TEST_RESULT_STR(strPtr(regExpPrefix(strNew("^ABC?"))), "ABC", "prefix stops at special character"); + TEST_RESULT_STR(strPtr(regExpPrefix(strNew("^ABC("))), "ABC", "prefix stops at special character"); + TEST_RESULT_STR(strPtr(regExpPrefix(strNew("^ABC["))), "ABC", "prefix stops at special character"); + TEST_RESULT_STR(strPtr(regExpPrefix(strNew("^ABC{"))), "ABC", "prefix stops at special character"); + TEST_RESULT_STR(strPtr(regExpPrefix(strNew("^ABC "))), "ABC", "prefix stops at special character"); + TEST_RESULT_STR(strPtr(regExpPrefix(strNew("^ABC|"))), "ABC", "prefix stops at special character"); + TEST_RESULT_STR(strPtr(regExpPrefix(strNew("^ABC\\"))), "ABC", "prefix stops at special character"); + + TEST_RESULT_STR(strPtr(regExpPrefix(strNew("^ABCDEF"))), "ABCDEF", "prefix is entire expression"); + } + // ***************************************************************************************************************************** if (testBegin("regExpMatchOne()")) { [parent: 8c504bd2f9e6]