Parcourir la source

Add kvraft code structure and some common types.

Jing Yang il y a 5 ans
Parent
commit
ff464e1603
2 fichiers modifiés avec 42 ajouts et 0 suppressions
  1. 39 0
      tests/kvraft/common.rs
  2. 3 0
      tests/kvraft/mod.rs

+ 39 - 0
tests/kvraft/common.rs

@@ -0,0 +1,39 @@
+use super::rand::RngCore;
+pub use anyhow::Result;
+use std::sync::atomic::{AtomicU64, Ordering};
+
+#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
+struct UniqueId {
+    clerk_id: u64,
+    sequence_id: u64,
+}
+
+#[derive(Debug)]
+struct UniqueIdSequence {
+    clerk_id: u64,
+    sequence_id: AtomicU64,
+}
+
+impl UniqueIdSequence {
+    pub fn new() -> Self {
+        Self {
+            clerk_id: rand::thread_rng().next_u64(),
+            sequence_id: AtomicU64::new(0),
+        }
+    }
+
+    pub fn zero(&self) -> UniqueId {
+        UniqueId {
+            clerk_id: self.clerk_id,
+            sequence_id: 0,
+        }
+    }
+
+    pub fn inc(&mut self) -> UniqueId {
+        let seq = self.sequence_id.fetch_add(1, Ordering::Relaxed);
+        UniqueId {
+            clerk_id: self.clerk_id,
+            sequence_id: seq,
+        }
+    }
+}

+ 3 - 0
tests/kvraft/mod.rs

@@ -0,0 +1,3 @@
+extern crate rand;
+
+mod common;