2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-07-12 18:58:39 -04:00
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
use std::hash::Hasher;
|
|
|
|
|
2022-07-12 18:58:39 -04:00
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_runtime::deno_webstorage::rusqlite::Connection;
|
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
/// A very fast insecure hasher that uses the xxHash algorithm.
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct FastInsecureHasher(twox_hash::XxHash64);
|
|
|
|
|
|
|
|
impl FastInsecureHasher {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_str(&mut self, text: &str) -> &mut Self {
|
|
|
|
self.write(text.as_bytes());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write(&mut self, bytes: &[u8]) -> &mut Self {
|
|
|
|
self.0.write(bytes);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-07-30 11:43:03 -04:00
|
|
|
pub fn write_u8(&mut self, value: u8) -> &mut Self {
|
|
|
|
self.0.write_u8(value);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
pub fn write_u64(&mut self, value: u64) -> &mut Self {
|
|
|
|
self.0.write_u64(value);
|
|
|
|
self
|
|
|
|
}
|
2022-07-12 18:58:39 -04:00
|
|
|
|
2022-09-07 15:06:18 -04:00
|
|
|
pub fn write_hashable(
|
|
|
|
&mut self,
|
|
|
|
hashable: &impl std::hash::Hash,
|
|
|
|
) -> &mut Self {
|
|
|
|
hashable.hash(&mut self.0);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
pub fn finish(&self) -> u64 {
|
|
|
|
self.0.finish()
|
|
|
|
}
|
2022-07-12 18:58:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs the common sqlite pragma.
|
|
|
|
pub fn run_sqlite_pragma(conn: &Connection) -> Result<(), AnyError> {
|
|
|
|
// Enable write-ahead-logging and tweak some other stuff
|
|
|
|
let initial_pragmas = "
|
|
|
|
-- enable write-ahead-logging mode
|
|
|
|
PRAGMA journal_mode=WAL;
|
|
|
|
PRAGMA synchronous=NORMAL;
|
|
|
|
PRAGMA temp_store=memory;
|
|
|
|
PRAGMA page_size=4096;
|
|
|
|
PRAGMA mmap_size=6000000;
|
|
|
|
PRAGMA optimize;
|
|
|
|
";
|
|
|
|
|
|
|
|
conn.execute_batch(initial_pragmas)?;
|
|
|
|
Ok(())
|
|
|
|
}
|