Skip to content

Commit d5172c6

Browse files
authored
Merge pull request #30 from driftluo/dump-to-0.3.5
Dump to 0.3.5
2 parents 43c3be9 + a1e6bf0 commit d5172c6

8 files changed

+95
-108
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "influx_db_client"
3-
version = "0.3.4"
3+
version = "0.3.5"
44
authors = ["piaoliu <[email protected]>"]
55
documentation = "https://docs.rs/influx_db_client/"
66
repository = "https://github.com/driftluo/InfluxDBClient-rs"

README.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@ This project has been able to run properly, PR is welcome.
1515

1616
## Usage
1717

18-
### Recommend
18+
### Use
1919

2020
```
2121
[dependencies]
22-
influx_db_client = "^0.3.4"
23-
24-
[patch.crates-io]
25-
influx_db_client = { git = 'https://github.com/driftluo/InfluxDBClient-rs' }
22+
influx_db_client = "^0.3.5"
2623
```
2724

2825
### http
@@ -88,7 +85,7 @@ fn main() {
8885

8986
This is the [API Document](https://docs.influxdata.com/influxdb/v1.2/tools/api/), it may apply to version 1.0 or higher.
9087

91-
I have tested it in version 1.0.2 and 1.3.5.
88+
I have tested it in version 1.0.2/1.3.5/1.5.
9289

9390
## Thanks
9491

src/client.rs

+36-32
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
use serde_json;
2-
use serde_json::de::IoRead as SerdeIoRead;
31
use hyper::client::Client as hyper_client;
2+
use hyper::client::RequestBuilder;
43
use hyper::client::Response;
54
use hyper::net::HttpsConnector;
6-
use hyper::client::RequestBuilder;
5+
use hyper::Url;
76
use hyper_native_tls::native_tls::TlsConnector;
87
use hyper_native_tls::NativeTlsClient;
9-
use hyper::Url;
8+
use serde_json;
9+
use serde_json::de::IoRead as SerdeIoRead;
1010
use std::io::Read;
11-
use std::time::Duration;
12-
use std::net::UdpSocket;
1311
use std::iter::FromIterator;
12+
use std::net::UdpSocket;
1413
use std::net::{SocketAddr, ToSocketAddrs};
15-
use {error, serialization, Node, Point, Points, Precision, Query, ChunkedQuery};
14+
use std::time::Duration;
15+
use {error, serialization, ChunkedQuery, Node, Point, Points, Precision, Query};
1616

1717
/// The client to influxdb
1818
#[derive(Debug)]
@@ -137,10 +137,9 @@ impl Client {
137137
None => param.push(("precision", "s")),
138138
};
139139

140-
match rp {
141-
Some(t) => param.push(("rp", t)),
142-
None => (),
143-
};
140+
if let Some(t) = rp {
141+
param.push(("rp", t))
142+
}
144143

145144
let url = self.build_url("write", Some(param));
146145

@@ -150,12 +149,12 @@ impl Client {
150149

151150
match res.status_raw().0 {
152151
204 => Ok(()),
153-
400 => Err(error::Error::SyntaxError(serialization::conversion(err))),
152+
400 => Err(error::Error::SyntaxError(serialization::conversion(&err))),
154153
401 | 403 => Err(error::Error::InvalidCredentials(
155154
"Invalid authentication credentials.".to_string(),
156155
)),
157156
404 => Err(error::Error::DataBaseDoesNotExist(
158-
serialization::conversion(err),
157+
serialization::conversion(&err),
159158
)),
160159
500 => Err(error::Error::RetentionPolicyDoesNotExist(err)),
161160
_ => Err(error::Error::Unknow("There is something wrong".to_string())),
@@ -180,7 +179,7 @@ impl Client {
180179
q: &str,
181180
epoch: Option<Precision>,
182181
) -> Result<ChunkedQuery<SerdeIoRead<Response>>, error::Error> {
183-
return self.query_raw_chunked(q, epoch);
182+
self.query_raw_chunked(q, epoch)
184183
}
185184

186185
/// Drop measurement
@@ -405,12 +404,16 @@ impl Client {
405404
}
406405
}
407406

408-
fn send_request(&self, q: &str, epoch: Option<Precision>, chunked: bool) -> Result<Response, error::Error> {
407+
fn send_request(
408+
&self,
409+
q: &str,
410+
epoch: Option<Precision>,
411+
chunked: bool,
412+
) -> Result<Response, error::Error> {
409413
let mut param = vec![("db", self.db.as_str()), ("q", q)];
410414

411-
match epoch {
412-
Some(ref t) => param.push(("epoch", t.to_str())),
413-
None => (),
415+
if let Some(ref t) = epoch {
416+
param.push(("epoch", t.to_str()))
414417
}
415418

416419
if chunked {
@@ -423,9 +426,9 @@ impl Client {
423426
let mut res = {
424427
if q_lower.starts_with("select") && !q_lower.contains("into")
425428
|| q_lower.starts_with("show")
426-
{
427-
self.client.get(url).send()?
428-
} else {
429+
{
430+
self.client.get(url).send()?
431+
} else {
429432
self.client.post(url).send()?
430433
}
431434
};
@@ -438,9 +441,9 @@ impl Client {
438441
let json_data: Query = serde_json::from_str(&context).unwrap();
439442

440443
Err(error::Error::SyntaxError(serialization::conversion(
441-
json_data.error.unwrap(),
444+
&json_data.error.unwrap(),
442445
)))
443-
},
446+
}
444447
401 | 403 => Err(error::Error::InvalidCredentials(
445448
"Invalid authentication credentials.".to_string(),
446449
)),
@@ -456,14 +459,18 @@ impl Client {
456459
let _ = response.read_to_string(&mut context);
457460

458461
let json_data: Query = serde_json::from_str(&context).unwrap();
459-
return Ok(json_data);
462+
Ok(json_data)
460463
}
461464

462465
/// Query and return to the native json structure
463-
fn query_raw_chunked(&self, q: &str, epoch: Option<Precision>) -> Result<ChunkedQuery<SerdeIoRead<Response>>, error::Error> {
466+
fn query_raw_chunked(
467+
&self,
468+
q: &str,
469+
epoch: Option<Precision>,
470+
) -> Result<ChunkedQuery<SerdeIoRead<Response>>, error::Error> {
464471
let response = self.send_request(q, epoch, true)?;
465472
let stream = serde_json::Deserializer::from_reader(response).into_iter::<Query>();
466-
return Ok(stream);
473+
Ok(stream)
467474
}
468475

469476
/// Constructs the full URL for an API call.
@@ -472,12 +479,9 @@ impl Client {
472479

473480
let mut authentication = Vec::new();
474481

475-
match self.authentication {
476-
Some(ref t) => {
477-
authentication.push(("u", &t.0));
478-
authentication.push(("p", &t.1));
479-
}
480-
None => {}
482+
if let Some(ref t) = self.authentication {
483+
authentication.push(("u", &t.0));
484+
authentication.push(("p", &t.1));
481485
}
482486

483487
let url = Url::parse_with_params(url.as_str(), authentication).unwrap();

src/error.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::fmt;
2-
use std::io;
31
use hyper;
42
use std::error::Error as StdError;
3+
use std::fmt;
4+
use std::io;
55

66
/// The error of influxdb client
77
#[derive(Debug, Deserialize, Serialize)]

src/keys.rs

+19-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use serde_json;
12
use std::collections::HashMap;
23
use std::iter::FromIterator;
3-
use serde_json;
44
use std::iter::Iterator;
55

66
/// Influxdb value, Please look at [this address](https://docs.influxdata.com/influxdb/v1.3/write_protocols/line_protocol_reference/)
@@ -142,7 +142,7 @@ pub struct Series {
142142
}
143143

144144
/// Time accuracy
145-
#[derive(Debug)]
145+
#[derive(Debug, Clone, Copy)]
146146
pub enum Precision {
147147
/// n
148148
Nanoseconds,
@@ -187,29 +187,23 @@ macro_rules! points {
187187
/// Create Point by macro
188188
#[macro_export]
189189
macro_rules! point {
190-
($x:expr) => {
191-
{
192-
Point::new($x)
193-
}
194-
};
195-
($x:expr, $y:expr, $z:expr) => {
196-
{
197-
Point {
198-
measurement: String::from($x),
199-
tags: $y,
200-
fields: $z,
201-
timestamp: None
202-
}
190+
($x:expr) => {{
191+
Point::new($x)
192+
}};
193+
($x:expr, $y:expr, $z:expr) => {{
194+
Point {
195+
measurement: String::from($x),
196+
tags: $y,
197+
fields: $z,
198+
timestamp: None,
203199
}
204-
};
205-
($x:expr, $y:expr, $z:expr, $a:expr) => {
206-
{
207-
Point {
208-
measurement: String::from($x),
209-
tags: $y,
210-
fields: $z,
211-
timestamp: Some($a)
212-
}
200+
}};
201+
($x:expr, $y:expr, $z:expr, $a:expr) => {{
202+
Point {
203+
measurement: String::from($x),
204+
tags: $y,
205+
fields: $z,
206+
timestamp: Some($a),
213207
}
214-
};
208+
}};
215209
}

src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ extern crate serde;
7373
extern crate serde_derive;
7474
extern crate serde_json;
7575

76-
/// Serialization module
77-
pub mod serialization;
78-
/// Error module
79-
pub mod error;
8076
/// All API on influxdb client, Including udp, http
8177
pub mod client;
78+
/// Error module
79+
pub mod error;
8280
/// Points and Query Data Deserialize
8381
pub mod keys;
82+
/// Serialization module
83+
pub mod serialization;
8484

8585
pub use client::{Client, TLSOption, UdpClient};
86-
pub use keys::{Node, Point, Points, Precision, Query, ChunkedQuery, Series, Value};
8786
pub use error::Error;
87+
pub use keys::{ChunkedQuery, Node, Point, Points, Precision, Query, Series, Value};

src/serialization.rs

+17-26
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ use {Point, Value};
44
pub(crate) fn line_serialization<T: Iterator<Item = Point>>(points: T) -> String {
55
let mut line = Vec::new();
66
for point in points {
7-
line.push(escape_measurement(point.measurement));
7+
line.push(escape_measurement(&point.measurement));
88

9-
for (tag, value) in point.tags.into_iter() {
9+
for (tag, value) in point.tags {
1010
line.push(",".to_string());
11-
line.push(escape_keys_and_tags(tag.to_string()));
11+
line.push(escape_keys_and_tags(&tag));
1212
line.push("=".to_string());
1313

1414
match value {
15-
Value::String(s) => line.push(escape_keys_and_tags(s.to_string())),
15+
Value::String(s) => line.push(escape_keys_and_tags(&s)),
1616
Value::Float(f) => line.push(f.to_string()),
1717
Value::Integer(i) => line.push(i.to_string() + "i"),
1818
Value::Boolean(b) => line.push({
@@ -27,7 +27,7 @@ pub(crate) fn line_serialization<T: Iterator<Item = Point>>(points: T) -> String
2727

2828
let mut was_first = true;
2929

30-
for (field, value) in point.fields.into_iter() {
30+
for (field, value) in point.fields {
3131
line.push(
3232
{
3333
if was_first {
@@ -38,12 +38,12 @@ pub(crate) fn line_serialization<T: Iterator<Item = Point>>(points: T) -> String
3838
}
3939
}.to_string(),
4040
);
41-
line.push(escape_keys_and_tags(field.to_string()));
41+
line.push(escape_keys_and_tags(&field));
4242
line.push("=".to_string());
4343

4444
match value {
4545
Value::String(s) => line.push(escape_string_field_value(
46-
s.to_string().replace("\\\"", "\\\\\""),
46+
&s.replace("\\\"", "\\\\\""),
4747
)),
4848
Value::Float(f) => line.push(f.to_string()),
4949
Value::Integer(i) => line.push(i.to_string() + "i"),
@@ -57,12 +57,9 @@ pub(crate) fn line_serialization<T: Iterator<Item = Point>>(points: T) -> String
5757
}
5858
}
5959

60-
match point.timestamp {
61-
Some(t) => {
62-
line.push(" ".to_string());
63-
line.push(t.to_string());
64-
}
65-
_ => {}
60+
if let Some(t) = point.timestamp {
61+
line.push(" ".to_string());
62+
line.push(t.to_string());
6663
}
6764

6865
line.push("\n".to_string())
@@ -88,7 +85,7 @@ pub(crate) fn quote_literal(value: &str) -> String {
8885
}
8986

9087
#[inline]
91-
pub(crate) fn conversion(value: String) -> String {
88+
pub(crate) fn conversion(value: &str) -> String {
9289
value
9390
.replace("\'", "")
9491
.replace("\"", "")
@@ -98,20 +95,20 @@ pub(crate) fn conversion(value: String) -> String {
9895
}
9996

10097
#[inline]
101-
fn escape_keys_and_tags(value: String) -> String {
98+
fn escape_keys_and_tags(value: &str) -> String {
10299
value
103100
.replace(",", "\\,")
104101
.replace("=", "\\=")
105102
.replace(" ", "\\ ")
106103
}
107104

108105
#[inline]
109-
fn escape_measurement(value: String) -> String {
106+
fn escape_measurement(value: &str) -> String {
110107
value.replace(",", "\\,").replace(" ", "\\ ")
111108
}
112109

113110
#[inline]
114-
fn escape_string_field_value(value: String) -> String {
111+
fn escape_string_field_value(value: &str) -> String {
115112
format!("\"{}\"", value.replace("\"", "\\\""))
116113
}
117114

@@ -136,25 +133,19 @@ mod test {
136133
#[test]
137134
fn escape_keys_and_tags_test() {
138135
assert_eq!(
139-
escape_keys_and_tags(String::from("foo, hello=world")),
136+
escape_keys_and_tags("foo, hello=world"),
140137
"foo\\,\\ hello\\=world"
141138
)
142139
}
143140

144141
#[test]
145142
fn escape_measurement_test() {
146-
assert_eq!(
147-
escape_measurement(String::from("foo, hello")),
148-
"foo\\,\\ hello"
149-
)
143+
assert_eq!(escape_measurement("foo, hello"), "foo\\,\\ hello")
150144
}
151145

152146
#[test]
153147
fn escape_string_field_value_test() {
154-
assert_eq!(
155-
escape_string_field_value(String::from("\"foo")),
156-
"\"\\\"foo\""
157-
)
148+
assert_eq!(escape_string_field_value("\"foo"), "\"\\\"foo\"")
158149
}
159150

160151
#[test]

0 commit comments

Comments
 (0)