Skip to content

Commit

Permalink
metalog: add refencode methods for ease-of-use
Browse files Browse the repository at this point in the history
Summary:
Add application-specific concepts so the callsite does not have to use
low-level APIs.

Reviewed By: muirdm

Differential Revision: D54201003

fbshipit-source-id: 3d713bfc4c56be041f724ae56c6a91922dd08bf0
  • Loading branch information
quark-zju authored and facebook-github-bot committed Feb 29, 2024
1 parent b367ed4 commit d8cff2e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions eden/scm/lib/metalog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod constants;
mod errors;
mod export;
mod metalog;
mod metalog_ext;
mod resolve;

pub use errors::Error;
Expand Down
64 changes: 64 additions & 0 deletions eden/scm/lib/metalog/src/metalog_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/

//! Extended APIs for application-specific metalog.
use std::collections::BTreeMap;

use anyhow::Result;
use types::HgId;

use crate::MetaLog;

impl MetaLog {
/// Decode bookmarks.
pub fn get_bookmarks(&self) -> Result<BTreeMap<String, HgId>> {
let decoded = match self.get("bookmarks")? {
Some(data) => refencode::decode_bookmarks(&data)?,
None => Default::default(),
};
Ok(decoded)
}

/// Decode remotenames.
pub fn get_remotenames(&self) -> Result<BTreeMap<String, HgId>> {
let decoded = match self.get("remotenames")? {
Some(data) => refencode::decode_remotenames(&data)?,
None => Default::default(),
};
Ok(decoded)
}

/// Decode visibleheads.
pub fn get_visibleheads(&self) -> Result<Vec<HgId>> {
let decoded = match self.get("visibleheads")? {
Some(data) => refencode::decode_visibleheads(&data)?,
None => Default::default(),
};
Ok(decoded)
}

/// Update bookmarks. This does not write to disk until `commit`.
pub fn set_bookmarks(&mut self, value: &BTreeMap<String, HgId>) -> Result<()> {
let encoded = refencode::encode_bookmarks(value);
self.set("bookmarks", &encoded)?;
Ok(())
}

/// Update remotenames. This does not write to disk until `commit`.
pub fn set_remotenames(&mut self, value: &BTreeMap<String, HgId>) -> Result<()> {
let encoded = refencode::encode_remotenames(value);
self.set("remotenames", &encoded)?;
Ok(())
}

/// Update visibleheads. This does not write to disk until `commit`.
pub fn set_visibleheads(&mut self, value: &[HgId]) -> Result<()> {
let encoded = refencode::encode_visibleheads(value);
self.set("visibleheads", &encoded)?;
Ok(())
}
}

0 comments on commit d8cff2e

Please sign in to comment.