Skip to content

Commit d8219d7

Browse files
authored
Merge pull request #37 from driftluo/modify-api
Modify part of api
2 parents 3f3106e + c4188d6 commit d8219d7

File tree

5 files changed

+106
-129
lines changed

5 files changed

+106
-129
lines changed

src/client.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ use bytes::Bytes;
22
use futures::prelude::*;
33
use reqwest::{Client as HttpClient, Response, Url};
44
use serde_json::de::IoRead;
5-
use std::io::Cursor;
6-
use std::iter::FromIterator;
7-
use std::net::UdpSocket;
8-
use std::net::{SocketAddr, ToSocketAddrs};
5+
use std::{io::Cursor, iter::FromIterator, net::SocketAddr, net::UdpSocket};
96

107
use crate::{error, serialization, ChunkedQuery, Node, Point, Points, Precision, Query};
118

@@ -63,8 +60,8 @@ impl Client {
6360
}
6461

6562
/// View the current db name
66-
pub fn get_db(&self) -> String {
67-
self.db.to_owned()
63+
pub fn get_db(&self) -> &str {
64+
self.db.as_str()
6865
}
6966

7067
/// Query whether the corresponding database exists, return bool
@@ -466,8 +463,8 @@ impl Client {
466463

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

469-
if param.is_some() {
470-
Url::parse_with_params(url.as_str(), param.unwrap()).unwrap()
466+
if let Some(param) = param {
467+
Url::parse_with_params(url.as_str(), param).unwrap()
471468
} else {
472469
url
473470
}
@@ -488,23 +485,20 @@ pub struct UdpClient {
488485

489486
impl UdpClient {
490487
/// Create a new udp client.
491-
/// panic when T can't convert to SocketAddr
492-
pub fn new<T: Into<String>>(address: T) -> Self {
488+
pub fn new(address: SocketAddr) -> Self {
493489
UdpClient {
494-
hosts: vec![address.into().to_socket_addrs().unwrap().next().unwrap()],
490+
hosts: vec![address],
495491
}
496492
}
497493

498494
/// add udp host.
499-
/// panic when T can't convert to SocketAddr
500-
pub fn add_host<T: Into<String>>(&mut self, address: T) {
501-
self.hosts
502-
.push(address.into().to_socket_addrs().unwrap().next().unwrap())
495+
pub fn add_host(&mut self, address: SocketAddr) {
496+
self.hosts.push(address)
503497
}
504498

505499
/// View current hosts
506-
pub fn get_host(&self) -> Vec<SocketAddr> {
507-
self.hosts.to_owned()
500+
pub fn get_host(&self) -> &[SocketAddr] {
501+
self.hosts.as_ref()
508502
}
509503

510504
/// Send Points to influxdb.

src/keys.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,19 @@ impl Point {
4242
}
4343

4444
/// Add a tag and its value
45-
pub fn add_tag<T: Into<String>>(&mut self, tag: T, value: Value) -> &mut Self {
45+
pub fn add_tag<T: Into<String>>(mut self, tag: T, value: Value) -> Self {
4646
self.tags.insert(tag.into(), value);
4747
self
4848
}
4949

5050
/// Add a field and its value
51-
pub fn add_field<T: Into<String>>(&mut self, field: T, value: Value) -> &mut Self {
51+
pub fn add_field<T: Into<String>>(mut self, field: T, value: Value) -> Self {
5252
self.fields.insert(field.into(), value);
5353
self
5454
}
5555

5656
/// Set the specified timestamp
57-
pub fn add_timestamp(&mut self, timestamp: i64) -> &mut Self {
57+
pub fn add_timestamp(mut self, timestamp: i64) -> Self {
5858
self.timestamp = Some(timestamp);
5959
self
6060
}
@@ -76,7 +76,7 @@ impl Points {
7676
}
7777

7878
/// Insert point into already existing points
79-
pub fn push(&mut self, point: Point) -> &mut Self {
79+
pub fn push(mut self, point: Point) -> Self {
8080
self.point.push(point);
8181
self
8282
}
@@ -134,11 +134,11 @@ pub struct Series {
134134
/// measurement
135135
pub name: String,
136136
/// tag
137-
pub tags: Option<serde_json::Map<String, serde_json::Value>>,
137+
pub tags: Option<HashMap<String, Value>>,
138138
/// field names and time
139139
pub columns: Vec<String>,
140140
/// values
141-
pub values: Vec<Vec<serde_json::Value>>,
141+
pub values: Vec<Vec<Value>>,
142142
}
143143

144144
/// Time accuracy

src/lib.rs

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,49 @@
77
//! ### http
88
//!
99
//! ```Rust
10-
//! #[macro_use]
11-
//! extern crate influx_db_client;
12-
//!
1310
//! use influx_db_client::{Client, Point, Points, Value, Precision};
1411
//!
15-
//! fn main() {
16-
//! // default with "http://127.0.0.1:8086", db with "test"
17-
//! let client = Client::default().set_authentication("root", "root");
18-
//!
19-
//! let mut point = point!("test1");
20-
//! point
21-
//! .add_field("foo", Value::String("bar".to_string()))
22-
//! .add_field("integer", Value::Integer(11))
23-
//! .add_field("float", Value::Float(22.3))
24-
//! .add_field("'boolean'", Value::Boolean(false));
25-
//!
26-
//! let point1 = Point::new("test1")
27-
//! .add_tag("tags", Value::String(String::from("\\\"fda")))
28-
//! .add_tag("number", Value::Integer(12))
29-
//! .add_tag("float", Value::Float(12.6))
30-
//! .add_field("fd", Value::String("'3'".to_string()))
31-
//! .add_field("quto", Value::String("\\\"fda".to_string()))
32-
//! .add_field("quto1", Value::String("\"fda".to_string()))
33-
//! .to_owned();
34-
//!
35-
//! let points = points!(point1, point);
36-
//!
37-
//! // if Precision is None, the default is second
38-
//! // Multiple write
39-
//! let _ = client.write_points(points, Some(Precision::Seconds), None).unwrap();
40-
//!
41-
//! // query, it's type is Option<Vec<Node>>
42-
//! let res = client.query("select * from test1", None).unwrap();
43-
//! println!("{:?}", res.unwrap()[0].series)
44-
//! }
12+
//! // default with "http://127.0.0.1:8086", db with "test"
13+
//! let client = Client::default().set_authentication("root", "root");
14+
//!
15+
//! let mut point = point!("test1");
16+
//! let point = point
17+
//! .add_field("foo", Value::String("bar".to_string()))
18+
//! .add_field("integer", Value::Integer(11))
19+
//! .add_field("float", Value::Float(22.3))
20+
//! .add_field("'boolean'", Value::Boolean(false));
21+
//!
22+
//! let point1 = Point::new("test1")
23+
//! .add_tag("tags", Value::String(String::from("\\\"fda")))
24+
//! .add_tag("number", Value::Integer(12))
25+
//! .add_tag("float", Value::Float(12.6))
26+
//! .add_field("fd", Value::String("'3'".to_string()))
27+
//! .add_field("quto", Value::String("\\\"fda".to_string()))
28+
//! .add_field("quto1", Value::String("\"fda".to_string()));
29+
//!
30+
//! let points = points!(point1, point);
31+
//!
32+
//! // if Precision is None, the default is second
33+
//! // Multiple write
34+
//! client.write_points(points, Some(Precision::Seconds), None).unwrap();
35+
//!
36+
//! // query, it's type is Option<Vec<Node>>
37+
//! let res = client.query("select * from test1", None).unwrap();
38+
//! println!("{:?}", res.unwrap()[0].series)
4539
//! ```
4640
//!
4741
//! ### udp
4842
//!
4943
//! ```Rust
50-
//! #[macro_use]
51-
//! extern crate influx_db_client;
52-
//!
5344
//! use influx_db_client::{UdpClient, Point, Value};
5445
//!
55-
//! fn main() {
56-
//! let mut udp = UdpClient::new("127.0.0.1:8089");
57-
//! udp.add_host("127.0.0.1:8090");
46+
//! let mut udp = UdpClient::new("127.0.0.1:8089".parse().unwrap());
47+
//! udp.add_host("127.0.0.1:8090".parse().unwrap());
5848
//!
59-
//! let mut point = point!("test");
60-
//! point.add_field("foo", Value::String(String::from("bar")));
49+
//! let mut point = point!("test");
50+
//! point.add_field("foo", Value::String(String::from("bar")));
6151
//!
62-
//! let _ = udp.write_point(point).unwrap();
63-
//! }
52+
//! udp.write_point(point).unwrap();
6453
//! ```
6554
6655
#![deny(warnings)]

src/serialization.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ mod test {
120120

121121
#[test]
122122
fn line_serialization_test() {
123-
let mut point = Point::new("test");
124-
point.add_field("somefield", Value::Integer(65));
125-
point.add_tag("sometag", Value::Boolean(false));
123+
let point = Point::new("test")
124+
.add_field("somefield", Value::Integer(65))
125+
.add_tag("sometag", Value::Boolean(false));
126126
let points = Points::new(point);
127127

128128
assert_eq!(

0 commit comments

Comments
 (0)