hawdb-executor 0.13.0

An embedded Rust graph database for local data-plane workloads
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
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hash, Hasher};
use std::num::NonZeroUsize;

use crate::kernel::OperatorMemoryTracker;
use hawdb_core::Result;

/// Hash the structural key once, including when a probe is followed by a merge
/// or insertion. Hash collisions never replace full-key equality.
#[derive(Debug, PartialEq, Eq)]
pub(super) struct HashedKey<K> {
    hash: u64,
    pub(super) key: K,
}

impl<K: Hash> HashedKey<K> {
    pub(super) fn new(key: K, state: &RandomState) -> Self {
        Self {
            hash: state.hash_one(&key),
            key,
        }
    }
}

impl<K> Hash for HashedKey<K> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        state.write_u64(self.hash);
    }
}

impl<K> HashedKey<K> {
    pub(super) fn with_hash(key: K, hash: u64) -> Self {
        Self { hash, key }
    }
}

struct GroupEntry<K, V> {
    key: HashedKey<K>,
    value: V,
    next: Option<NonZeroUsize>,
}

/// Dense groups keep their keys and states in one sortable allocation. Buckets
/// contain chain heads and tails, so collision candidates retain producer order
/// without scanning unrelated buckets. Collisions always compare complete keys.
/// Unlike collecting a HashMap into a sorted Vec, finishing needs no second
/// array of group headers. The tracker retains array capacity until that array
/// is freed.
pub(super) struct HashGroups<K, V> {
    entries: Vec<GroupEntry<K, V>>,
    buckets: Vec<Option<NonZeroUsize>>,
    bucket_tails: Vec<Option<NonZeroUsize>>,
}

impl<K, V> Default for HashGroups<K, V> {
    fn default() -> Self {
        Self {
            entries: Vec::new(),
            buckets: Vec::new(),
            bucket_tails: Vec::new(),
        }
    }
}

impl<K: Eq, V> HashGroups<K, V> {
    pub(super) fn len(&self) -> usize {
        self.entries.len()
    }

    /// Candidate enumeration for a multimap whose full keys live in its rows.
    /// The caller must compare those keys before producing a join result.
    pub(super) fn hashed_values(&self, hash: u64) -> impl Iterator<Item = &V> {
        let mut cursor = if self.buckets.is_empty() {
            None
        } else {
            self.buckets[hash as usize & (self.buckets.len() - 1)]
        };
        std::iter::from_fn(move || {
            while let Some(index) = cursor {
                let entry = &self.entries[index.get() - 1];
                cursor = entry.next;
                if entry.key.hash == hash {
                    return Some(&entry.value);
                }
            }
            None
        })
    }

    pub(super) fn into_values(self) -> impl Iterator<Item = (u64, V)> {
        self.entries
            .into_iter()
            .map(|entry| (entry.key.hash, entry.value))
    }

    pub(super) fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    fn find(&self, key: &HashedKey<K>) -> Option<usize> {
        if self.buckets.is_empty() {
            return None;
        }
        let mut cursor = self.buckets[key.hash as usize & (self.buckets.len() - 1)];
        while let Some(index) = cursor {
            let index = index.get() - 1;
            let entry = &self.entries[index];
            if entry.key == *key {
                return Some(index);
            }
            cursor = entry.next;
        }
        None
    }

    pub(super) fn get(&self, key: &HashedKey<K>) -> Option<&V> {
        self.find(key).map(|index| &self.entries[index].value)
    }

    pub(super) fn get_mut(&mut self, key: &HashedKey<K>) -> Option<&mut V> {
        self.find(key).map(|index| &mut self.entries[index].value)
    }

    fn next_capacity(&self) -> usize {
        self.buckets.len().saturating_mul(2).max(4)
    }

    fn array_bytes(capacity: usize) -> usize {
        capacity.saturating_mul(
            std::mem::size_of::<GroupEntry<K, V>>()
                .saturating_add(std::mem::size_of::<Option<NonZeroUsize>>().saturating_mul(2)),
        )
    }

    pub(super) fn insertion_bytes(&self, payload_bytes: usize) -> usize {
        if self.entries.len() == self.buckets.len() {
            // Old arrays stay charged while the new arrays are allocated.
            payload_bytes.saturating_add(Self::array_bytes(self.next_capacity()))
        } else {
            payload_bytes
        }
    }

    pub(super) fn insert(
        &mut self,
        key: HashedKey<K>,
        value: V,
        payload_bytes: usize,
        tracker: &mut OperatorMemoryTracker,
    ) -> Result<()> {
        self.insert_prepared(key, value, payload_bytes, tracker, None)
    }

    pub(super) fn insert_prepared(
        &mut self,
        key: HashedKey<K>,
        value: V,
        payload_bytes: usize,
        tracker: &mut OperatorMemoryTracker,
        key_allocation: Option<&mut crate::QueryMemoryLease>,
    ) -> Result<()> {
        debug_assert!(self.find(&key).is_none());
        let bytes = self.insertion_bytes(payload_bytes);
        if let Some(allocation) = key_allocation {
            tracker.try_charge_from(allocation, bytes)?;
        } else {
            tracker.try_charge(bytes)?;
        }
        if self.entries.len() == self.buckets.len() {
            let capacity = self.next_capacity();
            let old_bytes = Self::array_bytes(self.buckets.len());
            let mut entries = Vec::with_capacity(capacity);
            let mut buckets = vec![None; capacity];
            let mut bucket_tails: Vec<Option<NonZeroUsize>> = vec![None; capacity];
            entries.append(&mut self.entries);
            for index in 0..entries.len() {
                entries[index].next = None;
                let bucket = entries[index].key.hash as usize & (capacity - 1);
                let next = NonZeroUsize::new(index + 1);
                if let Some(tail) = bucket_tails[bucket] {
                    entries[tail.get() - 1].next = next;
                } else {
                    buckets[bucket] = next;
                }
                bucket_tails[bucket] = next;
            }
            self.entries = entries;
            self.buckets = buckets;
            self.bucket_tails = bucket_tails;
            tracker.release(old_bytes);
        }
        let bucket = key.hash as usize & (self.buckets.len() - 1);
        let next = NonZeroUsize::new(self.entries.len() + 1);
        self.entries.push(GroupEntry {
            key,
            value,
            next: None,
        });
        if let Some(tail) = self.bucket_tails[bucket] {
            self.entries[tail.get() - 1].next = next;
        } else {
            self.buckets[bucket] = next;
        }
        self.bucket_tails[bucket] = next;
        Ok(())
    }
}

impl<K: Ord, V> HashGroups<K, V> {
    pub(super) fn into_sorted(self) -> (impl Iterator<Item = (K, V)>, usize) {
        let Self {
            mut entries,
            buckets,
            bucket_tails,
        } = self;
        let released_bytes = buckets
            .len()
            .saturating_add(bucket_tails.len())
            .saturating_mul(std::mem::size_of::<Option<NonZeroUsize>>());
        drop(buckets);
        drop(bucket_tails);
        entries.sort_unstable_by(|left, right| left.key.key.cmp(&right.key.key));
        (
            entries
                .into_iter()
                .map(|entry| (entry.key.key, entry.value)),
            released_bytes,
        )
    }
}

/// Conservative per-entry headroom for buckets, growth, and the temporary
/// sorted output/run vector. Variable-sized key/state payloads are charged by
/// the operator separately. Keeping this charge per entry also retains it
/// while the table is consumed into a sorted vector.
pub(super) const fn hash_entry_overhead<K, V>() -> usize {
    std::mem::size_of::<(HashedKey<K>, V)>()
        .saturating_add(1)
        .saturating_mul(4)
        .saturating_add(32)
}

/// Charge capacity rather than a worst-case initial table for every distinct
/// value. Double-capacity headroom covers bucket/control storage and the old
/// table while an insertion grows the allocation.
pub(super) const fn hash_set_capacity_bytes<K>(capacity: usize) -> usize {
    if capacity == 0 {
        0
    } else {
        capacity
            .saturating_mul(2)
            .saturating_mul(std::mem::size_of::<K>().saturating_add(1))
            .saturating_add(32)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{QueryMemoryClass, QueryMemoryLedger};
    use hawdb_core::Value;
    use std::collections::{BTreeMap, HashMap};

    #[test]
    fn colliding_hashes_compare_complete_structural_keys() {
        let keys = [
            vec![Value::Null],
            vec![Value::Int(1)],
            vec![Value::Float(1.0)],
            vec![Value::Float(-0.0)],
            vec![Value::Float(0.0)],
            vec![Value::Float(f64::from_bits(0x7ff8_0000_0000_0001))],
            vec![Value::Float(f64::from_bits(0x7ff8_0000_0000_0002))],
            vec![Value::List(vec![Value::String("nested".to_string())])],
            vec![Value::Map(BTreeMap::from([(
                "key".to_string(),
                Value::Null,
            )]))],
        ];
        let mut groups = HashMap::new();
        for (index, key) in keys.iter().enumerate() {
            groups.insert(
                HashedKey {
                    hash: 0,
                    key: key.clone(),
                },
                index,
            );
        }
        assert_eq!(groups.len(), keys.len());
        for (index, key) in keys.into_iter().enumerate() {
            assert_eq!(groups.get(&HashedKey { hash: 0, key }), Some(&index));
        }
    }

    #[test]
    fn equal_keys_share_a_precomputed_hash() {
        let state = RandomState::new();
        let key = vec![Value::List(vec![Value::Float(f64::NAN), Value::Null])];
        assert_eq!(
            HashedKey::new(key.clone(), &state),
            HashedKey::new(key, &state)
        );
    }

    #[test]
    fn dense_groups_preserve_collisions_across_growth_and_sort() {
        let mut groups = HashGroups::<u64, u64>::default();
        let mut tracker = OperatorMemoryTracker::new(NonZeroUsize::new(1024 * 1024).unwrap());
        let keys = (0..129).rev().collect::<Vec<u64>>();
        for &key in &keys {
            groups
                .insert(HashedKey { hash: 0, key }, key * 2, 0, &mut tracker)
                .unwrap();
            assert_eq!(
                tracker.used_bytes,
                HashGroups::<u64, u64>::array_bytes(groups.buckets.len())
            );
            for existing in key..129 {
                assert_eq!(
                    groups.get(&HashedKey {
                        hash: 0,
                        key: existing
                    }),
                    Some(&(existing * 2))
                );
            }
        }
        *groups.get_mut(&HashedKey { hash: 0, key: 42 }).unwrap() = 999;
        assert_eq!(groups.get(&HashedKey { hash: 0, key: 999 }), None);
        assert_eq!(
            groups.hashed_values(0).copied().collect::<Vec<_>>(),
            keys.iter()
                .copied()
                .map(|key| if key == 42 { 999 } else { key * 2 })
                .collect::<Vec<_>>()
        );
        let entry_capacity = groups.entries.capacity();
        let (sorted, released_bytes) = groups.into_sorted();
        tracker.release(released_bytes);
        assert_eq!(
            tracker.used_bytes,
            entry_capacity * std::mem::size_of::<GroupEntry<u64, u64>>()
        );
        let output = sorted.collect::<Vec<_>>();
        assert_eq!(output.len(), 129);
        for (key, (actual_key, value)) in output.into_iter().enumerate() {
            assert_eq!(actual_key, key as u64);
            assert_eq!(value, if key == 42 { 999 } else { key as u64 * 2 });
        }
        tracker.reset();
        assert_eq!(tracker.used_bytes, 0);
    }

    #[test]
    fn dense_group_growth_is_admitted_before_changing_capacity_or_chains() {
        type Groups = HashGroups<u64, u64>;
        const PAYLOAD_BYTES: usize = 16;
        let growth_peak = Groups::array_bytes(4) + Groups::array_bytes(8) + 5 * PAYLOAD_BYTES;
        for root_limited in [false, true] {
            for allowed in [false, true] {
                let limit = NonZeroUsize::new(growth_peak - usize::from(!allowed)).unwrap();
                let large = NonZeroUsize::new(1024 * 1024).unwrap();
                let ledger = QueryMemoryLedger::new(if root_limited { limit } else { large });
                let operator_limit = if root_limited { large } else { limit };
                let mut tracker = OperatorMemoryTracker::with_account(
                    operator_limit,
                    ledger.account(
                        QueryMemoryClass::BlockingState,
                        "dense groups test",
                        operator_limit,
                    ),
                );
                let mut groups = Groups::default();
                for key in 0..4 {
                    groups
                        .insert(HashedKey { hash: 0, key }, key, PAYLOAD_BYTES, &mut tracker)
                        .unwrap();
                }
                let old_used = tracker.used_bytes;
                let result = groups.insert(
                    HashedKey { hash: 0, key: 4 },
                    4,
                    PAYLOAD_BYTES,
                    &mut tracker,
                );
                if allowed {
                    result.unwrap();
                    assert_eq!(groups.buckets.len(), 8);
                    assert_eq!(tracker.peak_bytes, growth_peak);
                    assert_eq!(
                        tracker.used_bytes,
                        Groups::array_bytes(8) + 5 * PAYLOAD_BYTES
                    );
                    assert_eq!(groups.get(&HashedKey { hash: 0, key: 4 }), Some(&4));
                } else {
                    assert!(result.is_err());
                    assert_eq!(groups.buckets.len(), 4);
                    assert_eq!(groups.entries.len(), 4);
                    assert_eq!(tracker.used_bytes, old_used);
                    assert_eq!(groups.get(&HashedKey { hash: 0, key: 4 }), None);
                }
                for key in 0..4 {
                    assert_eq!(groups.get(&HashedKey { hash: 0, key }), Some(&key));
                }
                drop(groups);
                drop(tracker);
                assert_eq!(ledger.snapshot().used_bytes, 0);
            }
        }
    }
}