chdb-rust 2.1.0

chDB FFI bindings for Rust(Experimental)
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Namespaces: the entry point to the durable control plane.
//!
//! A namespace is a location plus an engine factory. The location says where
//! objects live and which backend implements the conditional operations; the
//! factory says how to bring up an engine for whichever object is being opened.
//!
//! One constraint is not the namespace's to hide: chdb-core binds one data path
//! per process, so one process holds one open durable object at a time. Opening
//! a second returns an error naming both paths. Fan-out across objects is
//! therefore sequential, or spread across worker processes; a registry here
//! that pretended otherwise would only move the failure somewhere less obvious
//! (§3.6).

use std::path::{Path, PathBuf};
use std::sync::Arc;

use super::backend::Backend;
use super::backends::LocalBackend;
use super::engine::EngineFactory;
use super::errors::{err, Category, Result};
use super::keys::validate_object_id;
use super::object::{open_object, DurableObject, OpenOptions, Tuning};
use super::ChdbEngine;

/// Builds a backend for one object, given its id.
///
/// A namespace owns one and hands each object its own scoped backend, so no
/// code below the namespace can address a key outside its object. Supplying one
/// directly is also how a provider this crate does not ship — an S3-compatible
/// store, a fault-injecting wrapper — is plugged in.
pub type BackendFactory = Box<dyn Fn(&str) -> Result<Arc<dyn Backend>> + Send + Sync>;

/// Addresses durable objects on one backend.
pub struct Namespace {
    location: String,
    backend_factory: BackendFactory,
    engine_factory: EngineFactory,
    owner: Option<String>,
    scratch_root: Option<PathBuf>,
    tuning: Tuning,
}

impl std::fmt::Debug for Namespace {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Namespace")
            .field("location", &self.location)
            .field("owner", &self.owner)
            .finish_non_exhaustive()
    }
}

/// The query parameters of a namespace URL, percent-decoded.
#[cfg(feature = "durable-s3")]
///
/// Decoded because an `endpoint` value carries `://`, and a caller that
/// escaped it — as any URL builder would — should get the same backend as one
/// who did not.
fn query_of(url: &str) -> Vec<(String, String)> {
    let Some((_, query)) = url.split_once('?') else {
        return Vec::new();
    };
    query
        .split('&')
        .filter(|pair| !pair.is_empty())
        .map(|pair| {
            let (name, value) = pair.split_once('=').unwrap_or((pair, ""));
            (percent_decode(name), percent_decode(value))
        })
        .collect()
}

#[cfg(feature = "durable-s3")]
fn percent_decode(value: &str) -> String {
    let bytes = value.as_bytes();
    let mut out = Vec::with_capacity(bytes.len());
    let mut index = 0;
    while index < bytes.len() {
        match bytes[index] {
            b'%' if index + 2 < bytes.len() => {
                let hex = std::str::from_utf8(&bytes[index + 1..index + 3]).unwrap_or("");
                match u8::from_str_radix(hex, 16) {
                    Ok(byte) => {
                        out.push(byte);
                        index += 3;
                    }
                    // Not an escape after all: a literal `%` is not an error,
                    // and mangling it would be worse than passing it through.
                    Err(_) => {
                        out.push(b'%');
                        index += 1;
                    }
                }
            }
            byte => {
                out.push(byte);
                index += 1;
            }
        }
    }
    String::from_utf8_lossy(&out).into_owned()
}

/// The bucket and base prefix an `s3://` URL names.
#[cfg(feature = "durable-s3")]
fn s3_location(url: &str) -> Result<(String, String)> {
    let location = url
        .trim_start_matches("s3://")
        .split('?')
        .next()
        .unwrap_or_default();
    let (bucket, prefix) = match location.split_once('/') {
        Some((bucket, prefix)) => (bucket, prefix.trim_matches('/')),
        None => (location, ""),
    };
    if bucket.is_empty() {
        return Err(err(
            Category::Backend,
            format!("durable: an s3 namespace URL needs a bucket, got {url:?}"),
        ));
    }
    Ok((bucket.to_string(), prefix.to_string()))
}

/// The directory a `file://` or `local:` URL names, or a plain absolute path.
///
/// A typo in a configuration file fails here rather than at the first open.
fn local_root(url: &str) -> Result<PathBuf> {
    let url = url.split('?').next().unwrap_or(url);
    let path = url
        .strip_prefix("file://")
        .or_else(|| url.strip_prefix("file:"))
        .or_else(|| url.strip_prefix("local://"))
        .or_else(|| url.strip_prefix("local:"))
        .unwrap_or(url);
    if let Some(scheme_end) = path.find("://") {
        return Err(err(
            Category::Backend,
            format!(
                "durable: this build has no backend for the {:?} scheme; pass a backend factory \
                 with Namespace::with_backend for anything but a local directory",
                &path[..scheme_end]
            ),
        ));
    }
    let path = Path::new(path);
    if !path.is_absolute() {
        return Err(err(
            Category::Backend,
            format!("durable: a local namespace needs an absolute path, got {url:?}"),
        ));
    }
    Ok(path.to_path_buf())
}

impl Namespace {
    /// Binds a namespace to a local directory.
    ///
    /// ```no_run
    /// use chdb_rust::durable::Namespace;
    ///
    /// let namespace = Namespace::new("file:///var/lib/chdb-durable")?;
    /// # Ok::<(), chdb_rust::durable::Error>(())
    /// ```
    ///
    /// `file:///abs/path`, `local:/abs/path` and a bare `/abs/path` all name a
    /// directory holding one subdirectory per object. That backend is for
    /// development, tests and single-host use: recovery on another machine
    /// needs storage neither machine owns, which is an object store. Point a
    /// namespace at one with [`Self::with_backend`].
    ///
    /// # Errors
    ///
    /// Returns [`Category::Backend`] for a relative path or a scheme this build
    /// has no backend for.
    pub fn new(url: &str) -> Result<Self> {
        if url.starts_with("s3://") {
            return Self::s3(url);
        }
        let root = local_root(url)?;
        Ok(Self::with_backend(Box::new(move |id| {
            Ok(Arc::new(LocalBackend::new(root.join(id))?))
        }))
        .named(url))
    }

    /// Binds a namespace to an S3-compatible bucket.
    ///
    /// ```text
    /// AWS    s3://my-bucket/durable?region=eu-central-1
    /// R2     s3://my-bucket/durable?region=auto&endpoint=https://<id>.r2.cloudflarestorage.com
    /// MinIO  s3://my-bucket/durable?endpoint=http://127.0.0.1:9000
    /// ```
    ///
    /// Credentials are deliberately not among the parameters. They come from
    /// the environment or the shared credentials file, because a namespace URL
    /// is the sort of thing that gets logged, put in a config file and pasted
    /// into an issue. See [`Credentials::resolve`](super::Credentials::resolve)
    /// for the order, and for what to do with an SSO profile.
    #[cfg(feature = "durable-s3")]
    fn s3(url: &str) -> Result<Self> {
        use super::backends::{S3Backend, S3Options};

        let (bucket, base_prefix) = s3_location(url)?;
        let query = query_of(url);
        let value = |name: &str| {
            query
                .iter()
                .find(|(key, _)| key == name)
                .map(|(_, value)| value.clone())
                .filter(|value| !value.is_empty())
        };
        // `pathStyle` too, because that is what the Go binding's URLs use and
        // the same configuration string should work in both.
        let path_style = value("path_style")
            .or_else(|| value("pathStyle"))
            .map(|raw| raw == "true" || raw == "1");

        let region = value("region");
        let endpoint = value("endpoint");
        Ok(Self::with_backend(Box::new(move |id| {
            let prefix = if base_prefix.is_empty() {
                id.to_string()
            } else {
                format!("{base_prefix}/{id}")
            };
            Ok(Arc::new(S3Backend::new(S3Options {
                bucket: bucket.clone(),
                prefix,
                region: region.clone(),
                endpoint: endpoint.clone(),
                path_style,
                ..S3Options::default()
            })?))
        }))
        .named(url))
    }

    /// The `s3` scheme needs the `durable-s3` feature, which is what carries
    /// the HTTP and TLS stack it signs requests with.
    #[cfg(not(feature = "durable-s3"))]
    fn s3(url: &str) -> Result<Self> {
        Err(err(
            Category::Backend,
            format!(
                "durable: {url:?} needs the `durable-s3` feature, which is off in this build;                  enable it, or pass your own backend to Namespace::with_backend"
            ),
        ))
    }

    /// Binds a namespace to a backend of your own.
    ///
    /// The factory is called once per open with the object id, and must return a
    /// backend scoped to that object's prefix — nothing below this point checks
    /// that one object cannot address another's keys.
    pub fn with_backend(backend_factory: BackendFactory) -> Self {
        Self {
            location: "<custom backend>".to_string(),
            backend_factory,
            engine_factory: Box::new(|| Ok(Box::new(ChdbEngine::new()))),
            owner: None,
            scratch_root: None,
            tuning: Tuning::default(),
        }
    }

    fn named(mut self, location: &str) -> Self {
        self.location = location.to_string();
        self
    }

    /// Uses a different engine for every object this namespace opens.
    ///
    /// The default runs the chDB engine this process linked. A test double or a
    /// connection with extra arguments goes here.
    pub fn with_engine(mut self, engine_factory: EngineFactory) -> Self {
        self.engine_factory = engine_factory;
        self
    }

    /// The default writer name recorded in the lease of every object this
    /// namespace opens. Observability only.
    pub fn with_owner(mut self, owner: impl Into<String>) -> Self {
        self.owner = Some(owner.into());
        self
    }

    /// The default parent directory for scratch trees.
    pub fn with_scratch_root(mut self, root: impl Into<PathBuf>) -> Self {
        self.scratch_root = Some(root.into());
        self
    }

    /// The default lease and commit parameters for every object this namespace
    /// opens.
    ///
    /// A per-open [`OpenOptions::tuning`] wins, unless it is exactly
    /// [`Tuning::default`] — which is what an [`OpenOptions`] built with
    /// `..Default::default()` carries, and means "whatever the namespace says".
    pub fn with_tuning(mut self, tuning: Tuning) -> Self {
        self.tuning = tuning;
        self
    }

    /// Where this namespace points.
    pub fn location(&self) -> &str {
        &self.location
    }

    /// Opens one object, reporting whether it already existed.
    ///
    /// A caller creating a tenant on first use needs to tell "restored" from
    /// "created" without a separate probe, which is what the flag is for. A
    /// writer lease is taken unless [`OpenOptions::read_only`] is set.
    ///
    /// ```no_run
    /// use chdb_rust::durable::{Namespace, OpenOptions};
    /// use chdb_rust::format::OutputFormat;
    ///
    /// let namespace = Namespace::new("file:///var/lib/chdb-durable")?.with_owner("worker-1");
    /// let (object, existed) = namespace.open("tenant-123", OpenOptions {
    ///     database: Some("mem".to_string()),
    ///     ..OpenOptions::default()
    /// })?;
    ///
    /// if !existed {
    ///     object.execute("CREATE TABLE events (id UInt64) ENGINE = MergeTree ORDER BY id")?;
    /// }
    /// let ticket = object.execute("INSERT INTO events VALUES (1)")?;
    /// object.flush_through(ticket)?;   // now it survives losing this machine
    /// let rows = object.query("SELECT count() FROM events", OutputFormat::CSV)?;
    /// object.close()?;
    /// # Ok::<(), chdb_rust::durable::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// An open that fails leaves nothing behind: any lease it took is released,
    /// the engine is closed and the scratch directory is removed. See
    /// [`Category`](super::Category) for what the failures mean; the common ones
    /// are [`Category::NotFound`] for a read-only open of a missing object and
    /// [`Category::LeaseHeld`] when another writer has it.
    pub fn open(&self, id: &str, options: OpenOptions) -> Result<(DurableObject, bool)> {
        validate_object_id(id)?;
        let backend = (self.backend_factory)(id)?;
        let options = OpenOptions {
            owner: options.owner.or_else(|| self.owner.clone()),
            scratch_root: options.scratch_root.or_else(|| self.scratch_root.clone()),
            tuning: if options.tuning == Tuning::default() {
                self.tuning.clone()
            } else {
                options.tuning
            },
            ..options
        };
        open_object(id, backend, &self.engine_factory, options)
    }

    /// Runs one read-only query against several objects, opening them one at a
    /// time and returning the bytes together with each object id.
    ///
    /// This is a small fan-out convenience, not a cross-object query engine.
    /// chdb-core permits one active data path per process, so the objects must
    /// be restored and closed sequentially. A missing object returns
    /// [`Category::NotFound`], just like a direct read-only [`Self::open`].
    ///
    /// ```no_run
    /// use chdb_rust::durable::Namespace;
    /// use chdb_rust::format::OutputFormat;
    ///
    /// let namespace = Namespace::new("s3://my-bucket/agent-memory")?;
    /// let rows = namespace.scan(
    ///     "SELECT count() FROM memories",
    ///     ["tenant-a", "tenant-b"],
    ///     OutputFormat::CSV,
    /// )?;
    /// for (id, bytes) in rows {
    ///     println!("{id}: {}", String::from_utf8_lossy(&bytes));
    /// }
    /// # Ok::<(), chdb_rust::durable::Error>(())
    /// ```
    pub fn scan<I, S>(
        &self,
        sql: &str,
        ids: I,
        format: crate::format::OutputFormat,
    ) -> Result<Vec<(String, Vec<u8>)>>
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        let mut results = Vec::new();
        for id in ids {
            let id = id.as_ref();
            let (object, _) = self.open(
                id,
                OpenOptions {
                    read_only: true,
                    ..OpenOptions::default()
                },
            )?;
            let query = object.query(sql, format);
            let close = object.close();
            match (query, close) {
                (Ok(bytes), Ok(())) => results.push((id.to_owned(), bytes)),
                (Err(error), _) => return Err(error),
                (Ok(_), Err(error)) => return Err(error),
            }
        }
        Ok(results)
    }
}

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

    #[test]
    fn a_local_url_names_a_directory_in_three_spellings() -> Result<()> {
        assert_eq!(local_root("file:///var/lib/x")?, Path::new("/var/lib/x"));
        assert_eq!(local_root("local:/var/lib/x")?, Path::new("/var/lib/x"));
        assert_eq!(local_root("/var/lib/x")?, Path::new("/var/lib/x"));
        Ok(())
    }

    #[test]
    fn a_scheme_this_build_cannot_serve_fails_at_configuration_time() {
        let error = local_root("gs://bucket/prefix").expect_err("no GCS backend here");
        assert_eq!(error.category(), Category::Backend);
        assert!(error.to_string().contains("backend factory"), "{error}");
    }

    #[cfg(feature = "durable-s3")]
    #[test]
    fn an_s3_url_splits_into_a_bucket_and_a_prefix() {
        assert_eq!(
            s3_location("s3://my-bucket/durable/objects?region=eu-central-1").unwrap(),
            ("my-bucket".to_string(), "durable/objects".to_string())
        );
        assert_eq!(
            s3_location("s3://my-bucket").unwrap(),
            ("my-bucket".to_string(), String::new()),
            "a bucket with no prefix is the bucket root"
        );
        assert_eq!(
            s3_location("s3://my-bucket/").unwrap(),
            ("my-bucket".to_string(), String::new())
        );
        assert_eq!(
            s3_location("s3://").unwrap_err().category(),
            Category::Backend
        );
    }

    #[cfg(feature = "durable-s3")]
    #[test]
    fn a_query_value_survives_being_escaped_by_a_url_builder() {
        let query = query_of("s3://b/p?region=auto&endpoint=https%3A%2F%2Fx.example&pathStyle=1");
        assert_eq!(query[0], ("region".to_string(), "auto".to_string()));
        assert_eq!(
            query[1],
            ("endpoint".to_string(), "https://x.example".to_string()),
            "an escaped endpoint has to mean the same as an unescaped one"
        );
        assert_eq!(query[2], ("pathStyle".to_string(), "1".to_string()));

        assert!(query_of("s3://b/p").is_empty());
        // A stray percent is a literal, not a reason to fail a configuration.
        assert_eq!(percent_decode("100%"), "100%");
        assert_eq!(percent_decode("a%2Fb"), "a/b");
    }

    #[test]
    fn a_relative_root_is_refused() {
        assert!(local_root("file://relative/path").is_err());
        assert!(local_root("chdb-durable").is_err());
    }

    #[test]
    fn an_object_id_cannot_address_another_objects_prefix() {
        let namespace = Namespace::new("/tmp/chdb-durable-test").expect("a local namespace");
        for id in ["", "..", "a/b"] {
            let error = namespace
                .open(id, OpenOptions::default())
                .expect_err("an id that is not one segment");
            assert_eq!(error.category(), Category::Backend, "{id:?}");
        }
    }
}