Fusio-log is an append only log library for Rust. Leveraging fusio, fusio-log supports various backends like disk, wasm, and S3. You can use it in various scenarios that, such as write-ahead logging, manifest file, distributed log.
- Define data structure.
struct User {
id: u64,
name: String,
email: Option<String>,
}
- Implement
Encode
andDecode
trait for it - Start to use fusio-log
#[tokio::main]
async fn main() {
let temp_dir = TempDir::new().unwrap();
let path = Path::from_filesystem_path(temp_dir.path())
.unwrap()
.child("log");
let mut logger = Options::new(path).build::<User>().await.unwrap();
logger.write(&User {id: 1, name: "fusio-log".to_string(), None}).await.unwrap();
logger.write_batch([
User {id: 2, name: "fusio".to_string(), "[email protected]"}
User {id: 3, name: "tonbo".to_string(), None}
].into_iter()).await.unwrap();
logger.flush().await.unwrap();
logger.close().await.unwrap();
}
Recover from log file:
let stream = Options::new(path)
.recover::<User>()
.await
.unwrap();
while let Ok(users) = stream.try_next().await {
match users {
Some(users) => {
for user in users {
println("{}" user.id)
}
};
None => println();
}
}
let path = Path::from_url_path("log").unwrap();
let option = Options::new(path).fs(FsOptions::S3 {
bucket: "data".to_string(),
credential: Some(fusio::remotes::aws::AwsCredential {
key_id: "key_id".to_string(),
secret_key: "secret_key".to_string(),
token: None,
}),
endpoint: None,
region: Some("region".to_string()),
sign_payload: None,
checksum: None,
});
let mut logger = option.build::<User>().await.unwrap();
logger
.write(&User {
id: 100,
name: "Name".to_string(),
email: None,
})
.await
.unwrap();
Please make sure disable default features and enable web
feature.
fusio-log = {git = "https://github.com/tonbo-io/fusio-log", default-features = false, features = ["bytes", "web"]}
Then, use Path::from_opfs_path
to replace Path::from_filesystem_path
let temp_dir = TempDir::new().unwrap();
let path = Path::from_opfs_path(temp_dir.path())
.unwrap()
.child("log");
let mut logger = Options::new(path).build::<User>().await.unwrap();
logger.write(&User {id: 1, name: "fusio-log".to_string(), None}).await.unwrap();
cargo build
Build with wasm-pack
wasm-pack build --no-default-features --features web,bytes