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
use std::net::{SocketAddr, ToSocketAddrs};
use once_cell::sync::Lazy;
use regex::Regex;
pub fn extract_metastore_uri_and_index_id_from_index_uri(
mut index_uri: &str,
) -> anyhow::Result<(&str, &str)> {
static INDEX_URI_PATTERN: Lazy<Regex> = Lazy::new(|| Regex::new(r"^.+://.+/.+$").unwrap());
static INDEX_ID_PATTERN: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^[a-zA-Z][a-zA-Z0-9_\-]*$").unwrap());
if !INDEX_URI_PATTERN.is_match(index_uri) {
anyhow::bail!(
"Invalid index uri `{}`. Expected format is: `protocol://bucket/path-to-target`.",
index_uri
);
}
if index_uri.ends_with('/') {
index_uri = &index_uri[..index_uri.len() - 1];
}
let parts: Vec<&str> = index_uri.rsplitn(2, '/').collect();
if parts.len() != 2 {
anyhow::bail!("Failed to parse the uri into a metastore_uri and an index_id.");
}
if !INDEX_ID_PATTERN.is_match(parts[0]) {
anyhow::bail!("Invalid index_id `{}`. Only alpha-numeric, `-` and `_` characters allowed. Cannot start with `-`, `_` or digit.", parts[0]);
}
Ok((parts[1], parts[0]))
}
pub fn to_socket_addr(addr_str: &str) -> anyhow::Result<SocketAddr> {
if let Some(addr) = addr_str.to_socket_addrs()?.next() {
Ok(addr)
} else {
Err(anyhow::anyhow!(
"Unable to resolve the socket address. {}",
addr_str
))
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum QuickwitEnv {
UNSET,
LOCAL,
}
impl Default for QuickwitEnv {
fn default() -> Self {
Self::UNSET
}
}
pub fn get_quickwit_env() -> QuickwitEnv {
match std::env::var("QUICKWIT_ENV") {
Ok(val) if val == "LOCAL" => QuickwitEnv::LOCAL,
Ok(val) => {
panic!("unkown value set for QUICKWIT_ENV {}", val)
}
Err(_) => QuickwitEnv::UNSET,
}
}