Elffuss BraidHub

utopia-ia/elffuss-braid public

main · 6.2 KiBRawPermalink
1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 UtopiaIA
3
4//! What a store holds and how it is stored, from the indexes and journals
5//! alone.
6
7use std::fs;
8
9use braid_format::Kind;
10
11use crate::error::{Error, io_error};
12use crate::record::{CODEC_CHUNKED, CODEC_DELTA, CODEC_DICTIONARY, CODEC_RAW, CODEC_ZSTD};
13use crate::segments::list;
14use crate::{Codec, Store};
15
16/// The objects of one kind.
17#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18pub struct KindStats {
19 /// The kind.
20 pub kind: Kind,
21 /// Objects of this kind.
22 pub objects: u64,
23 /// Their payloads as stored, in bytes.
24 pub stored_bytes: u64,
25 /// Their payloads decoded, in bytes.
26 pub raw_bytes: u64,
27}
28
29/// The objects stored with one codec.
30#[derive(Clone, Copy, Debug, PartialEq, Eq)]
31pub struct CodecStats {
32 /// The codec.
33 pub codec: Codec,
34 /// Objects stored this way.
35 pub objects: u64,
36 /// Their payloads as stored, in bytes: for chunked blobs, their chunk
37 /// lists.
38 pub stored_bytes: u64,
39 /// Their payloads decoded, in bytes.
40 pub raw_bytes: u64,
41}
42
43/// What a store holds: see [`Store::stats`].
44#[derive(Clone, Debug, PartialEq, Eq)]
45pub struct Stats {
46 /// Distinct objects, chunks included: a chunk is a blob of its own.
47 pub objects: u64,
48 /// Objects of each kind, in code order.
49 pub kinds: Vec<KindStats>,
50 /// Objects of each codec, in code order.
51 pub codecs: Vec<CodecStats>,
52 /// Bytes of every object's payload as stored.
53 pub stored_bytes: u64,
54 /// Bytes of distinct content: every object's decoded size, except that
55 /// a chunked blob counts only through its chunks, so that content shared
56 /// by several versions counts once.
57 pub raw_bytes: u64,
58 /// Bytes of the objects' records, with their headers and padding.
59 pub record_bytes: u64,
60 /// Bytes of segment files.
61 pub segment_bytes: u64,
62 /// Bytes of indexes, merged indexes and journals.
63 pub index_bytes: u64,
64}
65
66impl Store {
67 /// What the store holds: objects by kind and by codec, with their sizes
68 /// as stored and decoded, and the size of the files that hold them.
69 ///
70 /// It reads the directory again first, then counts one copy of each
71 /// object from the indexes and journals, which it reads whole: no
72 /// payload is read, so the cost follows the number of objects, not
73 /// their size. Copies of an object written twice by racing writers, and
74 /// remains of writes cut short, count in the file sizes only.
75 pub fn stats(&self) -> Result<Stats, Error> {
76 self.segments.rescan()?;
77 let mut kinds: Vec<KindStats> = Kind::ALL
78 .iter()
79 .map(|&kind| KindStats {
80 kind,
81 objects: 0,
82 stored_bytes: 0,
83 raw_bytes: 0,
84 })
85 .collect();
86 let mut codecs: Vec<CodecStats> = Codec::ALL
87 .iter()
88 .map(|&codec| CodecStats {
89 codec,
90 objects: 0,
91 stored_bytes: 0,
92 raw_bytes: 0,
93 })
94 .collect();
95 let mut stats = Stats {
96 objects: 0,
97 kinds: Vec::new(),
98 codecs: Vec::new(),
99 stored_bytes: 0,
100 raw_bytes: 0,
101 record_bytes: 0,
102 segment_bytes: 0,
103 index_bytes: 0,
104 };
105 for entry in self.segments.known_entries()? {
106 let stored = u64::from(entry.stored_len);
107 stats.objects += 1;
108 stats.stored_bytes += stored;
109 stats.record_bytes += entry.header().record_len();
110 if entry.codec != CODEC_CHUNKED {
111 stats.raw_bytes += entry.raw_len;
112 }
113 if let Some(kind) = kinds.iter_mut().find(|k| k.kind == entry.kind) {
114 kind.objects += 1;
115 kind.stored_bytes += stored;
116 kind.raw_bytes += entry.raw_len;
117 }
118 if let Some(codec) = codecs.iter_mut().find(|c| c.codec.code() == entry.codec) {
119 codec.objects += 1;
120 codec.stored_bytes += stored;
121 codec.raw_bytes += entry.raw_len;
122 }
123 }
124 stats.kinds = kinds;
125 stats.codecs = codecs;
126 let dir = self.segments.dir();
127 let listing = list(dir)?;
128 let files = listing
129 .segments
130 .iter()
131 .flat_map(|(name, files)| {
132 [("seg", files.seg), ("jrn", files.jrn), ("idx", files.idx)]
133 .into_iter()
134 .filter(|(_, present)| *present)
135 .map(move |(extension, _)| (name, extension))
136 })
137 .chain(listing.merged.iter().map(|name| (name, "midx")));
138 for (name, extension) in files {
139 let path = self.segments.path(name, extension);
140 let len = match fs::metadata(&path) {
141 Ok(meta) => meta.len(),
142 // A journal removed by sealing, or an index by merging,
143 // since the listing.
144 Err(err) if err.kind() == std::io::ErrorKind::NotFound => 0,
145 Err(err) => return Err(io_error("read", &path)(err)),
146 };
147 if extension == "seg" {
148 stats.segment_bytes += len;
149 } else {
150 stats.index_bytes += len;
151 }
152 }
153 Ok(stats)
154 }
155}
156
157impl Codec {
158 /// Every codec, in code order.
159 pub const ALL: [Codec; 5] = [
160 Codec::Raw,
161 Codec::Zstd,
162 Codec::Dictionary,
163 Codec::Delta,
164 Codec::Chunked,
165 ];
166
167 /// The number records store.
168 pub const fn code(self) -> u8 {
169 match self {
170 Codec::Raw => CODEC_RAW,
171 Codec::Zstd => CODEC_ZSTD,
172 Codec::Dictionary => CODEC_DICTIONARY,
173 Codec::Delta => CODEC_DELTA,
174 Codec::Chunked => CODEC_CHUNKED,
175 }
176 }
177
178 /// A short, stable name for reports and JSON.
179 pub const fn name(self) -> &'static str {
180 match self {
181 Codec::Raw => "raw",
182 Codec::Zstd => "zstd",
183 Codec::Dictionary => "dictionary",
184 Codec::Delta => "delta",
185 Codec::Chunked => "chunked",
186 }
187 }
188}