kcode-k1-transaction-store 0.1.0

Stores immutable K1 transaction bytes in sector-aligned local files
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
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::fs::{self, File, OpenOptions};
use std::io::{self, Write};
use std::os::unix::fs::FileExt;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, RwLock};

pub const TX_ID_BYTES: usize = 12;
pub const SECTOR_BYTES: u64 = 4_096;
pub const INLINE_LIMIT: usize = 262_144;
const PAYLOAD_ALPHABET: &[u8; 64] =
    b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct TxId([u8; TX_ID_BYTES]);

impl TxId {
    pub const fn from_bytes(bytes: [u8; TX_ID_BYTES]) -> Self {
        Self(bytes)
    }

    pub const fn as_bytes(&self) -> &[u8; TX_ID_BYTES] {
        &self.0
    }

    pub const fn into_bytes(self) -> [u8; TX_ID_BYTES] {
        self.0
    }

    pub fn for_transaction(transaction: &[u8]) -> Self {
        let digest = Sha256::digest(transaction);
        let mut bytes = [0; TX_ID_BYTES];
        bytes.copy_from_slice(&digest[..TX_ID_BYTES]);
        Self(bytes)
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PutOutcome {
    Inserted(TxId),
    Duplicate(TxId),
}

#[derive(Debug)]
pub enum StoreError {
    Io(io::Error),
    AlreadyExists,
    InvalidStore,
    StoreFull,
    IdCollision(TxId),
    OutcomeUnknown(TxId),
    ReopenRequired,
    CorruptTransaction(TxId),
}

impl std::fmt::Display for StoreError {
    fn fmt(&self, output: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Io(error) => error.fmt(output),
            Self::AlreadyExists => output.write_str("the store already exists"),
            Self::InvalidStore => output.write_str("the store is invalid"),
            Self::StoreFull => output.write_str("the transaction file is full"),
            Self::IdCollision(id) => write!(output, "transaction identifier collision: {id:?}"),
            Self::OutcomeUnknown(id) => write!(output, "transaction outcome is unknown: {id:?}"),
            Self::ReopenRequired => output.write_str("the store must be reopened before writing"),
            Self::CorruptTransaction(id) => write!(output, "transaction is corrupt: {id:?}"),
        }
    }
}

impl std::error::Error for StoreError {}

impl From<io::Error> for StoreError {
    fn from(error: io::Error) -> Self {
        Self::Io(error)
    }
}

pub struct TransactionStore {
    data: File,
    payload: PathBuf,
    locations: RwLock<HashMap<TxId, u32>>,
    next_sector: Mutex<u64>,
    publication: Mutex<Publication>,
}

struct Publication {
    lookup: File,
    reopen_required: bool,
}

impl TransactionStore {
    pub fn create(root: &Path) -> Result<Self, StoreError> {
        match fs::create_dir(root) {
            Ok(()) => {}
            Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {
                return Err(StoreError::AlreadyExists);
            }
            Err(error) => return Err(error.into()),
        }
        let payload = root.join("payload");
        fs::create_dir(&payload)?;
        for first in PAYLOAD_ALPHABET {
            for second in PAYLOAD_ALPHABET {
                fs::create_dir(payload.join(format!(
                    "{}{}",
                    char::from(*first),
                    char::from(*second)
                )))?;
            }
        }
        File::open(&payload)?.sync_all()?;
        File::create(root.join("transactions.dat"))?.sync_all()?;
        File::create(root.join("lookup.dat"))?.sync_all()?;
        File::open(root)?.sync_all()?;
        let parent = root
            .parent()
            .filter(|path| !path.as_os_str().is_empty())
            .unwrap_or(Path::new("."));
        File::open(parent)?.sync_all()?;
        Self::open(root)
    }

    pub fn open(root: &Path) -> Result<Self, StoreError> {
        let data_path = root.join("transactions.dat");
        let lookup_path = root.join("lookup.dat");
        let payload = root.join("payload");
        if !root.is_dir() || !data_path.is_file() || !lookup_path.is_file() || !payload.is_dir() {
            return Err(StoreError::InvalidStore);
        }
        let mut lookup_bytes = fs::read(&lookup_path)?;
        let complete = lookup_bytes.len() / 16 * 16;
        if complete != lookup_bytes.len() {
            let lookup = OpenOptions::new().write(true).open(&lookup_path)?;
            lookup.set_len(complete as u64)?;
            lookup.sync_data()?;
            lookup_bytes.truncate(complete);
        }
        let mut locations = HashMap::with_capacity(lookup_bytes.len() / 16);
        for entry in lookup_bytes.chunks_exact(16) {
            let mut id = [0; TX_ID_BYTES];
            let mut sector = [0; 4];
            id.copy_from_slice(&entry[..12]);
            sector.copy_from_slice(&entry[12..]);
            if locations
                .insert(TxId::from_bytes(id), u32::from_le_bytes(sector))
                .is_some()
            {
                return Err(StoreError::InvalidStore);
            }
        }
        let data = OpenOptions::new().read(true).write(true).open(data_path)?;
        let next_sector = data.metadata()?.len().div_ceil(SECTOR_BYTES);
        if next_sector > u32::MAX as u64 + 1 {
            return Err(StoreError::StoreFull);
        }
        let lookup = OpenOptions::new().append(true).open(lookup_path)?;
        Ok(Self {
            data,
            payload,
            locations: RwLock::new(locations),
            next_sector: Mutex::new(next_sector),
            publication: Mutex::new(Publication {
                lookup,
                reopen_required: false,
            }),
        })
    }

    pub fn put(&self, transaction: &[u8]) -> Result<PutOutcome, StoreError> {
        let id = TxId::for_transaction(transaction);
        if let Some(sector) = self.location(id) {
            return self.compare(id, sector, transaction);
        }
        let external = transaction.len() > INLINE_LIMIT;
        if external {
            self.write_payload(id, transaction)?;
        }
        let sectors = if external {
            1
        } else {
            transaction
                .len()
                .checked_add(9)
                .ok_or(StoreError::StoreFull)?
                .div_ceil(SECTOR_BYTES as usize) as u64
        };
        let sector = self.allocate(sectors)?;
        let mut record = vec![0; sectors as usize * SECTOR_BYTES as usize];
        record[0] = u8::from(external);
        record[1..9].copy_from_slice(&(transaction.len() as u64).to_le_bytes());
        if !external {
            record[9..9 + transaction.len()].copy_from_slice(transaction);
        }
        self.data
            .write_all_at(&record, sector as u64 * SECTOR_BYTES)?;
        self.data.sync_data()?;
        self.publish(id, sector, transaction)
    }

    pub fn contains(&self, id: TxId) -> bool {
        self.location(id).is_some()
    }

    pub fn get(&self, id: TxId) -> Result<Option<Vec<u8>>, StoreError> {
        let Some(sector) = self.location(id) else {
            return Ok(None);
        };
        self.read_transaction(id, sector).map(Some)
    }

    fn location(&self, id: TxId) -> Option<u32> {
        self.locations
            .read()
            .unwrap_or_else(|error| error.into_inner())
            .get(&id)
            .copied()
    }

    fn allocate(&self, sectors: u64) -> Result<u32, StoreError> {
        let mut next = self
            .next_sector
            .lock()
            .unwrap_or_else(|error| error.into_inner());
        let end = next.checked_add(sectors).ok_or(StoreError::StoreFull)?;
        if end > u32::MAX as u64 + 1 {
            return Err(StoreError::StoreFull);
        }
        let sector = *next as u32;
        *next = end;
        Ok(sector)
    }

    fn write_payload(&self, id: TxId, transaction: &[u8]) -> Result<(), StoreError> {
        let path = self.payload_path(id);
        let shard = path.parent().ok_or(StoreError::InvalidStore)?.to_path_buf();
        let file = OpenOptions::new()
            .create(true)
            .truncate(false)
            .write(true)
            .open(path)?;
        file.write_all_at(transaction, 0)?;
        file.set_len(transaction.len() as u64)?;
        file.sync_all()?;
        File::open(shard)?.sync_all()?;
        Ok(())
    }

    fn payload_path(&self, id: TxId) -> PathBuf {
        let encoded = URL_SAFE_NO_PAD.encode(id.as_bytes());
        self.payload
            .join(&encoded[..2])
            .join(format!("{}.dat", &encoded[2..]))
    }

    fn publish(&self, id: TxId, sector: u32, transaction: &[u8]) -> Result<PutOutcome, StoreError> {
        let mut publication = self
            .publication
            .lock()
            .unwrap_or_else(|error| error.into_inner());
        if publication.reopen_required {
            return Err(StoreError::ReopenRequired);
        }
        if let Some(existing) = self.location(id) {
            drop(publication);
            return self.compare(id, existing, transaction);
        }
        let mut entry = [0; 16];
        entry[..12].copy_from_slice(id.as_bytes());
        entry[12..].copy_from_slice(&sector.to_le_bytes());
        if publication
            .lookup
            .write_all(&entry)
            .and_then(|()| publication.lookup.sync_data())
            .is_err()
        {
            publication.reopen_required = true;
            return Err(StoreError::OutcomeUnknown(id));
        }
        self.locations
            .write()
            .unwrap_or_else(|error| error.into_inner())
            .insert(id, sector);
        Ok(PutOutcome::Inserted(id))
    }

    fn compare(&self, id: TxId, sector: u32, transaction: &[u8]) -> Result<PutOutcome, StoreError> {
        if self.read_transaction(id, sector)? == transaction {
            Ok(PutOutcome::Duplicate(id))
        } else {
            Err(StoreError::IdCollision(id))
        }
    }

    fn read_transaction(&self, id: TxId, sector: u32) -> Result<Vec<u8>, StoreError> {
        let offset = sector as u64 * SECTOR_BYTES;
        let mut header = [0; 9];
        self.read_data(&mut header, offset, id)?;
        let length = u64::from_le_bytes(header[1..9].try_into().unwrap());
        let bytes = match header[0] {
            0 if length <= INLINE_LIMIT as u64 => {
                let mut bytes = vec![0; length as usize];
                self.read_data(&mut bytes, offset + 9, id)?;
                bytes
            }
            1 if length > INLINE_LIMIT as u64 => {
                let path = self.payload_path(id);
                let metadata = fs::metadata(&path).map_err(|error| match error.kind() {
                    io::ErrorKind::NotFound => StoreError::CorruptTransaction(id),
                    _ => StoreError::Io(error),
                })?;
                if metadata.len() != length {
                    return Err(StoreError::CorruptTransaction(id));
                }
                let bytes = fs::read(path)?;
                if bytes.len() as u64 != length {
                    return Err(StoreError::CorruptTransaction(id));
                }
                bytes
            }
            _ => return Err(StoreError::CorruptTransaction(id)),
        };
        if TxId::for_transaction(&bytes) != id {
            return Err(StoreError::CorruptTransaction(id));
        }
        Ok(bytes)
    }

    fn read_data(&self, bytes: &mut [u8], offset: u64, id: TxId) -> Result<(), StoreError> {
        self.data
            .read_exact_at(bytes, offset)
            .map_err(|error| match error.kind() {
                io::ErrorKind::UnexpectedEof => StoreError::CorruptTransaction(id),
                _ => StoreError::Io(error),
            })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicU64, Ordering};

    fn root() -> PathBuf {
        static NEXT: AtomicU64 = AtomicU64::new(0);
        let path = std::env::temp_dir().join(format!(
            "k1-store-{}-{}",
            std::process::id(),
            NEXT.fetch_add(1, Ordering::Relaxed)
        ));
        let _ = fs::remove_dir_all(&path);
        path
    }

    #[test]
    fn lifecycle_boundaries_duplicates_corruption_and_tail() {
        let root = root();
        let store = TransactionStore::create(&root).unwrap();
        assert_eq!(fs::read_dir(root.join("payload")).unwrap().count(), 4_096);
        let values = [vec![], vec![3; INLINE_LIMIT], vec![7; INLINE_LIMIT + 1]];
        let mut ids = Vec::new();
        for value in &values {
            let id = TxId::for_transaction(value);
            assert_eq!(store.put(value).unwrap(), PutOutcome::Inserted(id));
            assert_eq!(store.put(value).unwrap(), PutOutcome::Duplicate(id));
            assert_eq!(store.get(id).unwrap().unwrap(), *value);
            ids.push(id);
        }
        assert_eq!(fs::metadata(root.join("lookup.dat")).unwrap().len(), 48);
        let sector = store.location(ids[0]).unwrap();
        assert!(matches!(
            store.compare(ids[0], sector, b"different"),
            Err(StoreError::IdCollision(id)) if id == ids[0]
        ));
        drop(store);
        OpenOptions::new()
            .append(true)
            .open(root.join("lookup.dat"))
            .unwrap()
            .write_all(&[1, 2, 3])
            .unwrap();
        let store = TransactionStore::open(&root).unwrap();
        assert_eq!(fs::metadata(root.join("lookup.dat")).unwrap().len(), 48);
        for (id, value) in ids.into_iter().zip(values) {
            assert!(store.contains(id));
            assert_eq!(store.get(id).unwrap().unwrap(), value);
        }
        let id = TxId::for_transaction(b"intact");
        assert_eq!(store.put(b"intact").unwrap(), PutOutcome::Inserted(id));
        let sector = store.location(id).unwrap();
        store
            .data
            .write_all_at(b"x", sector as u64 * SECTOR_BYTES + 9)
            .unwrap();
        assert!(matches!(
            store.get(id),
            Err(StoreError::CorruptTransaction(found)) if found == id
        ));
        fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn concurrent_writes_allocate_disjoint_sectors_and_publish_once() {
        let root = root();
        let store = Arc::new(TransactionStore::create(&root).unwrap());
        let threads: Vec<_> = (0_u8..8)
            .map(|byte| {
                let store = Arc::clone(&store);
                std::thread::spawn(move || {
                    let value = vec![byte; 8_193];
                    let outcome = store.put(&value).unwrap();
                    (value, outcome)
                })
            })
            .collect();
        for thread in threads {
            let (value, outcome) = thread.join().unwrap();
            let PutOutcome::Inserted(id) = outcome else {
                panic!()
            };
            assert_eq!(store.get(id).unwrap().unwrap(), value);
        }
        let value = vec![11; INLINE_LIMIT + 1];
        let id = TxId::for_transaction(&value);
        let threads: Vec<_> = (0..2)
            .map(|_| {
                let store = Arc::clone(&store);
                let value = value.clone();
                std::thread::spawn(move || store.put(&value))
            })
            .collect();
        let outcomes: Vec<_> = threads
            .into_iter()
            .map(|thread| thread.join().unwrap())
            .collect();
        assert_eq!(
            outcomes
                .iter()
                .filter(|outcome| matches!(outcome, Ok(PutOutcome::Inserted(_))))
                .count(),
            1
        );
        assert!(outcomes.iter().all(|outcome| matches!(
            outcome,
            Ok(PutOutcome::Inserted(_)) | Ok(PutOutcome::Duplicate(_))
        )));
        assert_eq!(store.get(id).unwrap().unwrap(), value);
        assert_eq!(store.put(&value).unwrap(), PutOutcome::Duplicate(id));
        let sectors = store
            .locations
            .read()
            .unwrap_or_else(|error| error.into_inner());
        let mut locations: Vec<_> = sectors.values().copied().collect();
        locations.sort_unstable();
        locations.dedup();
        assert_eq!(locations.len(), sectors.len());
        fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn capacity_is_bounded() {
        let root = root();
        let store = TransactionStore::create(&root).unwrap();
        *store
            .next_sector
            .lock()
            .unwrap_or_else(|error| error.into_inner()) = u32::MAX as u64 + 1;
        assert!(matches!(store.put(b"full"), Err(StoreError::StoreFull)));
        fs::remove_dir_all(root).unwrap();
    }
}