use std::collections::{HashMap, HashSet};
use std::fmt::{Display, Write};
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use crate::core::codecs::Codecs;
use crate::core::search::sort::Sort;
#[cfg(test)]
use crate::core::store::dummy::dummy_directory::DummyDirectory;
use crate::core::util::StringHelper;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::version::Version;
pub struct SegmentInfo<D> {
pub(crate) name: Arc<str>,
max_doc: i32, pub(crate) dir: Arc<D>,
is_compound_file: bool,
id: [u8; StringHelper::ID_LENGTH],
#[cfg(test)]
id_str: String,
pub(crate) id_key: Arc<str>,
codec: Option<Codecs>,
diagnostics: HashMap<String, String>,
attributes: HashMap<String, String>,
pub(crate) index_sort: Option<Arc<Sort>>,
pub(crate) version: Option<Version>,
pub(crate) min_version: Option<Version>,
has_blocks: bool,
set_files: Option<Arc<HashSet<String>>>,
}
static EMPTY_SEGMENT_FILES: std::sync::LazyLock<Arc<HashSet<String>>> =
std::sync::LazyLock::new(Arc::default);
#[cfg(test)]
impl Default for SegmentInfo<DummyDirectory> {
fn default() -> Self {
let id = [0; 16];
let id_str = StringHelper::id_to_string(Some(&id));
SegmentInfo {
name: Arc::from(""),
max_doc: -1,
dir: Arc::new(DummyDirectory),
is_compound_file: false,
id: [0; 16],
id_str: id_str.clone(),
id_key: Arc::from(id_str),
diagnostics: HashMap::new(),
attributes: HashMap::new(),
codec: None,
index_sort: None,
version: None,
min_version: None,
has_blocks: false,
set_files: None,
}
}
}
pub const NO: i32 = -1; pub const YES: i32 = 1; impl<D> SegmentInfo<D> {
#[allow(clippy::too_many_arguments)]
pub fn new<N: Into<Arc<str>>>(
dir: Arc<D>,
version: Option<Version>,
min_version: Option<Version>,
name: N,
max_doc: i32,
is_compound_file: bool,
has_blocks: bool,
codec: Option<Codecs>,
diagnostics: HashMap<String, String>,
id: [u8; StringHelper::ID_LENGTH],
attributes: HashMap<String, String>,
index_sort: Option<Arc<Sort>>,
) -> Result<SegmentInfo<D>> {
if version.is_none() {
return Err(LuceneError::illegal_argument(
"segment version must not be None",
));
}
if id.len() != StringHelper::ID_LENGTH {
return Err(LuceneError::illegal_argument(format!("Invalid id: {id:?}")));
}
#[cfg(test)]
let id_key = {
let random = StringHelper::id_to_string(Some(&StringHelper::random_id()));
let id_str = StringHelper::id_to_string(Some(&id));
format!("{}-{}", id_str, random)
};
#[cfg(not(test))]
let mut id_digits = [0; 25];
#[cfg(not(test))]
let id_key = StringHelper::format_id(&StringHelper::random_id(), &mut id_digits);
Ok(SegmentInfo {
dir,
version,
min_version,
name: name.into(),
max_doc,
is_compound_file,
has_blocks,
codec,
diagnostics,
id,
#[cfg(test)]
id_str: StringHelper::id_to_string(Some(&id)),
id_key: id_key.into(),
attributes,
index_sort,
set_files: None,
})
}
}
impl<D> SegmentInfo<D> {
pub fn name(&self) -> &str {
&self.name
}
pub fn dir(&self) -> &Arc<D> {
&self.dir
}
pub(crate) fn set_diagnostics(&mut self, diagnostics: HashMap<String, String>) {
self.diagnostics = diagnostics;
}
pub fn add_diagnostics(&mut self, diagnostics: HashMap<String, String>) {
self.diagnostics.extend(diagnostics);
}
pub fn set_codec<C>(&mut self, codec: C) -> Result<()>
where
C: Into<Codecs>,
{
if self.codec.is_some() {
return Err(LuceneError::illegal_state("codec can only be set once"));
}
self.codec = Some(codec.into());
Ok(())
}
pub fn get_codec(&self) -> Result<&Codecs> {
self
.codec
.as_ref()
.ok_or_else(|| LuceneError::illegal_state("segment codec is not set"))
}
pub fn get_diagnostics(&self) -> &HashMap<String, String> {
&self.diagnostics
}
pub(crate) fn set_use_compound_file(&mut self, is_compound_file: bool) {
self.is_compound_file = is_compound_file;
}
pub fn get_use_compound_file(&self) -> bool {
self.is_compound_file
}
pub fn get_has_blocks(&self) -> bool {
self.has_blocks
}
pub(crate) fn set_has_blocks(&mut self) {
self.has_blocks = true;
}
pub fn max_doc(&self) -> Result<i32> {
if self.max_doc == -1 {
return Err(LuceneError::illegal_state("maxDoc isn't set yet"));
}
Ok(self.max_doc)
}
pub(crate) fn set_max_doc(&mut self, max_doc: i32) -> Result<()> {
if self.max_doc != -1 {
return Err(LuceneError::illegal_state(format!(
"maxDoc was already set: this.maxDoc={} vs maxDoc {}",
self.max_doc, max_doc
)));
}
self.max_doc = max_doc;
Ok(())
}
pub fn files(&self) -> Result<&HashSet<String>> {
self.files_snapshot().map(Arc::as_ref)
}
pub(crate) fn files_snapshot(&self) -> Result<&Arc<HashSet<String>>> {
match self.set_files {
Some(ref files) => Ok(files),
None => Err(LuceneError::illegal_state(format!(
"files were not set; segment={} maxDoc={}",
self.name, self.max_doc
))),
}
}
pub fn take_files(&mut self) -> Result<HashSet<String>> {
match self.set_files.take() {
Some(files) => Ok(Arc::unwrap_or_clone(files)),
None => Err(LuceneError::illegal_state(format!(
"files were not set; segment={} maxDoc={}",
self.name, self.max_doc
))),
}
}
pub fn set_files<I, S>(&mut self, files: I) -> Result<()>
where
I: IntoIterator<Item = S>,
S: AsRef<str> + Into<String>,
{
self.set_files = Some(Arc::clone(&EMPTY_SEGMENT_FILES));
self.add_files(files)
}
pub(crate) fn set_imported_files(&mut self, files: &HashSet<String>) -> Result<()> {
self.set_files = Some(Arc::clone(&EMPTY_SEGMENT_FILES));
self.check_file_names(files)?;
let renamed = self
.set_files
.insert(Arc::new(HashSet::with_capacity(files.len())));
let renamed = Arc::make_mut(renamed);
for file in files {
renamed.insert(renamed_segment_file(&self.name, file));
}
Ok(())
}
pub(crate) fn replace_files(&mut self, files: HashSet<String>) -> Result<()> {
self.set_files = Some(Arc::clone(&EMPTY_SEGMENT_FILES));
self.check_file_names(&files)?;
self.set_files = Some(Arc::new(
if files
.iter()
.all(|file| IndexFileNames::parse_segment_name(file) == self.name.as_ref())
{
files
} else {
files
.into_iter()
.map(|file| named_for_this_segment(&self.name, file))
.collect()
},
));
Ok(())
}
pub fn to_string(&self, del_count: i32) -> std::result::Result<String, std::fmt::Error> {
let mut description = String::new();
self.write_description(&mut description, del_count)?;
Ok(description)
}
pub(crate) fn write_description(
&self,
output: &mut impl Write,
del_count: i32,
) -> std::fmt::Result {
output.write_str(&self.name)?;
output.write_char('(')?;
if let Some(version) = &self.version {
write!(output, "{version}")?;
} else {
output.write_char('?')?;
}
output.write_char(')')?;
let cfs = if self.is_compound_file { 'c' } else { 'C' };
write!(output, ":{cfs}{}", self.max_doc)?;
if del_count != 0 {
write!(output, "/{del_count}")?;
}
if let Some(index_sort) = &self.index_sort {
write!(output, ":[indexSort={index_sort}]")?;
}
if !self.diagnostics.is_empty() {
write!(output, ":[diagnostics={:?}]", self.diagnostics)?;
}
if !self.attributes.is_empty() {
write!(output, ":[attributes={:?}]", self.attributes)?;
}
Ok(())
}
pub fn get_version_ref(&self) -> Option<&Version> {
self.version.as_ref()
}
pub fn get_min_version_ref(&self) -> Option<&Version> {
self.min_version.as_ref()
}
pub fn get_min_version(&self) -> Option<Version> {
self.min_version.as_ref().cloned()
}
pub fn get_id(&self) -> &[u8; StringHelper::ID_LENGTH] {
&self.id
}
pub fn get_id_key(&self) -> &str {
&self.id_key
}
pub fn add_files<I, S>(&mut self, files: I) -> Result<()>
where
I: IntoIterator<Item = S>,
S: AsRef<str> + Into<String>,
{
let files: smallvec::SmallVec<[S; 4]> = files.into_iter().collect();
self.check_file_names(&files)?;
match self.set_files {
Some(ref mut set_files) if !files.is_empty() => {
let set_files = Arc::make_mut(set_files);
set_files.reserve(files.len());
for f in files {
set_files.insert(named_for_this_segment(&self.name, f));
}
},
Some(_) => {},
None => return Err(LuceneError::illegal_state("set_files was not initialized")),
}
Ok(())
}
pub fn add_file<F>(&mut self, file: F) -> Result<()>
where
F: Into<String>,
{
let file = file.into();
self.check_file_names(std::iter::once(&file))?;
match self.set_files {
Some(ref mut set_files) => {
Arc::make_mut(set_files).insert(named_for_this_segment(&self.name, file));
},
None => return Err(LuceneError::illegal_state("set_files was not initialized")),
}
Ok(())
}
fn check_file_names<I, S>(&self, files: I) -> Result<()>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
for file in files {
let file = file.as_ref();
if !CODEC_FILE_PATTERN.is_match(file) {
return Err(LuceneError::illegal_argument(format!(
"Invalid codec filename '{}', must match: {}",
file,
CODEC_FILE_PATTERN.as_str()
)));
}
if file.as_bytes()[file.len().saturating_sub(4)..].eq_ignore_ascii_case(b".tmp") {
return Err(LuceneError::illegal_argument(format!(
"Invalid codec filename '{file}', cannot end with .tmp extension"
)));
}
}
Ok(())
}
pub fn get_attribute(&self, key: &str) -> Option<&str> {
self.attributes.get(key).map(|v| v.as_str())
}
pub fn put_attribute<K, V>(&mut self, key: K, value: V) -> Option<String>
where
K: Into<String>,
V: Into<String>,
{
let value = value.into();
let key = key.into();
self.attributes.insert(key, value)
}
pub fn get_attributes(&self) -> Result<&HashMap<String, String>> {
Ok(&self.attributes)
}
pub fn get_index_sort(&self) -> Option<Arc<Sort>> {
self.index_sort.as_ref().cloned()
}
pub(crate) fn index_sort_ref(&self) -> Option<&Sort> {
self.index_sort.as_deref()
}
pub(crate) fn flush_failure_mirror(&self) -> Result<Self> {
let mut mirror = Self::dummy(Arc::clone(&self.dir), Arc::clone(&self.name));
mirror.set_files = Some(Arc::clone(self.files_snapshot()?));
Ok(mirror)
}
pub(crate) fn dummy(dir: Arc<D>, name: Arc<str>) -> Self {
let id = [0u8; StringHelper::ID_LENGTH];
static DUMMY_ID: std::sync::LazyLock<Arc<str>> = std::sync::LazyLock::new(|| Arc::from("0"));
SegmentInfo {
name,
max_doc: 0,
dir,
is_compound_file: false,
id,
#[cfg(test)]
id_str: DUMMY_ID.to_string(),
id_key: Arc::clone(&DUMMY_ID),
diagnostics: HashMap::new(),
attributes: HashMap::new(),
codec: None,
index_sort: None,
version: None,
min_version: None,
has_blocks: false,
set_files: None,
}
}
}
impl<D> PartialEq for SegmentInfo<D> {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.dir, &other.dir) && self.name == other.name
}
}
impl<D> Eq for SegmentInfo<D> {}
impl<D> Hash for SegmentInfo<D> {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
Arc::as_ptr(&self.dir).hash(state);
self.name.hash(state);
}
}
impl<D> Display for SegmentInfo<D> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.write_description(f, 0)
}
}
#[cfg(test)]
impl<D> Clone for SegmentInfo<D> {
fn clone(&self) -> Self {
SegmentInfo {
name: self.name.clone(),
max_doc: self.max_doc,
dir: self.dir.clone(),
is_compound_file: self.is_compound_file,
id: self.id,
id_str: self.id_str.clone(),
id_key: self.id_key.clone(),
diagnostics: self.diagnostics.clone(),
attributes: self.attributes.clone(),
codec: self.codec.clone(),
index_sort: self.index_sort.clone(),
version: self.version.clone(),
min_version: self.min_version.clone(),
has_blocks: self.has_blocks,
set_files: self.set_files.clone(),
}
}
}
use crate::core::index::{CODEC_FILE_PATTERN, IndexFileNames};
pub(crate) fn renamed_segment_file(name: &str, file: &str) -> String {
let suffix = IndexFileNames::strip_segment_name(file);
if file.len() == name.len() + suffix.len() && file.starts_with(name) {
file.to_owned()
} else {
[name, suffix].concat()
}
}
pub fn named_for_this_segment<T: AsRef<str> + Into<String>>(name: &str, file: T) -> String {
let mut file = file.into();
let stripped = IndexFileNames::strip_segment_name(&file);
if file.len() == name.len() + stripped.len() && file.starts_with(name) {
file
} else {
let prefix_length = file.len() - stripped.len();
file.replace_range(..prefix_length, name);
file
}
}