Skip to content

Commit

Permalink
Added mrecordlog_cli.
Browse files Browse the repository at this point in the history
  • Loading branch information
fulmicoton committed Apr 4, 2024
1 parent 8a77dd2 commit ab6b937
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
workspace = { members = ["mrecordlog_cli"] }
[package]
name = "mrecordlog"
version = "0.4.0"
Expand Down
29 changes: 29 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# --- Builder

FROM rust:bullseye AS bin-builder

COPY . /mrecordlog

WORKDIR /mrecordlog/mrecordlog_cli

RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/mrecordlog/target \
cargo build --release --bin mrecordlog && \
ls /mrecordlog/target/release && \
mkdir -p /quickwit/bin && \
mv /mrecordlog/target/release/mrecordlog /quickwit/bin/mrecordlog

# --- ACTUAL image.

FROM debian:bullseye-slim AS quickwit

LABEL org.opencontainers.image.title="Quickwit MRecordlog utils CLI"
LABEL maintainer="Quickwit, Inc. <[email protected]>"
LABEL org.opencontainers.image.vendor="Quickwit, Inc."
LABEL org.opencontainers.image.licenses="AGPL-3.0"

WORKDIR /quickwit

COPY --from=bin-builder /quickwit/bin/mrecordlog /usr/local/bin/mrecordlog

ENTRYPOINT ["mrecordlog"]
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ fmt:
fix: fmt
@echo "Running cargo clippy --fix"
cargo clippy --fix --all-features --allow-dirty --allow-staged

push:
docker buildx build --platform linux/amd64 -t quickwit/mrecordlog:0.1 . --push

15 changes: 15 additions & 0 deletions mrecordlog_cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "mrecordlog_cli"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
mrecordlog = { path = "../" }
anyhow = "*"
structopt = "0.3"

[[bin]]
name = "mrecordlog"
path = "src/main.rs"
56 changes: 56 additions & 0 deletions mrecordlog_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::path::{Path, PathBuf};

use mrecordlog::MultiRecordLog;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
enum Command {
Summary {
#[structopt(short)]
wal_path: PathBuf,
},
Read {
#[structopt(short)]
wal_path: PathBuf,
#[structopt(short)]
queue_name: String,
},
}

fn run_summary(path: &Path) -> anyhow::Result<()> {
let multi_record_log = MultiRecordLog::open(path)?;
let summary = multi_record_log.summary();
for (queue, summary) in summary.queues {
println!("{}", queue);
println!("{summary:?}");
}
Ok(())
}

fn run_read_queue(path: &Path, queue_name: &str) -> anyhow::Result<()> {
let multi_record_log = MultiRecordLog::open(path)?;
for record in multi_record_log.range(queue_name, ..)? {
let Ok(payload_str) = std::str::from_utf8(&record.payload) else {
eprintln!("Payload is not utf8: {:?}", record.payload);
continue;
};
println!("{payload_str}");
}
Ok(())
}

fn main() -> anyhow::Result<()> {
let command = Command::from_args();
match command {
Command::Summary { wal_path } => {
run_summary(&wal_path)?;
}
Command::Read {
queue_name,
wal_path,
} => {
run_read_queue(&wal_path, &queue_name)?;
}
}
Ok(())
}
8 changes: 8 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ impl From<MissingQueue> for AppendError {
#[derive(Debug)]
pub struct MissingQueue(pub String);

impl std::fmt::Display for MissingQueue {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Missing queue: {}", self.0)
}
}

impl std::error::Error for MissingQueue {}

#[derive(Error, Debug)]
pub enum ReadRecordError {
#[error("Io error: {0}")]
Expand Down
14 changes: 0 additions & 14 deletions src/main.rs

This file was deleted.

0 comments on commit ab6b937

Please sign in to comment.