Skip to content

Commit 5863b73

Browse files
authored
feat: Add 'DownloadCachePiece' and 'CachePieceContent' tag (#57)
# Description This change introduces Tag fields DownloadCachePiece and CachePieceContent to distinguish between download pieces, persistent cache pieces and cache pieces, allowing for separate processing logic. Signed-off-by: fu220 <2863318196@qq.com> # Related Issue # Motivation and Context # Screenshots (if appropriate)
1 parent 4580586 commit 5863b73

File tree

7 files changed

+1223
-6
lines changed

7 files changed

+1223
-6
lines changed

benches/vortex.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use bytes::Bytes;
1818
use chrono::Utc;
1919
use criterion::{black_box, criterion_group, criterion_main, Criterion};
2020
use std::time::Duration;
21+
use vortex_protocol::tlv::cache_piece_content::CachePieceContent;
22+
use vortex_protocol::tlv::download_cache_piece::DownloadCachePiece;
2123
use vortex_protocol::tlv::download_persistent_cache_piece::DownloadPersistentCachePiece;
2224
use vortex_protocol::tlv::download_piece::DownloadPiece;
2325
use vortex_protocol::tlv::persistent_cache_piece_content::PersistentCachePieceContent;
@@ -43,6 +45,21 @@ fn vortex_packet_creation(c: &mut Criterion) {
4345
});
4446
});
4547

48+
group.bench_function("Create DownloadCachePiece Vortex", |b| {
49+
b.iter(|| {
50+
let tag = black_box(Tag::DownloadCachePiece);
51+
let value = black_box(
52+
DownloadCachePiece::new(
53+
"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef".to_string(),
54+
42,
55+
)
56+
.into(),
57+
);
58+
59+
let _ = Vortex::new(tag, value);
60+
});
61+
});
62+
4663
group.bench_function("Create DownloadPersistentCachePiece Vortex", |b| {
4764
b.iter(|| {
4865
let tag = black_box(Tag::DownloadPersistentCachePiece);
@@ -92,6 +109,34 @@ fn vortex_packet_serialization(c: &mut Criterion) {
92109
});
93110
});
94111

112+
group.bench_function("Serialize CachePieceContent Packet", |b| {
113+
b.iter(|| {
114+
let cache_piece_content = black_box(CachePieceContent::new(
115+
1,
116+
1,
117+
10,
118+
"crc32:864bbb04".to_string(),
119+
"127.0.0.1-foo".to_string(),
120+
1,
121+
Duration::from_secs(30),
122+
Utc::now().naive_utc(),
123+
));
124+
125+
let _bytes: Bytes = cache_piece_content.into();
126+
});
127+
});
128+
129+
group.bench_function("Serialize DownloadCachePiece Packet", |b| {
130+
b.iter(|| {
131+
let download_cache_piece = black_box(DownloadCachePiece::new(
132+
"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef".to_string(),
133+
42,
134+
));
135+
136+
let _bytes: Bytes = download_cache_piece.into();
137+
});
138+
});
139+
95140
group.bench_function("Serialize PersistentCachePieceContent Packet", |b| {
96141
b.iter(|| {
97142
let persistent_cache_piece_content = black_box(PersistentCachePieceContent::new(
@@ -160,6 +205,40 @@ fn vortex_packet_deserialization(c: &mut Criterion) {
160205
});
161206
});
162207

208+
group.bench_function("Deserialize CachePieceContent Packet", |b| {
209+
b.iter(|| {
210+
let bytes: Bytes = black_box(
211+
CachePieceContent::new(
212+
1,
213+
1,
214+
10,
215+
"crc32:864bbb04".to_string(),
216+
"127.0.0.1-foo".to_string(),
217+
1,
218+
Duration::from_secs(30),
219+
Utc::now().naive_utc(),
220+
)
221+
.into(),
222+
);
223+
224+
let _cache_piece_content: CachePieceContent = bytes.try_into().unwrap();
225+
});
226+
});
227+
228+
group.bench_function("Deserialize DownloadCachePiece Packet", |b| {
229+
b.iter(|| {
230+
let bytes: Bytes = black_box(
231+
DownloadCachePiece::new(
232+
"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef".to_string(),
233+
42,
234+
)
235+
.into(),
236+
);
237+
238+
let _download_cache_piece: DownloadCachePiece = bytes.try_into().unwrap();
239+
});
240+
});
241+
163242
group.bench_function("Deserialize PersistentCachePieceContent Packet", |b| {
164243
b.iter(|| {
165244
let bytes: Bytes = black_box(

docs/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ scalable file sharing capabilities.
2121
| 1 | Piece Content | The content of a piece, with a maximum size of 1 GiB per piece. |
2222
| 2 | Download Persistent Cache Piece | Download the content of a persistent cache piece from a peer. It is composed of `{Task ID}{Piece ID}`, where the Task ID is a SHA-256 value and the Piece ID is a number. |
2323
| 3 | Persistent Cache Piece Content | The content of a persistent cache piece, with a maximum size of 1 GiB per piece. |
24-
| 4-253 | Reserved | Reserved for future use. |
24+
| 4 | Download Cache Piece | Download the content of a cache piece from a peer. It is composed of `{Task ID}{Piece ID}`, where the Task ID is a SHA-256 value and the Piece ID is a number. |
25+
| 5 | Cache Piece Content | The content of a cache piece, with a maximum size of 1 GiB per piece. |
26+
| 6-253 | Reserved | Reserved for future use. |
2527
| 254 | Close | Close connection. |
2628
| 255 | Error | Error message. |
2729

@@ -44,9 +46,11 @@ scalable file sharing capabilities.
4446
- **Piece Content (Tag=0x01):** Raw piece data or piece fragments.
4547
- **Download Persistent Cache Piece (Tag=0x02):** Download the content of a persistent cache piece from a peer.
4648
- **Persistent Cache Piece Content (Tag=0x03):** Raw persistent cache piece data or persistent cache piece fragments.
49+
- **Download Cache Piece (Tag=0x04):** Download the content of a cache piece from a peer.
50+
- **Cache Piece Content (Tag=0x05):** Raw cache piece data or cache piece fragments.
4751
- **Error (Tag=0xFF):** Conveys error.
4852
- **Close (Tag=0xFE):** Indicates the end of a connection.
49-
- **Reserved Tags:** Tags 4-253 may be allocated for metadata, compression, encryption, or future protocol extensions.
53+
- **Reserved Tags:** Tags 6-253 may be allocated for metadata, compression, encryption, or future protocol extensions.
5054

5155
## Example
5256

src/lib.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ impl Header {
5959
}
6060
}
6161

62+
/// new_download_cache_piece creates a new Vortex packet header for download cache piece request.
63+
pub fn new_download_cache_piece() -> Self {
64+
let mut rng = thread_rng();
65+
Self {
66+
id: rng.gen(),
67+
tag: tlv::Tag::DownloadCachePiece,
68+
length: (tlv::download_cache_piece::TASK_ID_SIZE
69+
+ tlv::download_cache_piece::PIECE_NUMBER_SIZE) as u32,
70+
}
71+
}
72+
6273
/// new_download_persistent_cache_piece creates a new Vortex packet header for download persistent cache piece request.
6374
pub fn new_download_persistent_cache_piece() -> Self {
6475
let mut rng = thread_rng();
@@ -91,6 +102,16 @@ impl Header {
91102
}
92103
}
93104

105+
/// new_cache_piece_content creates a new Vortex packet header for cache piece content.
106+
pub fn new_cache_piece_content(value_length: u32) -> Self {
107+
let mut rng = thread_rng();
108+
Self {
109+
id: rng.gen(),
110+
tag: tlv::Tag::CachePieceContent,
111+
length: value_length,
112+
}
113+
}
114+
94115
/// new_persistent_cache_piece_content creates a new Vortex packet header for persistent cache piece content.
95116
pub fn new_persistent_cache_piece_content(value_length: u32) -> Self {
96117
let mut rng = thread_rng();
@@ -205,6 +226,8 @@ impl From<Header> for Bytes {
205226
pub enum Vortex {
206227
DownloadPiece(Header, tlv::download_piece::DownloadPiece),
207228
PieceContent(Header, tlv::piece_content::PieceContent),
229+
DownloadCachePiece(Header, tlv::download_cache_piece::DownloadCachePiece),
230+
CachePieceContent(Header, tlv::cache_piece_content::CachePieceContent),
208231
DownloadPersistentCachePiece(
209232
Header,
210233
tlv::download_persistent_cache_piece::DownloadPersistentCachePiece,
@@ -231,6 +254,8 @@ impl Vortex {
231254
match self {
232255
Vortex::DownloadPiece(header, _) => header.id,
233256
Vortex::PieceContent(header, _) => header.id,
257+
Vortex::DownloadCachePiece(header, _) => header.id,
258+
Vortex::CachePieceContent(header, _) => header.id,
234259
Vortex::DownloadPersistentCachePiece(header, _) => header.id,
235260
Vortex::PersistentCachePieceContent(header, _) => header.id,
236261
Vortex::Reserved(header) => header.id,
@@ -245,6 +270,8 @@ impl Vortex {
245270
match self {
246271
Vortex::DownloadPiece(header, _) => header.tag,
247272
Vortex::PieceContent(header, _) => header.tag,
273+
Vortex::DownloadCachePiece(header, _) => header.tag,
274+
Vortex::CachePieceContent(header, _) => header.tag,
248275
Vortex::DownloadPersistentCachePiece(header, _) => header.tag,
249276
Vortex::PersistentCachePieceContent(header, _) => header.tag,
250277
Vortex::Reserved(header) => header.tag,
@@ -259,6 +286,8 @@ impl Vortex {
259286
match self {
260287
Vortex::DownloadPiece(header, _) => header.length as usize,
261288
Vortex::PieceContent(header, _) => header.length as usize,
289+
Vortex::DownloadCachePiece(header, _) => header.length as usize,
290+
Vortex::CachePieceContent(header, _) => header.length as usize,
262291
Vortex::DownloadPersistentCachePiece(header, _) => header.length as usize,
263292
Vortex::PersistentCachePieceContent(header, _) => header.length as usize,
264293
Vortex::Reserved(header) => header.length as usize,
@@ -273,6 +302,8 @@ impl Vortex {
273302
match self {
274303
Vortex::DownloadPiece(header, _) => header,
275304
Vortex::PieceContent(header, _) => header,
305+
Vortex::DownloadCachePiece(header, _) => header,
306+
Vortex::CachePieceContent(header, _) => header,
276307
Vortex::DownloadPersistentCachePiece(header, _) => header,
277308
Vortex::PersistentCachePieceContent(header, _) => header,
278309
Vortex::Reserved(header) => header,
@@ -312,6 +343,9 @@ impl From<Vortex> for Bytes {
312343
fn from(packet: Vortex) -> Self {
313344
let (header, value) = match packet {
314345
Vortex::DownloadPiece(header, download_piece) => (header, download_piece.into()),
346+
Vortex::DownloadCachePiece(header, download_cache_piece) => {
347+
(header, download_cache_piece.into())
348+
}
315349
Vortex::DownloadPersistentCachePiece(header, download_persistent_cache_piece) => {
316350
(header, download_persistent_cache_piece.into())
317351
}
@@ -349,6 +383,11 @@ impl TryFrom<(tlv::Tag, Header, Bytes)> for Vortex {
349383
let download_piece = tlv::download_piece::DownloadPiece::try_from(value)?;
350384
Ok(Vortex::DownloadPiece(header, download_piece))
351385
}
386+
tlv::Tag::DownloadCachePiece => {
387+
let download_cache_piece =
388+
tlv::download_cache_piece::DownloadCachePiece::try_from(value)?;
389+
Ok(Vortex::DownloadCachePiece(header, download_cache_piece))
390+
}
352391
tlv::Tag::DownloadPersistentCachePiece => {
353392
let download_persistent_cache_piece =
354393
tlv::download_persistent_cache_piece::DownloadPersistentCachePiece::try_from(
@@ -505,6 +544,18 @@ mod tests {
505544
assert_eq!(bytes.len(), HEADER_SIZE + value.len());
506545
}
507546

547+
#[test]
548+
fn test_vortex_to_bytes_download_cache_piece() {
549+
let tag = Tag::DownloadCachePiece;
550+
let mut value = BytesMut::with_capacity(68);
551+
value.extend_from_slice("a".repeat(64).as_bytes());
552+
value.put_u32(42);
553+
let packet = Vortex::new(tag, value.clone().freeze()).unwrap();
554+
let bytes: Bytes = packet.into();
555+
556+
assert_eq!(bytes.len(), HEADER_SIZE + value.len());
557+
}
558+
508559
#[test]
509560
fn test_vortex_to_bytes_download_persistent_cache_piece() {
510561
let tag = Tag::DownloadPersistentCachePiece;

0 commit comments

Comments
 (0)