2025-01-19 19:24:12 -05:00
|
|
|
// Copyright (c) 2022-2025 Alex Chi Z
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2024-01-30 14:48:03 +08:00
|
|
|
#![allow(unused_variables)] // TODO(you): remove this lint after implementing this mod
|
|
|
|
|
#![allow(dead_code)] // TODO(you): remove this lint after implementing this mod
|
|
|
|
|
|
2024-01-26 18:14:34 +08:00
|
|
|
pub mod txn;
|
2024-01-30 12:14:32 +08:00
|
|
|
pub mod watermark;
|
2024-01-26 16:52:37 +08:00
|
|
|
|
2024-01-26 18:14:34 +08:00
|
|
|
use std::{
|
|
|
|
|
collections::{BTreeMap, HashSet},
|
2025-03-09 16:11:52 -04:00
|
|
|
sync::{Arc, atomic::AtomicBool},
|
2024-01-26 18:14:34 +08:00
|
|
|
};
|
2024-01-26 16:52:37 +08:00
|
|
|
|
2024-01-26 18:14:34 +08:00
|
|
|
use crossbeam_skiplist::SkipMap;
|
2024-01-26 16:52:37 +08:00
|
|
|
use parking_lot::Mutex;
|
|
|
|
|
|
2024-01-26 18:14:34 +08:00
|
|
|
use crate::lsm_storage::LsmStorageInner;
|
2024-01-26 16:52:37 +08:00
|
|
|
|
2024-01-26 18:14:34 +08:00
|
|
|
use self::{txn::Transaction, watermark::Watermark};
|
|
|
|
|
|
|
|
|
|
pub(crate) struct CommittedTxnData {
|
2024-01-26 22:20:06 +08:00
|
|
|
pub(crate) key_hashes: HashSet<u32>,
|
|
|
|
|
#[allow(dead_code)]
|
2024-01-26 18:14:34 +08:00
|
|
|
pub(crate) read_ts: u64,
|
2024-01-26 22:20:06 +08:00
|
|
|
#[allow(dead_code)]
|
2024-01-26 18:14:34 +08:00
|
|
|
pub(crate) commit_ts: u64,
|
|
|
|
|
}
|
2024-01-26 16:52:37 +08:00
|
|
|
|
|
|
|
|
pub(crate) struct LsmMvccInner {
|
|
|
|
|
pub(crate) write_lock: Mutex<()>,
|
2024-01-30 14:48:03 +08:00
|
|
|
pub(crate) commit_lock: Mutex<()>,
|
2024-01-26 16:52:37 +08:00
|
|
|
pub(crate) ts: Arc<Mutex<(u64, Watermark)>>,
|
2024-01-26 18:14:34 +08:00
|
|
|
pub(crate) committed_txns: Arc<Mutex<BTreeMap<u64, CommittedTxnData>>>,
|
2024-01-26 16:52:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl LsmMvccInner {
|
|
|
|
|
pub fn new(initial_ts: u64) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
write_lock: Mutex::new(()),
|
2024-01-30 14:48:03 +08:00
|
|
|
commit_lock: Mutex::new(()),
|
2024-01-26 16:52:37 +08:00
|
|
|
ts: Arc::new(Mutex::new((initial_ts, Watermark::new()))),
|
2024-01-26 18:14:34 +08:00
|
|
|
committed_txns: Arc::new(Mutex::new(BTreeMap::new())),
|
2024-01-26 16:52:37 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn latest_commit_ts(&self) -> u64 {
|
|
|
|
|
self.ts.lock().0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn update_commit_ts(&self, ts: u64) {
|
|
|
|
|
self.ts.lock().0 = ts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// All ts (strictly) below this ts can be garbage collected.
|
|
|
|
|
pub fn watermark(&self) -> u64 {
|
|
|
|
|
let ts = self.ts.lock();
|
|
|
|
|
ts.1.watermark().unwrap_or(ts.0)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-26 18:14:34 +08:00
|
|
|
pub fn new_txn(&self, inner: Arc<LsmStorageInner>, serializable: bool) -> Arc<Transaction> {
|
2024-01-26 16:52:37 +08:00
|
|
|
let mut ts = self.ts.lock();
|
|
|
|
|
let read_ts = ts.0;
|
|
|
|
|
ts.1.add_reader(read_ts);
|
2024-01-26 18:14:34 +08:00
|
|
|
Arc::new(Transaction {
|
|
|
|
|
inner,
|
|
|
|
|
read_ts,
|
|
|
|
|
local_storage: Arc::new(SkipMap::new()),
|
|
|
|
|
committed: Arc::new(AtomicBool::new(false)),
|
|
|
|
|
key_hashes: if serializable {
|
2024-01-26 22:20:06 +08:00
|
|
|
Some(Mutex::new((HashSet::new(), HashSet::new())))
|
2024-01-26 18:14:34 +08:00
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
},
|
2024-01-26 16:52:37 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|