tinystore 0.11.0

A small storage runtime for applications on SQLite: SQL, key-value state, durable jobs, files, search, logs and metrics in one directory
Documentation
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
//! blobs over the protocol: files by handle, the server holding nothing
//! between calls, as S3 does. A get answers what the file carries, and all
//! its bytes when they fit one message; a read streams a file's bytes, the
//! whole file checked or a range, on the ETag a get gave, as pieces within
//! the client's credit. An upload's pieces come as the client's DATA,
//! written in order as they come, the credit of each given back once it is
//! written, and its last DATA publishes the file.
//!
//! A getOrCreate is handed over as a once run is: the file there ends its
//! stream, or the run is the client's, which writes its function's result
//! with a put or an upload naming the stream in `fill`, then ends the
//! stream with its last DATA. The claim is the connection's while the stream
//! is open: the store renews it, and a CANCEL or the connection's end lets it
//! go at once.
//!
//! A directory's handle takes the reads of a set of files, its bytes from
//! the disk, and its own calls: a scan, its changes and a forget. A
//! connection that came with a token names no path of the server's machine:
//! it reaches the folders the server was given alone (BLOB-15).

use std::collections::{BTreeMap, HashMap, VecDeque};
use std::io::{Read, Seek, SeekFrom};
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, Weak};
use std::time::Duration;

use super::codec::{self, Map, Message};
use super::protocol::{
    BlobsAt, BlobsChange, BlobsChanges, BlobsClaim, BlobsDelete, BlobsDirectory, BlobsExpire, BlobsFilled, BlobsFolder,
    BlobsFound, BlobsGot, BlobsHead, BlobsInfo, BlobsList, BlobsMove, BlobsOpen, BlobsPage, BlobsPiece, BlobsRead,
    BlobsScanned, BlobsSetMaxSize, BlobsSince, BlobsTrim, BlobsTrimmed, BlobsUsage, BlobsWrite, BlobsWritten, Empty,
    Failure, Handle, method,
};
use super::{Lent, Link, Route};
use crate::blobs::{
    Change, Directory, FileCall, FileInfo, Files, Filling, Hand, Outcome, StoredFile, TIMEOUT, Upload, WhenFull,
};
use crate::sync::lock;
use crate::{Error, ErrorKind, Store, unix_millis};

use super::Answered;

pub(crate) fn route(called: u16) -> Route {
    match called {
        method::BLOBS_READ => Route::Read,
        method::BLOBS_UPLOAD => Route::Upload,
        method::BLOBS_GET_OR_CREATE => Route::Fill,
        _ => Route::Worker,
    }
}

/// What a connection opened, by handle, and its streams of a file's bytes.
#[derive(Default)]
pub(crate) struct Handles {
    open: super::ByHandle<Held>,
    reads: Arc<Mutex<HashMap<u32, Arc<Getting>>>>,
    uploads: Arc<Mutex<HashMap<u32, Arc<Uploading>>>>,
    /// The clients' getOrCreates, by stream, from the REQUEST to the last frame.
    fills: Mutex<HashMap<u32, ClientFill>>,
    /// Numbers each getOrCreate's asking, so that a wait that ends after its
    /// stream was cancelled, and its number named again, finds another there.
    asked: AtomicU64,
    /// The connection came with a token, and names no path (BLOB-15).
    token: bool,
    /// The folders the server was given, by their directory's name.
    given: Option<Arc<BTreeMap<String, PathBuf>>>,
}

/// What a handle names: a set of files, or a directory, which no call writes.
#[derive(Clone)]
enum Held {
    Files(Files),
    Directory(Directory),
}

impl Handles {
    /// The handles of a connection, as its host admits it and the folders it was given.
    pub(crate) fn of(connect: &crate::pipe::Connect) -> Handles {
        Handles { token: connect.admit.is_some(), given: connect.directories.clone(), ..Handles::default() }
    }

    /// The files or the directory a handle names, in the folder of these segments.
    fn held(&self, handle: u64, folder: &[String]) -> Result<Held, Failure> {
        let held = self.open.get(handle, || format!("handle {handle} names nothing this connection opened"))?;
        Ok(match held {
            Held::Files(files) => {
                Held::Files(folder.iter().fold(files, |files, segment| files.folder(segment.as_str())))
            }
            Held::Directory(directory) => {
                Held::Directory(folder.iter().fold(directory, |directory, segment| directory.folder(segment.as_str())))
            }
        })
    }

    /// The files a handle names, in the folder of these segments; a
    /// directory's handle is refused, since the store never writes one.
    fn at(&self, handle: u64, folder: &[String]) -> Result<Files, Failure> {
        match self.held(handle, folder)? {
            Held::Files(files) => Ok(files),
            Held::Directory(directory) => Err(Failure::invalid(format!(
                "handle {handle} names {directory:?}, which the store lists and reads and never writes"
            ))),
        }
    }

    /// The directory a handle names, as its own calls take it.
    fn directory_at(&self, handle: u64) -> Result<Directory, Failure> {
        match self.held(handle, &[])? {
            Held::Directory(directory) => Ok(directory),
            Held::Files(_) => Err(Failure::invalid(format!("handle {handle} names files, not a directory"))),
        }
    }

    /// Credit the client granted a read's stream.
    pub(crate) fn grant(&self, stream: u32, credit: u32) {
        let getting = lock(&self.reads).get(&stream).cloned();
        if let Some(getting) = getting {
            getting.grant(credit);
        }
    }

    /// The upload a client's DATA on `stream` is a piece of.
    pub(crate) fn uploading(&self, stream: u32) -> Option<Arc<Uploading>> {
        lock(&self.uploads).get(&stream).cloned()
    }

    /// Ends a read or an upload the client cancelled; says whether there was one.
    pub(crate) fn cancel(&self, stream: u32) -> bool {
        // out of its map before it ends, which takes the map's lock again:
        // an `if let` on the lock would hold it and wait for itself
        let getting = lock(&self.reads).remove(&stream);
        if let Some(getting) = getting {
            getting.end(Err(Failure::cancelled("the client cancelled its read")));
            return true;
        }
        let uploading = lock(&self.uploads).remove(&stream);
        if let Some(uploading) = uploading {
            uploading.cancel();
            return true;
        }
        false
    }

    /// Lets go of every stream at the session's end: a file read is closed,
    /// an upload's bytes removed.
    pub(crate) fn end(&self) {
        let reads: Vec<_> = lock(&self.reads).drain().map(|(_, getting)| getting).collect();
        for getting in reads {
            getting.drop_file();
        }
        let uploads: Vec<_> = lock(&self.uploads).drain().map(|(_, uploading)| uploading).collect();
        for uploading in uploads {
            uploading.cancel();
        }
        // a run handed over lets its claim go as it drops, outside the lock
        let fills: Vec<_> = lock(&self.fills).drain().collect();
        drop(fills);
    }

    /// Starts asking for a getOrCreate's path on `stream`, and numbers it.
    pub(crate) fn ask_fill(&self, stream: u32) -> u64 {
        let asked = self.asked.fetch_add(1, Ordering::Relaxed);
        lock(&self.fills).insert(stream, ClientFill::Asking(asked));
        asked
    }

    /// Settles the asking `asked` on `stream` as it came out, unless the
    /// stream asks no more. An answer is sent under the lock, so that a
    /// CANCEL finds the stream still asking or after its frames: `send`
    /// takes the answer and whether it ends the stream.
    pub(crate) fn settle_fill(
        &self,
        (stream, asked): (u32, u64),
        came: Asked,
        send: impl FnOnce(Result<Vec<u8>, Failure>, bool),
    ) {
        let mut fills = lock(&self.fills);
        if !matches!(fills.get(&stream), Some(ClientFill::Asking(asking)) if *asking == asked) {
            drop(fills);
            return drop(came);
        }
        match came {
            Asked::Answered(answer) => {
                fills.remove(&stream);
                send(answer, true);
            }
            Asked::Handed(handed) => {
                fills.insert(stream, ClientFill::Handed(handed));
                send(Ok(BlobsFilled::default().encode()), false);
            }
            Asked::Waiting => {}
        }
    }

    /// The run handed over on `stream`, taken out for its last DATA; none for
    /// a stream that is no getOrCreate's, or one still asking.
    pub(crate) fn take_handed(&self, stream: u32) -> Option<Box<Handed>> {
        let mut fills = lock(&self.fills);
        match fills.remove(&stream)? {
            ClientFill::Handed(handed) => Some(handed),
            asking => {
                fills.insert(stream, asking);
                None
            }
        }
    }

    /// The getOrCreate on `stream` the client cancelled, taken out.
    pub(crate) fn cancel_fill(&self, stream: u32) -> Option<ClientFill> {
        lock(&self.fills).remove(&stream)
    }

    /// The owner of the run handed over on `stream`, which a write of
    /// `files`' path `whole` lands.
    fn fill_owner(&self, stream: u64, files: &Files, whole: &str) -> Result<i64, Failure> {
        let fills = lock(&self.fills);
        let handed = u32::try_from(stream).ok().and_then(|stream| match fills.get(&stream) {
            Some(ClientFill::Handed(handed)) => Some(handed),
            _ => None,
        });
        match handed {
            Some(handed) if handed.filling.is_of(files, whole) => Ok(handed.filling.owner()),
            Some(_) => Err(Failure::invalid(format!("fill {stream}: the run of another path"))),
            None => Err(Failure::invalid(format!("fill {stream}: no getOrCreate's run this connection was handed"))),
        }
    }
}

/// Where a client's getOrCreate stands.
pub(crate) enum ClientFill {
    /// Asking for its path, or waiting for the run that holds it.
    Asking(u64),
    /// Handed to the client, until its last DATA, a CANCEL or the session's end.
    Handed(Box<Handed>),
}

/// A run handed to a client: its claim, and the files and the path its
/// outcome and its failure are said of.
pub(crate) struct Handed {
    filling: Filling,
    files: Files,
    whole: String,
}

/// What a getOrCreate's asking came to.
pub(crate) enum Asked {
    /// The file there, or a failure: the stream's end.
    Answered(Result<Vec<u8>, Failure>),
    Handed(Box<Handed>),
    /// A run of the store's holds the path; the stream asks again as it ends.
    Waiting,
}

/// What a getOrCreate waiting for a run is told as it ends: the failure that
/// run ended with, or none to ask again.
pub(crate) type Again = Box<dyn FnOnce(Option<Failure>) + Send>;

/// Asks for a getOrCreate's path: the file there, or the run claimed for the
/// client, or a wait for the run that holds it, `again` told as it ends.
/// `waited` says it waited before, which a file found counts.
pub(crate) fn get_or_create(handles: &Handles, body: &[u8], (waited, max_body): (bool, usize), again: Again) -> Asked {
    match asked_fill(handles, body, (waited, max_body), again) {
        Ok(asked) => asked,
        Err(failure) => Asked::Answered(Err(failure)),
    }
}

fn asked_fill(
    handles: &Handles,
    body: &[u8],
    (waited, max_body): (bool, usize),
    again: Again,
) -> Result<Asked, Failure> {
    let asked = BlobsClaim::decode(body)?;
    let files = handles.at(asked.handle, &asked.folder)?;
    let whole = files.key(&asked.path).to_get_or_create()?;
    let timeout = asked.timeout.map_or(TIMEOUT, Duration::from_millis);
    let counted = files.clone();
    let waiter = Box::new(move |failure: Option<Arc<crate::Error>>| {
        if failure.is_some() {
            Outcome::Failed.count(&counted);
        }
        again(failure.map(|failure| Failure::from(failure.duplicate())));
    });
    let mut waiter: Option<crate::blobs::Waiter> = Some(waiter);
    loop {
        if let Some(file) = files.get(&asked.path)? {
            (if waited { Outcome::Wait } else { Outcome::Hit }).count(&files);
            return Ok(Asked::Answered(filled(file, max_body)));
        }
        let told = waiter.take().ok_or_else(|| Failure::internal("a getOrCreate asked twice for one wait"))?;
        match files.hand(&whole, timeout, None, told).map_err(|error| files.fail(&whole, error))? {
            Hand::There => waiter = Some(Box::new(|_| {})),
            Hand::Waiting => return Ok(Asked::Waiting),
            Hand::Yours(filling) => return Ok(Asked::Handed(Box::new(Handed { filling, files, whole }))),
        }
    }
}

/// A getOrCreate's answer of the file it found: a get's answer, since
/// `blobs.Filled` numbers its info and bytes as `blobs.Got` does and leaves
/// its failure out, as the test beside it checks.
fn filled(mut file: StoredFile, max_body: usize) -> Result<Vec<u8>, Failure> {
    let info = info_of(file.info().clone());
    // as a get's: a larger file waits for its read
    if info.size > (max_body / 2) as u64 {
        return Ok(BlobsGot { info: Some(info), bytes: Vec::new() }.encode());
    }
    got_whole(info, &mut file)
}

/// Ends a run handed over at the client's last DATA: its write landed, or
/// was dropped, or the failure it names is every waiting call's answer.
pub(crate) fn run_ended(handed: Box<Handed>, body: &[u8]) -> Answered {
    let ended = BlobsFilled::decode(body)?;
    match ended.failure {
        Some(failure) => {
            Outcome::Failed.count(&handed.files);
            let error = handed.files.fail(&handed.whole, error_of(failure));
            handed.filling.fail(error);
        }
        None => Outcome::Run.count(&handed.files),
    }
    Ok(BlobsFilled::default().encode())
}

/// The error a client's failure names, its kind by its code: a code the
/// core does not know is `conflict`, as any failure of a function is.
fn error_of(failure: Failure) -> Error {
    let kind = ErrorKind::named(&failure.code).unwrap_or(ErrorKind::Conflict);
    Error::new(kind, failure.message)
}

/// Answers a call of one message: everything but a read and an upload. A
/// read connection's is `read_only`.
pub(crate) fn call(
    store: &Store,
    handles: &Handles,
    lent: Lent<'_>,
    called: u16,
    body: &[u8],
    (max_body, read_only): (usize, bool),
) -> Answered {
    match called {
        method::BLOBS_OPEN => open(store, handles, lent, BlobsOpen::decode(body)?, read_only),
        method::BLOBS_PUT => put(handles, BlobsWrite::decode(body)?),
        method::BLOBS_GET => get(handles, &BlobsAt::decode(body)?, max_body),
        method::BLOBS_HEAD => head(handles, &BlobsAt::decode(body)?),
        method::BLOBS_DELETE => delete(handles, BlobsDelete::decode(body)?),
        method::BLOBS_COPY | method::BLOBS_RENAME => moved(handles, called, BlobsMove::decode(body)?),
        method::BLOBS_EXPIRE => expire(handles, &BlobsExpire::decode(body)?),
        method::BLOBS_LIST => list(handles, BlobsList::decode(body)?),
        method::BLOBS_USAGE => usage(handles, &BlobsFolder::decode(body)?),
        method::BLOBS_CLEAR => clear(handles, &BlobsFolder::decode(body)?),
        method::BLOBS_DIRECTORY => directory(store, handles, BlobsDirectory::decode(body)?, read_only),
        method::BLOBS_SCAN => scan(handles, &BlobsFolder::decode(body)?),
        method::BLOBS_CHANGES => changes(handles, BlobsSince::decode(body)?),
        method::BLOBS_FORGET => forget(handles, &BlobsFolder::decode(body)?),
        method::BLOBS_SET_MAX_SIZE => set_max_size(handles, &BlobsSetMaxSize::decode(body)?),
        method::BLOBS_TRIM => trim(handles, &BlobsTrim::decode(body)?),
        other => Err(Failure::unimplemented(format!("method {other:#06x}"))),
    }
}

/// Answers a call made inside a database's transaction, on its stream: a
/// write of files kept in the database's file commits with its rows.
#[cfg(feature = "sql")]
pub(crate) fn call_in(handles: &Handles, tx: &crate::Transaction<'_>, called: u16, body: &[u8]) -> Answered {
    use crate::TxHandle as _;
    match called {
        method::BLOBS_PUT => {
            let mut asked = BlobsWrite::decode(body)?;
            if asked.fill.is_some() {
                return Err(Failure::invalid("a getOrCreate's write inside a transaction: write it outside"));
            }
            let files = handles.at(asked.handle, &asked.folder)?;
            let bytes = std::mem::take(&mut asked.bytes);
            let call = options(files.in_tx(tx).key(&asked.path), &asked);
            let info = if asked.create {
                call.create(&bytes)?.then(|| files.in_tx(tx).head(&asked.path)).transpose()?.flatten()
            } else {
                Some(call.put(&bytes)?)
            };
            Ok(BlobsWritten { info: info.map(info_of) }.encode())
        }
        method::BLOBS_HEAD => {
            let asked = BlobsAt::decode(body)?;
            let info = handles.at(asked.handle, &asked.folder)?.in_tx(tx).head(&asked.path)?;
            Ok(BlobsHead { info: info.map(info_of) }.encode())
        }
        method::BLOBS_DELETE => {
            let asked = BlobsDelete::decode(body)?;
            let files = handles.at(asked.handle, &asked.folder)?;
            let mut call = files.in_tx(tx).key(&asked.path);
            if let Some(etag) = asked.if_match {
                call = call.if_match(etag);
            }
            call.delete()?;
            Ok(Empty {}.encode())
        }
        method::BLOBS_COPY | method::BLOBS_RENAME => {
            let asked = BlobsMove::decode(body)?;
            let files = handles.at(asked.handle, &asked.folder)?;
            let mut call = files.in_tx(tx).key(&asked.to);
            if let Some(etag) = asked.if_match {
                call = call.if_match(etag);
            }
            let info = if called == method::BLOBS_COPY {
                call.copy_from(&asked.from)?
            } else {
                call.rename_from(&asked.from)?
            };
            Ok(info_of(info).encode())
        }
        other => Err(Failure::invalid(format!(
            "method {other:#06x} does not run inside a transaction: files join one with put, head, delete, copy \
             and rename"
        ))),
    }
}

/// Opens a set of files as the call says; a connection that only reads opens
/// only one the store holds, found first, so that the open finds it as that
/// did and makes nothing. Its reads record no use, and it gives the set no
/// idle term nor a ceiling it does not keep: each would be a write.
fn open(store: &Store, handles: &Handles, lent: Lent<'_>, asked: BlobsOpen, read_only: bool) -> Answered {
    if read_only && asked.idle.is_some() {
        return Err(Failure::permission("a read connection gives a set no idle: a read that renews a file writes it"));
    }
    let when_full = asked.when_full.as_deref().map(WhenFull::of).transpose()?;
    let mut files = match asked.database {
        #[cfg(feature = "sql")]
        Some(database) => {
            let shared = lent(database)?;
            if read_only {
                crate::blobs::set_in_file_as_it_is(&shared, &asked.name)?;
            }
            crate::blobs::FilesBuilder::in_file(shared, &asked.name)
        }
        #[cfg(not(feature = "sql"))]
        Some(database) => {
            let _ = lent;
            return Err(Failure::invalid(format!("database {database}: this server keeps no databases")));
        }
        None => {
            if read_only {
                crate::blobs::set_as_it_is(store, &asked.name)?;
            }
            store.files(&asked.name)
        }
    };
    if let Some(ttl) = asked.ttl {
        files = files.ttl(Duration::from_millis(ttl));
    }
    if let Some(most) = asked.max_file_size {
        files = files.max_file_size(most);
    }
    if let Some(idle) = asked.idle {
        files = files.idle(Duration::from_millis(idle));
    }
    let files = if read_only {
        let files = files.observing().open()?;
        let kept = files.kept_ceiling()?;
        let other_size = asked.max_size.is_some_and(|asked| kept.map(|(size, _)| size) != Some(asked));
        let other_when = when_full.is_some_and(|asked| kept.map(|(_, when)| when) != Some(asked));
        if other_size || other_when {
            return Err(Failure::permission(
                "a read connection writes no ceiling: open the set without max_size, or with the one it keeps",
            ));
        }
        files
    } else {
        if let Some(bytes) = asked.max_size {
            files = files.max_size(bytes);
        }
        if let Some(when_full) = when_full {
            files = files.when_full(when_full);
        }
        files.open()?
    };
    let handle = handles.open.keep(Held::Files(files));
    Ok(Handle { handle }.encode())
}

fn set_max_size(handles: &Handles, asked: &BlobsSetMaxSize) -> Answered {
    let files = handles.at(asked.handle, &[])?;
    let mut change = match asked.max_size {
        Some(bytes) => files.set_max_size(bytes),
        None => files.remove_max_size(),
    };
    if let Some(when_full) = asked.when_full.as_deref() {
        change = change.when_full(WhenFull::of(when_full)?);
    }
    if let Some(current) = asked.if_current {
        change = change.if_current(current);
    }
    change.apply()?;
    Ok(Empty {}.encode())
}

fn trim(handles: &Handles, asked: &BlobsTrim) -> Answered {
    let trim = handles.at(asked.handle, &[])?.trim(asked.to)?;
    Ok(BlobsTrimmed { evictions: trim.evictions, freed: trim.freed }.encode())
}

/// Opens a directory: on the path a connection of this machine names, or on
/// the folder the server was given under the name. One that came with a
/// token names no path, and opens only what the server was given; one that
/// only reads opens only a directory the store holds, and scans nothing.
fn directory(store: &Store, handles: &Handles, asked: BlobsDirectory, read_only: bool) -> Answered {
    let name = asked.name.as_str();
    let refused = |why: &str| Failure::invalid(format!("directory {name}: {why}"));
    let given = handles.given.as_ref().and_then(|given| given.get(name)).cloned();
    let path = match (asked.path, handles.token) {
        (Some(_), true) => return Err(refused("a connection that came with a token names no path of the server's")),
        (Some(path), false) => Some(PathBuf::from(path)),
        (None, true) if given.is_none() => return Err(refused("the server was given no directory of the name")),
        (None, _) => given,
    };
    if read_only {
        if asked.every.is_some() {
            return Err(refused("a scan on a connection that only reads"));
        }
        crate::blobs::directory_as_it_is(store, name)?;
    }
    let mut opening = store.directory(name).ignore(asked.ignore).growing(asked.growing);
    if let Some(path) = path {
        opening = opening.path(path);
    }
    if let Some(every) = asked.every {
        opening = opening.every(Duration::from_millis(every));
    }
    let handle = handles.open.keep(Held::Directory(opening.open()?));
    Ok(Handle { handle }.encode())
}

fn scan(handles: &Handles, asked: &BlobsFolder) -> Answered {
    let directory = handles.directory_at(asked.handle)?;
    let at = asked.folder.iter().fold(directory, |directory, segment| directory.folder(segment.as_str()));
    let scan = at.scan()?;
    let scanned = BlobsScanned {
        added: scan.added,
        changed: scan.changed,
        removed: scan.removed,
        skipped: scan.skipped,
        count: scan.count,
        size: scan.size,
    };
    Ok(scanned.encode())
}

fn changes(handles: &Handles, asked: BlobsSince) -> Answered {
    let directory = handles.directory_at(asked.handle)?;
    let mut call = directory.changes();
    if let Some(after) = asked.after {
        call = call.after(after);
    }
    let page = call.page()?;
    let told = |change: Change| match change {
        Change::Added => "added",
        Change::Changed => "changed",
        Change::Removed => "removed",
    };
    let items = page
        .items
        .into_iter()
        .map(|item| BlobsChange { path: item.path, change: told(item.change).to_owned(), info: item.info.map(info_of) })
        .collect();
    Ok(BlobsChanges { items, next: page.next }.encode())
}

fn forget(handles: &Handles, asked: &BlobsFolder) -> Answered {
    let directory = handles.directory_at(asked.handle)?;
    asked.folder.iter().fold(directory, |directory, segment| directory.folder(segment.as_str())).forget()?;
    Ok(Empty {}.encode())
}

fn put(handles: &Handles, mut asked: BlobsWrite) -> Answered {
    let files = handles.at(asked.handle, &asked.folder)?;
    let bytes = std::mem::take(&mut asked.bytes);
    let info = filling(handles, &files, &asked)?.written(&bytes, asked.create)?;
    Ok(BlobsWritten { info: info.map(info_of) }.encode())
}

/// A write's call, which lands the run of the getOrCreate its `fill` names.
fn filling<'f>(handles: &Handles, files: &'f Files, asked: &BlobsWrite) -> Result<FileCall<'f>, Failure> {
    let call = options(files.key(&asked.path), asked);
    let Some(stream) = asked.fill else { return Ok(call) };
    let whole = files.whole(&asked.path)?;
    Ok(call.filling(handles.fill_owner(stream, files, &whole)?))
}

fn get(handles: &Handles, asked: &BlobsAt, max_body: usize) -> Answered {
    let Some(mut file) = handles.held(asked.handle, &asked.folder)?.get(&asked.path)? else {
        return Ok(BlobsGot::default().encode());
    };
    let info = info_of(file.info().clone());
    // half a body, the rest room for what it carries; a larger file waits for its read
    if info.size > (max_body / 2) as u64 {
        return Ok(BlobsGot { info: Some(info), bytes: Vec::new() }.encode());
    }
    got_whole(info, &mut file)
}

/// A get's answer with all the file's bytes, read straight into the message
/// that carries them: `BlobsGot { info, bytes }.encode()`, as the test beside
/// it checks, without the file's bytes held twice. A mebibyte's answer copied
/// from bytes of their own cost a fresh mebibyte and its copy (issue #10).
fn got_whole(info: BlobsInfo, file: &mut StoredFile) -> Answered {
    let size = usize::try_from(info.size).map_err(|_| Failure::internal("a file past this machine's memory"))?;
    // as `BlobsGot::size` reserves: a map's head, two fields' numbers, a bin's head of five
    let mut out = Vec::with_capacity(3 + 1 + info.size() + 1 + 5 + size);
    let mut map = Map::open(&mut out);
    map.field(&mut out, 1);
    codec::write_message(&mut out, &info);
    if size > 0 {
        map.field(&mut out, 2);
        codec::write_bin_in_place(&mut out, size, |bytes| file.read_whole(bytes))?;
    }
    map.close(&mut out);
    Ok(out)
}

fn head(handles: &Handles, asked: &BlobsAt) -> Answered {
    let info = handles.held(asked.handle, &asked.folder)?.head(&asked.path)?;
    Ok(BlobsHead { info: info.map(info_of) }.encode())
}

fn delete(handles: &Handles, asked: BlobsDelete) -> Answered {
    let files = handles.at(asked.handle, &asked.folder)?;
    let mut call = files.key(&asked.path);
    if let Some(etag) = asked.if_match {
        call = call.if_match(etag);
    }
    call.delete()?;
    Ok(Empty {}.encode())
}

/// A copy or a rename, as `called` says: the file at `from`, at `to`.
fn moved(handles: &Handles, called: u16, asked: BlobsMove) -> Answered {
    let files = handles.at(asked.handle, &asked.folder)?;
    let mut call = files.key(&asked.to);
    if let Some(etag) = asked.if_match {
        call = call.if_match(etag);
    }
    let info = if called == method::BLOBS_COPY { call.copy_from(&asked.from)? } else { call.rename_from(&asked.from)? };
    Ok(info_of(info).encode())
}

fn expire(handles: &Handles, asked: &BlobsExpire) -> Answered {
    let files = handles.at(asked.handle, &asked.folder)?;
    let found = files.expire(&asked.path, Duration::from_millis(asked.after))?;
    Ok(BlobsFound { found }.encode())
}

fn list(handles: &Handles, asked: BlobsList) -> Answered {
    let limit = asked.limit.map(|limit| usize::try_from(limit).unwrap_or(usize::MAX));
    let page = match handles.held(asked.handle, &asked.folder)? {
        Held::Files(files) => {
            let mut list = files.list().prefix(asked.prefix);
            if let Some(after) = asked.after {
                list = list.after(after);
            }
            if let Some(limit) = limit {
                list = list.limit(limit);
            }
            list.page()?
        }
        Held::Directory(directory) => {
            let mut list = directory.list().prefix(asked.prefix);
            if let Some(after) = asked.after {
                list = list.after(after);
            }
            if let Some(limit) = limit {
                list = list.limit(limit);
            }
            list.page()?
        }
    };
    Ok(BlobsPage { files: page.files.into_iter().map(info_of).collect(), next: page.next }.encode())
}

fn usage(handles: &Handles, asked: &BlobsFolder) -> Answered {
    let usage = match handles.held(asked.handle, &asked.folder)? {
        Held::Files(files) => files.usage()?,
        Held::Directory(directory) => directory.usage()?,
    };
    Ok(BlobsUsage { count: usage.count, size: usage.size, max_size: usage.max_size, evictions: usage.evictions }
        .encode())
}

fn clear(handles: &Handles, asked: &BlobsFolder) -> Answered {
    handles.at(asked.handle, &asked.folder)?.clear()?;
    Ok(Empty {}.encode())
}

/// A write's options from its message.
fn options<'f>(mut call: FileCall<'f>, asked: &BlobsWrite) -> FileCall<'f> {
    if let Some(content_type) = &asked.content_type {
        call = call.content_type(content_type.clone());
    }
    for (name, value) in asked.meta.iter().flatten() {
        call = call.meta(name.clone(), value.clone());
    }
    if let Some(ttl) = asked.ttl {
        call = call.ttl(Duration::from_millis(ttl));
    }
    if let Some(etag) = &asked.if_match {
        call = call.if_match(etag.clone());
    }
    if let Some(size) = asked.size {
        call = call.size(size);
    }
    call
}

fn info_of(info: FileInfo) -> BlobsInfo {
    BlobsInfo {
        path: info.path,
        size: info.size,
        etag: info.etag,
        content_type: info.content_type,
        last_modified: unix_millis(info.last_modified),
        expires: info.expires.map(unix_millis),
        meta: (!info.meta.is_empty()).then_some(info.meta),
    }
}

/// The largest piece of a file a message carries: half of what the client
/// takes at once, so that one grant lets the next piece go while it reads
/// the last, less the piece's own few bytes.
fn piece_bound(link: &Link) -> usize {
    super::part_bound(link.max_body, link.credit, 16)
}

/// How a read's answer goes: whole in the RESPONSE, or the RESPONSE's piece
/// and then the rest within the client's credit.
pub(crate) enum Reading {
    Whole(Vec<u8>),
    Begun(Vec<u8>, Arc<Getting>),
}

/// Reads a file's bytes: the whole file, checked, or a range, which is not.
/// The file is opened here and held until the read ends, so that the bytes
/// are those of the ETag it was asked on, through a replace.
///
/// A growing directory's file holds to no version: the read goes on to the
/// size the file has as each piece is read, and each piece brings it.
pub(crate) fn read(handles: &Handles, stream: u32, body: &[u8], link: &Link) -> Result<Reading, Failure> {
    let asked = BlobsRead::decode(body)?;
    let held = handles.held(asked.handle, &asked.folder)?;
    let found = held.get(&asked.path)?;
    let growing = found.as_ref().is_some_and(StoredFile::is_growing);
    let mut file = match (found, &asked.if_match) {
        (Some(file), Some(etag)) if !growing && !file.matches(etag) => return Err(held.changed(&asked.path)),
        (None, Some(_)) => return Err(held.changed(&asked.path)),
        (None, None) => return Err(held.fail(&asked.path, Error::not_found("no file is there"))),
        (Some(file), _) => file,
    };
    let size = file.info().size;
    let (from, left) = if growing {
        (asked.offset.unwrap_or(0), asked.length.unwrap_or(u64::MAX))
    } else {
        let from = asked.offset.unwrap_or(0).min(size);
        (from, asked.length.map_or(size - from, |length| length.min(size - from)))
    };
    file.seek(SeekFrom::Start(from)).map_err(|error| Error::io("blobs: seek a file", error))?;
    let piece = piece_bound(link);
    let first = bound(piece, left);
    let (response, read) = read_piece(&mut file, first)?;
    let left = left - read as u64;
    if left == 0 || read < first {
        return Ok(Reading::Whole(response));
    }
    let state = GetState { file: Some(file), left, credit: link.credit, busy: false, ended: false };
    let getting = Arc::new(Getting {
        stream,
        piece,
        state: Mutex::new(state),
        link: link.clone(),
        reads: Arc::downgrade(&handles.reads),
    });
    lock(&handles.reads).insert(stream, Arc::clone(&getting));
    Ok(Reading::Begun(response, getting))
}

impl Held {
    fn get(&self, path: &str) -> crate::Result<Option<StoredFile>> {
        match self {
            Held::Files(files) => files.get(path),
            Held::Directory(directory) => directory.get(path),
        }
    }

    fn head(&self, path: &str) -> crate::Result<Option<FileInfo>> {
        match self {
            Held::Files(files) => files.head(path),
            Held::Directory(directory) => directory.head(path),
        }
    }

    /// A read on an ETag the path no longer holds, as a write's `ifMatch` is.
    fn changed(&self, path: &str) -> Failure {
        self.fail(path, Error::new(ErrorKind::Conflict, "the path holds no file with the ETag given"))
    }

    fn fail(&self, path: &str, error: Error) -> Failure {
        match self {
            Held::Files(files) => files.fail(path, error).into(),
            Held::Directory(directory) => directory.fail(path, error).into(),
        }
    }
}

/// The bytes the next piece reads: a piece's, or fewer when the read has
/// fewer left.
fn bound(piece: usize, left: u64) -> usize {
    usize::try_from(left).map_or(piece, |left| left.min(piece))
}

/// The next `length` bytes of a file as a piece's message, read straight
/// into it: `BlobsPiece { bytes }.encode()`, as the test beside it checks.
/// The read's last bytes wait until the whole file's SHA-256 is checked.
/// And the bytes it read: `length`, or of a growing file fewer at its end.
fn read_piece(file: &mut StoredFile, length: usize) -> Result<(Vec<u8>, usize), Failure> {
    if file.is_growing() {
        return growing_piece(file, length);
    }
    // as `BlobsPiece::size` reserves: a map's head, a field's number, a bin's head of five
    let mut out = Vec::with_capacity(3 + 1 + 5 + length);
    let mut map = Map::open(&mut out);
    if length > 0 {
        map.field(&mut out, 1);
        codec::write_bin_in_place(&mut out, length, |bytes| fill(file, bytes))?;
    }
    map.close(&mut out);
    Ok((out, length))
}

/// Up to `length` bytes of a growing file, as many as it holds now, with the
/// file's size and time as they were read.
fn growing_piece(file: &mut StoredFile, length: usize) -> Result<(Vec<u8>, usize), Failure> {
    let mut bytes = vec![0; length];
    let mut filled = 0;
    while filled < length {
        match file.read(&mut bytes[filled..]) {
            Ok(0) => break,
            Ok(read) => filled += read,
            Err(error) => return Err(Failure::from(read_error(error))),
        }
    }
    bytes.truncate(filled);
    let info = file.info();
    let piece = BlobsPiece { bytes, size: Some(info.size), last_modified: Some(unix_millis(info.last_modified)) };
    Ok((piece.encode(), filled))
}

/// Fills `bytes` from where the file's next read begins; the file reads to
/// its end, so that a read short of it is the store's fault.
fn fill(file: &mut StoredFile, bytes: &mut [u8]) -> Result<(), Failure> {
    let mut filled = 0;
    while filled < bytes.len() {
        match file.read(&mut bytes[filled..]) {
            Ok(0) => return Err(Failure::internal("a read of a file ended before its piece")),
            Ok(read) => filled += read,
            Err(error) => return Err(Failure::from(read_error(error))),
        }
    }
    Ok(())
}

/// The store's error a read carried, or an I/O one.
fn read_error(error: std::io::Error) -> crate::Error {
    // An OS error carries no inner error, and `into_inner` would drop it whole.
    if !error.get_ref().is_some_and(|inner| inner.is::<crate::Error>()) {
        return crate::Error::io("blobs: read a file", error);
    }
    match error.into_inner().map(|inner| inner.downcast::<crate::Error>()) {
        Some(Ok(error)) => *error,
        _ => crate::Error::internal("blobs: a read failed"),
    }
}

/// A read's bytes going out a piece at a time within the client's credit.
pub(crate) struct Getting {
    stream: u32,
    piece: usize,
    state: Mutex<GetState>,
    link: Link,
    reads: Weak<Mutex<HashMap<u32, Arc<Getting>>>>,
}

struct GetState {
    /// The file, here between two runs of the pieces that go.
    file: Option<StoredFile>,
    /// The read's bytes still to go.
    left: u64,
    credit: u64,
    /// A worker sends pieces now.
    busy: bool,
    ended: bool,
}

impl Getting {
    fn grant(self: &Arc<Self>, credit: u32) {
        lock(&self.state).credit += u64::from(credit);
        self.pump();
    }

    /// Sends what the credit takes, on a worker, unless one does already.
    pub(crate) fn pump(self: &Arc<Self>) {
        {
            let mut state = lock(&self.state);
            if state.busy || state.ended {
                return;
            }
            state.busy = true;
        }
        let getting = Arc::clone(self);
        (self.link.spawn)(Box::new(move || getting.send_pieces()));
    }

    fn send_pieces(&self) {
        // a piece's message: its bytes and a few of its own
        let room = (self.piece + 16) as u64;
        loop {
            let (mut file, left) = {
                let mut state = lock(&self.state);
                match state.file.take() {
                    Some(file) if !state.ended && state.credit >= room => (file, state.left),
                    file => {
                        state.file = file;
                        state.busy = false;
                        return;
                    }
                }
            };
            let length = bound(self.piece, left);
            let (body, read) = match read_piece(&mut file, length) {
                Ok(piece) => piece,
                Err(failure) => return self.end(Err(failure)),
            };
            let left = left - read as u64;
            // a growing file's read ends at the end it has now
            let ended = left == 0 || read < length;
            if read > 0 {
                // sent under the lock a cancel's end takes, as records' and
                // metrics' are: a piece read while a cancel ended the read
                // never goes after the stream's last frame
                let mut state = lock(&self.state);
                if state.ended {
                    return;
                }
                // the message's bytes, which the client gives back, and not the room:
                // counted apart, the two drift until the stream waits for good
                state.credit -= body.len() as u64;
                (self.link.send)(body);
            }
            if ended {
                return self.end(Ok(Empty {}.encode()));
            }
            let mut state = lock(&self.state);
            state.file = Some(file);
            state.left = left;
        }
    }

    fn end(&self, last: Answered) {
        {
            let mut state = lock(&self.state);
            if std::mem::replace(&mut state.ended, true) {
                return;
            }
            state.file = None;
        }
        if let Some(reads) = self.reads.upgrade() {
            lock(&reads).remove(&self.stream);
        }
        (self.link.finish)(last);
    }

    fn drop_file(&self) {
        let mut state = lock(&self.state);
        state.ended = true;
        state.file = None;
    }
}

/// Begins an upload: its file to write and the client's pieces to come.
pub(crate) fn upload(handles: &Handles, stream: u32, body: &[u8], link: &Link) -> Result<(), Failure> {
    let asked = BlobsWrite::decode(body)?;
    let files = handles.at(asked.handle, &asked.folder)?;
    let upload = filling(handles, &files, &asked)?.upload()?;
    let state =
        UploadState { upload: Some(upload), pieces: VecDeque::new(), busy: false, cancelled: false, ended: false };
    let uploading = Arc::new(Uploading {
        stream,
        create: asked.create,
        state: Mutex::new(state),
        link: link.clone(),
        uploads: Arc::downgrade(&handles.uploads),
    });
    lock(&handles.uploads).insert(stream, uploading);
    Ok(())
}

/// An upload under way: the client's pieces, written in order on a worker.
pub(crate) struct Uploading {
    stream: u32,
    /// Its last DATA writes only where no file is.
    create: bool,
    state: Mutex<UploadState>,
    link: Link,
    uploads: Weak<Mutex<HashMap<u32, Arc<Uploading>>>>,
}

struct UploadState {
    /// The upload, here while no worker writes its pieces.
    upload: Option<Upload>,
    /// The client's DATA not written yet, and whether each was its last.
    pieces: VecDeque<(Vec<u8>, bool)>,
    /// A worker holds the upload and writes the pieces.
    busy: bool,
    /// The client cancelled: whoever holds the upload lets it go, and then
    /// answers, so that `cancelled` means its bytes are gone.
    cancelled: bool,
    ended: bool,
}

impl Uploading {
    /// A DATA the client sent, its last when `last`: queued, and written in
    /// the order it came on a worker.
    pub(crate) fn piece(self: &Arc<Self>, body: Vec<u8>, last: bool) {
        {
            let mut state = lock(&self.state);
            if state.ended || state.cancelled {
                return;
            }
            state.pieces.push_back((body, last));
            if std::mem::replace(&mut state.busy, true) {
                return;
            }
        }
        let uploading = Arc::clone(self);
        (self.link.spawn)(Box::new(move || uploading.write_pieces()));
    }

    /// Ends the upload with its bytes removed: here, or by the worker that
    /// writes a piece once it is written. A last piece the worker took still
    /// publishes, and the answer says so.
    fn cancel(&self) {
        let upload = {
            let mut state = lock(&self.state);
            state.cancelled = true;
            if state.busy {
                return;
            }
            state.upload.take()
        };
        drop(upload);
        self.end(Err(Failure::cancelled("the client cancelled its upload")));
    }

    fn write_pieces(&self) {
        let Some(mut upload) = lock(&self.state).upload.take() else { return };
        loop {
            let (body, last) = {
                let mut state = lock(&self.state);
                if state.cancelled {
                    drop(state);
                    drop(upload);
                    return self.end(Err(Failure::cancelled("the client cancelled its upload")));
                }
                let Some(next) = state.pieces.pop_front() else {
                    state.upload = Some(upload);
                    state.busy = false;
                    return;
                };
                next
            };
            let length = u32::try_from(body.len()).unwrap_or(u32::MAX);
            let written = if body.is_empty() {
                Ok(())
            } else {
                BlobsPiece::decode(&body).and_then(|piece| upload.take(&piece.bytes).map_err(Failure::from))
            };
            match (written, last) {
                (Err(failure), _) => {
                    drop(upload);
                    return self.end(Err(failure));
                }
                (Ok(()), true) => {
                    let published = upload.publish(self.create).map_err(Failure::from);
                    return self.end(published.map(|info| BlobsWritten { info: info.map(info_of) }.encode()));
                }
                (Ok(()), false) => (self.link.grant)(length),
            }
        }
    }

    fn end(&self, last: Answered) {
        {
            let mut state = lock(&self.state);
            if std::mem::replace(&mut state.ended, true) {
                return;
            }
            state.pieces.clear();
        }
        if let Some(uploads) = self.uploads.upgrade() {
            lock(&uploads).remove(&self.stream);
        }
        (self.link.finish)(last);
    }
}

#[cfg(test)]
#[path = "blobs_tests.rs"]
mod tests;

#[cfg(test)]
#[path = "blobs_fill_tests.rs"]
mod fill_tests;