MySQL 9.3.0
Source Code Documentation
polyglot_store.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024, 2025, Oracle and/or its affiliates.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2.0,
6 * as published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms,
10 * as designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU General Public License, version 2.0, for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#ifndef MYSQLSHDK_SCRIPTING_POLYGLOT_UTILS_POLYGLOT_STORE_H_
27#define MYSQLSHDK_SCRIPTING_POLYGLOT_UTILS_POLYGLOT_STORE_H_
28
29#include <cstddef>
30#include <mutex>
31#include <unordered_map>
32
34
35namespace shcore {
36namespace polyglot {
37
38/**
39 * Helper class to handle polyglot values made permanent.
40 *
41 * An instance of this class will create a polyglot reference for the given
42 * value, making it permanent and available even across contexts.
43 *
44 * On destruction the created reference will be removed making the value subject
45 * for garbage collection if no additional references are held.
46 */
47class Store final {
48 public:
49 Store() = default;
50
51 explicit Store(poly_thread thread) noexcept : Store(thread, nullptr) {}
52
53 Store(poly_thread thread, poly_handle object);
54
55 Store(const Store &other) = delete;
56
57 Store &operator=(const Store &other) = delete;
58
59 Store(Store &&other) noexcept;
60 Store &operator=(Store &&other) noexcept;
61
62 ~Store() noexcept { reset(false); }
63
64 Store &reset(bool throw_on_error = true);
65
66 Store &operator=(std::nullptr_t) { return reset(false); }
67
68 explicit operator bool() const { return nullptr != m_reference; }
69
70 poly_reference get() const noexcept { return m_reference; }
71
72 poly_thread thread() const { return m_thread; }
73
74 private:
75 poly_thread m_thread = nullptr;
76 poly_reference m_reference = nullptr;
77};
78
79/**
80 * A container for stored values
81 */
82class Polyglot_storage final {
83 public:
84 explicit Polyglot_storage(poly_thread thread);
86
87 poly_reference store(poly_handle value);
88 void erase(poly_reference value);
89
90 void clear();
91
92 private:
93 poly_thread m_thread;
94 std::mutex m_mutex;
95 std::unordered_map<poly_reference, Store> m_stored_values;
96 bool m_cleared = false;
97};
98
99} // namespace polyglot
100} // namespace shcore
101
102#endif // MYSQLSHDK_SCRIPTING_POLYGLOT_UTILS_POLYGLOT_STORE_H_
A container for stored values.
Definition: polyglot_store.h:82
void erase(poly_reference value)
Definition: polyglot_store.cc:97
std::unordered_map< poly_reference, Store > m_stored_values
Definition: polyglot_store.h:95
std::mutex m_mutex
Definition: polyglot_store.h:94
poly_thread m_thread
Definition: polyglot_store.h:93
Polyglot_storage(poly_thread thread)
Definition: polyglot_store.cc:80
bool m_cleared
Definition: polyglot_store.h:96
poly_reference store(poly_handle value)
Definition: polyglot_store.cc:82
void clear()
Definition: polyglot_store.cc:114
~Polyglot_storage()
Definition: polyglot_store.cc:112
Helper class to handle polyglot values made permanent.
Definition: polyglot_store.h:47
Store(const Store &other)=delete
Store(poly_thread thread) noexcept
Definition: polyglot_store.h:51
Store & reset(bool throw_on_error=true)
Definition: polyglot_store.cc:60
~Store() noexcept
Definition: polyglot_store.h:62
poly_reference get() const noexcept
Definition: polyglot_store.h:70
poly_thread thread() const
Definition: polyglot_store.h:72
poly_thread m_thread
Definition: polyglot_store.h:75
poly_reference m_reference
Definition: polyglot_store.h:76
Store & operator=(const Store &other)=delete
Store & operator=(std::nullptr_t)
Definition: polyglot_store.h:66
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
Definition: file_system_exceptions.h:34