sqlite_hashes/
xxhash.rs

1use noncrypto_digests::{Xxh32, Xxh3_128, Xxh3_64, Xxh64};
2
3use crate::rusqlite::{Connection, Result};
4
5/// Register `xxh32`, `xxh64`, `xxh3_64`, `xxh3_128`, `xxh3_64` SQL functions with the given `SQLite` connection.
6/// The functions use [Rust xxHash implementation](https://github.com/DoumanAsh/xxhash-rust) to compute the hash of the argument(s) using zero as the seed value.
7///
8/// # Example
9///
10/// ```
11/// # // Use Python to convert:
12/// # //   print('"\\x' + '\\x'.join([f"{v:02X}" for v in [251, 0, 119, 249]])+'"')
13/// # use sqlite_hashes::rusqlite::{Connection, Result};
14/// # use sqlite_hashes::register_xxhash_functions;
15/// # fn main() -> Result<()> {
16/// let db = Connection::open_in_memory()?;
17/// register_xxhash_functions(&db)?;
18/// let hash: Vec<u8> = db.query_row("SELECT xxh32('hello')", [], |r| r.get(0))?;
19/// let expected = b"\xFB\x00\x77\xF9";
20/// assert_eq!(hash, expected);
21/// let hash: Vec<u8> = db.query_row("SELECT xxh64('hello')", [], |r| r.get(0))?;
22/// let expected = b"\x26\xC7\x82\x7D\x88\x9F\x6D\xA3";
23/// assert_eq!(hash, expected);
24/// let hash: Vec<u8> = db.query_row("SELECT xxh3_64('hello')", [], |r| r.get(0))?;
25/// let expected = b"\x95\x55\xE8\x55\x5C\x62\xDC\xFD";
26/// assert_eq!(hash, expected);
27/// let hash: Vec<u8> = db.query_row("SELECT xxh3_128('hello')", [], |r| r.get(0))?;
28/// let expected = b"\xb5\xe9\xc1\xad\x07\x1b\x3e\x7f\xc7\x79\xcf\xaa\x5e\x52\x38\x18";
29/// assert_eq!(hash, expected);
30/// # Ok(())
31/// # }
32/// ```
33pub fn register_xxhash_functions(conn: &Connection) -> Result<()> {
34    crate::scalar::create_hash_fn::<Xxh32>(conn, "xxh32")?;
35    crate::scalar::create_hash_fn::<Xxh64>(conn, "xxh64")?;
36    crate::scalar::create_hash_fn::<Xxh3_64>(conn, "xxh3_64")?;
37    crate::scalar::create_hash_fn::<Xxh3_128>(conn, "xxh3_128")
38}