Skip to main content

hyperdb_api/
process.rs

1// Copyright (c) 2026, Salesforce, Inc. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4//! Hyper server process management.
5//!
6//! This module provides [`HyperProcess`] for spawning and managing local hyperd server instances.
7//!
8//! # Callback Connection Architecture
9//!
10//! The `HyperProcess` uses a **callback connection** mechanism for reliable process lifecycle
11//! management. This works as follows:
12//!
13//! 1. **Startup**: The client creates a TCP listener on an ephemeral port (the "callback proxy")
14//!    and passes this address to hyperd via `--callback-connection`. Hyper connects back to this
15//!    listener and sends its actual listen endpoint over this connection.
16//!
17//! 2. **Runtime**: The callback connection remains open for the lifetime of the `HyperProcess`.
18//!    This connection acts as a "dead man's switch" - Hyper monitors it continuously.
19//!
20//! 3. **Graceful Shutdown**: When `HyperProcess` is dropped or explicitly shut down, the callback
21//!    connection is closed. Hyper detects this and initiates graceful shutdown automatically.
22//!
23//! 4. **Crash Safety**: If the client process crashes or is killed, the OS automatically closes
24//!    the TCP connection. Hyper detects this and shuts down gracefully, preventing orphan
25//!    processes. This is the key advantage over signal-based shutdown.
26//!
27//! # Protocol Details
28//!
29//! The callback connection protocol is simple:
30//! - Client listens on `127.0.0.1:<ephemeral_port>`
31//! - Client starts hyperd with `--callback-connection=tab.tcp://127.0.0.1:<port>`
32//! - Hyper connects to this address and sends: `[1 byte length][N bytes descriptor]`
33//! - The descriptor is the actual listen endpoint (e.g., `tab.tcp://localhost:54321`)
34//! - Connection stays open until shutdown is desired
35//!
36//! # Listen Modes
37//!
38//! `HyperProcess` supports different listen modes via [`ListenMode`]:
39//!
40//! - **`LibPq`** (default): `PostgreSQL` wire protocol for full read/write access
41//! - **Grpc**: gRPC protocol for query-only Arrow-based access
42//! - **Both**: Both protocols enabled (libpq for read/write, gRPC for Arrow queries)
43//!
44//! ```no_run
45//! use hyperdb_api::{HyperProcess, ListenMode, Parameters, Result};
46//!
47//! fn main() -> Result<()> {
48//!     // gRPC only
49//!     let mut params = Parameters::new();
50//!     params.set_listen_mode(ListenMode::Grpc { port: 0 });
51//!     let hyper = HyperProcess::new(None, Some(&params))?;
52//!     println!("gRPC endpoint: {}", hyper.grpc_endpoint().unwrap());
53//!
54//!     // Both libpq and gRPC
55//!     let mut params = Parameters::new();
56//!     params.set_listen_mode(ListenMode::Both { grpc_port: 7484 });
57//!     let hyper = HyperProcess::new(None, Some(&params))?;
58//!     println!("libpq endpoint: {}", hyper.endpoint().unwrap());
59//!     println!("gRPC endpoint: {}", hyper.grpc_endpoint().unwrap());
60//!     Ok(())
61//! }
62//! ```
63
64use std::io::Read;
65use std::net::{Shutdown, TcpListener, TcpStream};
66use std::path::{Path, PathBuf};
67use std::process::{Child, Command, Stdio};
68use std::sync::Arc;
69use std::sync::atomic::{AtomicBool, Ordering};
70use std::thread;
71use std::time::Duration;
72
73#[cfg(unix)]
74use std::os::unix::process::CommandExt;
75
76#[cfg(any(unix, windows))]
77use hyperdb_api_core::client::ConnectionEndpoint;
78
79use tracing::info;
80
81use crate::error::{Error, Result};
82
83/// Specifies which protocols `HyperProcess` should listen on.
84///
85/// # Examples
86///
87/// ```
88/// use hyperdb_api::{ListenMode, Parameters};
89///
90/// // LibPq only (default) - for full read/write access
91/// let mut params = Parameters::new();
92/// params.set_listen_mode(ListenMode::LibPq);
93///
94/// // gRPC only - for Arrow-based query access
95/// let mut params = Parameters::new();
96/// params.set_listen_mode(ListenMode::Grpc { port: 0 }); // auto-assign port
97///
98/// // Both protocols - libpq for writes, gRPC for Arrow queries
99/// let mut params = Parameters::new();
100/// params.set_listen_mode(ListenMode::Both { grpc_port: 7484 });
101/// ```
102#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
103pub enum ListenMode {
104    /// `PostgreSQL` wire protocol only (default).
105    ///
106    /// This is the traditional connection mode that supports all Hyper features
107    /// including read and write operations.
108    #[default]
109    LibPq,
110
111    /// gRPC protocol only.
112    ///
113    /// This mode is optimized for query-only workloads and returns results in
114    /// Arrow IPC format. Note that gRPC mode does not support write operations.
115    ///
116    /// Set `port` to 0 to auto-assign an available port.
117    Grpc {
118        /// The port to listen on (0 for auto-assign).
119        port: u16,
120    },
121
122    /// Both libpq and gRPC protocols.
123    ///
124    /// This mode enables full read/write access via libpq while also providing
125    /// gRPC access for Arrow-based queries. The libpq port is auto-assigned,
126    /// while the gRPC port is specified.
127    ///
128    /// Note: When using `Both` mode, the callback connection returns the libpq
129    /// endpoint. Use `HyperProcess::grpc_endpoint()` to get the gRPC endpoint.
130    Both {
131        /// The gRPC port to listen on (cannot be 0 - must be a specific port).
132        grpc_port: u16,
133    },
134}
135
136/// A running Hyper server instance.
137///
138/// This struct manages the lifecycle of a local Hyper server process using a callback
139/// connection for reliable shutdown. The server is automatically shut down when this
140/// object is dropped.
141///
142/// # Callback Connection (Dead Man's Switch)
143///
144/// Unlike traditional process management that relies on signals, `HyperProcess` maintains
145/// a TCP connection to the Hyper server. When this connection is closed (either explicitly
146/// or because the client process exits), Hyper automatically shuts down gracefully.
147///
148/// This provides several benefits:
149/// - **No orphan processes**: If your application crashes, Hyper shuts down automatically
150/// - **Graceful shutdown**: Hyper can flush data and clean up properly
151/// - **Cross-platform**: Works reliably on macOS, Linux, and Windows
152///
153/// # Example
154///
155/// ```no_run
156/// use hyperdb_api::{HyperProcess, Result};
157///
158/// fn main() -> Result<()> {
159///     // Start a Hyper server (auto-detect hyperd location)
160///     let hyper = HyperProcess::new(None, None)?;
161///
162///     println!("Hyper server running at: {}", hyper.endpoint().unwrap());
163///
164///     // Server automatically shuts down when `hyper` goes out of scope
165///     // via the callback connection mechanism
166///     Ok(())
167/// }
168/// ```
169#[must_use = "HyperProcess will shut down when dropped; store it to keep the server running"]
170#[derive(Debug)]
171pub struct HyperProcess {
172    /// The child process handle.
173    child: Option<Child>,
174    /// The libpq endpoint descriptor (host:port or socket path), if libpq is enabled.
175    endpoint: Option<String>,
176    /// The parsed connection endpoint for libpq.
177    connection_endpoint: Option<ConnectionEndpoint>,
178    /// The gRPC endpoint (host:port), if gRPC is enabled.
179    grpc_endpoint: Option<String>,
180    /// Path to the hyperd executable.
181    #[expect(
182        dead_code,
183        reason = "retained for diagnostics and future restart/respawn paths"
184    )]
185    hyperd_path: PathBuf,
186    /// Whether shutdown has been initiated.
187    shutdown_initiated: Arc<AtomicBool>,
188    /// The callback connection to hyperd.
189    /// Keeping this open maintains the "dead man's switch" - when dropped, Hyper shuts down.
190    callback_connection: Option<TcpStream>,
191    /// The listen mode this process was started with.
192    listen_mode: ListenMode,
193    /// The transport mode this process was started with.
194    transport_mode: TransportMode,
195    /// The socket directory for UDS connections (Unix only).
196    /// This directory is automatically cleaned up on drop.
197    #[cfg(unix)]
198    socket_directory: Option<PathBuf>,
199    /// The pipe name for Named Pipe connections (Windows only).
200    #[cfg(windows)]
201    pipe_name: Option<String>,
202    /// The log directory where hyperd writes its log files.
203    log_dir: Option<PathBuf>,
204}
205
206impl HyperProcess {
207    /// Starts a new Hyper server instance.
208    ///
209    /// This method:
210    /// 1. Creates a callback listener on an ephemeral port
211    /// 2. Starts the hyperd process with the callback connection address
212    /// 3. Waits for Hyper to connect back and provide its listen endpoint
213    ///
214    /// # Arguments
215    ///
216    /// * `hyper_path` - Optional path to the hyperd executable. If `None`, searches
217    ///   in common locations (`HYPERD_PATH` env var, then known build output paths).
218    /// * `parameters` - Optional parameters for the server.
219    ///
220    /// # Errors
221    ///
222    /// Returns an error if:
223    /// - The hyperd executable cannot be found
224    /// - The callback listener cannot be created
225    /// - The server fails to start
226    /// - Hyper doesn't connect back within the timeout (30 seconds)
227    ///
228    /// # Example
229    ///
230    /// ```no_run
231    /// use hyperdb_api::{HyperProcess, Result};
232    /// use std::path::Path;
233    ///
234    /// fn main() -> Result<()> {
235    ///     // Auto-detect hyperd location
236    ///     let hyper = HyperProcess::new(None, None)?;
237    ///
238    ///     // Or specify explicit path
239    ///     let hyper2 = HyperProcess::new(
240    ///         Some(Path::new("/path/to/hyperd")),
241    ///         None,
242    ///     )?;
243    ///     Ok(())
244    /// }
245    /// ```
246    pub fn new(hyper_path: Option<&Path>, parameters: Option<&Parameters>) -> Result<Self> {
247        let hyperd_path = match hyper_path {
248            Some(path) => path.to_path_buf(),
249            None => Self::find_hyperd()?,
250        };
251        Self::start_server(&hyperd_path, parameters)
252    }
253
254    /// Resolves the hyperd executable from the `HYPERD_PATH` environment
255    /// variable. The value can point at the executable directly, or at a
256    /// directory containing it.
257    ///
258    /// If `HYPERD_PATH` is unset, returns an error instructing the caller
259    /// to either set it or run the `hyperdb-bootstrap` downloader to
260    /// install a pinned release at `.hyperd/current/hyperd`.
261    fn find_hyperd() -> Result<PathBuf> {
262        #[cfg(windows)]
263        const HYPERD_EXE: &str = "hyperd.exe";
264        #[cfg(not(windows))]
265        const HYPERD_EXE: &str = "hyperd";
266
267        let Ok(path_str) = std::env::var("HYPERD_PATH") else {
268            // Walk up from CWD looking for .hyperd/current/<exe> written by
269            // `hyperdb-bootstrap download`. This lets `node examples/foo.mjs`
270            // run from any subdirectory of the repo without exporting HYPERD_PATH.
271            if let Ok(cwd) = std::env::current_dir() {
272                let mut dir = cwd.as_path();
273                loop {
274                    let candidate = dir.join(".hyperd").join("current").join(HYPERD_EXE);
275                    if candidate.exists() {
276                        return Ok(candidate);
277                    }
278                    match dir.parent() {
279                        Some(parent) => dir = parent,
280                        None => break,
281                    }
282                }
283            }
284            return Err(Error::config(
285                "HYPERD_PATH is not set. Point it at a hyperd executable, \
286                or run `make download-hyperd` (or `cargo run -p hyperdb-bootstrap -- download`) \
287                to install a pinned release at `.hyperd/current/hyperd`.",
288            ));
289        };
290
291        let path = PathBuf::from(&path_str);
292        if path.is_dir() {
293            let with_exe = path.join(HYPERD_EXE);
294            if with_exe.exists() {
295                return Ok(with_exe);
296            }
297            #[cfg(windows)]
298            {
299                let without_exe = path.join("hyperd");
300                if without_exe.exists() {
301                    return Ok(without_exe);
302                }
303            }
304            return Err(Error::config(format!(
305                "HYPERD_PATH set to '{path_str}' but {HYPERD_EXE} not found in that directory"
306            )));
307        }
308        if path.exists() {
309            return Ok(path);
310        }
311        #[cfg(windows)]
312        {
313            let with_ext = PathBuf::from(format!("{path_str}.exe"));
314            if with_ext.exists() {
315                return Ok(with_ext);
316            }
317        }
318        Err(Error::config(format!(
319            "HYPERD_PATH set to '{}' but hyperd executable not found (checked: {})",
320            path_str,
321            path.display()
322        )))
323    }
324
325    /// Starts the hyperd server process with callback connection.
326    fn start_server(hyperd_path: &Path, parameters: Option<&Parameters>) -> Result<Self> {
327        // Verify hyperd exists
328        if !hyperd_path.exists() {
329            return Err(Error::config(format!(
330                "Hyper executable not found at: {}",
331                hyperd_path.display()
332            )));
333        }
334
335        info!(
336            target: "hyperdb_api",
337            path = %hyperd_path.display(),
338            "hyperd-starting"
339        );
340
341        // Create callback listener on ephemeral port
342        // This is the "dead man's switch" - when this connection is closed, Hyper shuts down
343        let callback_listener = TcpListener::bind("127.0.0.1:0")
344            .map_err(|e| Error::connection_with_io("Failed to create callback listener", e))?;
345
346        let callback_port = callback_listener
347            .local_addr()
348            .map_err(|e| Error::connection_with_io("Failed to get callback port", e))?
349            .port();
350
351        // Set a timeout for accepting the callback connection
352        callback_listener.set_nonblocking(false).map_err(|e| {
353            Error::connection_with_io("Failed to set callback listener to blocking", e)
354        })?;
355
356        // Check if user wants to disable default parameters
357        let use_defaults = parameters.is_none_or(|p| !p.contains_key(NO_DEFAULT_PARAMETERS));
358
359        // Get the listen mode
360        let listen_mode = parameters.and_then(|p| p.listen_mode).unwrap_or_default();
361
362        // Get transport mode (default to TCP until UDS performance is validated)
363        // See IPC_IMPLEMENTATION.md for details
364        #[cfg(unix)]
365        let transport_mode = parameters
366            .and_then(|p| p.transport_mode)
367            .unwrap_or(TransportMode::Tcp);
368        #[cfg(windows)]
369        let transport_mode = parameters
370            .and_then(|p| p.transport_mode)
371            .unwrap_or(TransportMode::Tcp);
372        #[cfg(not(any(unix, windows)))]
373        let transport_mode = TransportMode::Tcp;
374
375        // Create socket directory for UDS if needed (Unix only)
376        #[cfg(unix)]
377        let socket_directory: Option<PathBuf> = if transport_mode == TransportMode::Ipc {
378            // Use custom directory if provided, otherwise create temp directory
379            let dir = if let Some(custom_dir) =
380                parameters.and_then(|p| p.domain_socket_directory.as_ref())
381            {
382                custom_dir.clone()
383            } else {
384                // Create a temp directory for the socket
385                let temp_dir = std::env::temp_dir().join(format!("hyper-{}", std::process::id()));
386                std::fs::create_dir_all(&temp_dir).map_err(|e| {
387                    Error::connection_with_io("Failed to create socket directory", e)
388                })?;
389                temp_dir
390            };
391            Some(dir)
392        } else {
393            None
394        };
395
396        // On non-Unix platforms there is no UDS socket directory; the variable
397        // is only referenced inside `#[cfg(unix)]` blocks so we do not need a
398        // placeholder binding here.
399
400        // Create pipe name for Named Pipes if needed (Windows only)
401        #[cfg(windows)]
402        let pipe_name: Option<String> = if transport_mode == TransportMode::Ipc {
403            Some(format!("hyper-{}", std::process::id()))
404        } else {
405            None
406        };
407
408        // Build command arguments
409        let mut cmd = Command::new(hyperd_path);
410
411        // The "run" subcommand starts the server
412        cmd.arg("run");
413
414        // Callback connection - Hyper will connect to this and send its endpoint
415        // When this connection is closed, Hyper will shut down gracefully
416        cmd.arg("--callback-connection")
417            .arg(format!("tab.tcp://127.0.0.1:{callback_port}"));
418
419        // Configure listen connection based on mode and transport
420        // Connection string formats:
421        // - tab.tcp://host:port - libpq over TCP
422        // - tab.domain://<dir>/domain/<name> - libpq over Unix Domain Socket
423        // - tcp.grpc://host:port - gRPC
424        #[cfg(unix)]
425        let listen_connection = if transport_mode == TransportMode::Ipc {
426            let socket_dir = socket_directory.as_ref().unwrap();
427            match listen_mode {
428                ListenMode::LibPq => format!("tab.domain://{}/domain/hyper", socket_dir.display()),
429                ListenMode::Grpc { port } => format!("tcp.grpc://127.0.0.1:{port}"),
430                ListenMode::Both { grpc_port } => {
431                    format!(
432                        "tab.domain://{}/domain/hyper,tcp.grpc://127.0.0.1:{}",
433                        socket_dir.display(),
434                        grpc_port
435                    )
436                }
437            }
438        } else {
439            match listen_mode {
440                ListenMode::LibPq => "tab.tcp://localhost:0".to_string(),
441                ListenMode::Grpc { port } => format!("tcp.grpc://127.0.0.1:{port}"),
442                ListenMode::Both { grpc_port } => {
443                    format!("tab.tcp://localhost:0,tcp.grpc://127.0.0.1:{grpc_port}")
444                }
445            }
446        };
447
448        #[cfg(windows)]
449        let listen_connection = if transport_mode == TransportMode::Ipc {
450            let pname = pipe_name.as_ref().unwrap();
451            match listen_mode {
452                ListenMode::LibPq => format!("tab.pipe://./pipe/{pname}"),
453                ListenMode::Grpc { port } => format!("tcp.grpc://127.0.0.1:{port}"),
454                ListenMode::Both { grpc_port } => {
455                    format!("tab.pipe://./pipe/{pname},tcp.grpc://127.0.0.1:{grpc_port}")
456                }
457            }
458        } else {
459            match listen_mode {
460                ListenMode::LibPq => "tab.tcp://localhost:0".to_string(),
461                ListenMode::Grpc { port } => format!("tcp.grpc://127.0.0.1:{port}"),
462                ListenMode::Both { grpc_port } => {
463                    format!("tab.tcp://localhost:0,tcp.grpc://127.0.0.1:{grpc_port}")
464                }
465            }
466        };
467
468        #[cfg(not(any(unix, windows)))]
469        let listen_connection = match listen_mode {
470            ListenMode::LibPq => "tab.tcp://localhost:0".to_string(),
471            ListenMode::Grpc { port } => format!("tcp.grpc://127.0.0.1:{}", port),
472            ListenMode::Both { grpc_port } => {
473                format!("tab.tcp://localhost:0,tcp.grpc://127.0.0.1:{}", grpc_port)
474            }
475        };
476
477        cmd.arg("--listen-connection").arg(&listen_connection);
478
479        // Helper to check if a parameter is already set by the user
480        let user_has_param =
481            |key: &str| -> bool { parameters.is_some_and(|p| p.contains_key(key)) };
482
483        // Apply default instance parameters (matching C++ HyperProcess behavior)
484        // These can be overridden by user parameters or disabled entirely with NO_DEFAULT_PARAMETERS
485        if use_defaults {
486            // Initial user for the Hyper instance
487            if !user_has_param("init_user") {
488                cmd.arg("--init-user=tableau_internal_user");
489            }
490
491            // Enable gRPC threads if gRPC mode is enabled
492            // Required for gRPC to function - without this, Hyper will fail to start with:
493            // "gRPC threads are required for running gRPC services"
494            // Using 4 threads as a reasonable default (can be overridden by user)
495            if matches!(
496                listen_mode,
497                ListenMode::Grpc { .. } | ListenMode::Both { .. }
498            ) && !user_has_param("grpc_threads")
499            {
500                cmd.arg("--grpc-threads=4");
501            }
502
503            // Enable gRPC result persistence if gRPC mode is enabled
504            // This is required for ADAPTIVE and ASYNC transfer modes
505            if matches!(
506                listen_mode,
507                ListenMode::Grpc { .. } | ListenMode::Both { .. }
508            ) && !user_has_param("grpc_persist_results")
509            {
510                cmd.arg("--grpc-persist-results=true");
511            }
512
513            // Default language setting
514            if !user_has_param("language") {
515                cmd.arg("--language=en_US");
516            }
517
518            // Log configuration: file-based JSON logging
519            if !user_has_param("log_config") {
520                cmd.arg(format!("--log-config={DEFAULT_LOG_CONFIG}"));
521            }
522
523            // Date style for date parsing (Month-Day-Year)
524            if !user_has_param("date_style") {
525                cmd.arg("--date-style=MDY");
526            }
527
528            // Enforce strict date_style (day/month/year ordering must match exactly)
529            if !user_has_param("date_style_lenient") {
530                cmd.arg("--date-style-lenient=false");
531            }
532
533            // Set default log directory to current directory
534            if !user_has_param("log_dir")
535                && let Ok(cwd) = std::env::current_dir()
536            {
537                cmd.arg(format!("--log-dir={}", cwd.display()));
538            }
539
540            // Disable password requirement for local development
541            if !user_has_param("no_password") {
542                cmd.arg("--no-password");
543            }
544
545            // Skip license check for local development
546            if !user_has_param("skip_license") {
547                cmd.arg("--skip-license");
548            }
549
550            // Default new .hyper databases to file format version 3, which
551            // adds support for 128-bit NUMERICs (required to ingest parquet
552            // files whose decimal columns are stored as DECIMAL128).
553            // File format 3 has shipped since Hyper 2022.4.0.
554            if !user_has_param("default_database_version") {
555                cmd.arg("--default-database-version=3");
556            }
557        }
558
559        // Add custom parameters from user
560        if let Some(params) = parameters {
561            for (key, value) in params.iter() {
562                // Skip internal/special parameters
563                if key == "callback_connection"
564                    || key == "listen_connection"
565                    || key == NO_DEFAULT_PARAMETERS
566                {
567                    continue;
568                }
569
570                // Convert underscores to dashes for command-line arguments
571                let cli_key = key.replace('_', "-");
572
573                if value.is_empty() {
574                    cmd.arg(format!("--{cli_key}"));
575                } else {
576                    cmd.arg(format!("--{cli_key}={value}"));
577                }
578            }
579        }
580
581        // Resolve the effective log directory for later access via log_dir()
582        let resolved_log_dir =
583            if let Some(user_dir) = parameters.and_then(|p| p.get("log_dir")).map(PathBuf::from) {
584                Some(user_dir)
585            } else if use_defaults {
586                std::env::current_dir().ok()
587            } else {
588                None
589            };
590
591        // Redirect stdout/stderr to null - we get the endpoint via callback connection
592        cmd.stdout(Stdio::null());
593        cmd.stderr(Stdio::null());
594
595        // On Unix, start hyperd in its own process group so it doesn't receive
596        // Ctrl-C (SIGINT) signals meant for the parent process. This allows the
597        // parent to handle Ctrl-C gracefully and properly shut down hyperd via
598        // the callback connection mechanism.
599        #[cfg(unix)]
600        cmd.process_group(0);
601
602        // On Windows, prevent a console window from flashing when spawning hyperd.
603        // CREATE_NO_WINDOW (0x08000000) suppresses the creation of a visible console.
604        #[cfg(windows)]
605        {
606            use std::os::windows::process::CommandExt;
607            const CREATE_NO_WINDOW: u32 = 0x08000000;
608            cmd.creation_flags(CREATE_NO_WINDOW);
609        }
610
611        // Start the process
612        let child = cmd.spawn().map_err(|e| {
613            Error::connection_with_io(
614                format!("Failed to start Hyper server at {}", hyperd_path.display()),
615                e,
616            )
617        })?;
618
619        // Wait for Hyper to connect back to our callback listener
620        let (callback_connection, callback_endpoint) = Self::wait_for_callback(&callback_listener)?;
621
622        // Determine the endpoints based on listen mode and callback response
623        let (endpoint, grpc_endpoint) = match listen_mode {
624            ListenMode::LibPq => {
625                // Callback returns libpq endpoint
626                (Some(callback_endpoint), None)
627            }
628            ListenMode::Grpc { port } => {
629                // Callback returns gRPC endpoint (with resolved port if auto-assigned)
630                let grpc_ep = if port == 0 {
631                    // The callback returns the actual gRPC endpoint with resolved port
632                    callback_endpoint
633                } else {
634                    format!("127.0.0.1:{port}")
635                };
636                (None, Some(grpc_ep))
637            }
638            ListenMode::Both { grpc_port } => {
639                // Callback returns libpq endpoint, gRPC uses specified port
640                (
641                    Some(callback_endpoint),
642                    Some(format!("127.0.0.1:{grpc_port}")),
643                )
644            }
645        };
646
647        // Parse connection endpoint if we have a libpq endpoint
648        let connection_endpoint = endpoint.as_ref().map(|ep| {
649            #[cfg(unix)]
650            {
651                // Check if it's a UDS path (contains path separator but no colon with port)
652                if ep.starts_with('/') || socket_directory.is_some() {
653                    // UDS endpoint - construct from socket directory
654                    if let Some(ref dir) = socket_directory {
655                        return ConnectionEndpoint::domain_socket(dir, "hyper");
656                    }
657                    // Parse as path
658                    let path = std::path::Path::new(ep);
659                    let dir = path.parent().unwrap_or(std::path::Path::new("/"));
660                    let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("hyper");
661                    return ConnectionEndpoint::domain_socket(dir, name);
662                }
663            }
664            #[cfg(windows)]
665            {
666                // Check if it's a Named Pipe endpoint
667                if let Some(ref pname) = pipe_name {
668                    return ConnectionEndpoint::named_pipe(".", pname);
669                }
670            }
671            // TCP endpoint (host:port format)
672            let parts: Vec<&str> = ep.split(':').collect();
673            if parts.len() == 2
674                && let Ok(port) = parts[1].parse::<u16>()
675            {
676                return ConnectionEndpoint::tcp(parts[0], port);
677            }
678            ConnectionEndpoint::tcp("localhost", 7483) // fallback
679        });
680
681        Ok(HyperProcess {
682            child: Some(child),
683            endpoint,
684            connection_endpoint,
685            grpc_endpoint,
686            hyperd_path: hyperd_path.to_path_buf(),
687            shutdown_initiated: Arc::new(AtomicBool::new(false)),
688            callback_connection: Some(callback_connection),
689            listen_mode,
690            transport_mode,
691            log_dir: resolved_log_dir,
692            #[cfg(unix)]
693            socket_directory,
694            #[cfg(windows)]
695            pipe_name,
696        })
697    }
698
699    /// Waits for Hyper to connect to our callback listener and send its endpoint.
700    ///
701    /// Protocol:
702    /// - Hyper connects to the callback listener
703    /// - Hyper sends: [1 byte length][N bytes connection descriptor string]
704    /// - Connection descriptor format: "tab.tcp://host:port"
705    fn wait_for_callback(listener: &TcpListener) -> Result<(TcpStream, String)> {
706        // Set a timeout for accepting connections
707        listener.set_nonblocking(true).ok();
708
709        let timeout = Duration::from_secs(60);
710        let start = std::time::Instant::now();
711
712        // Poll for incoming connection with timeout
713        let mut stream = loop {
714            if start.elapsed() > timeout {
715                return Err(Error::internal(
716                    "Timeout waiting for Hyper to connect to callback listener. \
717                    Hyper may have failed to start - check hyperd logs for details.",
718                ));
719            }
720
721            match listener.accept() {
722                Ok((stream, _addr)) => break stream,
723                Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
724                    thread::sleep(Duration::from_millis(50));
725                }
726                Err(e) => {
727                    return Err(Error::connection_with_io(
728                        "Failed to accept callback connection",
729                        e,
730                    ));
731                }
732            }
733        };
734
735        // Set stream back to blocking for reading
736        stream.set_nonblocking(false).map_err(|e| {
737            Error::connection_with_io("Failed to set callback stream to blocking", e)
738        })?;
739
740        // Set read timeout
741        stream.set_read_timeout(Some(Duration::from_secs(10))).ok();
742
743        // Read the endpoint descriptor from Hyper
744        // Protocol: [1 byte length][N bytes descriptor string]
745        let mut len_buf = [0u8; 1];
746        stream.read_exact(&mut len_buf).map_err(|e| {
747            Error::connection_with_io("Failed to read endpoint length from Hyper", e)
748        })?;
749
750        let len = len_buf[0] as usize;
751        if len == 0 {
752            return Err(Error::internal("Hyper sent empty endpoint descriptor"));
753        }
754
755        let mut descriptor_buf = vec![0u8; len];
756        stream.read_exact(&mut descriptor_buf).map_err(|e| {
757            Error::connection_with_io("Failed to read endpoint descriptor from Hyper", e)
758        })?;
759
760        let descriptor = String::from_utf8(descriptor_buf)
761            .map_err(|e| Error::internal(format!("Invalid UTF-8 in endpoint descriptor: {e}")))?;
762
763        // Trim null bytes and whitespace that Hyper may include
764        let descriptor = descriptor.trim_matches(|c: char| c == '\0' || c.is_whitespace());
765
766        // Parse the connection descriptor (format: "tab.tcp://host:port")
767        let endpoint = Self::parse_connection_descriptor(descriptor)?;
768
769        // Clear read timeout for the connection we'll keep open
770        stream.set_read_timeout(None).ok();
771
772        info!(
773            target: "hyperdb_api",
774            %endpoint,
775            "hyperd-started"
776        );
777
778        Ok((stream, endpoint))
779    }
780
781    /// Parses a connection descriptor to extract host:port, socket path, or pipe path.
782    ///
783    /// Input formats:
784    /// - "tab.tcp://host:port" → "host:port"
785    /// - "tab.domain://<dir>/domain/<name>" → "<dir>/domain/<name>" (socket path)
786    /// - "tab.pipe://<host>/pipe/<name>" → "<host>/pipe/<name>" (named pipe)
787    /// - "tcp.grpc://host:port" → "host:port"
788    fn parse_connection_descriptor(descriptor: &str) -> Result<String> {
789        // Handle domain socket format
790        if let Some(rest) = descriptor.strip_prefix("tab.domain://") {
791            // Return the full path for UDS
792            if let Some(idx) = rest.find("/domain/") {
793                let dir = &rest[..idx];
794                let name = &rest[idx + 8..]; // "/domain/".len() == 8
795                let socket_path = format!("{dir}/domain/{name}");
796                return Ok(socket_path);
797            }
798            return Ok(rest.to_string());
799        }
800
801        // Handle named pipe format
802        if let Some(rest) = descriptor.strip_prefix("tab.pipe://") {
803            // Format: tab.pipe://<host>/pipe/<name>
804            // Return as pipe path: \\<host>\pipe\<name>
805            if let Some(idx) = rest.find("/pipe/") {
806                let host = &rest[..idx];
807                let name = &rest[idx + 6..]; // "/pipe/".len() == 6
808                let pipe_path = format!(r"\\{host}\pipe\{name}");
809                return Ok(pipe_path);
810            }
811            return Ok(rest.to_string());
812        }
813
814        // Handle TCP prefixes
815        let without_prefix = descriptor
816            .strip_prefix("tab.tcp://")
817            .or_else(|| descriptor.strip_prefix("tcp.grpc://"))
818            .or_else(|| descriptor.strip_prefix("tcp.grpctls://"))
819            .or_else(|| descriptor.strip_prefix("tcp://"))
820            .unwrap_or(descriptor);
821
822        // Validate it looks like host:port
823        if without_prefix.contains(':') && !without_prefix.is_empty() {
824            Ok(without_prefix.to_string())
825        } else {
826            Err(Error::internal(format!(
827                "Invalid connection descriptor format: '{descriptor}'. Expected '<scheme>://host:port' or 'tab.domain://<dir>/domain/<name>'"
828            )))
829        }
830    }
831
832    /// Returns the libpq endpoint for connecting to this instance.
833    ///
834    /// The endpoint is in the format "host:port" (e.g., "localhost:54321").
835    ///
836    /// Returns `None` if the process was started in gRPC-only mode.
837    /// Use [`grpc_endpoint`](Self::grpc_endpoint) for gRPC connections.
838    #[must_use]
839    pub fn endpoint(&self) -> Option<&str> {
840        self.endpoint.as_deref()
841    }
842
843    /// Returns the libpq endpoint, or an error if not available.
844    ///
845    /// This is a convenience method to avoid `unwrap()` calls when you need
846    /// the endpoint and want proper error handling.
847    ///
848    /// # Errors
849    ///
850    /// Returns an error if this process was started in gRPC-only mode.
851    ///
852    /// # Example
853    ///
854    /// ```no_run
855    /// use hyperdb_api::{HyperProcess, Result};
856    ///
857    /// fn main() -> Result<()> {
858    ///     let hyper = HyperProcess::new(None, None)?;
859    ///     let endpoint = hyper.require_endpoint()?; // No unwrap() needed!
860    ///     println!("Server running at: {}", endpoint);
861    ///     Ok(())
862    /// }
863    /// ```
864    pub fn require_endpoint(&self) -> crate::error::Result<&str> {
865        self.endpoint().ok_or_else(|| {
866            crate::error::Error::internal(
867                "HyperProcess does not have a libpq endpoint (gRPC-only mode). \
868                 Use grpc_endpoint() instead or start with LibPq or Both listen mode.",
869            )
870        })
871    }
872
873    /// Returns the gRPC endpoint for connecting to this instance.
874    ///
875    /// The endpoint is in the format "host:port" (e.g., "127.0.0.1:7484").
876    ///
877    /// Returns `None` if the process was started in libpq-only mode.
878    #[must_use]
879    pub fn grpc_endpoint(&self) -> Option<&str> {
880        self.grpc_endpoint.as_deref()
881    }
882
883    /// Returns the gRPC endpoint, or an error if not available.
884    ///
885    /// This is a convenience method to avoid `unwrap()` calls when you need
886    /// the gRPC endpoint and want proper error handling.
887    ///
888    /// # Errors
889    ///
890    /// Returns an error if this process was started in libpq-only mode.
891    pub fn require_grpc_endpoint(&self) -> crate::error::Result<&str> {
892        self.grpc_endpoint().ok_or_else(|| {
893            crate::error::Error::internal(
894                "HyperProcess does not have a gRPC endpoint (libpq-only mode). \
895                 Use endpoint() instead or start with Grpc or Both listen mode.",
896            )
897        })
898    }
899
900    /// Returns the gRPC endpoint as a full URL suitable for gRPC clients.
901    ///
902    /// Returns the endpoint prefixed with "http://" (e.g., "<http://127.0.0.1:7484>").
903    /// Returns `None` if the process was started in libpq-only mode.
904    #[must_use]
905    pub fn grpc_url(&self) -> Option<String> {
906        self.grpc_endpoint.as_ref().map(|ep| format!("http://{ep}"))
907    }
908
909    /// Returns the gRPC URL, or an error if not available.
910    ///
911    /// This is a convenience method to avoid `unwrap()` calls when you need
912    /// the gRPC URL and want proper error handling.
913    ///
914    /// # Errors
915    ///
916    /// Returns an error if this process was started in libpq-only mode.
917    pub fn require_grpc_url(&self) -> crate::error::Result<String> {
918        Ok(format!("http://{}", self.require_grpc_endpoint()?))
919    }
920
921    /// Returns the listen mode this process was started with.
922    #[must_use]
923    pub fn listen_mode(&self) -> ListenMode {
924        self.listen_mode
925    }
926
927    /// Returns the transport mode this process was started with.
928    #[must_use]
929    pub fn transport_mode(&self) -> TransportMode {
930        self.transport_mode
931    }
932
933    /// Returns the connection endpoint for this process.
934    ///
935    /// This returns a [`ConnectionEndpoint`] that can be used to connect
936    /// to this Hyper instance via TCP, Unix Domain Socket, or Named Pipe.
937    #[must_use]
938    pub fn connection_endpoint(&self) -> Option<&ConnectionEndpoint> {
939        self.connection_endpoint.as_ref()
940    }
941
942    /// Returns the log directory where hyperd writes its log files.
943    ///
944    /// The log file is typically `hyperd.log` within this directory.
945    /// Returns `None` if the log directory could not be determined (e.g.,
946    /// default parameters were disabled and no `log_dir` was specified).
947    ///
948    /// This is useful for setting up [`QueryStatsProvider`](crate::QueryStatsProvider)
949    /// implementations that parse the Hyper log file.
950    #[must_use]
951    pub fn log_dir(&self) -> Option<&Path> {
952        self.log_dir.as_deref()
953    }
954
955    /// Returns the socket directory used for UDS connections (Unix only).
956    ///
957    /// Returns `None` if the process is using TCP or if no socket directory was created.
958    #[cfg(unix)]
959    #[must_use]
960    pub fn socket_directory(&self) -> Option<&Path> {
961        self.socket_directory.as_deref()
962    }
963
964    /// Returns the pipe name used for Named Pipe connections (Windows only).
965    ///
966    /// Returns `None` if the process is using TCP.
967    #[cfg(windows)]
968    #[must_use]
969    pub fn pipe_name(&self) -> Option<&str> {
970        self.pipe_name.as_deref()
971    }
972
973    /// Returns the process ID of the Hyper server.
974    #[must_use]
975    pub fn pid(&self) -> Option<u32> {
976        self.child.as_ref().map(std::process::Child::id)
977    }
978
979    /// Returns whether the Hyper server process is still running.
980    #[must_use]
981    pub fn is_running(&self) -> bool {
982        if let Some(ref child) = self.child {
983            // Try to check if process is alive without waiting
984            #[cfg(unix)]
985            {
986                match Command::new("kill")
987                    .args(["-0", &child.id().to_string()])
988                    .output()
989                {
990                    Ok(output) => output.status.success(),
991                    Err(_) => false,
992                }
993            }
994            #[cfg(not(unix))]
995            {
996                // On Windows, we can't easily check without waiting
997                // Assume it's running if we have a handle
998                let _ = child; // Silence unused variable warning
999                true
1000            }
1001        } else {
1002            false
1003        }
1004    }
1005
1006    /// Returns true if the hyperd child process has exited (or no child exists).
1007    ///
1008    /// Uses [`std::process::Child::try_wait`] under the hood, which is correct
1009    /// on both Unix and Windows. On Unix this also reaps any zombie state as a
1010    /// side effect — a hyperd that has been SIGKILLed but not yet `wait()`ed
1011    /// on by the parent will be observed as exited and cleaned up here.
1012    ///
1013    /// Prefer this over [`Self::is_running`] when the caller owns the
1014    /// `HyperProcess` mutably and needs an authoritative liveness signal.
1015    /// `is_running` uses `kill -0` on Unix (which incorrectly reports zombies
1016    /// as alive) and is a no-op on Windows.
1017    pub fn has_exited(&mut self) -> bool {
1018        match self.child.as_mut() {
1019            Some(child) => match child.try_wait() {
1020                Ok(Some(_status)) => true,
1021                Ok(None) => false,
1022                Err(_) => true,
1023            },
1024            None => true,
1025        }
1026    }
1027
1028    /// Shuts down the Hyper server gracefully with a timeout.
1029    ///
1030    /// This closes the callback connection, which signals Hyper to shut down gracefully.
1031    /// If Hyper doesn't exit within the timeout, it will be forcefully terminated.
1032    ///
1033    /// # Arguments
1034    ///
1035    /// * `timeout` - Maximum time to wait for graceful shutdown before force-killing.
1036    ///
1037    /// # Errors
1038    ///
1039    /// Returns an error if the shutdown fails.
1040    pub fn shutdown_timeout(mut self, timeout: Duration) -> Result<()> {
1041        self.shutdown_initiated.store(true, Ordering::SeqCst);
1042        self.do_shutdown(Some(timeout))
1043    }
1044
1045    /// Shuts down the Hyper server gracefully, waiting indefinitely.
1046    ///
1047    /// This closes the callback connection and waits for Hyper to exit.
1048    /// Use [`shutdown_timeout`](Self::shutdown_timeout) if you need a timeout.
1049    ///
1050    /// # Errors
1051    ///
1052    /// Returns an error if the shutdown fails.
1053    pub fn shutdown_graceful(mut self) -> Result<()> {
1054        self.shutdown_initiated.store(true, Ordering::SeqCst);
1055        self.do_shutdown(None)
1056    }
1057
1058    /// Closes the callback connection to signal Hyper to shut down.
1059    ///
1060    /// This is the graceful shutdown mechanism - Hyper monitors the callback connection
1061    /// and will initiate shutdown when it's closed.
1062    fn close_callback_connection(&mut self) {
1063        if let Some(conn) = self.callback_connection.take() {
1064            // Gracefully shutdown both directions
1065            let _ = conn.shutdown(Shutdown::Both);
1066            // Connection is dropped here, closing the socket
1067        }
1068    }
1069
1070    /// Internal shutdown implementation.
1071    fn do_shutdown(&mut self, timeout: Option<Duration>) -> Result<()> {
1072        info!(target: "hyperdb_api", "hyperd-shutdown");
1073
1074        // Step 1: Close the callback connection to signal graceful shutdown
1075        // Hyper will detect this and begin shutting down
1076        self.close_callback_connection();
1077
1078        if let Some(mut child) = self.child.take() {
1079            // Step 2: Wait for the process to exit
1080            let wait_result = if let Some(timeout) = timeout {
1081                // Wait with timeout
1082                let start = std::time::Instant::now();
1083                loop {
1084                    match child.try_wait() {
1085                        Ok(Some(status)) => break Ok(status),
1086                        Ok(None) => {
1087                            if start.elapsed() > timeout {
1088                                // Step 3: Force kill ONLY after timeout
1089                                // This should rarely happen if Hyper is healthy
1090                                #[cfg(unix)]
1091                                {
1092                                    // Try SIGTERM first
1093                                    let _ = Command::new("kill")
1094                                        .args(["-TERM", &child.id().to_string()])
1095                                        .output();
1096                                    thread::sleep(Duration::from_millis(100));
1097                                }
1098                                // Then force kill
1099                                let _ = child.kill();
1100                                break child.wait().map_err(|e| {
1101                                    Error::connection_with_io("Failed to wait for hyperd", e)
1102                                });
1103                            }
1104                            thread::sleep(Duration::from_millis(100));
1105                        }
1106                        Err(e) => {
1107                            break Err(Error::connection_with_io("Failed to wait for hyperd", e));
1108                        }
1109                    }
1110                }
1111            } else {
1112                // Wait indefinitely
1113                child
1114                    .wait()
1115                    .map_err(|e| Error::connection_with_io("Failed to wait for hyperd", e))
1116            };
1117
1118            wait_result?;
1119        }
1120
1121        Ok(())
1122    }
1123}
1124
1125impl Drop for HyperProcess {
1126    fn drop(&mut self) {
1127        if !self.shutdown_initiated.load(Ordering::SeqCst) {
1128            // Try to gracefully shutdown with a short timeout
1129            let _ = self.do_shutdown(Some(Duration::from_secs(5)));
1130        }
1131
1132        // Clean up socket directory if we created one
1133        #[cfg(unix)]
1134        if let Some(ref dir) = self.socket_directory {
1135            // Only clean up if it's a temp directory we created (contains our PID)
1136            let dir_name = dir.file_name().and_then(|n| n.to_str()).unwrap_or("");
1137            if dir_name.starts_with("hyper-") {
1138                let _ = std::fs::remove_dir_all(dir);
1139            }
1140        }
1141    }
1142}
1143
1144// SAFETY: `HyperProcess` owns its `std::process::Child` handle and (optionally)
1145// a TCP connection. Both are themselves `Send`, and no field holds thread-local
1146// state or a non-`Send` raw pointer. Ownership of a `HyperProcess` therefore
1147// transfers cleanly across thread boundaries.
1148unsafe impl Send for HyperProcess {}
1149
1150/// Special parameter key that disables default instance parameters when present.
1151///
1152/// By default, [`HyperProcess`] starts hyperd with a set of sensible default parameters
1153/// (matching the C++ `HyperProcess` behavior). If you need full control over all parameters,
1154/// include this key in your [`Parameters`] to disable all defaults.
1155pub(crate) const NO_DEFAULT_PARAMETERS: &str = "no_default_parameters";
1156
1157/// Default log configuration for hyperd: file-based JSON logging.
1158const DEFAULT_LOG_CONFIG: &str = "file,json,all,hyperd,0";
1159
1160/// Parameters for configuring the Hyper server.
1161///
1162/// When starting a [`HyperProcess`], a set of default parameters are automatically applied
1163/// (matching the C++ `HyperProcess` behavior):
1164///
1165/// | Parameter | Default Value | Description |
1166/// |-----------|---------------|-------------|
1167/// | `init_user` | `tableau_internal_user` | Initial user for the Hyper instance |
1168/// | `language` | `en_US` | Default language setting |
1169/// | `log_config` | `file,json,all,hyperd,0` | Log configuration |
1170/// | `date_style` | `MDY` | Date format (Month-Day-Year) |
1171/// | `date_style_lenient` | `false` | Strict date parsing |
1172/// | `log_dir` | Current directory | Log file directory |
1173/// | `no_password` | (flag) | Disable password requirement |
1174/// | `skip_license` | (flag) | Skip license check |
1175/// | `default_database_version` | `3` | File format version for newly created `.hyper` databases (v3 adds 128-bit NUMERIC support, required for DECIMAL128 parquet columns) |
1176///
1177/// To disable these defaults, add the `no_default_parameters` key (for example
1178/// `params.set("no_default_parameters", "")` via [`Parameters::set`].
1179///
1180/// # Listen Modes
1181///
1182/// Use [`set_listen_mode`](Parameters::set_listen_mode) to configure which protocols Hyper listens on:
1183///
1184/// ```
1185/// use hyperdb_api::{ListenMode, Parameters};
1186///
1187/// // gRPC only (for Arrow-based queries)
1188/// let mut params = Parameters::new();
1189/// params.set_listen_mode(ListenMode::Grpc { port: 0 });
1190///
1191/// // Both libpq and gRPC
1192/// let mut params = Parameters::new();
1193/// params.set_listen_mode(ListenMode::Both { grpc_port: 7484 });
1194/// ```
1195///
1196/// # Example
1197///
1198/// ```
1199/// use hyperdb_api::Parameters;
1200///
1201/// let mut params = Parameters::new();
1202/// params.set("log_file_size_limit", "100k");
1203/// params.set("log_file_max_count", "7");
1204/// ```
1205///
1206/// # Transport Modes
1207///
1208/// Use [`set_transport_mode`](Parameters::set_transport_mode) to control whether Hyper uses
1209/// TCP or IPC (Unix Domain Sockets):
1210///
1211/// ```
1212/// use hyperdb_api::{Parameters, TransportMode};
1213///
1214/// let mut params = Parameters::new();
1215/// params.set_transport_mode(TransportMode::Tcp); // Force TCP instead of IPC
1216/// ```
1217///
1218/// Transport mode for `HyperProcess` connections.
1219///
1220/// Controls whether the server uses TCP or Unix Domain Sockets (IPC) for connections.
1221/// On Unix systems, IPC is the default for better local performance.
1222/// On Windows, TCP is always used.
1223#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
1224pub enum TransportMode {
1225    /// Use IPC (Unix Domain Sockets on Unix, Named Pipes on Windows).
1226    /// This is the default mode and provides better performance for local connections.
1227    #[default]
1228    Ipc,
1229
1230    /// Use TCP/IP connections.
1231    /// Required when connecting from remote clients or when IPC is not available.
1232    Tcp,
1233}
1234
1235/// Parameters for configuring the Hyper server.
1236///
1237/// When starting a [`HyperProcess`], a set of default parameters are automatically applied
1238/// (matching the C++ `HyperProcess` behavior). You can override these defaults or disable
1239/// them entirely by adding the `no_default_parameters` key (for example
1240/// `params.set("no_default_parameters", "")` via [`Parameters::set`].
1241///
1242/// # Transport Modes
1243///
1244/// Use [`set_transport_mode`](Self::set_transport_mode) to control whether Hyper uses
1245/// TCP or IPC (Unix Domain Sockets on Unix systems).
1246///
1247/// # Example
1248///
1249/// ```
1250/// use hyperdb_api::{Parameters, TransportMode};
1251///
1252/// let mut params = Parameters::new();
1253/// params.set("log_file_size_limit", "100k");
1254/// params.set_transport_mode(TransportMode::Tcp); // Force TCP instead of IPC
1255/// ```
1256#[derive(Debug, Clone, Default)]
1257pub struct Parameters {
1258    values: Vec<(String, String)>,
1259    /// The listen mode for the Hyper server.
1260    pub(crate) listen_mode: Option<ListenMode>,
1261    /// The transport mode (TCP or IPC/UDS).
1262    pub(crate) transport_mode: Option<TransportMode>,
1263    /// Custom domain socket directory (Unix only).
1264    #[cfg(unix)]
1265    pub(crate) domain_socket_directory: Option<PathBuf>,
1266}
1267
1268impl Parameters {
1269    /// Creates a new empty Parameters instance.
1270    #[must_use]
1271    pub fn new() -> Self {
1272        Parameters {
1273            values: Vec::new(),
1274            listen_mode: None,
1275            transport_mode: None,
1276            #[cfg(unix)]
1277            domain_socket_directory: None,
1278        }
1279    }
1280
1281    /// Sets the transport mode (TCP or IPC/UDS).
1282    ///
1283    /// By default, `HyperProcess` uses IPC (Unix Domain Sockets on Unix) for better
1284    /// performance. Use `TransportMode::Tcp` if you need TCP connections.
1285    ///
1286    /// # Example
1287    ///
1288    /// ```
1289    /// use hyperdb_api::{Parameters, TransportMode};
1290    ///
1291    /// let mut params = Parameters::new();
1292    /// params.set_transport_mode(TransportMode::Tcp); // Use TCP instead of IPC
1293    /// ```
1294    pub fn set_transport_mode(&mut self, mode: TransportMode) -> &mut Self {
1295        self.transport_mode = Some(mode);
1296        self
1297    }
1298
1299    /// Returns the configured transport mode.
1300    #[must_use]
1301    pub fn transport_mode(&self) -> Option<TransportMode> {
1302        self.transport_mode
1303    }
1304
1305    /// Sets a custom domain socket directory (Unix only).
1306    ///
1307    /// By default, `HyperProcess` creates sockets in a temporary directory.
1308    /// Use this to specify a custom location.
1309    #[cfg(unix)]
1310    pub fn set_domain_socket_directory(&mut self, dir: impl Into<PathBuf>) -> &mut Self {
1311        self.domain_socket_directory = Some(dir.into());
1312        self
1313    }
1314
1315    /// Returns the configured domain socket directory (Unix only).
1316    #[cfg(unix)]
1317    #[must_use]
1318    pub fn domain_socket_directory(&self) -> Option<&Path> {
1319        self.domain_socket_directory.as_deref()
1320    }
1321
1322    /// Sets a parameter value.
1323    ///
1324    /// # Arguments
1325    ///
1326    /// * `key` - The parameter name.
1327    /// * `value` - The parameter value (empty string for flags).
1328    pub fn set(&mut self, key: impl Into<String>, value: impl Into<String>) -> &mut Self {
1329        let key = key.into();
1330        let value = value.into();
1331
1332        // Update existing or add new
1333        if let Some(entry) = self.values.iter_mut().find(|(k, _)| k == &key) {
1334            entry.1 = value;
1335        } else {
1336            self.values.push((key, value));
1337        }
1338
1339        self
1340    }
1341
1342    /// Sets the listen mode for the Hyper server.
1343    ///
1344    /// This controls which protocols the server listens on:
1345    /// - [`ListenMode::LibPq`]: `PostgreSQL` wire protocol only (default)
1346    /// - [`ListenMode::Grpc`]: gRPC protocol only (query-only, Arrow results)
1347    /// - [`ListenMode::Both`]: Both protocols enabled
1348    ///
1349    /// # Example
1350    ///
1351    /// ```
1352    /// use hyperdb_api::{ListenMode, Parameters};
1353    ///
1354    /// let mut params = Parameters::new();
1355    /// params.set_listen_mode(ListenMode::Grpc { port: 0 }); // Auto-assign port
1356    /// ```
1357    pub fn set_listen_mode(&mut self, mode: ListenMode) -> &mut Self {
1358        self.listen_mode = Some(mode);
1359        self
1360    }
1361
1362    /// Returns the configured listen mode, if any.
1363    #[must_use]
1364    pub fn listen_mode(&self) -> Option<ListenMode> {
1365        self.listen_mode
1366    }
1367
1368    /// Gets a parameter value.
1369    #[must_use]
1370    pub fn get(&self, key: &str) -> Option<&str> {
1371        self.values
1372            .iter()
1373            .find(|(k, _)| k == key)
1374            .map(|(_, v)| v.as_str())
1375    }
1376
1377    /// Returns whether the parameters contain the given key.
1378    #[must_use]
1379    pub fn contains_key(&self, key: &str) -> bool {
1380        self.values.iter().any(|(k, _)| k == key)
1381    }
1382
1383    /// Returns an iterator over the parameters.
1384    pub fn iter(&self) -> impl Iterator<Item = (&str, &str)> {
1385        self.values.iter().map(|(k, v)| (k.as_str(), v.as_str()))
1386    }
1387
1388    /// Returns whether the parameters are empty.
1389    #[must_use]
1390    pub fn is_empty(&self) -> bool {
1391        self.values.is_empty()
1392    }
1393
1394    /// Returns the number of parameters.
1395    #[must_use]
1396    pub fn len(&self) -> usize {
1397        self.values.len()
1398    }
1399}
1400
1401#[cfg(test)]
1402mod tests {
1403    use super::*;
1404
1405    #[test]
1406    fn test_parameters() {
1407        let mut params = Parameters::new();
1408        params.set("key1", "value1");
1409        params.set("key2", "value2");
1410
1411        assert_eq!(params.get("key1"), Some("value1"));
1412        assert_eq!(params.get("key2"), Some("value2"));
1413        assert_eq!(params.get("key3"), None);
1414        assert_eq!(params.len(), 2);
1415    }
1416
1417    #[test]
1418    fn test_parameters_update() {
1419        let mut params = Parameters::new();
1420        params.set("key", "value1");
1421        params.set("key", "value2");
1422
1423        assert_eq!(params.get("key"), Some("value2"));
1424        assert_eq!(params.len(), 1);
1425    }
1426
1427    #[test]
1428    fn test_parse_connection_descriptor() {
1429        assert_eq!(
1430            HyperProcess::parse_connection_descriptor("tab.tcp://localhost:12345").unwrap(),
1431            "localhost:12345"
1432        );
1433        assert_eq!(
1434            HyperProcess::parse_connection_descriptor("tab.tcp://127.0.0.1:7483").unwrap(),
1435            "127.0.0.1:7483"
1436        );
1437        assert_eq!(
1438            HyperProcess::parse_connection_descriptor("tcp://localhost:8080").unwrap(),
1439            "localhost:8080"
1440        );
1441        // Already in host:port format
1442        assert_eq!(
1443            HyperProcess::parse_connection_descriptor("localhost:9999").unwrap(),
1444            "localhost:9999"
1445        );
1446    }
1447
1448    #[test]
1449    fn test_parse_connection_descriptor_named_pipe() {
1450        assert_eq!(
1451            HyperProcess::parse_connection_descriptor("tab.pipe://./pipe/hyper-12345").unwrap(),
1452            r"\\.\pipe\hyper-12345"
1453        );
1454        assert_eq!(
1455            HyperProcess::parse_connection_descriptor("tab.pipe://server1/pipe/mydb").unwrap(),
1456            r"\\server1\pipe\mydb"
1457        );
1458    }
1459
1460    #[test]
1461    fn test_parse_connection_descriptor_invalid() {
1462        assert!(HyperProcess::parse_connection_descriptor("").is_err());
1463        assert!(HyperProcess::parse_connection_descriptor("invalid").is_err());
1464    }
1465
1466    #[test]
1467    fn test_parameters_contains_key() {
1468        let mut params = Parameters::new();
1469        params.set("key1", "value1");
1470
1471        assert!(params.contains_key("key1"));
1472        assert!(!params.contains_key("key2"));
1473    }
1474
1475    #[test]
1476    fn test_no_default_parameters_constant() {
1477        // Verify the constant matches what C++ uses
1478        assert_eq!(NO_DEFAULT_PARAMETERS, "no_default_parameters");
1479    }
1480
1481    #[test]
1482    fn test_parameters_with_no_defaults() {
1483        let mut params = Parameters::new();
1484        params.set(NO_DEFAULT_PARAMETERS, "");
1485        params.set("init_user", "custom_user");
1486
1487        assert!(params.contains_key(NO_DEFAULT_PARAMETERS));
1488        assert_eq!(params.get("init_user"), Some("custom_user"));
1489    }
1490}