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
//! Value types — one backing structure per Redis type.
//!
//! ```
//! use kevy_store::{SetCondition, Store, Value};
//! let mut s = Store::new();
//! s.set(b"n", b"42".to_vec(), None, SetCondition::Always);
//! s.sadd(b"tags", &[b"a".as_slice()])?;
//! let mut types = Vec::new();
//! s.snapshot_each(|_, v: &Value, _| types.push(v.type_name()));
//! types.sort();
//! assert_eq!(types, ["set", "string"]);
//! # Ok::<(), kevy_store::StoreError>(())
//! ```
use crate*;
use VecDeque;
use Ordering;
pub use SmallBytes;
use ;
use RankTree;
/// Backing structure for a Hash value — [`KevyMap`] keyed by [`SmallBytes`]
/// (22 B inline / heap-else). Field names ≤22B (the vast majority — `name`,
/// `email`, etc.) live entirely inside the bucket, saving the 24 B Vec
/// metadata + heap allocation per field on a 22-byte budget.
///
/// ```
/// use kevy_store::{HashData, SmallBytes};
/// let mut h = HashData::new();
/// h.insert(SmallBytes::from_slice(b"name"), SmallBytes::from_slice(b"ada"));
/// assert_eq!(h.get(b"name".as_slice()).map(|v| v.as_ref()), Some(&b"ada"[..]));
/// ```
pub type HashData = ;
/// Backing structure for a List value (a ring-buffer deque — O(1) both ends).
///
/// ```
/// use kevy_store::ListData;
/// let mut l = ListData::new();
/// l.push_back(b"b".to_vec());
/// l.push_front(b"a".to_vec());
/// assert_eq!(l, [b"a".to_vec(), b"b".to_vec()]);
/// ```
pub type ListData = ;
/// Backing structure for a Set value — [`KevySet`] of [`SmallBytes`].
///
/// ```
/// use kevy_store::{SetData, SmallBytes};
/// let mut s = SetData::new();
/// assert!(s.insert(SmallBytes::from_slice(b"m")));
/// assert!(!s.insert(SmallBytes::from_slice(b"m")), "members are unique");
/// assert!(s.contains(b"m".as_slice()));
/// ```
pub type SetData = ;
/// A total-ordered f64 score (Redis scores are never NaN). `total_cmp` gives a
/// total order so scores can key an ordered container.
///
/// `PartialEq` is written rather than derived, and it must stay that way.
/// Derived, it is `f64`'s `==`, which disagrees with the `total_cmp` below
/// on `-0.0`: `==` calls it equal to `0.0`, `total_cmp` orders it before.
/// Rust requires of an `Ord` key that `a == b` exactly when `a.cmp(b)` is
/// `Equal`, and a `Score` that breaks that is a key whose container and
/// whose callers disagree about which entries are the same one.
///
/// It was latent while nothing compared two `Score`s — `ZSetData::insert`
/// reached the tree only through `Ord`, and its unconditional
/// remove-then-insert never had to ask. `ZADD z -0 m` is accepted and
/// `ZADD z2 -0 a; ZADD z2 0 b` really does order `a` first, so the first
/// caller to write `old == score` would have skipped a live update and left
/// `by_member` and `by_score` holding different scores for one member.
/// NaN cannot arrive: the parser refuses it.
///
/// ```
/// use kevy_store::Score;
/// assert!(Score::new(-0.0) < Score::new(0.0));
/// assert_ne!(Score::new(-0.0), Score::new(0.0));
/// assert_eq!(Score::new(1.5).value(), 1.5);
/// ```
f64);
/// A score-range endpoint for `ZRANGEBYSCORE`/`ZCOUNT` (inclusive or exclusive).
/// Use `value = ±INFINITY` for `-inf`/`+inf`.
///
/// ```
/// use kevy_store::ScoreBound;
/// let lo = ScoreBound::exclusive(1.0);
/// assert!(lo.exclusive && lo.value == 1.0);
/// assert!(!ScoreBound::inclusive(f64::INFINITY).exclusive);
/// ```
/// Sorted set: a member→score map plus an order-statistic B-tree keyed by
/// `(score, member)` ([`kevy_ranktree::RankTree`] — every node carries its
/// subtree count), so rank queries (`ZRANK`, `ZRANGE` by rank, `ZCOUNT`,
/// score-bound seeks) are O(log N) descents instead of linear walks.
///
/// ```
/// use kevy_store::{Store, Value};
/// let mut s = Store::new();
/// let members: Vec<(f64, &[u8])> =
/// [(3.0, &b"c"[..]), (1.0, b"a"), (2.0, b"b"), (4.0, b"d"), (5.0, b"e")].to_vec();
/// s.zadd(b"z", &members)?;
/// let mut order = Vec::new();
/// s.snapshot_each(|_, v, _| {
/// if let Value::ZSet(z) = v {
/// order = z.ordered().map(|(m, _)| m.to_vec()).collect();
/// }
/// });
/// assert_eq!(order, [b"a", b"b", b"c", b"d", b"e"]);
/// # Ok::<(), kevy_store::StoreError>(())
/// ```
pub use crate;
pub use crateBioDropSender;
pub use crate;
/// Per-bucket footprint for a sorted set's member map (open-addressing Swiss
/// table). Approximation, not exact: includes metadata byte per slot plus the
/// boxed `K`/`V` cell, padded for 7/8 load factor. Hashes are charged their
/// table's real size instead (`hash_weight`).
pub const HASH_SLOT_BYTES: u64 = 32;
pub const SET_SLOT_BYTES: u64 = 24;
/// `VecDeque` ring-buffer slot per stored `Vec<u8>` header (24 B Vec metadata).
pub const LIST_SLOT_BYTES: u64 = 24;
/// `BTreeSet`/`BTreeMap` per-entry overhead (node pointers + B-tree node
/// padding) — the stream index's accounting constant.
pub const BTREE_SLOT_BYTES: u64 = 40;
/// `kevy_ranktree::RankTree` per-key overhead. Measured from the structure:
/// the `(Score, SmallBytes)` key slot is 32 B; nodes hold ≤15 keys in a Vec
/// whose buffer rounds to 16 slots at ~2/3 fill (≈10-11 live keys), so the
/// key arrays amortise to ≈48 B per key; the per-node fixed cost (56 B
/// header + Box allocation, ~1 node per 10 keys) and the internal nodes'
/// child-pointer arrays add ≈8 B more. 64 errs slightly high (allocator
/// size-class rounding), keeping `used_memory` a conservative upper bound —
/// same policy as [`ENTRY_OVERHEAD`].
pub const RANKTREE_SLOT_BYTES: u64 = 64;
/// A keyspace slot's amortised cost: the inline 24-byte `SmallBytes` key
/// cell, the `Entry` and a control byte, over the table's load. The tiered
/// store charges each cold stub this in `stub_bytes`, the estimate of what
/// its cold keys cost in memory.
///
/// `used_memory` does not use it: the keyspace table is charged there at
/// its real size, whatever its load.
///
/// ```
/// use kevy_store::{ENTRY_OVERHEAD, SetCondition, Store};
/// let mut s = Store::new();
/// s.set(b"k", b"v".to_vec(), None, SetCondition::Always);
/// // one key in a sixteen-slot table: the table costs far more than a slot
/// assert!(s.used_memory() > ENTRY_OVERHEAD);
/// ```
pub const ENTRY_OVERHEAD: u64 = 96;
pub
/// What a new hash field adds to its hash's weight besides the table: the
/// allocator's footprint of the field name's heap and of the value's heap
/// (`value_heap` bytes), each 0 when it is short enough to sit inline in
/// the slot. The slot itself is part of the table, which is charged as the
/// table grows rather than per field.
///
/// ```
/// use kevy_store::{SmallBytes, hash_field_weight};
/// let short = SmallBytes::from_slice(b"name");
/// let long = SmallBytes::from_slice(&[b'f'; 64]);
/// // an inline field name and value add nothing
/// assert_eq!(hash_field_weight(&short, 0), 0);
/// // a spilled one adds what the allocator holds for it: 64 bytes asked, 80 held
/// assert_eq!(hash_field_weight(&long, 0), 80);
/// assert_eq!(hash_field_weight(&short, 900), 912);
/// ```
/// Per-member delta a new set member charges. Mirrors [`hash_field_weight`]
/// for the set variant (no separate value, single bucket slot).
///
/// ```
/// use kevy_store::{SmallBytes, set_member_weight};
/// let inline = SmallBytes::from_slice(b"m");
/// let spilled = SmallBytes::from_slice(&[b'm'; 64]);
/// assert_eq!(
/// set_member_weight(&spilled) - set_member_weight(&inline),
/// spilled.heap_bytes() as u64
/// );
/// ```
/// Per-item delta a new list element charges (Vec header slot + heap cap).
///
/// ```
/// use kevy_store::list_item_weight;
/// // the slot cost is fixed; the element's capacity is added on top
/// assert_eq!(list_item_weight(100) - list_item_weight(0), 100);
/// ```
/// Per-member delta a new zset member charges: hash slot for `by_member` +
/// rank-tree slot for `by_score` + the member's heap bytes — twice, because
/// a heap-spilling member (>22 B) is stored in both structures (inline
/// members cost 0 here, matching [`Value::weight`]'s ZSet arm).
///
/// ```
/// use kevy_store::{SmallBytes, zset_member_weight};
/// let inline = SmallBytes::from_slice(b"m");
/// let spilled = SmallBytes::from_slice(&[b'm'; 64]);
/// // a spilled member is stored twice: in the map and in the rank tree
/// assert_eq!(
/// zset_member_weight(&spilled) - zset_member_weight(&inline),
/// 2 * spilled.heap_bytes() as u64
/// );
/// ```