2017-03-10 17:52:36 +00:00
|
|
|
#pragma once
|
2018-08-30 18:40:46 +00:00
|
|
|
|
2019-03-31 20:55:36 +00:00
|
|
|
#include <map>
|
2019-03-29 01:02:05 +00:00
|
|
|
#include <memory>
|
2019-03-31 20:55:36 +00:00
|
|
|
#include <mutex>
|
2019-03-29 01:02:05 +00:00
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2019-03-23 19:40:16 +00:00
|
|
|
#include <Functions/likePatternToRegexp.h>
|
2019-10-13 13:22:09 +00:00
|
|
|
#include <Common/Exception.h>
|
2018-08-30 18:40:46 +00:00
|
|
|
#include <Common/ObjectPool.h>
|
2019-03-23 19:40:16 +00:00
|
|
|
#include <Common/OptimizedRegularExpression.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/ProfileEvents.h>
|
2019-03-23 19:40:16 +00:00
|
|
|
#include <common/StringRef.h>
|
2017-03-11 00:27:59 +00:00
|
|
|
|
2020-04-16 12:31:57 +00:00
|
|
|
#if !defined(ARCADIA_BUILD)
|
|
|
|
# include "config_functions.h"
|
|
|
|
#endif
|
2019-03-31 20:55:36 +00:00
|
|
|
|
2019-03-25 21:31:00 +00:00
|
|
|
#if USE_HYPERSCAN
|
2020-04-16 12:31:57 +00:00
|
|
|
# include <hs.h>
|
2019-03-23 19:40:16 +00:00
|
|
|
#endif
|
2017-03-10 17:52:36 +00:00
|
|
|
|
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
2019-03-23 19:40:16 +00:00
|
|
|
extern const Event RegexpCreated;
|
2017-03-10 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-10 00:04:08 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2019-03-23 19:40:16 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_ALLOCATE_MEMORY;
|
|
|
|
extern const int LOGICAL_ERROR;
|
2019-12-30 14:46:02 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
2019-03-23 19:40:16 +00:00
|
|
|
}
|
2017-03-10 17:52:36 +00:00
|
|
|
|
|
|
|
namespace Regexps
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using Regexp = OptimizedRegularExpressionImpl<false>;
|
|
|
|
using Pool = ObjectPoolMap<Regexp, String>;
|
|
|
|
|
|
|
|
template <bool like>
|
2019-03-23 19:40:16 +00:00
|
|
|
inline Regexp createRegexp(const std::string & pattern, int flags)
|
|
|
|
{
|
|
|
|
return {pattern, flags};
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
template <>
|
2019-03-23 19:40:16 +00:00
|
|
|
inline Regexp createRegexp<true>(const std::string & pattern, int flags)
|
|
|
|
{
|
|
|
|
return {likePatternToRegexp(pattern), flags};
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
template <bool like, bool no_capture>
|
|
|
|
inline Pool::Pointer get(const std::string & pattern)
|
|
|
|
{
|
|
|
|
/// C++11 has thread-safe function-local statics on most modern compilers.
|
2019-03-23 19:40:16 +00:00
|
|
|
static Pool known_regexps; /// Different variables for different pattern parameters.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
return known_regexps.get(pattern, [&pattern]
|
|
|
|
{
|
|
|
|
int flags = OptimizedRegularExpression::RE_DOT_NL;
|
|
|
|
if (no_capture)
|
|
|
|
flags |= OptimizedRegularExpression::RE_NO_CAPTURE;
|
|
|
|
|
|
|
|
ProfileEvents::increment(ProfileEvents::RegexpCreated);
|
|
|
|
return new Regexp{createRegexp<like>(pattern, flags)};
|
|
|
|
});
|
|
|
|
}
|
2017-03-10 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 21:31:00 +00:00
|
|
|
#if USE_HYPERSCAN
|
2019-03-23 19:40:16 +00:00
|
|
|
|
|
|
|
namespace MultiRegexps
|
|
|
|
{
|
|
|
|
template <typename Deleter, Deleter deleter>
|
|
|
|
struct HyperscanDeleter
|
|
|
|
{
|
|
|
|
template <typename T>
|
|
|
|
void operator()(T * ptr) const
|
|
|
|
{
|
|
|
|
deleter(ptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-13 13:22:09 +00:00
|
|
|
/// Helper unique pointers to correctly delete the allocated space when hyperscan cannot compile something and we throw an exception.
|
2019-03-29 01:02:05 +00:00
|
|
|
using CompilerError = std::unique_ptr<hs_compile_error_t, HyperscanDeleter<decltype(&hs_free_compile_error), &hs_free_compile_error>>;
|
2019-03-31 20:19:22 +00:00
|
|
|
using ScratchPtr = std::unique_ptr<hs_scratch_t, HyperscanDeleter<decltype(&hs_free_scratch), &hs_free_scratch>>;
|
|
|
|
using DataBasePtr = std::unique_ptr<hs_database_t, HyperscanDeleter<decltype(&hs_free_database), &hs_free_database>>;
|
|
|
|
|
2019-10-13 13:22:09 +00:00
|
|
|
/// Database is thread safe across multiple threads and Scratch is not but we can copy it whenever we use it in the searcher.
|
2019-03-31 20:19:22 +00:00
|
|
|
class Regexps
|
|
|
|
{
|
|
|
|
public:
|
2019-10-13 13:22:09 +00:00
|
|
|
Regexps(hs_database_t * db_, hs_scratch_t * scratch_) : db{db_}, scratch{scratch_} { }
|
2019-03-31 20:19:22 +00:00
|
|
|
|
2019-04-01 05:05:52 +00:00
|
|
|
hs_database_t * getDB() const { return db.get(); }
|
|
|
|
hs_scratch_t * getScratch() const { return scratch.get(); }
|
2019-10-13 13:22:09 +00:00
|
|
|
|
2019-03-31 20:19:22 +00:00
|
|
|
private:
|
|
|
|
DataBasePtr db;
|
|
|
|
ScratchPtr scratch;
|
|
|
|
};
|
2019-03-23 19:40:16 +00:00
|
|
|
|
2019-03-31 20:55:36 +00:00
|
|
|
struct Pool
|
|
|
|
{
|
2019-10-13 13:22:09 +00:00
|
|
|
/// Mutex for finding in map.
|
2019-03-31 20:55:36 +00:00
|
|
|
std::mutex mutex;
|
2019-10-13 13:22:09 +00:00
|
|
|
/// Patterns + possible edit_distance to database and scratch.
|
2019-03-31 20:55:36 +00:00
|
|
|
std::map<std::pair<std::vector<String>, std::optional<UInt32>>, Regexps> storage;
|
|
|
|
};
|
2019-03-23 19:40:16 +00:00
|
|
|
|
2019-10-25 03:25:02 +00:00
|
|
|
template <bool save_indices, bool CompileForEditDistance>
|
2019-03-31 20:55:36 +00:00
|
|
|
inline Regexps constructRegexps(const std::vector<String> & str_patterns, std::optional<UInt32> edit_distance)
|
2019-03-23 19:40:16 +00:00
|
|
|
{
|
2019-03-31 20:55:36 +00:00
|
|
|
(void)edit_distance;
|
|
|
|
/// Common pointers
|
2019-10-13 13:22:09 +00:00
|
|
|
std::vector<const char *> patterns;
|
2019-03-31 20:55:36 +00:00
|
|
|
std::vector<unsigned int> flags;
|
2019-03-23 19:40:16 +00:00
|
|
|
|
2019-03-31 20:55:36 +00:00
|
|
|
/// Pointer for external edit distance compilation
|
|
|
|
std::vector<hs_expr_ext> ext_exprs;
|
|
|
|
std::vector<const hs_expr_ext *> ext_exprs_ptrs;
|
2019-03-29 01:02:05 +00:00
|
|
|
|
2019-10-13 13:22:09 +00:00
|
|
|
patterns.reserve(str_patterns.size());
|
2019-03-31 20:55:36 +00:00
|
|
|
flags.reserve(str_patterns.size());
|
2019-03-29 01:02:05 +00:00
|
|
|
|
2019-03-31 20:55:36 +00:00
|
|
|
if constexpr (CompileForEditDistance)
|
|
|
|
{
|
|
|
|
ext_exprs.reserve(str_patterns.size());
|
|
|
|
ext_exprs_ptrs.reserve(str_patterns.size());
|
|
|
|
}
|
2019-03-29 01:02:05 +00:00
|
|
|
|
2019-03-31 20:55:36 +00:00
|
|
|
for (const StringRef ref : str_patterns)
|
|
|
|
{
|
2019-10-13 13:22:09 +00:00
|
|
|
patterns.push_back(ref.data);
|
|
|
|
/* Flags below are the pattern matching flags.
|
|
|
|
* HS_FLAG_DOTALL is a compile flag where matching a . will not exclude newlines. This is a good
|
|
|
|
* performance practice accrording to Hyperscan API. https://intel.github.io/hyperscan/dev-reference/performance.html#dot-all-mode
|
|
|
|
* HS_FLAG_ALLOWEMPTY is a compile flag where empty strings are allowed to match.
|
|
|
|
* HS_FLAG_UTF8 is a flag where UTF8 literals are matched.
|
|
|
|
* HS_FLAG_SINGLEMATCH is a compile flag where each pattern match will be returned only once. it is a good performance practice
|
|
|
|
* as it is said in the Hyperscan documentation. https://intel.github.io/hyperscan/dev-reference/performance.html#single-match-flag
|
|
|
|
*/
|
|
|
|
flags.push_back(HS_FLAG_DOTALL | HS_FLAG_SINGLEMATCH | HS_FLAG_ALLOWEMPTY | HS_FLAG_UTF8);
|
2019-03-29 01:02:05 +00:00
|
|
|
if constexpr (CompileForEditDistance)
|
|
|
|
{
|
2019-10-13 13:22:09 +00:00
|
|
|
/// Hyperscan currently does not support UTF8 matching with edit distance.
|
2019-05-05 06:51:36 +00:00
|
|
|
flags.back() &= ~HS_FLAG_UTF8;
|
2019-03-31 20:55:36 +00:00
|
|
|
ext_exprs.emplace_back();
|
2019-10-13 13:22:09 +00:00
|
|
|
/// HS_EXT_FLAG_EDIT_DISTANCE is a compile flag responsible for Levenstein distance.
|
2019-03-31 20:55:36 +00:00
|
|
|
ext_exprs.back().flags = HS_EXT_FLAG_EDIT_DISTANCE;
|
|
|
|
ext_exprs.back().edit_distance = edit_distance.value();
|
|
|
|
ext_exprs_ptrs.push_back(&ext_exprs.back());
|
2019-03-29 01:02:05 +00:00
|
|
|
}
|
2019-03-31 20:55:36 +00:00
|
|
|
}
|
|
|
|
hs_database_t * db = nullptr;
|
|
|
|
hs_compile_error_t * compile_error;
|
2019-03-29 01:02:05 +00:00
|
|
|
|
2019-03-23 19:40:16 +00:00
|
|
|
|
2019-03-31 20:55:36 +00:00
|
|
|
std::unique_ptr<unsigned int[]> ids;
|
2019-03-23 22:49:38 +00:00
|
|
|
|
2019-10-13 13:22:09 +00:00
|
|
|
/// We mark the patterns to provide the callback results.
|
2019-10-25 03:25:02 +00:00
|
|
|
if constexpr (save_indices)
|
2019-03-31 20:55:36 +00:00
|
|
|
{
|
2019-10-13 13:22:09 +00:00
|
|
|
ids.reset(new unsigned int[patterns.size()]);
|
|
|
|
for (size_t i = 0; i < patterns.size(); ++i)
|
2019-03-31 20:55:36 +00:00
|
|
|
ids[i] = i + 1;
|
|
|
|
}
|
2019-03-23 22:49:38 +00:00
|
|
|
|
2019-03-31 20:55:36 +00:00
|
|
|
hs_error_t err;
|
|
|
|
if constexpr (!CompileForEditDistance)
|
|
|
|
err = hs_compile_multi(
|
2019-10-13 13:22:09 +00:00
|
|
|
patterns.data(),
|
2019-03-31 20:55:36 +00:00
|
|
|
flags.data(),
|
|
|
|
ids.get(),
|
2019-10-13 13:22:09 +00:00
|
|
|
patterns.size(),
|
2019-03-31 20:55:36 +00:00
|
|
|
HS_MODE_BLOCK,
|
|
|
|
nullptr,
|
|
|
|
&db,
|
|
|
|
&compile_error);
|
|
|
|
else
|
|
|
|
err = hs_compile_ext_multi(
|
2019-10-13 13:22:09 +00:00
|
|
|
patterns.data(),
|
2019-03-31 20:55:36 +00:00
|
|
|
flags.data(),
|
|
|
|
ids.get(),
|
|
|
|
ext_exprs_ptrs.data(),
|
2019-10-13 13:22:09 +00:00
|
|
|
patterns.size(),
|
2019-03-31 20:55:36 +00:00
|
|
|
HS_MODE_BLOCK,
|
|
|
|
nullptr,
|
|
|
|
&db,
|
|
|
|
&compile_error);
|
|
|
|
|
|
|
|
if (err != HS_SUCCESS)
|
|
|
|
{
|
2019-10-13 13:22:09 +00:00
|
|
|
/// CompilerError is a unique_ptr, so correct memory free after the exception is thrown.
|
2019-03-31 20:55:36 +00:00
|
|
|
CompilerError error(compile_error);
|
2019-03-23 22:49:38 +00:00
|
|
|
|
2019-03-31 20:55:36 +00:00
|
|
|
if (error->expression < 0)
|
|
|
|
throw Exception(String(error->message), ErrorCodes::LOGICAL_ERROR);
|
2019-03-29 01:02:05 +00:00
|
|
|
else
|
2019-03-31 20:55:36 +00:00
|
|
|
throw Exception(
|
|
|
|
"Pattern '" + str_patterns[error->expression] + "' failed with error '" + String(error->message),
|
2019-12-30 14:46:02 +00:00
|
|
|
ErrorCodes::BAD_ARGUMENTS);
|
2019-03-31 20:55:36 +00:00
|
|
|
}
|
2019-03-23 19:40:16 +00:00
|
|
|
|
2019-03-31 20:55:36 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::RegexpCreated);
|
2019-03-23 19:40:16 +00:00
|
|
|
|
2019-10-13 13:22:09 +00:00
|
|
|
/// We allocate the scratch space only once, then copy it across multiple threads with hs_clone_scratch
|
|
|
|
/// function which is faster than allocating scratch space each time in each thread.
|
2019-03-31 20:55:36 +00:00
|
|
|
hs_scratch_t * scratch = nullptr;
|
|
|
|
err = hs_alloc_scratch(db, &scratch);
|
2019-03-31 20:19:22 +00:00
|
|
|
|
2019-10-13 13:22:09 +00:00
|
|
|
/// If not HS_SUCCESS, it is guaranteed that the memory would not be allocated for scratch.
|
2019-03-31 20:55:36 +00:00
|
|
|
if (err != HS_SUCCESS)
|
|
|
|
throw Exception("Could not allocate scratch space for hyperscan", ErrorCodes::CANNOT_ALLOCATE_MEMORY);
|
2019-03-31 20:19:22 +00:00
|
|
|
|
2019-03-31 20:55:36 +00:00
|
|
|
return Regexps{db, scratch};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If CompileForEditDistance is False, edit_distance must be nullopt
|
2019-10-13 13:22:09 +00:00
|
|
|
/// Also, we use templates here because each instantiation of function
|
|
|
|
/// template has its own copy of local static variables which must not be the same
|
|
|
|
/// for different hyperscan compilations.
|
2019-10-25 03:25:02 +00:00
|
|
|
template <bool save_indices, bool CompileForEditDistance>
|
2019-03-31 20:55:36 +00:00
|
|
|
inline Regexps * get(const std::vector<StringRef> & patterns, std::optional<UInt32> edit_distance)
|
|
|
|
{
|
|
|
|
/// C++11 has thread-safe function-local statics on most modern compilers.
|
|
|
|
static Pool known_regexps; /// Different variables for different pattern parameters.
|
|
|
|
|
|
|
|
std::vector<String> str_patterns;
|
|
|
|
str_patterns.reserve(patterns.size());
|
|
|
|
for (const StringRef & ref : patterns)
|
|
|
|
str_patterns.push_back(ref.toString());
|
|
|
|
|
2019-10-13 13:22:09 +00:00
|
|
|
/// Get the lock for finding database.
|
2019-03-31 20:55:36 +00:00
|
|
|
std::unique_lock lock(known_regexps.mutex);
|
|
|
|
|
2019-03-31 21:04:45 +00:00
|
|
|
auto it = known_regexps.storage.find({str_patterns, edit_distance});
|
|
|
|
|
2019-10-13 13:22:09 +00:00
|
|
|
/// If not found, compile and let other threads wait.
|
2019-03-31 20:55:36 +00:00
|
|
|
if (known_regexps.storage.end() == it)
|
2019-10-13 13:22:09 +00:00
|
|
|
it = known_regexps.storage
|
|
|
|
.emplace(
|
|
|
|
std::pair{str_patterns, edit_distance},
|
2019-10-25 03:25:02 +00:00
|
|
|
constructRegexps<save_indices, CompileForEditDistance>(str_patterns, edit_distance))
|
2019-10-13 13:22:09 +00:00
|
|
|
.first;
|
|
|
|
/// If found, unlock and return the database.
|
2019-03-31 20:59:18 +00:00
|
|
|
lock.unlock();
|
2019-03-31 21:04:45 +00:00
|
|
|
|
2019-03-31 20:55:36 +00:00
|
|
|
return &it->second;
|
2019-03-23 19:40:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 21:31:00 +00:00
|
|
|
#endif // USE_HYPERSCAN
|
2019-03-23 19:40:16 +00:00
|
|
|
|
2017-03-10 17:52:36 +00:00
|
|
|
}
|