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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use std::io::{Read, Seek, SeekFrom, Write};
use std::sync::Arc;
use crate::data_types::{zstd_encoder, CompressionType, Loc};
pub struct Ids {
location: u64,
block_index_pos: u64,
block_locations: Option<Vec<u64>>,
block_size: usize,
pub data: Option<Vec<u8>>, pub compression_type: CompressionType,
cache: Option<(u32, Vec<u8>)>,
}
impl Default for Ids {
fn default() -> Self {
Ids {
location: 0,
block_index_pos: 0,
block_locations: None,
block_size: 2 * 1024 * 1024,
data: None,
compression_type: CompressionType::ZSTD,
cache: None,
}
}
}
impl Ids {
pub fn add_id(&mut self, id: Arc<String>) -> Vec<Loc> {
if self.data.is_none() {
self.data = Some(Vec::with_capacity(self.block_size));
}
let data = self.data.as_mut().unwrap();
let mut start = data.len();
data.extend(id.as_bytes());
let end = data.len() - 1;
let starting_block = start / self.block_size;
let ending_block = end / self.block_size;
let mut locs = Vec::new();
for block in starting_block..=ending_block {
let block_start = start % self.block_size;
let block_end = if block == ending_block {
end % self.block_size
} else {
self.block_size - 1
};
start = block_end + 1;
locs.push(Loc::Loc(block as u32, block_start as u32, block_end as u32));
}
locs
}
fn emit_blocks(&mut self) -> Vec<Vec<u8>> {
let data = self.data.as_ref().unwrap();
let mut blocks = Vec::new();
let len = data.len();
for i in (0..len).step_by(self.block_size) {
let end = std::cmp::min(i + self.block_size, data.len());
blocks.push(data[i..end].to_vec());
}
blocks
}
pub fn write_to_buffer<W>(&mut self, mut out_buf: &mut W) -> Option<u64>
where
W: Write + Seek,
{
self.data.as_ref()?;
let bincode_config = bincode::config::standard().with_fixed_int_encoding();
let mut block_locations_pos: u64 = 0;
let starting_pos = out_buf.seek(SeekFrom::Current(0)).unwrap();
bincode::encode_into_std_write(&self.compression_type, &mut out_buf, bincode_config)
.unwrap();
bincode::encode_into_std_write(&block_locations_pos, &mut out_buf, bincode_config).unwrap();
bincode::encode_into_std_write(&self.block_size, &mut out_buf, bincode_config).unwrap();
let blocks = self.emit_blocks();
let mut block_locations = Vec::new();
let mut compressor = zstd_encoder(7);
for block in blocks {
let block_start = out_buf.seek(SeekFrom::Current(0)).unwrap();
let compressed_block = compressor.compress(&block).unwrap();
bincode::encode_into_std_write(&compressed_block, &mut out_buf, bincode_config)
.unwrap();
block_locations.push(block_start);
}
block_locations_pos = out_buf.seek(SeekFrom::Current(0)).unwrap();
bincode::encode_into_std_write(&block_locations, &mut out_buf, bincode_config).unwrap();
self.block_locations = Some(block_locations); let end = out_buf.seek(SeekFrom::Current(0)).unwrap();
out_buf.seek(SeekFrom::Start(starting_pos)).unwrap();
bincode::encode_into_std_write(&self.compression_type, &mut out_buf, bincode_config)
.unwrap();
bincode::encode_into_std_write(&block_locations_pos, &mut out_buf, bincode_config).unwrap();
bincode::encode_into_std_write(&self.block_size, &mut out_buf, bincode_config).unwrap();
out_buf.seek(SeekFrom::Start(end)).unwrap();
Some(starting_pos)
}
pub fn from_buffer<R>(mut in_buf: &mut R, starting_pos: u64) -> Self
where
R: Read + Seek,
{
let bincode_config = bincode::config::standard().with_fixed_int_encoding();
let mut ids = Ids::default();
in_buf.seek(SeekFrom::Start(starting_pos)).unwrap();
ids.compression_type = bincode::decode_from_std_read(&mut in_buf, bincode_config).unwrap();
ids.block_index_pos = bincode::decode_from_std_read(&mut in_buf, bincode_config).unwrap();
ids.block_size = bincode::decode_from_std_read(&mut in_buf, bincode_config).unwrap();
ids.location = starting_pos;
in_buf.seek(SeekFrom::Start(ids.block_index_pos)).unwrap();
ids.block_locations =
Some(bincode::decode_from_std_read(&mut in_buf, bincode_config).unwrap());
ids
}
pub fn get_id<R>(&mut self, mut in_buf: &mut R, loc: &[Loc]) -> String
where
R: Read + Seek,
{
let bincode_config = bincode::config::standard().with_fixed_int_encoding();
let block_locations = self.block_locations.as_ref().unwrap();
let mut id = String::with_capacity(64);
if self.cache.is_some() {
for (block, (start, end)) in loc.iter().map(|x| x.original_format(self.block_size as u32)) {
let cache = self.cache.as_mut().unwrap();
if block == cache.0 {
let start = start as usize;
let end = end as usize;
id.push_str(std::str::from_utf8(&cache.1[start..=end]).unwrap());
} else {
let mut decompressor = zstd::bulk::Decompressor::new().unwrap();
decompressor.include_magicbytes(false).unwrap();
for i in loc {
let block_location = block_locations[block as usize];
in_buf.seek(SeekFrom::Start(block_location)).unwrap();
let compressed_block: Vec<u8> =
bincode::decode_from_std_read(&mut in_buf, bincode_config).unwrap();
let decompressed_block = decompressor
.decompress(&compressed_block, self.block_size)
.unwrap();
let start = start as usize;
let end = end as usize;
id.push_str(std::str::from_utf8(&decompressed_block[start..=end]).unwrap());
}
}
}
} else {
let mut decompressor = zstd::bulk::Decompressor::new().unwrap();
decompressor.include_magicbytes(false).unwrap();
for (block, (start, end)) in loc.iter().map(|x| x.original_format(self.block_size as u32)) {
let block_location = block_locations[block as usize];
in_buf.seek(SeekFrom::Start(block_location)).unwrap();
let compressed_block: Vec<u8> =
bincode::decode_from_std_read(&mut in_buf, bincode_config).unwrap();
let decompressed_block = decompressor
.decompress(&compressed_block, self.block_size)
.unwrap();
let start = start as usize;
let end = end as usize;
id.push_str(std::str::from_utf8(&decompressed_block[start..=end]).unwrap());
}
}
id
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
#[test]
fn test_add_header() {
let mut ids = Ids {
block_size: 10,
..Default::default()
};
let test_ids = vec![
"Medtr5g026775.t1",
"ARABIDOPSIS_SUPER_COOL_GENE",
"ID WITH A SPACE EVEN THOUGH ITS INVALID",
"same, but lowercase....",
];
let mut locs = Vec::new();
for id in test_ids.iter() {
locs.push(ids.add_id(Arc::new(id.to_string())));
}
let mut buffer = Cursor::new(Vec::new());
ids.write_to_buffer(&mut buffer);
let mut ids = Ids::from_buffer(&mut buffer, 0);
for i in 0..test_ids.len() {
let id = ids.get_id(&mut buffer, &locs[i]);
assert_eq!(id, test_ids[i]);
}
}
}