mariadb/sql/clone_handler.h
2025-09-05 02:22:27 +05:30

145 lines
4.6 KiB
C++

/* Copyright (c) 2017, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
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, version 2.0, 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, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@file clone_handler.h
Clone handler interface to access clone plugin
*/
#ifndef CLONE_HANDLER_INCLUDED
#define CLONE_HANDLER_INCLUDED
#include <atomic>
#include <string>
#include <set>
#include <tuple>
# if defined __GNUC__ && __GNUC__ > 5 && __GNUC__ < 9 && !defined(_WIN32) && !defined(__APPLE__) && !defined(_clang_)
#include <experimental/filesystem>
namespace fsys= std::experimental::filesystem;
#else
#include <filesystem>
namespace fsys= std::filesystem;
#endif
#include <functional>
#include <my_global.h>
#include "sql_plugin.h"
class THD;
struct Mysql_clone;
/**
Number of PSI_statement_info instruments
for clone statements.
*/
#define CLONE_PSI_STATEMENT_COUNT 5
/**
Clone plugin handler to convenient way to. Takes
*/
class Clone_handler {
public:
/** Constructor: Initialize plugin name */
Clone_handler(const char *plugin_name_arg) : m_plugin_handle(nullptr) {
m_plugin_name.assign(plugin_name_arg);
}
/** Initialize plugin handle
@return error code */
int init();
/** Clone handler interface for local clone.
@param[in] thd server thread handle
@param[in] data_dir cloned data directory
@return error code */
int clone_local(THD *thd, const char *data_dir);
/** @return false only if no user data is dropped yet. */
static bool is_data_dropped() { return (s_is_data_dropped); }
/** Must set before dropping any user data. */
static void set_drop_data() { s_is_data_dropped.store(true); }
/** @return true, if clone provisioning in progress. */
static bool is_provisioning() { return (s_provision_in_progress > 0); }
private:
/** Validate clone data directory and convert to os format
@param[in] in_dir user specified clone directory
@param[out] out_dir data directory in native os format
@return error code */
int validate_dir(const char *in_dir, char *out_dir);
private:
/** True if clone provisioning in progress. */
static std::atomic<int> s_provision_in_progress;
/** True, if any user data is dropped by clone. */
static std::atomic<bool> s_is_data_dropped;
/** Clone plugin name */
std::string m_plugin_name;
/** Clone plugin handle */
Mysql_clone *m_plugin_handle;
};
/** Check if the clone plugin is installed and lock. If the plugin is ready,
return the handler to caller.
@param[in] thd server thread handle
@param[out] plugin plugin reference
@return clone handler on success otherwise NULL */
Clone_handler *clone_plugin_lock(THD *thd, plugin_ref *plugin);
/** Unlock the clone plugin.
@param[in] thd server thread handle
@param[out] plugin plugin reference */
void clone_plugin_unlock(THD *thd, plugin_ref plugin);
namespace clone_common
{
/** Check if string ends with given suffix.
@return true if string ends with given suffix. */
bool ends_with(const char *str, const char *suffix);
std::tuple<std::string, std::string, std::string>
convert_filepath_to_tablename(const char *filepath);
bool is_log_table(const char *dbname, const char *tablename);
bool is_stats_table(const char *dbname, const char *tablename);
void foreach_file_in_db_dirs(const char *dir_path,
std::function<bool(const char *)> func);
int foreach_file_in_dir(
const fsys::path& dir_path,
const std::function<void(const fsys::path&)>& callback,
const std::set<std::string>& file_extns= {},
const std::set<fsys::file_type>& file_types= {fsys::file_type::regular},
int max_depth= 1);
std::string read_table_version_id(File file);
} // namespace clone_common
#endif /* CLONE_HANDLER_INCLUDED */