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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
use log::*;
use std::{
sync::{Arc},
path::{Path, PathBuf},
};
use async_std::{
prelude::*,
fs::{self, OpenOptions},
io::{SeekFrom, Cursor},
};
use cyfs_base::*;
use cyfs_util::*;
use crate::{
ndn::*
};
struct StoreImpl {
ndc: Box<dyn NamedDataCache>,
tracker: Box<dyn TrackerCache>,
}
#[derive(Clone)]
pub struct TrackedChunkStore(Arc<StoreImpl>);
impl TrackedChunkStore {
pub fn new(
ndc: Box<dyn NamedDataCache>,
tracker: Box<dyn TrackerCache>,
) -> Self {
Self(Arc::new(StoreImpl {
ndc,
tracker,
}))
}
pub async fn track_chunk(&self, chunk: &ChunkId) -> BuckyResult<()> {
let request = InsertChunkRequest {
chunk_id: chunk.to_owned(),
state: ChunkState::Unknown,
ref_objects: None,
trans_sessions: None,
flags: 0,
};
self.ndc().insert_chunk(&request).await.map_err(|e| {
error!("record file chunk to ndc error! chunk={}, {}",chunk, e);
e
})
}
pub async fn track_file(&self, file: &File) -> BuckyResult<()> {
let file_id = file.desc().calculate_id();
match file.body() {
Some(body) => {
let chunk_list = body.content().inner_chunk_list();
match chunk_list {
Some(chunks) => {
for chunk in chunks {
let ref_obj = ChunkObjectRef {
object_id: file_id.to_owned(),
relation: ChunkObjectRelation::FileBody,
};
let req = InsertChunkRequest {
chunk_id: chunk.to_owned(),
state: ChunkState::Unknown,
ref_objects: Some(vec![ref_obj]),
trans_sessions: None,
flags: 0,
};
self.ndc().insert_chunk(&req).await.map_err(|e| {
error!("record file chunk to ndc error! file={}, chunk={}, {}", file_id, chunk, e);
e
})?;
info!("insert chunk of file to ndc, chunk:{}, file:{}", chunk, file_id);
}
Ok(())
}
None => Err(BuckyError::new(
BuckyErrorCode::NotSupport,
format!("file object should has chunk list: {}", file_id),
)),
}
}
None => {
Err(BuckyError::new(
BuckyErrorCode::InvalidFormat,
format!("file object should has body: {}", file_id),
))
}
}
}
pub async fn track_file_in_path(
&self,
file: File,
path: PathBuf
) -> BuckyResult<()> {
let _ = self.track_file(&file).await?;
TrackedChunkListWriter::new(
path,
&ChunkListDesc::from_file(&file)?,
self.ndc(),
self.tracker()
).track_path().await
}
fn ndc(&self) -> &dyn NamedDataCache {
self.0.ndc.as_ref()
}
fn tracker(&self) -> &dyn TrackerCache {
self.0.tracker.as_ref()
}
async fn read_chunk_from_file(chunk: &ChunkId, path: &Path, offset: u64) -> BuckyResult<Box<dyn AsyncReadWithSeek + Unpin + Send + Sync>> {
debug!("begin read {} from file {:?}", chunk, path);
let mut file = OpenOptions::new()
.read(true)
.open(path)
.await
.map_err(|e| {
let msg = format!("open file {:?} failed for {}", path, e);
error!("{}", msg);
BuckyError::new(BuckyErrorCode::IoError, msg)
})?;
let actual_offset = file.seek(SeekFrom::Start(offset)).await.map_err(|e| {
let msg = format!("seek file {:?} to offset {} failed for {}", path, offset, e);
error!("{}", msg);
BuckyError::new(BuckyErrorCode::IoError, msg)
})?;
if actual_offset != offset {
let msg = format!(
"seek file {:?} to offset {} actual offset {}",
path, offset, actual_offset
);
error!("{}", msg);
return Err(BuckyError::new(BuckyErrorCode::IoError, msg));
}
let mut content = Vec::with_capacity(chunk.len());
unsafe { content.set_len(chunk.len()) };
file.read_exact(&mut content).await.map_err(|e| {
let msg = format!(
"read chunk from file {:?} error, chunk={}, len={}, {}",
path,
chunk,
chunk.len(),
e
);
error!("{}", msg);
BuckyError::new(BuckyErrorCode::IoError, msg)
})?;
let actual_id = ChunkId::calculate(content.as_slice()).await?;
if actual_id.eq(chunk) {
debug!("read {} from file {:?}", chunk, path);
Ok(Box::new(Cursor::new(content)))
} else {
let msg = format!("content in file {:?} not match chunk id", path);
error!("{}", msg);
Err(BuckyError::new(BuckyErrorCode::NotFound, msg))
}
}
async fn is_chunk_stored_in_file(&self, chunk: &ChunkId, path: &Path) -> BuckyResult<bool> {
let request = GetTrackerPositionRequest {
id: chunk.to_string(),
direction: Some(TrackerDirection::Store),
};
let ret = self.0.tracker.get_position(&request).await?;
if ret.len() == 0 {
Ok(false)
} else {
for c in ret {
match &c.pos {
TrackerPostion::File(exists) => {
if path.eq(Path::new(exists)) {
return Ok(true);
}
}
TrackerPostion::FileRange(fr) => {
if path.eq(Path::new(&fr.path)) {
return Ok(true);
}
}
_ => {}
}
}
Ok(false)
}
}
}
#[async_trait::async_trait]
impl ChunkReader for TrackedChunkStore {
fn clone_as_reader(&self) -> Box<dyn ChunkReader> {
Box::new(self.clone())
}
async fn exists(&self, chunk: &ChunkId) -> bool {
let request = GetChunkRequest {
chunk_id: chunk.clone(),
flags: 0,
};
match self.ndc().get_chunk(&request).await {
Ok(c) => {
if let Some(c) = c {
c.state == ChunkState::Ready
} else {
false
}
}
Err(e) => {
error!("got chunk state {} from database failed for {}", chunk, e);
false
}
}
}
async fn get(&self, chunk: &ChunkId) -> BuckyResult<Box<dyn AsyncReadWithSeek + Unpin + Send + Sync>> {
let request = GetTrackerPositionRequest {
id: chunk.to_string(),
direction: Some(TrackerDirection::Store),
};
let ret = self.tracker().get_position(&request).await?;
if ret.len() == 0 {
Err(BuckyError::new(
BuckyErrorCode::NotFound,
"chunk not exists",
))
} else {
for c in ret {
let read_ret = match &c.pos {
TrackerPostion::File(path) => {
Self::read_chunk_from_file(chunk, Path::new(path), 0).await
}
TrackerPostion::FileRange(fr) => {
Self::read_chunk_from_file(
chunk,
Path::new(fr.path.as_str()),
fr.range_begin,
)
.await
}
_ => Err(BuckyError::new(
BuckyErrorCode::InvalidFormat,
"unsupport reader",
)),
};
match read_ret {
Ok(reader) => {
return Ok(reader);
},
Err(e) => {
let _ = self
.0
.tracker
.remove_position(&RemoveTrackerPositionRequest {
id: chunk.to_string(),
direction: Some(TrackerDirection::Store),
pos: Some(c.pos.clone()),
})
.await;
error!(
"read {} from tracker position {:?} failed for {}",
chunk, c.pos, e
);
continue;
}
}
}
error!("read {} from all tracker position failed", chunk);
Err(BuckyError::new(
BuckyErrorCode::NotFound,
"chunk not exists",
))
}
}
}
struct WriterImpl {
path: PathBuf,
tmp_path: Option<PathBuf>,
chunk: ChunkId,
ndc: Box<dyn NamedDataCache>,
tracker: Box<dyn TrackerCache>,
}
#[derive(Clone)]
pub struct TrackedChunkWriter(Arc<WriterImpl>);
impl std::fmt::Display for TrackedChunkWriter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "TrackedChunkWriter{{path:{:?}}}", self.path())
}
}
impl TrackedChunkWriter {
fn from_path(
path: &Path,
chunk: &ChunkId,
ndc: &dyn NamedDataCache,
tracker: &dyn TrackerCache,
) -> Self {
let tmp_path = format!(
"{}-{}",
path.file_name().unwrap().to_str().unwrap(),
bucky_time_now()
);
Self::new(
path.to_owned(),
Some(path.parent().unwrap().join(tmp_path.as_str())),
chunk,
ndc,
tracker,
)
}
fn new(
path: PathBuf,
tmp_path: Option<PathBuf>,
chunk: &ChunkId,
ndc: &dyn NamedDataCache,
tracker: &dyn TrackerCache,
) -> Self {
Self(Arc::new(WriterImpl {
path,
tmp_path,
chunk: chunk.clone(),
ndc: ndc.clone(),
tracker: tracker.clone(),
}))
}
pub async fn track_path(&self) -> BuckyResult<()> {
let request = UpdateChunkStateRequest {
chunk_id: self.chunk().clone(),
current_state: None,
state: ChunkState::Ready,
};
let _ = self.0.ndc.update_chunk_state(&request).await.map_err(|e| {
error!("{} add to tracker failed for {}", self, e);
e
})?;
let request = AddTrackerPositonRequest {
id: self.chunk().to_string(),
direction: TrackerDirection::Store,
pos: TrackerPostion::File(self.path().to_str().unwrap().to_string()),
flags: 0,
};
self.0.tracker.add_position(&request).await.map_err(|e| {
error!("{} add to tracker failed for {}", self, e);
e
})?;
Ok(())
}
fn path(&self) -> &Path {
self.0.path.as_path()
}
fn chunk(&self) -> &ChunkId {
&self.0.chunk
}
async fn write_inner<R: async_std::io::Read + Unpin>(&self, reader: R) -> BuckyResult<()> {
if self.chunk().len() == 0 {
return Ok(());
}
let path = self.0.tmp_path.as_ref().map(|p| p.as_path()).unwrap_or(self.path());
let file = OpenOptions::new().create(true).write(true).open(path).await
.map_err(|e| {
let msg = format!("{} open file failed for {}", self, e);
error!("{}", msg);
BuckyError::new(BuckyErrorCode::IoError, msg)
})?;
let _ = async_std::io::copy(reader, file).await
.map_err(|e| {
let msg = format!(
"{} write chunk file failed for {}",
self,
e
);
error!("{}", msg);
BuckyError::new(BuckyErrorCode::IoError, msg)
})?;
if self.0.tmp_path.is_some() {
let tmp_path = self.0.tmp_path.as_ref().unwrap().as_path();
let ret = fs::rename(tmp_path, self.path()).await;
if ret.is_err() {
if !self.path().exists() {
let msg = format!("{} rename tmp file failed for {}", self, ret.err().unwrap());
error!("{}", msg);
return Err(BuckyError::new(BuckyErrorCode::IoError, msg));
}
}
}
info!("{} writen chunk to file", self);
self.track_path().await
}
pub async fn write<R: async_std::io::Read + Unpin>(&self, reader: R) -> BuckyResult<()> {
if self.chunk().len() == 0 {
return Ok(());
}
let ret = self.write_inner(reader).await;
if self.0.tmp_path.is_some() {
let tmp_path = self.0.tmp_path.as_ref().unwrap().as_path();
let _ = fs::remove_file(tmp_path).await;
}
ret
}
}
struct ListWriterImpl {
path: PathBuf,
desc: ChunkListDesc,
ndc: Box<dyn NamedDataCache>,
tracker: Box<dyn TrackerCache>,
}
#[derive(Clone)]
pub struct TrackedChunkListWriter(Arc<ListWriterImpl>);
impl std::fmt::Display for TrackedChunkListWriter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "TrackedChunkListWriter{{path:{:?}}}", self.path())
}
}
impl TrackedChunkListWriter {
fn new(
path: PathBuf,
desc: &ChunkListDesc,
ndc: &dyn NamedDataCache,
tracker: &dyn TrackerCache) -> Self {
Self(Arc::new(ListWriterImpl {
path,
desc: desc.clone(),
ndc: ndc.clone(),
tracker: tracker.clone(),
}))
}
async fn track_chunk_index(&self, chunk: &ChunkId, index: usize) -> BuckyResult<()> {
let offset = self.chunk_list().offset_of(index).unwrap();
let request = UpdateChunkStateRequest {
chunk_id: chunk.clone(),
current_state: None,
state: ChunkState::Ready,
};
let _ = self.0.ndc.update_chunk_state(&request).await.map_err(|e| {
error!("{} add {} to tracker failed for {}", self, chunk, e);
e
})?;
let request = AddTrackerPositonRequest {
id: chunk.to_string(),
direction: TrackerDirection::Store,
pos: TrackerPostion::FileRange(PostionFileRange {
path: self.path().to_str().unwrap().to_string(),
range_begin: offset,
range_end: offset + chunk.len() as u64,
}),
flags: 0,
};
self.0.tracker.add_position(&request).await.map_err(|e| {
error!("{} add {} to tracker failed for {}", self, chunk, e);
e
})?;
Ok(())
}
pub async fn track_path(&self) -> BuckyResult<()> {
for (index, chunk) in self.chunk_list().chunks().iter().enumerate() {
let _ = self.track_chunk_index(chunk, index).await?;
}
Ok(())
}
fn path(&self) -> &Path {
self.0.path.as_path()
}
fn chunk_list(&self) -> &ChunkListDesc {
&self.0.desc
}
pub async fn write<R: async_std::io::Read + Unpin>(&self, reader: R) -> BuckyResult<()> {
if self.chunk_list().total_len() == 0 {
return Ok(());
}
let mut reader = reader;
let mut file = OpenOptions::new()
.create(true)
.write(true)
.open(self.path())
.await
.map_err(|e| {
let msg = format!("{} open file failed for {}", self, e);
error!("{}", msg);
BuckyError::new(BuckyErrorCode::IoError, msg)
})?;
file.set_len(self.chunk_list().total_len())
.await
.map_err(|e| {
let msg = format!(
"{} create trans data file with len {} failed for {}",
self,
self.chunk_list().total_len(),
e
);
error!("{}", msg);
BuckyError::new(BuckyErrorCode::IoError, msg)
})?;
file.set_len(self.chunk_list().total_len()).await.map_err(|e| {
let msg = format!(
"{} create trans data file with len {} failed for {}",
self,
self.chunk_list().total_len(),
e
);
error!("{}", msg);
BuckyError::new(BuckyErrorCode::IoError, msg)
})?;
for (index, chunk) in self.chunk_list().chunks().iter().enumerate() {
if chunk.len() == 0 {
continue;
}
let mut buffer = vec![0u8; chunk.len()];
reader.read_exact(&mut buffer[..]).await?;
file.write_all(&buffer[..]).await?;
let _ = self.track_chunk_index(chunk, index).await?;
}
Ok(())
}
}
impl TrackedChunkStore {
pub async fn chunk_writer(
&self,
chunk: &ChunkId,
path: PathBuf
) -> BuckyResult<TrackedChunkWriter> {
let _ = self.track_chunk(chunk).await?;
Ok(TrackedChunkWriter::new(path, None, chunk, self.ndc(), self.tracker()))
}
pub async fn chunk_list_writer(
&self,
chunk_list: &ChunkListDesc,
path: PathBuf
) -> BuckyResult<TrackedChunkListWriter> {
for chunk in chunk_list.chunks() {
let _ = self.track_chunk(chunk).await?;
}
Ok(TrackedChunkListWriter::new(path, chunk_list, self.ndc(), self.tracker()))
}
pub async fn file_writer(
&self,
file: &File,
path: PathBuf
) -> BuckyResult<TrackedChunkListWriter> {
let _ = self.track_file(file).await?;
Ok(TrackedChunkListWriter::new(path, &ChunkListDesc::from_file(&file)?, self.ndc(), self.tracker()))
}
}