1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use sanakirja::{Representable, Alignment};
use std;
use super::patch_id::*;
use Hash;
use bs58;
const LINE_ID_SIZE: usize = 8;
pub const KEY_SIZE: usize = PATCH_ID_SIZE + LINE_ID_SIZE;
pub const ROOT_KEY: Key<PatchId> = Key {
patch: ROOT_PATCH_ID,
line: LineId([0; LINE_ID_SIZE]),
};
use hex::ToHex;
impl Key<PatchId> {
pub fn to_hex(&self) -> String {
let h = self.line.to_hex();
self.patch.to_hex() + &h.trim_right_matches('0')
}
}
impl Key<PatchId> {
pub fn to_base58(&self) -> String {
self.patch.to_base58() + &bs58::encode(&self.line.0).into_string()
}
}
impl Key<Hash> {
pub fn to_base58(&self) -> String {
self.patch.to_base58() + &bs58::encode(&self.line.0).into_string()
}
}
impl Key<PatchId> {
pub fn is_root(&self) -> bool {
self == &ROOT_KEY
}
pub fn from_hex(hex: &str) -> Option<Self> {
let mut s = [0; KEY_SIZE];
if super::from_hex(hex, &mut s) {
Some(unsafe { std::mem::transmute(s) })
} else {
None
}
}
}
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)]
pub struct LineId([u8; LINE_ID_SIZE]);
impl ToHex for LineId {
fn to_hex(&self) -> String {
self.0.to_hex().trim_right_matches('0').to_string()
}
}
impl std::fmt::Debug for LineId {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(fmt, "LineId(0x{})", self.to_hex())
}
}
impl LineId {
pub fn new() -> LineId {
LineId([0; LINE_ID_SIZE])
}
pub fn is_root(&self) -> bool {
self.0.iter().all(|x| *x == 0)
}
}
use byteorder::{ByteOrder, LittleEndian};
impl std::ops::Add<usize> for LineId {
type Output = LineId;
fn add(self, x: usize) -> Self::Output {
let a = LittleEndian::read_u64(&self.0);
let mut b = LineId::new();
LittleEndian::write_u64(&mut b.0, a + x as u64);
b
}
}
impl std::ops::AddAssign<usize> for LineId {
fn add_assign(&mut self, x: usize) {
*self = self.clone() + x
}
}
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)]
#[repr(packed)]
pub struct Key<H> {
pub patch: H,
pub line: LineId,
}
impl<T> AsRef<LineId> for Key<T> {
fn as_ref(&self) -> &LineId {
&self.line
}
}
impl Key<PatchId> {
#[doc(hidden)]
pub fn to_unsafe(&self) -> UnsafeKey {
UnsafeKey(self)
}
#[doc(hidden)]
pub unsafe fn from_unsafe<'a>(p: UnsafeKey) -> &'a Self {
&*p.0
}
}
impl<T: Clone> Key<Option<T>> {
pub fn unwrap_patch(&self) -> Key<T> {
Key {
patch: self.patch.as_ref().unwrap().clone(),
line: self.line.clone(),
}
}
}
#[derive(Clone, Copy)]
pub struct UnsafeKey(*const Key<PatchId>);
impl std::fmt::Debug for UnsafeKey {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
unsafe {
Key::from_unsafe(*self).fmt(fmt)
}
}
}
impl Representable for UnsafeKey {
fn alignment() -> Alignment {
Alignment::B1
}
fn onpage_size(&self) -> u16 {
let size = std::mem::size_of::<Key<PatchId>>();
debug_assert_eq!(size, PATCH_ID_SIZE + LINE_ID_SIZE);
size as u16
}
unsafe fn write_value(&self, p: *mut u8) {
trace!("write_value {:?}", p);
std::ptr::copy(self.0, p as *mut Key<PatchId>, 1)
}
unsafe fn read_value(p: *const u8) -> Self {
trace!("read_value {:?}", p);
UnsafeKey(p as *const Key<PatchId>)
}
unsafe fn cmp_value<T>(&self, _: &T, x: Self) -> std::cmp::Ordering {
let a: &Key<PatchId> = Key::from_unsafe(*self);
let b: &Key<PatchId> = Key::from_unsafe(x);
a.cmp(b)
}
type PageOffsets = std::iter::Empty<u64>;
fn page_offsets(&self) -> Self::PageOffsets { std::iter::empty() }
}