Research-Stack/2-Search-Space/search/stract/crates/file-store/src/lib.rs

60 lines
1.9 KiB
Rust

// Stract is an open source web search engine.
// Copyright (C) 2024 Stract ApS
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
//! A collection of simple disk-based data structures.
pub type Result<T> = std::result::Result<T, anyhow::Error>;
pub mod const_serializable;
pub mod iterable;
pub mod peekable;
pub mod random_lookup;
pub mod temp;
pub use const_serializable::ConstSerializable;
pub use peekable::Peekable;
use temp::TempDir;
// taken from https://docs.rs/sled/0.34.7/src/sled/config.rs.html#445
pub fn gen_temp_path() -> std::path::PathBuf {
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::SystemTime;
static SALT_COUNTER: AtomicUsize = AtomicUsize::new(0);
let seed = SALT_COUNTER.fetch_add(1, Ordering::SeqCst) as u128;
let now = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_nanos()
<< 48;
let pid = u128::from(std::process::id());
let salt = (pid << 16) + now + seed;
if cfg!(target_os = "linux") {
// use shared memory for temporary linux files
format!("/dev/shm/pagecache.tmp.{salt}").into()
} else {
std::env::temp_dir().join(format!("pagecache.tmp.{salt}"))
}
}
pub fn gen_temp_dir() -> Result<TempDir> {
TempDir::new()
}