history table refactoring

This commit is contained in:
Alex 2023-01-09 14:53:27 +01:00
parent b337895fce
commit 32715d462e
Signed by untrusted user: lx
GPG key ID: 0E496D15096376BE
6 changed files with 23 additions and 23 deletions

View file

@ -27,7 +27,7 @@ use crate::index_counter::*;
use crate::key_table::*; use crate::key_table::*;
#[cfg(feature = "k2v")] #[cfg(feature = "k2v")]
use crate::k2v::{history_table::*, item_table::*, poll::*, rpc::*}; use crate::k2v::{history_table::*, item_table::*, sub::*, rpc::*};
/// An entire Garage full of data /// An entire Garage full of data
pub struct Garage { pub struct Garage {

View file

@ -5,18 +5,21 @@ use garage_db as db;
use garage_table::crdt::*; use garage_table::crdt::*;
use garage_table::*; use garage_table::*;
use crate::k2v::poll::*; use crate::k2v::sub::*;
mod v08 { mod v08 {
use crate::k2v::causality::K2VNodeId; use crate::k2v::causality::K2VNodeId;
pub use crate::k2v::item_table::v08::{DvvsValue, K2VItemPartition}; pub use crate::k2v::item_table::v08::{DvvsValue, K2VItem, K2VItemPartition};
use garage_util::crdt; use garage_util::crdt;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] #[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
pub struct K2VHistoryEntry { pub struct K2VHistoryEntry {
/// Partition key: a K2V partition // Partition key: the partition key of ins_item
pub partition: K2VItemPartition,
/// The inserted item
pub ins_item: K2VItem,
/// Sort key: the node ID and its local counter /// Sort key: the node ID and its local counter
pub node_counter: K2VHistorySortKey, pub node_counter: K2VHistorySortKey,
@ -25,12 +28,8 @@ mod v08 {
/// The timesamp of the update (!= counter, counters are incremented /// The timesamp of the update (!= counter, counters are incremented
/// by one, timestamps are real clock timestamps) /// by one, timestamps are real clock timestamps)
pub timestamp: u64, pub timestamp: u64,
/// The sort key of the item that was inserted
pub ins_sort_key: String,
/// The inserted value
pub ins_value: DvvsValue,
/// Whether this history entry is too old and should be deleted /// Mark this history entry for deletion
pub deleted: crdt::Bool, pub deleted: crdt::Bool,
} }
@ -49,6 +48,7 @@ pub use v08::*;
impl Crdt for K2VHistoryEntry { impl Crdt for K2VHistoryEntry {
fn merge(&mut self, other: &Self) { fn merge(&mut self, other: &Self) {
self.ins_item.merge(&other.ins_item);
self.deleted.merge(&other.deleted); self.deleted.merge(&other.deleted);
} }
} }
@ -66,7 +66,7 @@ impl SortKey for K2VHistorySortKey {
impl Entry<K2VItemPartition, K2VHistorySortKey> for K2VHistoryEntry { impl Entry<K2VItemPartition, K2VHistorySortKey> for K2VHistoryEntry {
fn partition_key(&self) -> &K2VItemPartition { fn partition_key(&self) -> &K2VItemPartition {
&self.partition &self.ins_item.partition
} }
fn sort_key(&self) -> &K2VHistorySortKey { fn sort_key(&self) -> &K2VHistorySortKey {
&self.node_counter &self.node_counter

View file

@ -11,7 +11,7 @@ use garage_table::*;
use crate::index_counter::*; use crate::index_counter::*;
use crate::k2v::causality::*; use crate::k2v::causality::*;
use crate::k2v::poll::*; use crate::k2v::sub::*;
pub const ENTRIES: &str = "entries"; pub const ENTRIES: &str = "entries";
pub const CONFLICTS: &str = "conflicts"; pub const CONFLICTS: &str = "conflicts";

View file

@ -3,5 +3,6 @@ pub mod causality;
pub mod history_table; pub mod history_table;
pub mod item_table; pub mod item_table;
pub mod poll; pub(crate) mod sub;
pub mod rpc; pub mod rpc;

View file

@ -32,7 +32,7 @@ use garage_table::{PartitionKey, Table};
use crate::k2v::causality::*; use crate::k2v::causality::*;
use crate::k2v::history_table::*; use crate::k2v::history_table::*;
use crate::k2v::item_table::*; use crate::k2v::item_table::*;
use crate::k2v::poll::*; use crate::k2v::sub::*;
/// RPC messages for K2V /// RPC messages for K2V
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@ -292,8 +292,9 @@ impl K2VRpcHandler {
self.item_table self.item_table
.data .data
.update_entry_with(&item.partition, &item.sort_key, |tx, ent| { .update_entry_with(&item.partition, &item.sort_key, |tx, ent| {
let local_counter_key = item.partition.hash();
let old_local_counter = tx let old_local_counter = tx
.get(&self.local_counter_tree, b"counter")? .get(&self.local_counter_tree, &local_counter_key)?
.and_then(|x| x.try_into().ok()) .and_then(|x| x.try_into().ok())
.map(u64::from_be_bytes) .map(u64::from_be_bytes)
.unwrap_or_default(); .unwrap_or_default();
@ -314,20 +315,18 @@ impl K2VRpcHandler {
tx.insert( tx.insert(
&self.local_counter_tree, &self.local_counter_tree,
b"counter", &local_counter_key,
u64::to_be_bytes(new_local_counter), u64::to_be_bytes(new_local_counter),
)?; )?;
let hist_entry = K2VHistoryEntry { let hist_entry = K2VHistoryEntry {
partition: ent.partition.clone(), ins_item: ent.clone(),
node_counter: K2VHistorySortKey { node_counter: K2VHistorySortKey {
node: make_node_id(self.system.id), node: make_node_id(self.system.id),
counter: new_local_counter, counter: new_local_counter,
}, },
prev_counter: old_local_counter, prev_counter: old_local_counter,
timestamp: now, timestamp: now,
ins_sort_key: item.sort_key.clone(),
ins_value: item.value.clone(),
deleted: false.into(), deleted: false.into(),
}; };
self.history_table.queue_insert(tx, &hist_entry)?; self.history_table.queue_insert(tx, &hist_entry)?;

View file

@ -95,21 +95,21 @@ impl SubscriptionManager {
impl PollRange { impl PollRange {
fn matches(&self, entry: &K2VHistoryEntry) -> bool { fn matches(&self, entry: &K2VHistoryEntry) -> bool {
entry.partition == self.partition entry.ins_item.partition == self.partition
&& self && self
.prefix .prefix
.as_ref() .as_ref()
.map(|x| entry.ins_sort_key.starts_with(x)) .map(|x| entry.ins_item.sort_key.starts_with(x))
.unwrap_or(true) .unwrap_or(true)
&& self && self
.start .start
.as_ref() .as_ref()
.map(|x| entry.ins_sort_key >= *x) .map(|x| entry.ins_item.sort_key >= *x)
.unwrap_or(true) .unwrap_or(true)
&& self && self
.end .end
.as_ref() .as_ref()
.map(|x| entry.ins_sort_key < *x) .map(|x| entry.ins_item.sort_key < *x)
.unwrap_or(true) .unwrap_or(true)
} }
} }