-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess.rs
More file actions
132 lines (120 loc) · 4.4 KB
/
process.rs
File metadata and controls
132 lines (120 loc) · 4.4 KB
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
// Copyright (c) 2026, Salesforce, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT
use napi::bindgen_prelude::*;
use napi_derive::napi;
use std::path::Path;
use crate::connection::Connection;
use crate::types::CreateMode;
// =============================================================================
// HyperProcess
// =============================================================================
/// Manages a local Hyper server process.
///
/// The server is automatically started when a `HyperProcess` is created and
/// stopped when `close()` is called. You **must** call `close()` when done
/// to ensure the server process is properly terminated.
///
/// @example
/// ```js
/// const hyper = new HyperProcess();
/// const conn = await hyper.connectToDatabase('test.hyper', CreateMode.CreateIfNotExists);
/// // ... use the connection ...
/// await conn.close();
/// hyper.close();
/// ```
#[napi]
#[derive(Debug)]
pub struct HyperProcess {
inner: Option<hyperdb_api::HyperProcess>,
}
#[napi]
impl HyperProcess {
#[allow(
clippy::needless_pass_by_value,
reason = "call-site ergonomics: function consumes logically-owned parameters, refactoring signatures is not worth per-site churn"
)]
/// Creates and starts a new Hyper server process.
///
/// The server binary (`hyperd`) is automatically located. Pass a custom
/// path if it's not in the standard location.
///
/// @param hyperPath - Optional path to the `hyperd` binary.
#[napi(constructor)]
pub fn new(hyper_path: Option<String>) -> Result<Self> {
let path = hyper_path.as_ref().map(|p| Path::new(p.as_str()));
let process = hyperdb_api::HyperProcess::new(path, None)
.map_err(|e| Error::from_reason(e.to_string()))?;
Ok(HyperProcess {
inner: Some(process),
})
}
/// Returns the server endpoint (e.g., "localhost:7483").
///
/// Use this to connect to the server via `Connection.connect()`.
#[napi(getter)]
pub fn endpoint(&self) -> Result<String> {
let process = self
.inner
.as_ref()
.ok_or_else(|| Error::from_reason("HyperProcess is closed"))?;
process
.endpoint()
.map(std::string::ToString::to_string)
.ok_or_else(|| Error::from_reason("No endpoint available"))
}
/// Convenience method: connects to this server with a database.
///
/// @param databasePath - Path to the database file.
/// @param createMode - How to handle database creation.
#[napi]
pub async fn connect_to_database(
&self,
database_path: String,
create_mode: CreateMode,
) -> Result<Connection> {
let process = self
.inner
.as_ref()
.ok_or_else(|| Error::from_reason("HyperProcess is closed"))?;
let endpoint = process
.endpoint()
.map(std::string::ToString::to_string)
.ok_or_else(|| Error::from_reason("No endpoint available"))?;
Connection::connect(endpoint, database_path, create_mode).await
}
#[allow(
clippy::unnecessary_wraps,
reason = "signature retained for API symmetry / future fallibility; returning Result/Option keeps callers from breaking when the function later grows failure cases"
)]
/// Stops the Hyper server process.
///
/// This must be called when you're done to ensure proper cleanup.
/// After calling this, no further connections can be made.
#[napi]
pub fn close(&mut self) -> Result<()> {
// Drop the process, which triggers graceful shutdown
self.inner.take();
Ok(())
}
/// Returns true if the process is still running.
#[napi(getter)]
pub fn is_open(&self) -> bool {
self.inner.is_some()
}
/// Returns the path to the Hyper log file (`hyperd.log`).
///
/// Use this with `conn.enableQueryStats(hyper.logPath)` to enable
/// detailed query performance statistics collection.
///
/// Returns `null` if the log directory could not be determined.
#[napi(getter)]
pub fn log_path(&self) -> Result<Option<String>> {
let process = self
.inner
.as_ref()
.ok_or_else(|| Error::from_reason("HyperProcess is closed"))?;
Ok(process
.log_dir()
.map(|dir| dir.join("hyperd.log").to_string_lossy().to_string()))
}
}