polydat 0.1.0

Polydat — generation kernel for deterministic variate generation in nb-rs (formerly nbrs-variates)
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
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0

//! Non-deterministic random data generators for prototyping and testing.
//!
//! These nodes use thread-local RNG and produce different outputs on
//! each call regardless of input coordinates. They are NOT reproducible
//! across runs. Use the deterministic hash-based nodes for production
//! workloads.
//!
//! All "random" nodes are 0→1 (no inputs) to make the non-deterministic
//! nature clear. The "hashed line/extract" nodes are 1→1 (deterministic,
//! coordinate-driven) and use the bundled text data files.

use std::cell::RefCell;

use crate::node::{GkNode, NodeMeta, Port, PortType, Slot, Value};
use xxhash_rust::xxh3::xxh3_64;

// =================================================================
// Bundled data files (included at compile time)
// =================================================================

/// ~93KB of Lorem Ipsum text from nosqlbench's data files.
pub static LOREM_IPSUM: &str = include_str!("../../data/lorem_ipsum_full.txt");
/// First names
pub static NAMES: &str = include_str!("../../data/names.txt");
/// Last names
pub static LASTNAMES: &str = include_str!("../../data/lastnames.txt");
/// Career titles
pub static CAREERS: &str = include_str!("../../data/careers.txt");
/// Company names
pub static COMPANIES: &str = include_str!("../../data/companies.txt");
/// Variable/metric words
pub static VARIABLE_WORDS: &str = include_str!("../../data/variable_words.txt");

// =================================================================
// Thread-local xorshift64 PRNG
// =================================================================

thread_local! {
    static RNG: RefCell<u64> = RefCell::new(
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos() as u64
    );
}

fn next_u64() -> u64 {
    RNG.with(|r| {
        let mut s = *r.borrow();
        s ^= s << 13;
        s ^= s >> 7;
        s ^= s << 17;
        *r.borrow_mut() = s;
        s
    })
}

fn next_f64() -> f64 {
    next_u64() as f64 / u64::MAX as f64
}

// =================================================================
// Non-deterministic random nodes (0→1)
// =================================================================

/// Random u64 in [min, max).
///
/// Signature: `() -> (u64)`
pub struct RandomRange {
    meta: NodeMeta,
    min: u64,
    range: u64,
}

impl RandomRange {
    pub fn new(min: u64, max: u64) -> Self {
        assert!(max > min);
        Self {
            meta: NodeMeta {
                name: "random_range".into(),
                outs: vec![Port::u64("output")],
                ins: Vec::new(),
            },
            min,
            range: max - min,
        }
    }
}

impl GkNode for RandomRange {
    fn meta(&self) -> &NodeMeta { &self.meta }
    fn eval(&self, _inputs: &[Value], outputs: &mut [Value]) {
        outputs[0] = Value::U64(self.min + (next_u64() % self.range));
    }
}

/// Random f64 in [min, max).
///
/// Signature: `() -> (f64)`
pub struct RandomF64 {
    meta: NodeMeta,
    min: f64,
    range: f64,
}

impl RandomF64 {
    pub fn new(min: f64, max: f64) -> Self {
        Self {
            meta: NodeMeta {
                name: "random_f64".into(),
                outs: vec![Port::f64("output")],
                ins: Vec::new(),
            },
            min,
            range: max - min,
        }
    }
}

impl GkNode for RandomF64 {
    fn meta(&self) -> &NodeMeta { &self.meta }
    fn eval(&self, _inputs: &[Value], outputs: &mut [Value]) {
        outputs[0] = Value::F64(self.min + next_f64() * self.range);
    }
}

/// Random byte buffer of a fixed size.
///
/// Signature: `() -> (bytes)`
pub struct RandomBytes {
    meta: NodeMeta,
    size: usize,
}

impl RandomBytes {
    pub fn new(size: usize) -> Self {
        Self {
            meta: NodeMeta {
                name: "random_bytes".into(),
                outs: vec![Port::new("output", PortType::Bytes)],
                ins: Vec::new(),
            },
            size,
        }
    }
}

impl GkNode for RandomBytes {
    fn meta(&self) -> &NodeMeta { &self.meta }
    fn eval(&self, _inputs: &[Value], outputs: &mut [Value]) {
        let mut buf = Vec::with_capacity(self.size);
        while buf.len() < self.size {
            let take = (self.size - buf.len()).min(8);
            buf.extend_from_slice(&next_u64().to_le_bytes()[..take]);
        }
        outputs[0] = Value::Bytes(buf.into());
    }
}

/// Random string from a character set.
///
/// Signature: `() -> (String)`
pub struct RandomString {
    meta: NodeMeta,
    chars: Vec<char>,
    length: usize,
}

impl RandomString {
    pub fn alphanumeric(length: usize) -> Self {
        Self::from_charset("A-Za-z0-9", length)
    }

    pub fn from_charset(spec: &str, length: usize) -> Self {
        Self {
            meta: NodeMeta {
                name: "random_string".into(),
                outs: vec![Port::new("output", PortType::Str)],
                ins: Vec::new(),
            },
            chars: parse_charset(spec),
            length,
        }
    }
}

impl GkNode for RandomString {
    fn meta(&self) -> &NodeMeta { &self.meta }
    fn eval(&self, _inputs: &[Value], outputs: &mut [Value]) {
        let s: String = (0..self.length)
            .map(|_| self.chars[(next_u64() as usize) % self.chars.len()])
            .collect();
        outputs[0] = Value::Str(s.into());
    }
}

/// Random boolean with a given probability of true.
///
/// Signature: `() -> (bool)`
pub struct RandomBool {
    meta: NodeMeta,
    threshold: u64,
}

impl RandomBool {
    pub fn new(probability: f64) -> Self {
        Self {
            meta: NodeMeta {
                name: "random_bool".into(),
                outs: vec![Port::bool("output")],
                ins: Vec::new(),
            },
            threshold: (probability.clamp(0.0, 1.0) * u64::MAX as f64) as u64,
        }
    }
}

impl GkNode for RandomBool {
    fn meta(&self) -> &NodeMeta { &self.meta }
    fn eval(&self, _inputs: &[Value], outputs: &mut [Value]) {
        outputs[0] = Value::Bool(next_u64() < self.threshold);
    }
}

// =================================================================
// Deterministic text extraction nodes (1→1, hash-based)
// =================================================================

/// Extract a substring from bundled lorem ipsum text using a hash-based
/// offset. Deterministic: same input → same extract.
///
/// Signature: `(input: u64) -> (String)`
///
/// This is the equivalent of nosqlbench's `HashedLoremExtractToString`.
pub struct HashedLoremExtract {
    meta: NodeMeta,
    min_len: usize,
    max_len: usize,
}

impl HashedLoremExtract {
    pub fn new(min_len: usize, max_len: usize) -> Self {
        assert!(max_len >= min_len);
        Self {
            meta: NodeMeta {
                name: "hashed_lorem_extract".into(),
                outs: vec![Port::new("output", PortType::Str)],
                ins: vec![Slot::Wire(Port::u64("input"))],
            },
            min_len,
            max_len,
        }
    }
}

impl GkNode for HashedLoremExtract {
    fn meta(&self) -> &NodeMeta { &self.meta }
    fn eval(&self, inputs: &[Value], outputs: &mut [Value]) {
        let h = inputs[0].as_u64();
        let len_range = self.max_len - self.min_len + 1;
        let extract_len = self.min_len + ((h as usize) % len_range);
        let max_offset = LOREM_IPSUM.len().saturating_sub(extract_len);
        let h2 = xxh3_64(&h.to_le_bytes());
        let offset = if max_offset > 0 { (h2 as usize) % (max_offset + 1) } else { 0 };
        let end = (offset + extract_len).min(LOREM_IPSUM.len());
        // Align to char boundaries
        let start = LOREM_IPSUM.floor_char_boundary(offset);
        let end = LOREM_IPSUM.ceil_char_boundary(end);
        outputs[0] = Value::Str(LOREM_IPSUM[start..end].to_string().into());
    }
}

/// Select a deterministic line from a bundled text file using hash.
///
/// Signature: `(input: u64) -> (String)`
///
/// Equivalent to nosqlbench's `HashedLineToString`. The text file is
/// pre-split into lines at init time.
pub struct HashedLineToString {
    meta: NodeMeta,
    lines: Vec<String>,
}

impl HashedLineToString {
    /// Create from a bundled text source.
    pub fn new(text: &str) -> Self {
        let lines: Vec<String> = text.lines()
            .map(|l| l.to_string())
            .filter(|l| !l.is_empty())
            .collect();
        assert!(!lines.is_empty(), "text source must have at least one line");
        Self {
            meta: NodeMeta {
                name: "hashed_line_to_string".into(),
                outs: vec![Port::new("output", PortType::Str)],
                ins: vec![Slot::Wire(Port::u64("input"))],
            },
            lines,
        }
    }

    /// From bundled first names.
    pub fn names() -> Self { Self::new(NAMES) }
    /// From bundled last names.
    pub fn lastnames() -> Self { Self::new(LASTNAMES) }
    /// From bundled careers.
    pub fn careers() -> Self { Self::new(CAREERS) }
    /// From bundled company names.
    pub fn companies() -> Self { Self::new(COMPANIES) }
}

impl GkNode for HashedLineToString {
    fn meta(&self) -> &NodeMeta { &self.meta }
    fn eval(&self, inputs: &[Value], outputs: &mut [Value]) {
        let h = inputs[0].as_u64();
        let idx = (h as usize) % self.lines.len();
        outputs[0] = Value::Str(self.lines[idx].clone().into());
    }
}

fn parse_charset(spec: &str) -> Vec<char> {
    let mut chars = Vec::new();
    let spec_chars: Vec<char> = spec.chars().collect();
    let mut i = 0;
    while i < spec_chars.len() {
        if i + 2 < spec_chars.len() && spec_chars[i + 1] == '-' {
            for c in spec_chars[i]..=spec_chars[i + 2] { chars.push(c); }
            i += 3;
        } else {
            chars.push(spec_chars[i]);
            i += 1;
        }
    }
    chars
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn lorem_ipsum_bundled() {
        assert!(LOREM_IPSUM.len() > 90_000, "lorem ipsum should be ~93KB");
        assert!(LOREM_IPSUM.starts_with("Lorem ipsum"));
    }

    #[test]
    fn names_bundled() {
        assert!(!NAMES.is_empty());
        assert!(NAMES.lines().count() > 10);
    }

    #[test]
    fn random_range_bounded() {
        let node = RandomRange::new(10, 20);
        let mut out = [Value::None];
        for _ in 0..1000 {
            node.eval(&[], &mut out);
            assert!((10..20).contains(&out[0].as_u64()));
        }
    }

    #[test]
    fn random_f64_bounded() {
        let node = RandomF64::new(1.0, 5.0);
        let mut out = [Value::None];
        for _ in 0..1000 {
            node.eval(&[], &mut out);
            let v = out[0].as_f64();
            assert!(v >= 1.0 && v < 5.0, "out of range: {v}");
        }
    }

    #[test]
    fn random_string_charset() {
        let node = RandomString::alphanumeric(20);
        let mut out = [Value::None];
        node.eval(&[], &mut out);
        assert_eq!(out[0].as_str().len(), 20);
        assert!(out[0].as_str().chars().all(|c| c.is_ascii_alphanumeric()));
    }

    #[test]
    fn hashed_lorem_extract_deterministic() {
        let node = HashedLoremExtract::new(50, 100);
        let mut out1 = [Value::None];
        let mut out2 = [Value::None];
        node.eval(&[Value::U64(42)], &mut out1);
        node.eval(&[Value::U64(42)], &mut out2);
        assert_eq!(out1[0].as_str(), out2[0].as_str());
    }

    #[test]
    fn hashed_lorem_extract_size_range() {
        let node = HashedLoremExtract::new(20, 50);
        let mut out = [Value::None];
        for i in 0..100u64 {
            let h = xxh3_64(&i.to_le_bytes());
            node.eval(&[Value::U64(h)], &mut out);
            let len = out[0].as_str().len();
            assert!(len >= 19 && len <= 55, "len={len}"); // char boundary wiggle
        }
    }

    #[test]
    fn hashed_lorem_extract_varies() {
        let node = HashedLoremExtract::new(10, 20);
        let mut out1 = [Value::None];
        let mut out2 = [Value::None];
        let h1 = xxh3_64(&0u64.to_le_bytes());
        let h2 = xxh3_64(&1u64.to_le_bytes());
        node.eval(&[Value::U64(h1)], &mut out1);
        node.eval(&[Value::U64(h2)], &mut out2);
        assert_ne!(out1[0].as_str(), out2[0].as_str());
    }

    #[test]
    fn hashed_line_names() {
        let node = HashedLineToString::names();
        let mut out = [Value::None];
        let h = xxh3_64(&42u64.to_le_bytes());
        node.eval(&[Value::U64(h)], &mut out);
        assert!(!out[0].as_str().is_empty());
    }

    #[test]
    fn hashed_line_careers() {
        let node = HashedLineToString::careers();
        let mut out = [Value::None];
        let h = xxh3_64(&42u64.to_le_bytes());
        node.eval(&[Value::U64(h)], &mut out);
        assert!(!out[0].as_str().is_empty());
    }

    #[test]
    fn hashed_line_deterministic() {
        let node = HashedLineToString::names();
        let mut out1 = [Value::None];
        let mut out2 = [Value::None];
        node.eval(&[Value::U64(12345)], &mut out1);
        node.eval(&[Value::U64(12345)], &mut out2);
        assert_eq!(out1[0].as_str(), out2[0].as_str());
    }
}