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
use std::ops::{Bound, RangeBounds};
use crate::{
block::{
Block, BlockEncoding, BlockId, BlockIndex, BlockList, BlockSignature, BlockStore,
BlockStoreInput, FileId as StoreFileId, FileOffset, HoleLen, ReadBlock,
},
errors::InternalError,
};
use super::{exclusive::ExclusiveFileId, store::SqlStore};
impl<'conn> SqlStore<'conn> {
pub fn write_file_block(
&mut self,
exclusive: ExclusiveFileId,
offset: FileOffset,
block_list: BlockList,
input: BlockStoreInput,
) -> crate::Result<BlockList> {
self.data_store()
.write_block(exclusive.file_id(), offset, block_list, input)
}
pub fn truncate_file(
&mut self,
exclusive: ExclusiveFileId,
block_list: BlockList,
len: FileOffset,
) -> crate::Result<BlockList> {
self.data_store()
.truncate(exclusive.file_id(), block_list, len)
}
}
impl<'conn> SqlStore<'conn> {
pub fn commit(self) -> crate::Result<()> {
Ok(self.db.commit()?)
}
fn insert_block(&mut self, input: BlockStoreInput) -> crate::Result<BlockId> {
let mut stmt = self.db.prepare_cached(
r#"
INSERT OR IGNORE INTO
liteboxfs_blocks (length, encoding, hash, data)
VALUES
(?, ?, ?, ?);
"#,
)?;
let encoding = input.encoding();
let logical_len = input.len();
let params = match &input {
BlockStoreInput::Data { bytes, hash, .. } => {
self.block_buf.clear();
self.block_buf.extend_from_slice(bytes);
rusqlite::params![
logical_len,
encoding,
hash.as_ref().to_owned(),
self.block_buf.as_slice()
]
}
BlockStoreInput::Hole { len } => {
rusqlite::params![
*len as usize,
encoding,
rusqlite::types::Null,
rusqlite::types::Null
]
}
};
let rows_inserted = stmt.execute(params)?;
if rows_inserted > 0 {
return Ok(BlockId::from(self.db.last_insert_rowid()));
}
// Block already exists.
match input {
BlockStoreInput::Data { hash, .. } => {
let mut stmt = self.db.prepare_cached(
r#"
SELECT
id
FROM
liteboxfs_blocks
WHERE
hash = ?;
"#,
)?;
Ok(
stmt.query_one(rusqlite::params![hash.as_ref().to_owned()], |row| {
row.get(0)
})?,
)
}
BlockStoreInput::Hole { len } => {
let mut stmt = self.db.prepare_cached(
r#"
SELECT
id
FROM
liteboxfs_blocks
WHERE
hash IS NULL
AND length = ?;
"#,
)?;
Ok(stmt.query_one(rusqlite::params![len as usize], |row| row.get(0))?)
}
}
}
}
impl<'conn> BlockStore for SqlStore<'conn> {
fn list_blocks(&self, file: StoreFileId) -> crate::Result<BlockList> {
let mut stmt = self.db.prepare_cached(
r#"
SELECT
liteboxfs_blocks.id,
liteboxfs_blocks.length,
liteboxfs_blocks.hash
FROM
liteboxfs_file_blocks
JOIN
liteboxfs_blocks ON liteboxfs_file_blocks.block = liteboxfs_blocks.id
WHERE
liteboxfs_file_blocks.file = ?
ORDER BY
liteboxfs_file_blocks."index" ASC;
"#,
)?;
let blocks = stmt.query_map(rusqlite::params![file], |row| {
let block_id: i64 = row.get(0)?;
let block_len: usize = row.get(1)?;
let block_hash: Option<Vec<u8>> = row.get(2)?;
Ok(match block_hash {
Some(hash) => Ok(Block {
id: block_id.into(),
signature: BlockSignature::Data {
hash: match hash.as_slice().try_into() {
Ok(hash) => hash,
Err(err) => return Ok(Err(err)),
},
len: block_len,
},
}),
None => Ok(Block {
id: block_id.into(),
signature: BlockSignature::Hole {
len: block_len as u64,
},
}),
})
})?;
let blocks: Vec<_> = blocks.collect::<Result<Result<Vec<_>, _>, _>>()??;
Ok(blocks.into())
}
fn read_block(&mut self, block: BlockId, buf: &mut Vec<u8>) -> crate::Result<ReadBlock> {
let mut stmt = self.db.prepare_cached(
r#"
SELECT
length,
encoding,
hash,
data
FROM
liteboxfs_blocks
WHERE
id = ?;
"#,
)?;
let mut rows = stmt.query(rusqlite::params![block])?;
if let Some(row) = rows.next()? {
let block_len: usize = row.get(0)?;
let encoding: BlockEncoding = row.get(1)?;
#[cfg(not(feature = "compression"))]
if encoding != BlockEncoding::NONE {
return Err(InternalError::CompressionDisabled.into());
}
let block_hash: Option<Vec<u8>> = row.get(2)?;
let block_data: Option<Vec<u8>> = row.get(3)?;
match (block_hash, block_data) {
(Some(hash), Some(data)) => {
buf.extend_from_slice(&data);
Ok(ReadBlock {
signature: BlockSignature::Data {
hash: hash.as_slice().try_into()?,
len: block_len,
},
encoding,
})
}
(None, None) => Ok(ReadBlock {
signature: BlockSignature::Hole {
len: block_len as HoleLen,
},
encoding,
}),
_ => Err(InternalError::MismatchedBlockHashAndData.into()),
}
} else {
Err(InternalError::BlockNotFound { id: block }.into())
}
}
fn replace_block(
&mut self,
file: StoreFileId,
index: BlockIndex,
input: BlockStoreInput,
) -> crate::Result<BlockId> {
let block_id = self.insert_block(input)?;
{
let mut stmt = self.db.prepare_cached(
r#"
INSERT OR REPLACE INTO
liteboxfs_file_blocks (file, "index", block)
VALUES
(?, ?, ?);
"#,
)?;
stmt.execute(rusqlite::params![file, index, block_id])?;
}
Ok(block_id)
}
fn insert_block(
&mut self,
file: StoreFileId,
index: BlockIndex,
input: BlockStoreInput,
) -> crate::Result<BlockId> {
let block_id = self.insert_block(input)?;
// We need to shift each block index forward by one. However, SQLite doesn't allow you to
// control which order rows are updated in, which means the naive approach would result in
// a UNIQUE constraint violation when one row index is incremented before the one that
// comes after it.
//
// To work around this, we shift in two phases. The shift offset has to be chosen so that
// *both* phases' source and target ranges are disjoint from each other and from any
// unshifted rows. Concretely, we shift up by `max_index + 2`, then back down by
// `max_index + 1` for a net shift of +1. The `+2` ensures the phase-2 source range
// `[index + max_index + 2, 2 * max_index + 2]` lies strictly above its target range
// `[index + 1, max_index + 1]` — important for the `index == 0` case, where a smaller
// offset would leave the largest shifted row's target equal to the smallest unshifted
// row's source (e.g. shift `{0..=5}` to `{6..=11}` and back to `{1..=6}` — row 11→6
// collides with row 6 if 11 moves first).
let max_index: usize = {
let mut stmt = self.db.prepare_cached(
r#"
SELECT
COALESCE(MAX("index"), 0)
FROM
liteboxfs_file_blocks
WHERE
file = ?;
"#,
)?;
stmt.query_one(rusqlite::params![file], |row| row.get(0))?
};
{
let mut stmt = self.db.prepare_cached(
r#"
UPDATE
liteboxfs_file_blocks
SET
"index" = "index" + ? + 2
WHERE
file = ?
AND
"index" >= ?;
"#,
)?;
stmt.execute(rusqlite::params![max_index, file, index])?;
}
{
let mut stmt = self.db.prepare_cached(
r#"
UPDATE
liteboxfs_file_blocks
SET
"index" = "index" - ? - 1
WHERE
file = ?
AND
"index" >= ?;
"#,
)?;
stmt.execute(rusqlite::params![max_index, file, index + max_index + 2])?;
}
{
let mut stmt = self.db.prepare_cached(
r#"
INSERT OR ABORT INTO
liteboxfs_file_blocks (file, "index", block)
VALUES
(?, ?, ?);
"#,
)?;
stmt.execute(rusqlite::params![file, index, block_id])?;
}
Ok(block_id)
}
// This optimizes for the append case by saving us from needing to shift any indices.
fn append_block(
&mut self,
file: StoreFileId,
index: BlockIndex,
input: BlockStoreInput,
) -> crate::Result<BlockId> {
let block_id = self.insert_block(input)?;
{
let mut stmt = self.db.prepare_cached(
r#"
INSERT INTO
liteboxfs_file_blocks (file, "index", block)
VALUES
(?, ?, ?);
"#,
)?;
stmt.execute(rusqlite::params![file, index, block_id])?;
}
Ok(block_id)
}
fn remove_blocks<R: RangeBounds<usize>>(
&mut self,
file: StoreFileId,
blocks: R,
) -> crate::Result<Vec<BlockId>> {
let start = match blocks.start_bound() {
Bound::Included(index) => *index,
Bound::Excluded(index) => *index + 1,
Bound::Unbounded => 0,
};
let end = match blocks.end_bound() {
Bound::Included(index) => *index + 1,
Bound::Excluded(index) => *index,
Bound::Unbounded => usize::MAX,
};
// The two-phase shift below relies on `max_index` being larger than every index in the
// shift's WHERE clause, so it has to be sampled *before* the DELETE.
let max_index: usize = {
let mut stmt = self.db.prepare_cached(
r#"
SELECT
COALESCE(MAX("index"), 0)
FROM
liteboxfs_file_blocks
WHERE
file = ?;
"#,
)?;
stmt.query_one(rusqlite::params![file], |row| row.get(0))?
};
let removed = {
let mut stmt = self.db.prepare_cached(
r#"
DELETE FROM
liteboxfs_file_blocks
WHERE
file = ?
AND "index" >= ?
AND "index" < ?
RETURNING
block;
"#,
)?;
let blocks = stmt.query_map(rusqlite::params![file, start, end], |row| {
let block_id: i64 = row.get(0)?;
Ok(BlockId::from(block_id))
})?;
blocks.collect::<Result<Vec<_>, _>>()?
};
// Shift subsequent blocks down to keep the per-file index sequence contiguous, matching
// the in-memory block list's view of the file. The shift is split into two phases for
// the same reason as `insert_block`: a single naive `UPDATE` could trigger a UNIQUE
// constraint violation when a shifted value collides with another unshifted row in the
// same statement.
if !removed.is_empty() && end < usize::MAX {
let shift = end - start;
{
let mut stmt = self.db.prepare_cached(
r#"
UPDATE
liteboxfs_file_blocks
SET
"index" = "index" + ?
WHERE
file = ?
AND
"index" >= ?;
"#,
)?;
stmt.execute(rusqlite::params![max_index, file, end])?;
}
{
let mut stmt = self.db.prepare_cached(
r#"
UPDATE
liteboxfs_file_blocks
SET
"index" = "index" - ? - ?
WHERE
file = ?
AND
"index" >= ?;
"#,
)?;
stmt.execute(rusqlite::params![max_index, shift, file, max_index + end])?;
}
}
Ok(removed)
}
}
impl<'conn> BlockStore for &mut SqlStore<'conn> {
fn list_blocks(&self, file: StoreFileId) -> crate::Result<BlockList> {
<SqlStore<'conn> as BlockStore>::list_blocks(*self, file)
}
fn read_block(&mut self, block: BlockId, buf: &mut Vec<u8>) -> crate::Result<ReadBlock> {
<SqlStore<'conn> as BlockStore>::read_block(*self, block, buf)
}
fn replace_block(
&mut self,
file: StoreFileId,
index: BlockIndex,
input: BlockStoreInput,
) -> crate::Result<BlockId> {
<SqlStore<'conn> as BlockStore>::replace_block(*self, file, index, input)
}
fn insert_block(
&mut self,
file: StoreFileId,
index: BlockIndex,
input: BlockStoreInput,
) -> crate::Result<BlockId> {
<SqlStore<'conn> as BlockStore>::insert_block(*self, file, index, input)
}
fn append_block(
&mut self,
file: StoreFileId,
index: BlockIndex,
input: BlockStoreInput,
) -> crate::Result<BlockId> {
<SqlStore<'conn> as BlockStore>::append_block(*self, file, index, input)
}
fn remove_blocks<R: RangeBounds<usize>>(
&mut self,
file: StoreFileId,
blocks: R,
) -> crate::Result<Vec<BlockId>> {
<SqlStore<'conn> as BlockStore>::remove_blocks(*self, file, blocks)
}
}