Skip to content

Commit

Permalink
Sort operations directly into json map
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes committed Mar 11, 2022
1 parent c3464df commit 59d6ee7
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use async_trait::async_trait;
use dashmap::DashMap;
use persist_query::PersistError;
use relay_config::LocalPersistConfig;
use serde_json::{Map, Value};
use sha1::{Digest, Sha1};
use std::collections::BTreeMap;

use crate::OperationPersister;

Expand Down Expand Up @@ -54,13 +54,16 @@ impl OperationPersister for LocalPersister {
}

fn finalize(&self) -> Result<(), PersistError> {
let ordered: BTreeMap<_, _> = self
.query_map
.iter()
.map(|x| (x.key().clone(), x.value().clone()))
.collect();
let mut vec = Vec::from_iter(self.query_map.iter());
vec.sort_by(|a, b| a.key().partial_cmp(b.key()).unwrap());

let content = serde_json::to_string_pretty(&ordered)?;
let mut json_map = Map::with_capacity(vec.len());

for entry in vec {
json_map.insert(entry.key().clone(), Value::String(entry.value().clone()));
}

let content = serde_json::to_string_pretty(&json_map)?;
std::fs::write(&self.config.file, content)?;

Ok(())
Expand Down

0 comments on commit 59d6ee7

Please sign in to comment.