Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PS-9148: Add caching of dictionary table for component_masking_functions (Old) #5275

Open
wants to merge 8 commits into
base: 8.0
Choose a base branch
from
14 changes: 14 additions & 0 deletions components/masking_functions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,41 @@ endif()
set(DATAMASKING_SOURCES
src/component.cpp

src/masking_functions/bookshelf.cpp
src/masking_functions/charset_string.cpp
src/masking_functions/charset_string_operations.cpp
src/masking_functions/dictionary.cpp
src/masking_functions/query_builder.cpp
src/masking_functions/query_cache.cpp
src/masking_functions/random_string_generators.cpp
src/masking_functions/registration_routines.cpp
src/masking_functions/sql_context.cpp
src/masking_functions/sql_escape_functions.cpp
src/masking_functions/sys_vars.cpp

include/masking_functions/bookshelf_fwd.hpp
include/masking_functions/bookshelf.hpp
include/masking_functions/charset_string_fwd.hpp
include/masking_functions/charset_string.hpp
include/masking_functions/charset_string_operations.hpp
include/masking_functions/command_service_tuple_fwd.hpp
include/masking_functions/command_service_tuple.hpp
include/masking_functions/component_sys_variable_service_tuple_fwd.hpp
include/masking_functions/component_sys_variable_service_tuple.hpp
include/masking_functions/dictionary_fwd.hpp
include/masking_functions/dictionary.hpp
include/masking_functions/primitive_singleton.hpp
include/masking_functions/query_builder_fwd.hpp
include/masking_functions/query_builder.hpp
include/masking_functions/query_cache_fwd.hpp
include/masking_functions/query_cache.hpp
include/masking_functions/random_string_generators.hpp
include/masking_functions/registration_routines.hpp
include/masking_functions/sql_context.hpp
include/masking_functions/sql_escape_functions.hpp
include/masking_functions/string_service_tuple_fwd.hpp
include/masking_functions/string_service_tuple.hpp
include/masking_functions/sys_vars.hpp
)

### Configuration ###
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* Copyright (c) 2024 Percona LLC and/or its affiliates. All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */

#ifndef MASKING_FUNCTIONS_BOOKSHELF_HPP
#define MASKING_FUNCTIONS_BOOKSHELF_HPP

#include "masking_functions/bookshelf_fwd.hpp"

#include <string>
#include <string_view>
#include <unordered_map>

#include "masking_functions/dictionary_fwd.hpp"

namespace masking_functions {

class bookshelf {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ cppcoreguidelines-special-member-functions ⚠️
class bookshelf defines a copy assignment operator, a move constructor and a move assignment operator but does not define a destructor or a copy constructor

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ cppcoreguidelines-special-member-functions ⚠️
class bookshelf defines a non-default destructor, a copy assignment operator, a move constructor and a move assignment operator but does not define a copy constructor

public:
bookshelf();
bookshelf(const dictionary &) = delete;
bookshelf(bookshelf &&) = delete;
bookshelf &operator=(const bookshelf &) = delete;
bookshelf &operator=(bookshelf &&) = delete;
~bookshelf();

bool contains(const std::string &dictionary_name,
const std::string &term) const noexcept;
// returns empty std::string_view if no such dictionary exist
std::string_view get_random(
const std::string &dictionary_name) const noexcept;
bool remove(const std::string &dictionary_name) noexcept;
bool remove(const std::string &dictionary_name,
const std::string &term) noexcept;
bool insert(const std::string &dictionary_name, const std::string &term);

private:
// TODO: in c++20 change to method signatures to accept std::string_view
// and container to std::unordered_map<std::string, dictionary_ptr,
// transparent_string_like_hash, std::equal_to<>>.
using dictionary_container = std::unordered_map<std::string, dictionary_ptr>;
dictionary_container dictionaries_;
};

} // namespace masking_functions

#endif // MASKING_FUNCTIONS_BOOKSHELF_HPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Copyright (c) 2024 Percona LLC and/or its affiliates. All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */

#ifndef MASKING_FUNCTIONS_BOOKSHELF_FWD_HPP
#define MASKING_FUNCTIONS_BOOKSHELF_FWD_HPP

#include <memory>

namespace masking_functions {

class bookshelf;

using bookshelf_ptr = std::unique_ptr<bookshelf>;

} // namespace masking_functions

#endif // MASKING_FUNCTIONS_BOOKSHELF_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace masking_functions {
// mysql_command_query{
// mysql_service_mysql_command_query,
// mysql_service_mysql_command_query_result,
// mysql_service_mysql_command_field_info,
// mysql_service_mysql_command_options,
// mysql_service_mysql_command_factory
// };
Expand All @@ -43,6 +44,7 @@ namespace masking_functions {
struct command_service_tuple {
SERVICE_TYPE(mysql_command_query) * query;
SERVICE_TYPE(mysql_command_query_result) * query_result;
SERVICE_TYPE(mysql_command_field_info) * field_info;
SERVICE_TYPE(mysql_command_options) * options;
SERVICE_TYPE(mysql_command_factory) * factory;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Copyright (c) 2023 Percona LLC and/or its affiliates. All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */

#ifndef MASKING_FUNCTIONS_COMPONENT_SYS_VARIABLE_SERVICE_TUPLE_HPP
#define MASKING_FUNCTIONS_COMPONENT_SYS_VARIABLE_SERVICE_TUPLE_HPP

#include <mysql/components/service.h>

#include <mysql/components/services/component_sys_var_service.h>

#include "masking_functions/component_sys_variable_service_tuple_fwd.hpp"

namespace masking_functions {

// A set of MySQL query services required to perform system variable
// registration / unregistration.
// It is recommended to be used in a combination with the
// 'primitive_singleton' class template.
//
// primitive_singleton<component_sys_variable_service_tuple>::instance() =
// component_sys_variable_service_tuple{
// component_sys_variable_register,
// component_sys_variable_unregister
// };
// ...
// sql_context
// ctx{primitive_singleton<component_sys_variable_service_tuple>::instance()};
struct component_sys_variable_service_tuple {
SERVICE_TYPE(component_sys_variable_register) * registrator;
SERVICE_TYPE(component_sys_variable_unregister) * unregistrator;
};

} // namespace masking_functions

#endif // MASKING_FUNCTIONS_COMPONENT_SYS_VARIABLE_SERVICE_TUPLE_HPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Copyright (c) 2023 Percona LLC and/or its affiliates. All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */

#ifndef MASKING_FUNCTIONS_COMPONENT_SYS_VARIABLE_SERVICE_TUPLE_FWD_HPP
#define MASKING_FUNCTIONS_COMPONENT_SYS_VARIABLE_SERVICE_TUPLE_FWD_HPP

namespace masking_functions {

struct component_sys_variable_service_tuple;

} // namespace masking_functions

#endif // MASKING_FUNCTIONS_COMPONENT_SYS_VARIABLE_SERVICE_TUPLE_FWD_HPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* Copyright (c) 2024 Percona LLC and/or its affiliates. All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */

#ifndef MASKING_FUNCTIONS_DICTIONARY_HPP
#define MASKING_FUNCTIONS_DICTIONARY_HPP

#include "masking_functions/dictionary_fwd.hpp"

#include <string>
#include <string_view>
#include <unordered_set>

namespace masking_functions {

class dictionary {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ cppcoreguidelines-special-member-functions ⚠️
class dictionary defines a copy constructor, a copy assignment operator, a move constructor and a move assignment operator but does not define a destructor

public:
// a convenience constructor that creates a dictionary with one term
explicit dictionary(const std::string &term);

dictionary(const dictionary &) = delete;
dictionary(dictionary &&) = delete;
dictionary &operator=(const dictionary &) = delete;
dictionary &operator=(dictionary &&) = delete;

~dictionary() = default;

bool is_empty() const noexcept { return terms_.empty(); }

bool contains(const std::string &term) const noexcept;
// returns empty std::string_view if the dictionary is empty
std::string_view get_random() const noexcept;
bool insert(const std::string &term);
bool remove(const std::string &term) noexcept;

private:
// TODO: in c++20 change to method signatures to accept std::string_view
// and container to std::unordered_set<std::string,
// transparent_string_like_hash, std::equal_to<>>.
using term_container = std::unordered_set<std::string>;
term_container terms_;
};

} // namespace masking_functions

#endif // MASKING_FUNCTIONS_DICTIONARY_HPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Copyright (c) 2024 Percona LLC and/or its affiliates. All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */

#ifndef MASKING_FUNCTIONS_DICTIONARY_FWD_HPP
#define MASKING_FUNCTIONS_DICTIONARY_FWD_HPP

#include <memory>

namespace masking_functions {

class dictionary;

using dictionary_ptr = std::unique_ptr<dictionary>;

} // namespace masking_functions

#endif // MASKING_FUNCTIONS_DICTIONARY_FWD_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
#ifndef MASKING_FUNCTIONS_QUERY_BUILDER_HPP
#define MASKING_FUNCTIONS_QUERY_BUILDER_HPP

#include "masking_functions/query_builder_fwd.hpp"

#include <string>
#include <string_view>

#include "masking_functions/charset_string_fwd.hpp"

namespace masking_functions {

// A helper class which allows to easily construct SQL-statements necessary
Expand All @@ -29,14 +29,13 @@ class query_builder {
public:
static constexpr std::string_view default_result_character_set = "utf8mb4";

static constexpr std::string_view default_database_name = "mysql";
static constexpr std::string_view default_table_name = "masking_dictionaries";
static constexpr std::string_view default_dictionary_field_name =
"Dictionary";
static constexpr std::string_view default_term_field_name = "Term";

query_builder(
std::string_view database_name = default_database_name,
explicit query_builder(
std::string_view database_name,
std::string_view table_name = default_table_name,
std::string_view dictionary_field_name = default_dictionary_field_name,
std::string_view term_field_name = default_term_field_name)
Expand All @@ -56,26 +55,18 @@ class query_builder {
return term_field_name_;
}

std::string select_random_term_for_dictionary(
const charset_string &dictionary_name) const {
return select_term_for_dictionary_internal(dictionary_name, nullptr);
}
std::string check_term_presence_in_dictionary(
const charset_string &dictionary_name, const charset_string &term) const {
return select_term_for_dictionary_internal(dictionary_name, &term);
}
std::string select_all_from_dictionary() const;

std::string insert_ignore_record(const charset_string &dictionary_name,
const charset_string &term) const;
std::string insert_ignore_record(const std::string &dictionary_name,
const std::string &term) const;

std::string delete_for_dictionary(
const charset_string &dictionary_name) const {
std::string delete_for_dictionary(const std::string &dictionary_name) const {
return delete_for_dictionary_and_opt_term_internal(dictionary_name,
nullptr);
}

std::string delete_for_dictionary_and_term(
const charset_string &dictionary_name, const charset_string &term) const {
std::string delete_for_dictionary_and_term(const std::string &dictionary_name,
const std::string &term) const {
return delete_for_dictionary_and_opt_term_internal(dictionary_name, &term);
}

Expand All @@ -85,13 +76,8 @@ class query_builder {
std::string dictionary_field_name_;
std::string term_field_name_;

std::string select_term_for_dictionary_internal(
const charset_string &dictionary_name,
const charset_string *opt_term) const;

std::string delete_for_dictionary_and_opt_term_internal(
const charset_string &dictionary_name,
const charset_string *opt_term) const;
const std::string &dictionary_name, const std::string *opt_term) const;
};

} // namespace masking_functions
Expand Down
Loading
Loading