Skip to main content

reifydb_core/
fingerprint.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::ops::Deref;
5
6use reifydb_runtime::hash::Hash128;
7use serde::{Deserialize, Serialize};
8
9#[repr(transparent)]
10#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
11#[serde(transparent)]
12pub struct StatementFingerprint(pub Hash128);
13
14impl Deref for StatementFingerprint {
15	type Target = u128;
16
17	fn deref(&self) -> &Self::Target {
18		&self.0.0
19	}
20}
21
22impl StatementFingerprint {
23	#[inline]
24	pub const fn new(value: u128) -> Self {
25		Self(Hash128(value))
26	}
27
28	#[inline]
29	pub const fn as_u128(&self) -> u128 {
30		self.0.0
31	}
32
33	#[inline]
34	pub const fn to_le_bytes(&self) -> [u8; 16] {
35		self.0.0.to_le_bytes()
36	}
37
38	#[inline]
39	pub const fn from_le_bytes(bytes: [u8; 16]) -> Self {
40		Self(Hash128(u128::from_le_bytes(bytes)))
41	}
42
43	#[inline]
44	pub fn to_hex(&self) -> String {
45		self.0.to_hex_string_prefixed()
46	}
47}
48
49impl From<Hash128> for StatementFingerprint {
50	fn from(hash: Hash128) -> Self {
51		Self(hash)
52	}
53}
54
55impl From<StatementFingerprint> for Hash128 {
56	fn from(fp: StatementFingerprint) -> Self {
57		fp.0
58	}
59}
60
61impl From<u128> for StatementFingerprint {
62	fn from(value: u128) -> Self {
63		Self(Hash128(value))
64	}
65}
66
67#[repr(transparent)]
68#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
69#[serde(transparent)]
70pub struct RequestFingerprint(pub Hash128);
71
72impl Deref for RequestFingerprint {
73	type Target = u128;
74
75	fn deref(&self) -> &Self::Target {
76		&self.0.0
77	}
78}
79
80impl RequestFingerprint {
81	#[inline]
82	pub const fn new(value: u128) -> Self {
83		Self(Hash128(value))
84	}
85
86	#[inline]
87	pub const fn as_u128(&self) -> u128 {
88		self.0.0
89	}
90
91	#[inline]
92	pub const fn to_le_bytes(&self) -> [u8; 16] {
93		self.0.0.to_le_bytes()
94	}
95
96	#[inline]
97	pub const fn from_le_bytes(bytes: [u8; 16]) -> Self {
98		Self(Hash128(u128::from_le_bytes(bytes)))
99	}
100
101	#[inline]
102	pub fn to_hex(&self) -> String {
103		self.0.to_hex_string_prefixed()
104	}
105}
106
107impl From<Hash128> for RequestFingerprint {
108	fn from(hash: Hash128) -> Self {
109		Self(hash)
110	}
111}
112
113impl From<RequestFingerprint> for Hash128 {
114	fn from(fp: RequestFingerprint) -> Self {
115		fp.0
116	}
117}
118
119impl From<u128> for RequestFingerprint {
120	fn from(value: u128) -> Self {
121		Self(Hash128(value))
122	}
123}
124
125#[repr(transparent)]
126#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
127#[serde(transparent)]
128pub struct CompilationFingerprint(pub Hash128);
129
130impl Deref for CompilationFingerprint {
131	type Target = u128;
132
133	fn deref(&self) -> &Self::Target {
134		&self.0.0
135	}
136}
137
138impl CompilationFingerprint {
139	#[inline]
140	pub const fn new(value: u128) -> Self {
141		Self(Hash128(value))
142	}
143
144	#[inline]
145	pub const fn as_u128(&self) -> u128 {
146		self.0.0
147	}
148
149	#[inline]
150	pub const fn to_le_bytes(&self) -> [u8; 16] {
151		self.0.0.to_le_bytes()
152	}
153
154	#[inline]
155	pub const fn from_le_bytes(bytes: [u8; 16]) -> Self {
156		Self(Hash128(u128::from_le_bytes(bytes)))
157	}
158
159	#[inline]
160	pub fn to_hex(&self) -> String {
161		self.0.to_hex_string_prefixed()
162	}
163}
164
165impl From<Hash128> for CompilationFingerprint {
166	fn from(hash: Hash128) -> Self {
167		Self(hash)
168	}
169}
170
171impl From<CompilationFingerprint> for Hash128 {
172	fn from(fp: CompilationFingerprint) -> Self {
173		fp.0
174	}
175}
176
177impl From<u128> for CompilationFingerprint {
178	fn from(value: u128) -> Self {
179		Self(Hash128(value))
180	}
181}