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
use crate::manifest::StoredArchive;
use crate::repository::{Chunk, ChunkID, ChunkSettings, EncryptedKey};
use async_trait::async_trait;
use chrono::prelude::*;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use thiserror::Error;
pub mod common;
pub mod flatfile;
pub mod mem;
pub mod multifile;
pub mod object_wrappers;
pub use object_wrappers::{backend_to_object, BackendObject};
#[derive(Error, Debug)]
pub enum BackendError {
#[error("I/O Error")]
IOError(#[from] std::io::Error),
#[error("Data not found")]
DataNotFound,
#[error("Segment Error")]
SegmentError(String),
#[error("Manifest Error")]
ManifestError(String),
#[error("Index Error")]
IndexError(String),
#[error("MessagePack Decode Error")]
MsgPackDecodeError(#[from] rmp_serde::decode::Error),
#[error("MessagePack Encode Error")]
MsgPackEncodeError(#[from] rmp_serde::encode::Error),
#[error("Failed to lock file")]
FileLockError,
#[error("Cancelled oneshot")]
CancelledOneshotError(#[from] futures::channel::oneshot::Canceled),
#[error("Chunk Unpacking Error")]
ChunkUnpackError(#[from] asuran_core::repository::chunk::ChunkError),
#[error("Unknown Error")]
Unknown(String),
}
pub type Result<T> = std::result::Result<T, BackendError>;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SegmentDescriptor {
pub segment_id: u64,
pub start: u64,
}
#[async_trait]
pub trait Manifest: Send + Sync + std::fmt::Debug + 'static {
type Iterator: Iterator<Item = StoredArchive> + 'static;
async fn last_modification(&mut self) -> Result<DateTime<FixedOffset>>;
async fn chunk_settings(&mut self) -> ChunkSettings;
async fn archive_iterator(&mut self) -> Self::Iterator;
async fn write_chunk_settings(&mut self, settings: ChunkSettings) -> Result<()>;
async fn write_archive(&mut self, archive: StoredArchive) -> Result<()>;
async fn touch(&mut self) -> Result<()>;
}
#[async_trait]
pub trait Index: Send + Sync + std::fmt::Debug + 'static {
async fn lookup_chunk(&mut self, id: ChunkID) -> Option<SegmentDescriptor>;
async fn set_chunk(&mut self, id: ChunkID, location: SegmentDescriptor) -> Result<()>;
async fn known_chunks(&mut self) -> HashSet<ChunkID>;
async fn commit_index(&mut self) -> Result<()>;
async fn count_chunk(&mut self) -> usize;
}
#[async_trait]
pub trait Backend: 'static + Send + Sync + std::fmt::Debug + 'static {
type Manifest: Manifest + 'static;
type Index: Index + 'static;
fn get_index(&self) -> Self::Index;
async fn write_key(&self, key: &EncryptedKey) -> Result<()>;
async fn read_key(&self) -> Result<EncryptedKey>;
fn get_manifest(&self) -> Self::Manifest;
async fn read_chunk(&mut self, location: SegmentDescriptor) -> Result<Chunk>;
async fn write_chunk(&mut self, chunk: Chunk) -> Result<SegmentDescriptor>;
async fn close(&mut self);
fn get_object_handle(&self) -> BackendObject;
}
pub trait BackendClone: Backend + Clone {}
impl<T: ?Sized> BackendClone for T where T: Backend + Clone {}
#[derive(Copy, PartialEq, Eq, Clone, Serialize, Deserialize, Debug)]
pub enum TransactionType {
Insert,
Delete,
}