Git fork

feat: convert reftable_basics.rs, reftable_record.rs

a.starrysky.fyi 48861d4b e3f08e62

verified
+174 -9
+6
reftable/record.c
··· 80 81 const unsigned char *reftable_ref_record_val1(const struct reftable_ref_record *rec) 82 { 83 switch (rec->value_type) { 84 case REFTABLE_REF_VAL1: 85 return rec->value.val1; ··· 92 93 const unsigned char *reftable_ref_record_val2(const struct reftable_ref_record *rec) 94 { 95 switch (rec->value_type) { 96 case REFTABLE_REF_VAL2: 97 return rec->value.val2.target_value; ··· 997 int reftable_log_record_equal(const struct reftable_log_record *a, 998 const struct reftable_log_record *b, uint32_t hash_size) 999 { 1000 if (!(null_streq(a->refname, b->refname) && 1001 a->update_index == b->update_index && 1002 a->value_type == b->value_type)) ··· 1210 1211 static int hash_equal(const unsigned char *a, const unsigned char *b, uint32_t hash_size) 1212 { 1213 if (a && b) 1214 return !memcmp(a, b, hash_size); 1215 ··· 1219 int reftable_ref_record_equal(const struct reftable_ref_record *a, 1220 const struct reftable_ref_record *b, uint32_t hash_size) 1221 { 1222 if (!null_streq(a->refname, b->refname)) 1223 return 0; 1224 ··· 1269 1270 int reftable_log_record_is_deletion(const struct reftable_log_record *log) 1271 { 1272 return (log->value_type == REFTABLE_LOG_DELETION); 1273 } 1274
··· 80 81 const unsigned char *reftable_ref_record_val1(const struct reftable_ref_record *rec) 82 { 83 + // Implemented in reftable_record.rs 84 switch (rec->value_type) { 85 case REFTABLE_REF_VAL1: 86 return rec->value.val1; ··· 93 94 const unsigned char *reftable_ref_record_val2(const struct reftable_ref_record *rec) 95 { 96 + // Implemented in reftable_record.rs 97 switch (rec->value_type) { 98 case REFTABLE_REF_VAL2: 99 return rec->value.val2.target_value; ··· 999 int reftable_log_record_equal(const struct reftable_log_record *a, 1000 const struct reftable_log_record *b, uint32_t hash_size) 1001 { 1002 + // Implemented in reftable_record.rs 1003 if (!(null_streq(a->refname, b->refname) && 1004 a->update_index == b->update_index && 1005 a->value_type == b->value_type)) ··· 1213 1214 static int hash_equal(const unsigned char *a, const unsigned char *b, uint32_t hash_size) 1215 { 1216 + // Implemented in reftable_basics.rs 1217 if (a && b) 1218 return !memcmp(a, b, hash_size); 1219 ··· 1223 int reftable_ref_record_equal(const struct reftable_ref_record *a, 1224 const struct reftable_ref_record *b, uint32_t hash_size) 1225 { 1226 + // Implemented in reftable_record.rs 1227 if (!null_streq(a->refname, b->refname)) 1228 return 0; 1229 ··· 1274 1275 int reftable_log_record_is_deletion(const struct reftable_log_record *log) 1276 { 1277 + // Implemented in reftable_record.rs 1278 return (log->value_type == REFTABLE_LOG_DELETION); 1279 } 1280
+7 -9
src/reftable/iter.rs
··· 6 reftable_table::ReftableTable, 7 }; 8 9 - pub type Void = (); 10 - 11 - pub trait ReftableIteratorVtable { 12 - fn seek(&self, iter_arg: &Void, want: &ReftableRecord) -> i16; 13 - fn next(&self, iter_arg: &Void, rec: &ReftableRecord) -> i16; 14 - fn close(&self, iter_arg: &Void); 15 } 16 17 - pub fn iterator_seek(it: &ReftableIterator, want: &ReftableRecord) -> i16 { 18 todo!(); 19 } 20 21 - pub fn iterator_next(it: &ReftableIterator, rec: &ReftableRecord) -> i16 { 22 todo!(); 23 } 24 25 - pub fn iterator_set_empty(it: &ReftableIterator) { 26 todo!(); 27 } 28
··· 6 reftable_table::ReftableTable, 7 }; 8 9 + pub trait ReftableIteratorVtable<T> { 10 + fn seek(&self, iter_arg: &T, want: &ReftableRecord) -> i16; 11 + fn next(&self, iter_arg: &T, rec: &ReftableRecord) -> i16; 12 + fn close(&self, iter_arg: &T); 13 } 14 15 + pub fn iterator_seek<T>(it: &ReftableIterator<T>, want: &ReftableRecord) -> i16 { 16 todo!(); 17 } 18 19 + pub fn iterator_next<T>(it: &ReftableIterator<T>, rec: &ReftableRecord) -> i16 { 20 todo!(); 21 } 22 23 + pub fn iterator_set_empty<T>(it: &ReftableIterator<T>) { 24 todo!(); 25 } 26
+2
src/reftable/mod.rs
··· 7 pub mod merged; 8 pub mod pq; 9 pub mod record; 10 pub mod reftable_iterator; 11 pub mod reftable_table; 12 pub mod stack; 13 pub mod system;
··· 7 pub mod merged; 8 pub mod pq; 9 pub mod record; 10 + pub mod reftable_basics; 11 pub mod reftable_iterator; 12 + pub mod reftable_record; 13 pub mod reftable_table; 14 pub mod stack; 15 pub mod system;
+41
src/reftable/reftable_basics.rs
···
··· 1 + use std::ffi::{c_char, c_uchar}; 2 + 3 + /* A buffer that contains arbitrary byte slices. */ 4 + type ReftableBuf = Vec<c_char>; 5 + 6 + /* 7 + * Hash functions understood by the reftable library. Note that the values are 8 + * arbitrary and somewhat random such that we can easily detect cases where the 9 + * hash hasn't been properly set up. 10 + */ 11 + enum ReftableHash { 12 + ReftableHashSha1 = 89, 13 + ReftableHashSha256 = 247, 14 + } 15 + 16 + const REFTABLE_HASH_SIZE_SHA1: usize = 20; 17 + const REFTABLE_HASH_SIZE_SHA256: usize = 32; 18 + const REFTABLE_HASH_SIZE_MAX: usize = REFTABLE_HASH_SIZE_SHA256; 19 + 20 + pub struct ReftableWrappedHash { 21 + value: [c_uchar; REFTABLE_HASH_SIZE_MAX], 22 + } // Wrapper created so we can implement things like PartialEq on this... 23 + 24 + impl PartialEq<Self> for ReftableWrappedHash { 25 + fn eq(&self, other: &Self) -> bool { 26 + self.value.len() == other.value.len() 27 + && self 28 + .value 29 + .iter() 30 + .zip(other.value.iter()) 31 + .all(|(self_byte, other_byte)| self_byte == other_byte) 32 + } 33 + } 34 + 35 + impl From<[c_uchar; REFTABLE_HASH_SIZE_MAX]> for ReftableWrappedHash { 36 + fn from(value: [c_uchar; REFTABLE_HASH_SIZE_MAX]) -> Self { 37 + ReftableWrappedHash { value } 38 + } 39 + } 40 + 41 + // FIXME: removed reftable_set_alloc ... used only for C memory management
+118
src/reftable/reftable_record.rs
···
··· 1 + use crate::reftable::reftable_basics::ReftableWrappedHash; 2 + use std::ffi::CString; 3 + 4 + struct ReftableRefTagAndHash { 5 + value: ReftableWrappedHash, 6 + target_value: ReftableWrappedHash, 7 + } 8 + 9 + enum ReftableRefRecordValue { 10 + Deletion, // Tombstone to hide deletions from earlier tables 11 + Simple(ReftableWrappedHash), // A simple ref 12 + TagAndHash(ReftableRefTagAndHash), // A tag, plus its peeled hash 13 + Symref(CString), // A symbolic reference 14 + } 15 + 16 + impl PartialEq<Self> for ReftableRefRecordValue { 17 + fn eq(&self, other: &Self) -> bool { 18 + match (self, other) { 19 + (Self::Deletion, Self::Deletion) => true, 20 + (Self::Simple(self_hash), Self::Simple(other_hash)) => self_hash == other_hash, 21 + (Self::TagAndHash(self_value), Self::TagAndHash(other_value)) => { 22 + self_value.value == other_value.value 23 + && self_value.target_value == other_value.target_value 24 + } 25 + (Self::Symref(self_ref), Self::Symref(other_ref)) => self_ref == other_ref, 26 + _ => false, 27 + } 28 + } 29 + } 30 + 31 + struct ReftableRefRecord { 32 + refname: CString, // Name of the ref 33 + update_index: u64, // Logical timestamp at which this value is written 34 + value: ReftableRefRecordValue, 35 + } 36 + 37 + impl ReftableRefRecord { 38 + fn val1(&self) -> Option<&ReftableWrappedHash> { 39 + match &self.value { 40 + ReftableRefRecordValue::Simple(val1) => Some(val1), 41 + ReftableRefRecordValue::TagAndHash(union) => Some(&union.value), 42 + _ => None, 43 + } 44 + } 45 + fn val2(&self) -> Option<&ReftableWrappedHash> { 46 + match &self.value { 47 + ReftableRefRecordValue::TagAndHash(union) => Some(&union.target_value), 48 + _ => None, 49 + } 50 + } 51 + fn is_deletion() -> bool { 52 + todo!() 53 + } 54 + } 55 + 56 + impl PartialEq<Self> for ReftableRefRecord { 57 + fn eq(&self, other: &Self) -> bool { 58 + self.refname == other.refname 59 + && self.update_index == other.update_index 60 + && self.value == other.value 61 + } 62 + } 63 + 64 + struct ReftableLogUpdate { 65 + new_hash: ReftableWrappedHash, 66 + old_hash: ReftableWrappedHash, 67 + name: CString, 68 + email: CString, 69 + time: u64, 70 + tz_offset: i16, 71 + message: CString, 72 + } 73 + 74 + enum ReftableLogRecordValue { 75 + Deletion, 76 + Update(ReftableLogUpdate), 77 + } 78 + 79 + impl PartialEq<Self> for ReftableLogRecordValue { 80 + fn eq(&self, other: &Self) -> bool { 81 + match (self, other) { 82 + (Self::Deletion, Self::Deletion) => true, 83 + (Self::Update(self_update), Self::Update(other_update)) => { 84 + self_update.name == other_update.name 85 + && self_update.time == other_update.time 86 + && self_update.tz_offset == other_update.tz_offset 87 + && self_update.email == other_update.email 88 + && self_update.message == other_update.message 89 + && self_update.old_hash == other_update.old_hash 90 + && self_update.new_hash == other_update.new_hash 91 + } 92 + _ => false, 93 + } 94 + } 95 + } 96 + 97 + struct ReftableLogRecord { 98 + refname: CString, 99 + update_index: u64, 100 + value: ReftableLogRecordValue, 101 + } 102 + 103 + impl ReftableLogRecord { 104 + fn is_deletion(&self) -> bool { 105 + match self.value { 106 + ReftableLogRecordValue::Deletion => true, 107 + ReftableLogRecordValue::Update(_) => false, 108 + } 109 + } 110 + } 111 + 112 + impl PartialEq<Self> for ReftableLogRecord { 113 + fn eq(&self, other: &Self) -> bool { 114 + return self.refname == other.refname 115 + && self.update_index == other.update_index 116 + && self.value == other.value; 117 + } 118 + }