Skip to content

Commit

Permalink
fix: open cache file
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodus committed Apr 15, 2024
1 parent fae795a commit a3d2007
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rdkafka::{
Message,
};
use serde::Deserialize;
use std::io::Write as _;
use std::{collections::HashMap, fs::File, path::PathBuf};
use thegraph_core::types::alloy_primitives::Address;

Expand Down Expand Up @@ -104,7 +105,13 @@ struct DB {

impl DB {
fn new(file: PathBuf, window: Duration) -> anyhow::Result<Self> {
let cache = File::options().create(true).truncate(false).open(&file)?;
let cache = File::options()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(&file)
.context("open cache file")?;
let modified: DateTime<Utc> = DateTime::from(cache.metadata()?.modified()?);
let mut data: HashMap<Address, Vec<u128>> =
serde_json::from_reader(&cache).unwrap_or_default();
Expand Down Expand Up @@ -139,8 +146,9 @@ impl DB {
}
}

let file = File::create(&self.file)?;
let mut file = File::create(&self.file)?;
serde_json::to_writer(&file, &self.data)?;
file.flush()?;

self.last_flush = now;
Ok(())
Expand Down

0 comments on commit a3d2007

Please sign in to comment.