Skip to content

Commit

Permalink
Make TransactionId.0 private
Browse files Browse the repository at this point in the history
  • Loading branch information
cberner committed Nov 9, 2023
1 parent 7e5354d commit cdb1d48
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ struct AtomicTransactionId {
impl AtomicTransactionId {
fn new(last_id: TransactionId) -> Self {
Self {
inner: AtomicU64::new(last_id.0),
inner: AtomicU64::new(last_id.raw_id()),
}
}

fn next(&self) -> TransactionId {
let id = self.inner.fetch_add(1, Ordering::AcqRel);
TransactionId(id)
TransactionId::new(id)
}
}

Expand Down Expand Up @@ -483,7 +483,7 @@ impl Database {
mem,
)?;
let lookup_key = FreedTableKey {
transaction_id: oldest_unprocessed_free_transaction.0,
transaction_id: oldest_unprocessed_free_transaction.raw_id(),
pagination_id: 0,
};
for result in freed_table.range::<FreedTableKey>(lookup_key..)? {
Expand Down Expand Up @@ -609,7 +609,7 @@ impl Database {

let freed_root = mem.get_freed_root();
// Allow processing of all transactions, since this is the main freed tree
Self::mark_freed_tree(freed_root, mem, TransactionId(0))?;
Self::mark_freed_tree(freed_root, mem, TransactionId::new(0))?;
let freed_table: ReadOnlyTable<FreedTableKey, FreedPageList<'static>> = ReadOnlyTable::new(
"internal freed table".to_string(),
freed_root,
Expand All @@ -620,7 +620,7 @@ impl Database {
// Make sure we don't reprocess those frees, as that would result in a double-free
let oldest_unprocessed_transaction =
if let Some(entry) = freed_table.range::<FreedTableKey>(..)?.next() {
TransactionId(entry?.0.value().transaction_id)
TransactionId::new(entry?.0.value().transaction_id)
} else {
mem.get_last_committed_transaction_id()?
};
Expand Down
10 changes: 9 additions & 1 deletion src/transaction_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ use std::collections::btree_set::BTreeSet;
use std::mem::size_of;

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
pub(crate) struct TransactionId(pub u64);
pub(crate) struct TransactionId(u64);

impl TransactionId {
pub(crate) fn new(value: u64) -> TransactionId {
Self(value)
}

pub(crate) fn raw_id(&self) -> u64 {
self.0
}

pub(crate) fn next(&self) -> TransactionId {
TransactionId(self.0 + 1)
}
Expand Down
6 changes: 3 additions & 3 deletions src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ impl<'db> WriteTransaction<'db> {
{
entry?.key().transaction_id
} else {
self.transaction_id.0
self.transaction_id.raw_id()
};

let mut freed_tree = BtreeMut::new(
Expand Down Expand Up @@ -1018,7 +1018,7 @@ impl<'db> WriteTransaction<'db> {
// We assume below that PageNumber is length 8
assert_eq!(PageNumber::serialized_size(), 8);
let lookup_key = FreedTableKey {
transaction_id: oldest_live_read.0,
transaction_id: oldest_live_read.raw_id(),
pagination_id: 0,
};

Expand Down Expand Up @@ -1058,7 +1058,7 @@ impl<'db> WriteTransaction<'db> {
let chunk_size = 100;
let buffer_size = FreedPageList::required_bytes(chunk_size);
let key = FreedTableKey {
transaction_id: self.transaction_id.0,
transaction_id: self.transaction_id.raw_id(),
pagination_id: pagination_counter,
};
let mut access_guard =
Expand Down
4 changes: 2 additions & 2 deletions src/tree_store/page_store/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ impl TransactionHeader {
} else {
None
};
let transaction_id = TransactionId(get_u64(&data[TRANSACTION_ID_OFFSET..]));
let transaction_id = TransactionId::new(get_u64(&data[TRANSACTION_ID_OFFSET..]));

let result = Self {
version,
Expand Down Expand Up @@ -394,7 +394,7 @@ impl TransactionHeader {
.copy_from_slice(&checksum.to_le_bytes());
}
result[TRANSACTION_ID_OFFSET..(TRANSACTION_ID_OFFSET + size_of::<u64>())]
.copy_from_slice(&self.transaction_id.0.to_le_bytes());
.copy_from_slice(&self.transaction_id.raw_id().to_le_bytes());
let checksum = xxh3_checksum(&result[..SLOT_CHECKSUM_OFFSET]);
result[SLOT_CHECKSUM_OFFSET..(SLOT_CHECKSUM_OFFSET + size_of::<Checksum>())]
.copy_from_slice(&checksum.to_le_bytes());
Expand Down
2 changes: 1 addition & 1 deletion src/tree_store/page_store/page_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl TransactionalMemory {
PageNumber::new(0, page_number, required_order)
};

let mut header = DatabaseHeader::new(layout, TransactionId(0), tracker_page);
let mut header = DatabaseHeader::new(layout, TransactionId::new(0), tracker_page);

header.recovery_required = false;
storage
Expand Down
4 changes: 2 additions & 2 deletions src/tree_store/page_store/savepoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl<'a> SerializedSavepoint<'a> {
pub(crate) fn from_savepoint(savepoint: &Savepoint) -> Self {
let mut result = vec![savepoint.version];
result.extend(savepoint.id.0.to_le_bytes());
result.extend(savepoint.transaction_id.0.to_le_bytes());
result.extend(savepoint.transaction_id.raw_id().to_le_bytes());

if let Some((root, checksum)) = savepoint.user_root {
result.push(1);
Expand Down Expand Up @@ -291,7 +291,7 @@ impl<'a> SerializedSavepoint<'a> {
Savepoint {
version,
id: SavepointId(id),
transaction_id: TransactionId(transaction_id),
transaction_id: TransactionId::new(transaction_id),
user_root,
system_root,
freed_root,
Expand Down

0 comments on commit cdb1d48

Please sign in to comment.