环境
- Time 2022-12-11
- WSL-Ubuntu 22.04
- Rust 1.65.0
前言
说明
参考:https://github.com/pingcap/talent-plan
目标
在上一节的基础上,将转为 JSON 的日志操作记录到文件。
Cargo.toml
[package]
edition = "2021"
name = "kvs"
version = "1.0.0"[dependencies]
clap = {version = "4", features = ["derive"]}
serde = {version = "1", features = ["derive"]}
serde_json = "1"
lib.rs
use cmd::Command;
use log::CommandLog;mod cmd;
mod log;#[derive(Default)]
pub struct KvStore {log: CommandLog,
}impl KvStore {pub fn get(&mut self, key: &str) -> Option<String> {self.log.get(key)}pub fn set(&mut self, key: String, value: String) -> Option<String> {let command = Command::Set { key, value };self.log.set(command)}pub fn remove(&mut self, key: String) -> Option<String> {let command = Command::Remove { key };self.log.remove(command)}
}
log.rs
use std::fs::{File, OpenOptions};
use std::io::{BufWriter, Write};use crate::cmd::Command;pub struct CommandLog {writer: BufWriter<File>,
}impl Default for CommandLog {fn default() -> Self {let path = "/root/log/1.log";Self {writer: new_writer(path),}}
}impl CommandLog {pub fn get(&mut self, _: &str) -> Option<String> {None}pub fn set(&mut self, command: Command) -> Option<String> {let json = serde_json::to_string(&command).unwrap();self.writer.write_all(json.as_bytes()).unwrap();None}pub fn remove(&mut self, command: Command) -> Option<String> {let json = serde_json::to_string(&command).unwrap();self.writer.write_all(json.as_bytes()).unwrap();None}
}fn new_writer(path: &str) -> BufWriter<File> {let file = OpenOptions::new().append(true).create(true).open(path).unwrap();BufWriter::new(file)
}
运行
使用 cargo build --release 构建后运行。
root@jiangbo12490:~/git/game/target/release# ./kvs set name JiangBo
SET KEY: name, VALUE: None
root@jiangbo12490:~/git/game/target/release# ./kvs set age 44
SET KEY: age, VALUE: None
root@jiangbo12490:~/git/game/target/release# ./kvs get name
GET KEY: name, VALUE: None
root@jiangbo12490:~/git/game/target/release# ./kvs rm name
REM KEY: name, VALUE: None
查看文件
root@jiangbo12490:~/log# cat /root/log/1.log
{"Set":{"key":"name","value":"JiangBo"}}{"Set":{"key":"age","value":"44"}}{"Remove":{"key":"name"}}
总结
将命令操作转为 JSON 后,存储到文件中。