Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Line Proto Serializer with References to Line Proto Docs #159

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 55 additions & 25 deletions influxdb/src/query/line_proto_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,26 @@ impl LineProtoTerm<'_> {
}
}

/// Serializes Field Values.
fn escape_field_value(v: &Type, use_v2: bool) -> String {
use Type::*;
match v {
Boolean(v) => {
if *v {
"true"
} else {
"false"
}
}
.to_string(),
Float(v) => v.to_string(),
SignedInteger(v) => format!("{}i", v),
UnsignedInteger(v) => {
if use_v2 {
format!("{}u", v)
} else {
format!("{}i", v)
}
}
Boolean(v) => Self::escape_boolean(v),
Float(v) => Self::escape_float(v),
SignedInteger(v) => Self::escape_signed_integer(v),
UnsignedInteger(v) => Self::escape_unsigned_integer(v, use_v2),
Text(v) => format!(r#""{}""#, Self::escape_any(v, &QUOTES_SLASHES)),
}
}

/// Serializes Tag Values. InfluxDB stores tag values as strings, so we format everything to string.
///
/// V2: https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/#tag-set
/// V1: https://docs.influxdata.com/influxdb/v1/write_protocols/line_protocol_tutorial/#data-types
fn escape_tag_value(v: &Type) -> String {
use Type::*;
match v {
Boolean(v) => {
if *v {
"true"
} else {
"false"
}
}
.to_string(),
Boolean(v) => Self::escape_boolean(v),
Float(v) => format!(r#"{}"#, v),
SignedInteger(v) => format!(r#"{}"#, v),
UnsignedInteger(v) => format!(r#"{}"#, v),
Expand All @@ -82,6 +67,51 @@ impl LineProtoTerm<'_> {
fn escape_any(s: &str, re: &Regex) -> String {
re.replace_all(s, r"\$0").to_string()
}

/// Escapes a Rust f64 to InfluxDB Line Protocol
///
/// https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/#float
/// IEEE-754 64-bit floating-point numbers. Default numerical type.
/// InfluxDB supports scientific notation in float field values, but this crate does not serialize them.
fn escape_float(v: &f64) -> String {
v.to_string()
}

/// Escapes a Rust bool to InfluxDB Line Protocol
///
/// https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/#boolean
/// Stores true or false values.
fn escape_boolean(v: &bool) -> String {
if *v {
"true"
} else {
"false"
}
.to_string()
}

/// Escapes a Rust i64 to InfluxDB Line Protocol
///
/// https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/#integer
/// Signed 64-bit integers. Trailing i on the number specifies an integer.
fn escape_signed_integer(v: &i64) -> String {
format!("{}i", v)
}

/// Escapes a Rust u64 to InfluxDB Line Protocol
///
/// https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/#uinteger
/// Unsigned 64-bit integers. Trailing u on the number specifies an unsigned integer.
///
/// InfluxDB version 1 does not know unsigned, we fallback to (signed) integer:
/// https://docs.influxdata.com/influxdb/v1/write_protocols/line_protocol_tutorial/#data-types
fn escape_unsigned_integer(v: &u64, use_v2: bool) -> String {
if use_v2 {
format!("{}u", v)
} else {
format!("{}i", v)
}
}
}

#[cfg(test)]
Expand Down
Loading