dag: expose version in namedag states

Summary:
This will be used to detect if the NameDag was changed between reloads,
and decide whether we need to invalidate caches or not.

Reviewed By: andll

Differential Revision: D29888938

fbshipit-source-id: 377879bd8d28c92feca80c025613a65139ccb866
This commit is contained in:
Jun Wu 2021-07-26 15:21:54 -07:00 committed by Facebook GitHub Bot
parent 1c4cf6a5ec
commit 64c0d7e986
4 changed files with 38 additions and 2 deletions

View File

@ -37,6 +37,7 @@ minibytes = { path = "../minibytes" }
nonblocking = { path = "../nonblocking" }
parking_lot = "0.10.2"
quickcheck = { version = "0.9", optional = true }
rand = { version = "0.7", features = ["small_rng"] }
serde = { version = "1.0.126", features = ["derive", "rc"] }
tempfile = { version = "3.1", optional = true }
thiserror = "1.0"

View File

@ -10,6 +10,7 @@ use crate::errors::bug;
use crate::iddag::IdDag;
use crate::iddagstore::IndexedLogStore;
use crate::idmap::IdMap;
use crate::ops::IntVersion;
use crate::ops::Open;
use crate::ops::Persist;
use crate::ops::TryClone;
@ -119,6 +120,15 @@ impl Persist for NameDagState {
}
}
impl IntVersion for NameDagState {
fn int_version(&self) -> (u64, u64) {
match &self.mlog {
Some(mlog) => mlog.version(),
None => (0, 0),
}
}
}
impl TryClone for NameDagState {
fn try_clone(&self) -> Result<Self> {
Ok(Self {

View File

@ -9,6 +9,7 @@ use super::AbstractNameDag;
use crate::iddag::IdDag;
use crate::iddagstore::InProcessStore;
use crate::idmap::MemIdMap;
use crate::ops::IntVersion;
use crate::ops::Open;
use crate::ops::Persist;
use crate::Id;
@ -28,7 +29,17 @@ pub type MemNameDag =
pub struct MemNameDagPath;
#[derive(Debug, Clone)]
pub struct MemNameDagState;
pub struct MemNameDagState {
version: (u64, u64),
}
impl Default for MemNameDagState {
fn default() -> Self {
Self {
version: (rand::random(), 0),
}
}
}
impl Open for MemNameDagPath {
type OpenTarget = MemNameDag;
@ -42,7 +53,7 @@ impl Open for MemNameDagPath {
path: self.clone(),
snapshot: Default::default(),
pending_heads: Default::default(),
state: MemNameDagState,
state: MemNameDagState::default(),
id: format!("mem:{}", next_id()),
overlay_map: Default::default(),
overlay_map_next_id: Id::MIN,
@ -72,10 +83,17 @@ impl Persist for MemNameDagState {
}
fn persist(&mut self, _lock: &Self::Lock) -> Result<()> {
self.version.1 += 1;
Ok(())
}
}
impl IntVersion for MemNameDagState {
fn int_version(&self) -> (u64, u64) {
self.version
}
}
fn next_id() -> u64 {
static ID: AtomicU64 = AtomicU64::new(0);
ID.fetch_add(1, atomic::Ordering::AcqRel)

View File

@ -498,6 +498,13 @@ pub trait Open: Clone {
fn open(&self) -> Result<Self::OpenTarget>;
}
/// Has an integer tuple version that can be used to test if the data was
/// changed. If the first number changes, it means incompatible changes.
/// If only the second number increases, it means append-only changes.
pub trait IntVersion {
fn int_version(&self) -> (u64, u64);
}
/// Fallible clone.
pub trait TryClone {
fn try_clone(&self) -> Result<Self>