Skip to content
This repository has been archived by the owner on Apr 15, 2023. It is now read-only.

Commit

Permalink
Pushing in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
chotchki committed Oct 8, 2021
1 parent 2837532 commit a598c0b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ bytes = "1"
futures = "0.3"
hex-literal = "0.3.1"
lru = "0.6.6"
moka = { version = "0.6", features = ["future"] }
nom = "7"
log = "0.4"
simplelog = "^0.10.0"
Expand Down
61 changes: 59 additions & 2 deletions src/engine/io/block_layer/lock_manager.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,60 @@
pub struct LockManager;
use std::sync::Arc;

impl LockManager {}
use super::file_manager::{FileManager, FileManagerError};
use crate::engine::io::page_formats::{PageId, PageOffset};
use moka::future::Cache;
use thiserror::Error;
use tokio::sync::RwLock;

/// The LockManager is used for cooperative access to pages in the system.
///
/// Before accessing the I/O layer you must get a read or write lock on
/// the page you need to access. Only AFTER you have the lock you should
/// ask for the page.
///
/// TODO: Find a way that I can do this in a type enforcing way.
#[derive(Clone)]
pub struct LockManager {
file_manager: Arc<FileManager>,
locks: Cache<(PageId, PageOffset), Arc<RwLock<()>>>,
}

impl LockManager {
pub fn new(file_manager: Arc<FileManager>) -> LockManager {
LockManager {
file_manager,
locks: Cache::new(1000),
}
}

pub async fn get_offset(&self, page_id: PageId) -> Result<PageOffset, LockManagerError> {
Ok(self.file_manager.get_offset(&page_id).await?)
}

pub async fn get_offset_non_zero(
&self,
page_id: PageId,
) -> Result<PageOffset, LockManagerError> {
let mut offset = PageOffset(0);
while offset == PageOffset(0) {
offset = self.file_manager.get_offset(&page_id).await?;
}
Ok(offset)
}

pub async fn get_lock(&self, page_id: PageId, offset: PageOffset) -> Arc<RwLock<()>> {
self.locks
.get_or_insert_with(
(page_id, offset),
async move { Arc::new(RwLock::const_new(())) },
)
.await
}
}

#[derive(Debug, Error)]
pub enum LockManagerError {
#[error(transparent)]
FileManagerError(#[from] FileManagerError),
}

0 comments on commit a598c0b

Please sign in to comment.