ok/jj
1
0
Fork 0
forked from mirrors/jj

index, stacked_table: use u32::to_le_bytes() to reinterpret as bytes

This commit is contained in:
Yuya Nishihara 2023-12-18 17:46:06 +09:00
parent 392539fa29
commit 9ec89bcf86
2 changed files with 20 additions and 27 deletions

View file

@ -24,7 +24,6 @@ use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use blake2::Blake2b512; use blake2::Blake2b512;
use byteorder::{LittleEndian, WriteBytesExt};
use digest::Digest; use digest::Digest;
use itertools::Itertools; use itertools::Itertools;
use smallvec::SmallVec; use smallvec::SmallVec;
@ -172,11 +171,10 @@ impl MutableIndexSegment {
fn serialize_parent_filename(&self, buf: &mut Vec<u8>) { fn serialize_parent_filename(&self, buf: &mut Vec<u8>) {
if let Some(parent_file) = &self.parent_file { if let Some(parent_file) = &self.parent_file {
buf.write_u32::<LittleEndian>(parent_file.name().len() as u32) buf.extend((parent_file.name().len() as u32).to_le_bytes());
.unwrap();
buf.extend_from_slice(parent_file.name().as_bytes()); buf.extend_from_slice(parent_file.name().as_bytes());
} else { } else {
buf.write_u32::<LittleEndian>(0).unwrap(); buf.extend(0_u32.to_le_bytes());
} }
} }
@ -184,21 +182,19 @@ impl MutableIndexSegment {
assert_eq!(self.graph.len(), self.lookup.len()); assert_eq!(self.graph.len(), self.lookup.len());
let num_commits = self.graph.len() as u32; let num_commits = self.graph.len() as u32;
buf.write_u32::<LittleEndian>(num_commits).unwrap(); buf.extend(num_commits.to_le_bytes());
// We'll write the actual value later // We'll write the actual value later
let parent_overflow_offset = buf.len(); let parent_overflow_offset = buf.len();
buf.write_u32::<LittleEndian>(0_u32).unwrap(); buf.extend(0_u32.to_le_bytes());
let mut parent_overflow = vec![]; let mut parent_overflow = vec![];
for entry in &self.graph { for entry in &self.graph {
let flags = 0; let flags = 0_u32;
buf.write_u32::<LittleEndian>(flags).unwrap(); buf.extend(flags.to_le_bytes());
buf.write_u32::<LittleEndian>(entry.generation_number) buf.extend(entry.generation_number.to_le_bytes());
.unwrap();
buf.write_u32::<LittleEndian>(entry.parent_positions.len() as u32) buf.extend((entry.parent_positions.len() as u32).to_le_bytes());
.unwrap();
let mut parent1_pos = IndexPosition(0); let mut parent1_pos = IndexPosition(0);
let parent_overflow_pos = parent_overflow.len() as u32; let parent_overflow_pos = parent_overflow.len() as u32;
for (i, parent_pos) in entry.parent_positions.iter().enumerate() { for (i, parent_pos) in entry.parent_positions.iter().enumerate() {
@ -208,8 +204,8 @@ impl MutableIndexSegment {
parent_overflow.push(*parent_pos); parent_overflow.push(*parent_pos);
} }
} }
buf.write_u32::<LittleEndian>(parent1_pos.0).unwrap(); buf.extend(parent1_pos.0.to_le_bytes());
buf.write_u32::<LittleEndian>(parent_overflow_pos).unwrap(); buf.extend(parent_overflow_pos.to_le_bytes());
assert_eq!(entry.change_id.as_bytes().len(), self.change_id_length); assert_eq!(entry.change_id.as_bytes().len(), self.change_id_length);
buf.extend_from_slice(entry.change_id.as_bytes()); buf.extend_from_slice(entry.change_id.as_bytes());
@ -220,14 +216,13 @@ impl MutableIndexSegment {
for (commit_id, pos) in &self.lookup { for (commit_id, pos) in &self.lookup {
buf.extend_from_slice(commit_id.as_bytes()); buf.extend_from_slice(commit_id.as_bytes());
buf.write_u32::<LittleEndian>(pos.0).unwrap(); buf.extend(pos.0.to_le_bytes());
} }
(&mut buf[parent_overflow_offset..][..4]) buf[parent_overflow_offset..][..4]
.write_u32::<LittleEndian>(parent_overflow.len() as u32) .copy_from_slice(&(parent_overflow.len() as u32).to_le_bytes());
.unwrap();
for parent_pos in parent_overflow { for parent_pos in parent_overflow {
buf.write_u32::<LittleEndian>(parent_pos.0).unwrap(); buf.extend(parent_pos.0.to_le_bytes());
} }
} }

View file

@ -29,7 +29,7 @@ use std::path::PathBuf;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use blake2::{Blake2b512, Digest}; use blake2::{Blake2b512, Digest};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt};
use tempfile::NamedTempFile; use tempfile::NamedTempFile;
use thiserror::Error; use thiserror::Error;
@ -256,20 +256,18 @@ impl MutableTable {
let mut buf = vec![]; let mut buf = vec![];
if let Some(parent_file) = &self.parent_file { if let Some(parent_file) = &self.parent_file {
buf.write_u32::<LittleEndian>(parent_file.name.len() as u32) buf.extend((parent_file.name.len() as u32).to_le_bytes());
.unwrap();
buf.extend_from_slice(parent_file.name.as_bytes()); buf.extend_from_slice(parent_file.name.as_bytes());
} else { } else {
buf.write_u32::<LittleEndian>(0).unwrap(); buf.extend(0_u32.to_le_bytes());
} }
buf.write_u32::<LittleEndian>(self.entries.len() as u32) buf.extend((self.entries.len() as u32).to_le_bytes());
.unwrap();
let mut value_offset = 0; let mut value_offset = 0_u32;
for (key, value) in &self.entries { for (key, value) in &self.entries {
buf.extend_from_slice(key); buf.extend_from_slice(key);
buf.write_u32::<LittleEndian>(value_offset).unwrap(); buf.extend(value_offset.to_le_bytes());
value_offset += value.len() as u32; value_offset += value.len() as u32;
} }
for value in self.entries.values() { for value in self.entries.values() {