use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use crate::durable::write_atomic;
use crate::error::{Error, Result, corrupt};
use crate::group::{self, CommittedOffset, Consumer, ConsumerConfig};
use crate::meta::{self, Meta, Value};
use crate::topic::{self, Producer, TopicConfig, TopicDescription, TopicReader};
const META_FILE: &str = "mojay.toml";
const TOPICS_DIR: &str = "topics";
const GROUPS_DIR: &str = "groups";
const FORMAT_VERSION: u64 = 1;
#[derive(Debug, Clone)]
pub struct Db {
root: PathBuf,
}
impl Db {
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
let root = path.as_ref().to_path_buf();
fs::create_dir_all(root.join(TOPICS_DIR))?;
fs::create_dir_all(root.join(GROUPS_DIR))?;
let meta_path = root.join(META_FILE);
match fs::read_to_string(&meta_path) {
Ok(text) => check_format(&meta_path, &text)?,
Err(e) if e.kind() == io::ErrorKind::NotFound => {
let entries = [("format_version", Value::Int(FORMAT_VERSION))];
let text = meta::format("Mojay data directory.", &entries);
write_atomic(&meta_path, text.as_bytes())?;
}
Err(e) => return Err(e.into()),
}
Ok(Self { root })
}
pub fn create_topic(&self, name: &str, config: TopicConfig) -> Result<()> {
topic::create(&self.topics_dir(), name, &config)
}
pub fn list_topics(&self) -> Result<Vec<String>> {
let mut names = Vec::new();
for entry in fs::read_dir(self.topics_dir())? {
let entry = entry?;
let Ok(name) = entry.file_name().into_string() else {
continue;
};
if !name.starts_with('.') && entry.file_type()?.is_dir() {
names.push(name);
}
}
names.sort();
Ok(names)
}
pub fn producer(&self, topic: &str) -> Result<Producer> {
Producer::open(&self.existing_topic_dir(topic)?, topic)
}
pub fn reader(&self, topic: &str) -> Result<TopicReader> {
TopicReader::open(&self.existing_topic_dir(topic)?, topic)
}
pub fn consumer(
&self,
group: &str,
topics: &[&str],
config: ConsumerConfig,
) -> Result<Consumer> {
let readers = topics
.iter()
.map(|topic| self.reader(topic))
.collect::<Result<_>>()?;
Consumer::open(&self.root.join(GROUPS_DIR), group, readers, config)
}
pub fn describe_topic(&self, topic: &str) -> Result<TopicDescription> {
topic::describe(&self.existing_topic_dir(topic)?, topic)
}
pub fn committed_offsets(&self, topic: &str) -> Result<Vec<CommittedOffset>> {
let partitions = self.describe_topic(topic)?.config.partitions;
group::committed_offsets(&self.root.join(GROUPS_DIR), topic, partitions)
}
fn topics_dir(&self) -> PathBuf {
self.root.join(TOPICS_DIR)
}
fn existing_topic_dir(&self, name: &str) -> Result<PathBuf> {
topic::validate_name(name)?;
let dir = self.topics_dir().join(name);
if !dir.is_dir() {
return Err(Error::UnknownTopic(name.to_string()));
}
Ok(dir)
}
}
fn check_format(path: &Path, text: &str) -> Result<()> {
let meta = Meta::parse(path, text)?;
meta.only(&["format_version"])?;
let version = meta.int("format_version")?;
if version != FORMAT_VERSION {
return Err(corrupt(
path,
format!("unsupported format version {version} (expected {FORMAT_VERSION})"),
));
}
Ok(())
}