qualia_core_db/did_qi/
git_object.rs1use sha2::{Digest, Sha256};
8
9use super::id::DidQi;
10use super::{QiError, QiStore};
11
12pub const GIT_STORE_CAP: usize = 32;
13pub const MAX_BLOB: usize = super::document::MAX_SIGNED;
14pub const MAX_RECORD: usize = MAX_BLOB;
15
16#[derive(Clone, Copy)]
17struct GitSlot {
18 did: [u8; 32],
19 object_id: [u8; 32],
20 generation: u64,
21 len: u16,
22 bytes: [u8; MAX_BLOB],
23}
24
25pub struct GitObjectStore {
26 slots: Box<[Option<GitSlot>]>,
27}
28
29impl GitObjectStore {
30 pub fn new() -> Self {
32 Self {
33 slots: vec![None; GIT_STORE_CAP].into_boxed_slice(),
34 }
35 }
36
37 pub fn current_generation(&self, id: &DidQi) -> Result<u64, QiError> {
39 match self.find_latest(id) {
40 Some((_, slot)) => Ok(slot.generation),
41 None => Err(QiError::NotFound),
42 }
43 }
44
45 pub fn is_stale(&self, id: &DidQi, generation: u64) -> Result<bool, QiError> {
47 Ok(generation < self.current_generation(id)?)
48 }
49
50 pub fn object_id_of(&self, id: &DidQi) -> Result<[u8; 32], QiError> {
52 match self.find_latest(id) {
53 Some((_, slot)) => Ok(slot.object_id),
54 None => Err(QiError::NotFound),
55 }
56 }
57
58 fn find_latest(&self, id: &DidQi) -> Option<(usize, GitSlot)> {
59 let mut best: Option<(usize, GitSlot)> = None;
60 let mut i = 0;
61 while i < GIT_STORE_CAP {
62 if let Some(slot) = self.slots[i] {
63 if slot.did == id.0 {
64 match best {
65 None => best = Some((i, slot)),
66 Some((_, b)) if slot.generation >= b.generation => best = Some((i, slot)),
67 _ => {}
68 }
69 }
70 }
71 i += 1;
72 }
73 best
74 }
75
76 fn find_slot(&self, id: &DidQi, generation: u64) -> Option<usize> {
77 let mut i = 0;
78 while i < GIT_STORE_CAP {
79 if let Some(slot) = self.slots[i] {
80 if slot.did == id.0 && slot.generation == generation {
81 return Some(i);
82 }
83 }
84 i += 1;
85 }
86 None
87 }
88
89 fn free_slot(&self) -> Option<usize> {
90 let mut i = 0;
91 while i < GIT_STORE_CAP {
92 if self.slots[i].is_none() {
93 return Some(i);
94 }
95 i += 1;
96 }
97 None
98 }
99}
100
101impl Default for GitObjectStore {
102 fn default() -> Self {
103 Self::new()
104 }
105}
106
107impl QiStore for GitObjectStore {
108 fn put(&mut self, id: &DidQi, generation: u64, payload: &[u8]) -> Result<(), QiError> {
109 if payload.len() > MAX_BLOB {
110 return Err(QiError::CanonicalTooLarge);
111 }
112 let mut bytes = [0u8; MAX_BLOB];
113 bytes[..payload.len()].copy_from_slice(payload);
114 let slot = GitSlot {
115 did: id.0,
116 object_id: blob_object_id(payload),
117 generation,
118 len: payload.len() as u16,
119 bytes,
120 };
121 if let Some(i) = self.find_slot(id, generation) {
122 self.slots[i] = Some(slot);
123 return Ok(());
124 }
125 let i = self.free_slot().ok_or(QiError::StoreFull)?;
126 self.slots[i] = Some(slot);
127 Ok(())
128 }
129
130 fn get(&self, id: &DidQi, out: &mut [u8]) -> Result<(u64, usize), QiError> {
131 let (_, slot) = self.find_latest(id).ok_or(QiError::NotFound)?;
132 let n = slot.len as usize;
133 if out.len() < n {
134 return Err(QiError::BufferTooSmall);
135 }
136 out[..n].copy_from_slice(&slot.bytes[..n]);
137 Ok((slot.generation, n))
138 }
139}
140
141pub fn write_decimal(n: usize, out: &mut [u8]) -> Result<usize, QiError> {
143 if n == 0 {
144 if out.is_empty() {
145 return Err(QiError::BufferTooSmall);
146 }
147 out[0] = b'0';
148 return Ok(1);
149 }
150 let mut tmp = [0u8; 20];
151 let mut x = n;
152 let mut i = 20;
153 while x > 0 {
154 i -= 1;
155 tmp[i] = b'0' + (x % 10) as u8;
156 x /= 10;
157 }
158 let len = 20 - i;
159 if out.len() < len {
160 return Err(QiError::BufferTooSmall);
161 }
162 out[..len].copy_from_slice(&tmp[i..]);
163 Ok(len)
164}
165
166