MySQL 9.4.0
Source Code Documentation
json_hash.h
Go to the documentation of this file.
1#ifndef JSON_HASH_INCLUDED
2#define JSON_HASH_INCLUDED
3
4/* Copyright (c) 2025, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27#include <assert.h>
28#include <stddef.h>
29#include <iterator>
30#include <map>
31#include <memory> // unique_ptr
32#include <new>
33#include <string>
34#include <string_view>
35#include <type_traits> // is_base_of
36#include <utility>
37#include <vector>
38
39#include "extra/xxhash/my_xxhash.h"
40#include "sql-common/json_dom.h"
41
42/// Helper class for building a hash key. This class can be used to plugin any
43/// individual hash algorithm by overriding add_character() and add_string()
44/// functions.
46 public:
47 virtual void add_character(uchar ch) = 0;
48
50 char tmp[8];
51 int8store(tmp, ll);
52 add_string(tmp, sizeof(tmp));
53 }
54
55 void add_double(double d) {
56 // Make -0.0 and +0.0 have the same key.
57 if (d == 0) {
59 return;
60 }
61
62 char tmp[8];
63 float8store(tmp, d);
64 add_string(tmp, sizeof(tmp));
65 }
66
67 virtual void add_string(const char *str, size_t len) = 0;
68
69 /**
70 Return the computed hash value in integer form. This is optional as each
71 seperate hasher would like to produce hash in different forms.
72 */
73 virtual ulonglong get_hash_value() { return 0; }
74
75 virtual ~Json_wrapper_hasher() = default;
76};
77
78/// Helper class for building a hash key.
80 private:
82
83 public:
84 explicit Json_wrapper_crc_hasher(ulonglong hash_val) : m_crc(hash_val) {}
85
86 ulonglong get_hash_value() override { return m_crc; }
87
88 void add_character(uchar ch) override { add_to_crc(ch); }
89
90 void add_string(const char *str, size_t len) override {
91 for (size_t idx = 0; idx < len; idx++) {
92 add_to_crc(*str++);
93 }
94 }
95
96 private:
97 /**
98 Add another character to the evolving crc.
99
100 @param[in] ch The character to add
101 */
102 void add_to_crc(uchar ch) {
103 // This logic was cribbed from sql_executor.cc/unique_hash
104 m_crc = ((m_crc << 8) + (((uchar)ch))) +
105 (m_crc >> (8 * sizeof(ha_checksum) - 8));
106 }
107};
108
110 XXH3_state_t *state = nullptr;
111
112 public:
113 explicit Json_wrapper_xxh_hasher() : state(XXH3_createState()) {
114 XXH3_128bits_reset(this->state);
115 }
116 void add_string(const char *str, size_t len) override {
117 if (len == 0) {
118 add_character('\0');
119 } else {
120 XXH3_128bits_update(this->state, str, len);
121 }
122 }
123 void reset() { XXH3_128bits_reset(this->state); }
124 XXH128_hash_t get_digest() { return XXH3_128bits_digest(this->state); }
125 void add_character(uchar ch) override {
126 XXH3_128bits_update(this->state, &ch, 1);
127 }
128 ~Json_wrapper_xxh_hasher() override { XXH3_freeState(this->state); }
129};
130
131constexpr uint HEX_ENC_ETAG_SIZE = 16;
132
135 const JsonSerializationErrorHandler &error_handler,
136 const std::unordered_set<std::string> *json_arrayagg_keys = nullptr,
137 std::string *path = nullptr);
138
139void XXH128_hash_hex(XXH128_hash_t h, String *s);
140
141XXH128_hash_t add_xxh128_hash(XXH128_hash_t l, XXH128_hash_t r);
142
143#endif
Error handler for the functions that serialize a JSON value in the JSON binary storage format.
Definition: json_error_handler.h:49
Helper class for building a hash key.
Definition: json_hash.h:79
void add_string(const char *str, size_t len) override
Definition: json_hash.h:90
ulonglong m_crc
Definition: json_hash.h:81
void add_character(uchar ch) override
Definition: json_hash.h:88
ulonglong get_hash_value() override
Return the computed hash value in integer form.
Definition: json_hash.h:86
Json_wrapper_crc_hasher(ulonglong hash_val)
Definition: json_hash.h:84
void add_to_crc(uchar ch)
Add another character to the evolving crc.
Definition: json_hash.h:102
Helper class for building a hash key.
Definition: json_hash.h:45
void add_double(double d)
Definition: json_hash.h:55
virtual ulonglong get_hash_value()
Return the computed hash value in integer form.
Definition: json_hash.h:73
void add_integer(longlong ll)
Definition: json_hash.h:49
virtual void add_string(const char *str, size_t len)=0
virtual ~Json_wrapper_hasher()=default
virtual void add_character(uchar ch)=0
Definition: json_hash.h:109
void add_string(const char *str, size_t len) override
Definition: json_hash.h:116
XXH128_hash_t get_digest()
Definition: json_hash.h:124
XXH3_state_t * state
Definition: json_hash.h:110
~Json_wrapper_xxh_hasher() override
Definition: json_hash.h:128
void reset()
Definition: json_hash.h:123
Json_wrapper_xxh_hasher()
Definition: json_hash.h:113
void add_character(uchar ch) override
Definition: json_hash.h:125
Abstraction for accessing JSON values irrespective of whether they are (started out as) binary JSON v...
Definition: json_dom.h:1160
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:169
JSON DOM.
constexpr uint HEX_ENC_ETAG_SIZE
Definition: json_hash.h:131
bool calculate_etag_for_json(const Json_wrapper &wr, Json_wrapper_hasher &hash_key, const JsonSerializationErrorHandler &error_handler, const std::unordered_set< std::string > *json_arrayagg_keys=nullptr, std::string *path=nullptr)
Definition: json_hash.cc:61
void XXH128_hash_hex(XXH128_hash_t h, String *s)
Definition: json_hash.cc:54
XXH128_hash_t add_xxh128_hash(XXH128_hash_t l, XXH128_hash_t r)
Definition: json_hash.cc:46
void float8store(char *V, double M)
Definition: my_byteorder.h:210
void int8store(char *pT, ulonglong A)
Definition: my_byteorder.h:192
std::uint32_t ha_checksum
Definition: my_checksum.h:106
unsigned long long int ulonglong
Definition: my_inttypes.h:56
unsigned char uchar
Definition: my_inttypes.h:52
long long int longlong
Definition: my_inttypes.h:55
static char * path
Definition: mysqldump.cc:150
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1084
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86
const uchar * hash_key(const uchar *el, size_t *length)
Definition: sql_auth_cache.cc:3316