Serialize WAL entry to binary format Binary Format: - Magic (4 bytes): WAL_MAGIC - Entry Type (1 byte): WALEntryType - Transaction ID (8 bytes): u64 - Global Sequence (8 bytes): u64 - Txn Sequence (8 bytes): u64 - Timestamp (8 bytes): u64 (nanos since UNIX_EPOCH) - Operation Type (1 byte): Option (255 = None) - Description Length (4 bytes): u32 - Description (variable): UTF-8 bytes
(&self)
| 132 | /// - Description (variable): UTF-8 bytes |
| 133 | /// - Checksum (4 bytes): CRC32 |
| 134 | pub fn serialize(&self) -> Vec<u8> { |
| 135 | let mut buffer = Vec::with_capacity(256); |
| 136 | |
| 137 | // Magic number |
| 138 | buffer.extend_from_slice(&WAL_MAGIC.to_le_bytes()); |
| 139 | |
| 140 | // Entry type |
| 141 | buffer.push(self.entry_type as u8); |
| 142 | |
| 143 | // Transaction ID |
| 144 | buffer.extend_from_slice(&self.transaction_id.id().to_le_bytes()); |
| 145 | |
| 146 | // Global sequence |
| 147 | buffer.extend_from_slice(&self.global_sequence.to_le_bytes()); |
| 148 | |
| 149 | // Transaction sequence |
| 150 | buffer.extend_from_slice(&self.txn_sequence.to_le_bytes()); |
| 151 | |
| 152 | // Timestamp |
| 153 | let timestamp_nanos = self |
| 154 | .timestamp |
| 155 | .duration_since(UNIX_EPOCH) |
| 156 | .unwrap_or_default() |
| 157 | .as_nanos() as u64; |
| 158 | buffer.extend_from_slice(×tamp_nanos.to_le_bytes()); |
| 159 | |
| 160 | // Operation type (255 = None) |
| 161 | let op_type_byte = match &self.operation_type { |
| 162 | // Read operations |
| 163 | Some(OperationType::Select) => 0, |
| 164 | Some(OperationType::Match) => 1, |
| 165 | // Write operations |
| 166 | Some(OperationType::Insert) => 10, |
| 167 | Some(OperationType::Update) => 11, |
| 168 | Some(OperationType::Set) => 12, |
| 169 | Some(OperationType::Delete) => 13, |
| 170 | Some(OperationType::Remove) => 14, |
| 171 | // Schema operations |
| 172 | Some(OperationType::CreateTable) => 20, |
| 173 | Some(OperationType::CreateGraph) => 21, |
| 174 | Some(OperationType::AlterTable) => 22, |
| 175 | Some(OperationType::DropTable) => 23, |
| 176 | Some(OperationType::DropGraph) => 24, |
| 177 | // Security operations |
| 178 | Some(OperationType::CreateUser) => 25, |
| 179 | Some(OperationType::DropUser) => 26, |
| 180 | Some(OperationType::CreateRole) => 27, |
| 181 | Some(OperationType::DropRole) => 28, |
| 182 | Some(OperationType::GrantRole) => 29, |
| 183 | Some(OperationType::RevokeRole) => 30, |
| 184 | // Transaction control |
| 185 | Some(OperationType::Begin) => 31, |
| 186 | Some(OperationType::Commit) => 32, |
| 187 | Some(OperationType::Rollback) => 33, |
| 188 | // Other |
| 189 | Some(OperationType::Other) => 99, |
| 190 | None => 255, |
| 191 | }; |
no test coverage detected