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
- Proposal Name: `generic-kv-services`
- Start Date: 2022-10-03
- RFC PR: [apache/opendal#793](https://github.com/apache/opendal/pull/793)
- Tracking Issue: [apache/opendal#794](https://github.com/apache/opendal/issues/794)
# Summary
Add generic kv services support OpenDAL.
# Motivation
OpenDAL now has some kv services support:
- memory
- redis
However, maintaining them is complex and very easy to be wrong. We don't want to implement similar logic for every kv
service. This RFC intends to introduce a generic kv service so that we can:
- Implement OpenDAL Accessor on this generic kv service
- Add new kv service support via generic kv API.
# Guide-level explanation
No user-side changes.
# Reference-level explanation
OpenDAL will introduce a generic kv service:
```rust
trait KeyValueAccessor {
async fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>>;
async fn set(&self, key: &[u8], value: &[u8]) -> Result<()>;
}
```
We will implement the OpenDAL service on `KeyValueAccessor`. To add new kv service support, users only need to implement
it against `KeyValueAccessor`.
## Spec
This RFC is mainly inspired
by [TiFS: FUSE based on TiKV](https://github.com/Hexilee/tifs/blob/main/contribution/design.md). We will use the
same `ScopedKey` idea in `TiFS`.
```rust
pub enum ScopedKey {
Meta,
Inode(u64),
Block {
ino: u64,
block: u64,
},
Entry {
parent: u64,
name: String,
},
}
```
We can encode a scoped key into a byte array as a key. Following is the common layout of an encoded key.
```text
+ 1byte +<----------------------------+ dynamic size +------------------------------------>+
| | |
| | |
| | |
| | |
| | |
| | |
| v v
+------------------------------------------------------------------------------------------+
| | |
| scope | body |
| | |
+-------+----------------------------------------------------------------------------------+
```
### Meta
There is only one key in the meta scope. The meta key is designed to store metadata of our filesystem. Following is the
layout of an encoded meta key.
```text
+ 1byte +
| |
| |
| |
| |
| |
| |
| v
+-------+
| |
| 0 |
| |
+-------+
```
This key will store data:
```rust
pub struct Meta {
inode_next: u64,
}
```
The meta-structure contains only an auto-increasing counter `inode_next`, designed to generate an inode number.
### Inode
Keys in the inode scope are designed to store attributes of files. Following is the layout of an encoded inode key.
```text
+ 1byte +<-------------------------------+ 8bytes +--------------------------------------->+
| | |
| | |
| | |
| | |
| | |
| | |
| v v
+------------------------------------------------------------------------------------------+
| | |
| 1 | inode number |
| | |
+-------+----------------------------------------------------------------------------------+
```
This key will store data:
```rust
pub struct Inode {
meta: Metadata,
blocks: HashMap<u64, u32>,
}
```
blocks is the map from `block_id` -> `size`. We will use this map to calculate the correct blocks to read.
### Block
Keys in the block scope are designed to store blocks of a file. Following is the layout of an encoded block key.
```text
+ 1byte +<----------------- 8bytes ---------------->+<------------------- 8bytes ----------------->+
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| v v v
+--------------------------------------------------------------------------------------------------+
| | | |
| 2 | inode number | block index |
| | | |
+-------+-------------------------------------------+----------------------------------------------+
```
### Entry
Keys in the file index scope are designed to store the entry of the file. Following is the layout of an encoded file
entry key.
```text
+ 1byte +<----------------- 8bytes ---------------->+<-------------- dynamic size ---------------->+
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| v v v
+--------------------------------------------------------------------------------------------------+
| | | |
| 3 | inode number of parent directory | file name in utf-8 encoding |
| | | |
+-------+-------------------------------------------+----------------------------------------------+
```
Store the correct inode number for this file.
```rust
pub struct Index {
pub ino: u64,
}
```
# Drawbacks
None.
# Rationale and alternatives
None.
# Prior art
None.
# Unresolved questions
None.
# Future possibilities
None.