use std::fmt;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use crate::core::persistent::PersistentKeyIndex;
use crate::types::KeyOrdinal;
pub const KEY_INDEX_FORMAT_REVISION: u32 = 2;
pub const KEY_INDEX_PARSER_REVISION: u32 = 1;
pub const KEY_INDEX_NORMALIZATION_REVISION: u32 = 1;
pub const KEY_INDEX_REVISION: &str = "f2-p1-n1";
const DEFAULT_MAX_INDEX_BYTES: u64 = 64 * 1024 * 1024 * 1024;
const DEFAULT_MAX_METADATA_BYTES: usize = 64 * 1024 * 1024;
const DEFAULT_BUILD_MEMORY_BYTES: usize = 32 * 1024 * 1024;
const DEFAULT_CHUNK_BYTES: usize = 64 * 1024;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyIndexOptions {
pub(crate) max_index_bytes: u64,
pub(crate) max_metadata_bytes: usize,
pub(crate) build_memory_bytes: usize,
pub(crate) chunk_bytes: usize,
pub(crate) scratch_directory: Option<PathBuf>,
}
impl KeyIndexOptions {
pub const fn new() -> Self {
Self {
max_index_bytes: DEFAULT_MAX_INDEX_BYTES,
max_metadata_bytes: DEFAULT_MAX_METADATA_BYTES,
build_memory_bytes: DEFAULT_BUILD_MEMORY_BYTES,
chunk_bytes: DEFAULT_CHUNK_BYTES,
scratch_directory: None,
}
}
pub const fn with_max_index_bytes(mut self, value: u64) -> Self {
self.max_index_bytes = value;
self
}
pub const fn with_max_metadata_bytes(mut self, value: usize) -> Self {
self.max_metadata_bytes = value;
self
}
pub const fn with_build_memory_bytes(mut self, value: usize) -> Self {
self.build_memory_bytes = value;
self
}
pub const fn with_chunk_bytes(mut self, value: usize) -> Self {
self.chunk_bytes = value;
self
}
pub fn with_scratch_directory(mut self, directory: impl AsRef<Path>) -> Self {
self.scratch_directory = Some(directory.as_ref().to_path_buf());
self
}
pub const fn max_index_bytes(&self) -> u64 {
self.max_index_bytes
}
pub const fn max_metadata_bytes(&self) -> usize {
self.max_metadata_bytes
}
pub const fn build_memory_bytes(&self) -> usize {
self.build_memory_bytes
}
pub const fn chunk_bytes(&self) -> usize {
self.chunk_bytes
}
pub fn scratch_directory(&self) -> Option<&Path> {
self.scratch_directory.as_deref()
}
}
impl Default for KeyIndexOptions {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct KeyIndexSourceIdentity {
pub(crate) source_bytes: u64,
pub(crate) source_modified_unix_nanos: i128,
pub(crate) key_count: u64,
}
impl KeyIndexSourceIdentity {
pub const fn new(source_bytes: u64, source_modified_unix_nanos: i128, key_count: u64) -> Self {
Self {
source_bytes,
source_modified_unix_nanos,
key_count,
}
}
pub const fn source_bytes(self) -> u64 {
self.source_bytes
}
pub const fn source_modified_unix_nanos(self) -> i128 {
self.source_modified_unix_nanos
}
pub const fn key_count(self) -> u64 {
self.key_count
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct KeyIndexBuild {
pub(crate) source_identity: KeyIndexSourceIdentity,
pub(crate) bytes_written: u64,
}
impl KeyIndexBuild {
pub const fn source_identity(self) -> KeyIndexSourceIdentity {
self.source_identity
}
pub const fn bytes_written(self) -> u64 {
self.bytes_written
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum KeyIndexRejection {
InvalidMagic,
UnsupportedFormatRevision {
found: u32,
},
UnsupportedEndianMarker {
found: u32,
},
IncompatibleParserRevision {
found: u32,
},
IncompatibleNormalizationRevision {
found: u32,
},
SourceIdentityMismatch,
SourceLengthMismatch {
expected: u64,
actual: u64,
},
SourceModifiedMismatch {
expected: i128,
actual: i128,
},
KeyCountMismatch {
expected: u64,
actual: u64,
},
FileLengthMismatch {
declared: u64,
actual: u64,
},
InvalidLayout(&'static str),
ChecksumMismatch {
section: &'static str,
chunk: Option<u64>,
expected: u32,
actual: u32,
},
SourceKeyMismatch {
ordinal: KeyOrdinal,
},
}
impl fmt::Display for KeyIndexRejection {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidMagic => formatter.write_str("invalid magic"),
Self::UnsupportedFormatRevision { found } => {
write!(formatter, "unsupported format revision {found}")
}
Self::UnsupportedEndianMarker { found } => {
write!(formatter, "unsupported endian marker {found:#010x}")
}
Self::IncompatibleParserRevision { found } => {
write!(formatter, "incompatible parser revision {found}")
}
Self::IncompatibleNormalizationRevision { found } => {
write!(formatter, "incompatible normalization revision {found}")
}
Self::SourceIdentityMismatch => formatter.write_str("source identity mismatch"),
Self::SourceLengthMismatch { expected, actual } => write!(
formatter,
"source length mismatch: expected {expected}, got {actual}"
),
Self::SourceModifiedMismatch { expected, actual } => write!(
formatter,
"source modification time mismatch: expected {expected}, got {actual}"
),
Self::KeyCountMismatch { expected, actual } => write!(
formatter,
"source key-count mismatch: expected {expected}, got {actual}"
),
Self::FileLengthMismatch { declared, actual } => write!(
formatter,
"file length mismatch: declared {declared}, got {actual}"
),
Self::InvalidLayout(reason) => write!(formatter, "invalid layout: {reason}"),
Self::ChecksumMismatch {
section,
chunk,
expected,
actual,
} => match chunk {
Some(chunk) => write!(
formatter,
"checksum mismatch for {section} chunk {chunk}: expected {expected:#010x}, got {actual:#010x}"
),
None => write!(
formatter,
"checksum mismatch for {section}: expected {expected:#010x}, got {actual:#010x}"
),
},
Self::SourceKeyMismatch { ordinal } => {
write!(
formatter,
"source key mismatch at ordinal {}",
ordinal.get()
)
}
}
}
}
impl std::error::Error for KeyIndexRejection {}
#[derive(Clone)]
pub struct KeyIndex {
pub(crate) inner: Arc<PersistentKeyIndex>,
}
impl KeyIndex {
pub(crate) fn new(inner: PersistentKeyIndex) -> Self {
Self {
inner: Arc::new(inner),
}
}
pub fn source_identity(&self) -> KeyIndexSourceIdentity {
self.inner.source_identity()
}
pub fn len(&self) -> u64 {
self.inner.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl fmt::Debug for KeyIndex {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("KeyIndex")
.field("revision", &KEY_INDEX_REVISION)
.field("rows", &self.len())
.field("source_bytes", &self.source_identity().source_bytes())
.finish_non_exhaustive()
}
}