dag: impl Persist on IdMaps

Summary: IdMap fits the Persist trait.

Reviewed By: sfilipco

Differential Revision: D24399494

fbshipit-source-id: 97b84d155f4b9bb3006bfad116defa4fca6330d6
This commit is contained in:
Jun Wu 2020-10-20 15:15:45 -07:00 committed by Facebook GitHub Bot
parent 625b8ab4d5
commit b249950984
2 changed files with 55 additions and 21 deletions

View File

@ -11,6 +11,7 @@ use crate::errors::bug;
use crate::errors::programming;
use crate::id::{Group, Id, VertexName};
use crate::ops::IdConvert;
use crate::ops::Persist;
use crate::ops::PrefixLookup;
use crate::Result;
use byteorder::{BigEndian, ReadBytesExt};
@ -126,20 +127,10 @@ impl IdMap {
// Take a filesystem lock. The file name 'lock' is taken by indexedlog
// running on Windows, so we choose another file name here.
let lock_file = {
let mut path = self.path.clone();
path.push("wlock");
File::open(&path).or_else(|_| {
fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&path)
})?
};
lock_file.lock_exclusive()?;
let lock_file = self.lock()?;
// Reload. So we get latest data.
self.reload()?;
self.reload(&lock_file)?;
Ok(SyncableIdMap {
map: self,
@ -147,15 +138,6 @@ impl IdMap {
})
}
/// Reload from the filesystem. Discard pending changes.
fn reload(&mut self) -> Result<()> {
self.log.clear_dirty()?;
self.log.sync()?;
// Invalidate the next free id cache.
self.cached_next_free_ids = Default::default();
Ok(())
}
/// Find name by a specified integer id.
pub fn find_name_by_id(&self, id: Id) -> Result<Option<&[u8]>> {
let key = id.0.to_be_bytes();
@ -391,6 +373,41 @@ impl IdMapWrite for IdMap {
}
}
impl Persist for IdMap {
type Lock = File;
fn lock(&self) -> Result<Self::Lock> {
let lock_file = {
let mut path = self.path.clone();
path.push("wlock");
File::open(&path).or_else(|_| {
fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&path)
})?
};
lock_file.lock_exclusive()?;
Ok(lock_file)
}
fn reload(&mut self, _lock: &Self::Lock) -> Result<()> {
self.log.clear_dirty()?;
self.log.sync()?;
// Invalidate the next free id cache.
self.cached_next_free_ids = Default::default();
Ok(())
}
fn persist(&mut self, _lock: &Self::Lock) -> Result<()> {
if self.need_rebuild_non_master {
return bug("cannot persist with re-assigned ids unresolved");
}
self.log.sync()?;
Ok(())
}
}
impl PrefixLookup for IdMap {
fn vertexes_by_hex_prefix(&self, hex_prefix: &[u8], limit: usize) -> Result<Vec<VertexName>> {
self.find_names_by_hex_prefix(hex_prefix, limit)

View File

@ -8,6 +8,7 @@
use super::IdMapWrite;
use crate::id::{Group, Id, VertexName};
use crate::ops::IdConvert;
use crate::ops::Persist;
use crate::ops::PrefixLookup;
use crate::Result;
use std::collections::{BTreeMap, HashMap};
@ -91,6 +92,22 @@ impl IdMapWrite for MemIdMap {
}
}
impl Persist for MemIdMap {
type Lock = ();
fn lock(&self) -> Result<Self::Lock> {
Ok(())
}
fn reload(&mut self, _lock: &Self::Lock) -> Result<()> {
Ok(())
}
fn persist(&mut self, _lock: &Self::Lock) -> Result<()> {
Ok(())
}
}
impl PrefixLookup for MemIdMap {
fn vertexes_by_hex_prefix(&self, hex_prefix: &[u8], limit: usize) -> Result<Vec<VertexName>> {
let start = VertexName::from_hex(hex_prefix)?;