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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
//! # Skytable client [![Crates.io](https://img.shields.io/crates/v/skytable?style=flat-square)](https://crates.io/crates/skytable) [![Test](https://github.com/skytable/client-rust/actions/workflows/test.yml/badge.svg)](https://github.com/skytable/client-rust/actions/workflows/test.yml) [![docs.rs](https://img.shields.io/docsrs/skytable?style=flat-square)](https://docs.rs/skytable) [![GitHub release (latest SemVer including pre-releases)](https://img.shields.io/github/v/release/skytable/client-rust?include_prereleases&style=flat-square)](https://github.com/skytable/client-rust/releases)
//!
//! ## Introduction
//!
//! This library is the official client for the free and open-source NoSQL database
//! [Skytable](https://github.com/skytable/skytable). First, go ahead and install Skytable by
//! following the instructions [here](https://docs.skytable.io/getting-started). This library supports
//! all Skytable versions that work with the [Skyhash 1.1 Protocol](https://docs.skytable.io/protocol/skyhash).
//! This version of the library was tested with the latest Skytable release
//! (release [0.7.5](https://github.com/skytable/skytable/releases/v0.7.5)).
//!
//! ## Features
//!
//! - Sync API
//! - Async API
//! - TLS in both sync/async APIs
//! - Query pipelining
//! - Easy conversion from Skyhash types into Rust types
//! - Connection pooling for sync/async
//! - Use both sync/async APIs at the same time
//! - Always up-to-date
//!
//! ## Using this library
//!
//! This library only ships with the bare minimum that is required for interacting with Skytable. Once you have
//! Skytable installed and running, you're ready to follow this guide!
//!
//! We'll start by creating a new binary application and then running actions. Create a new binary application
//! by running:
//!
//! ```shell
//! cargo new skyapp
//! ```
//!
//! **Tip**: You can see a full list of the available actions [here](https://docs.skytable.io/actions-overview).
//!
//! First add this to your `Cargo.toml` file:
//!
//! ```toml
//! skytable = "0.7.0-alpha.4"
//! ```
//!
//! Now open up your `src/main.rs` file and establish a connection to the server while also adding some
//! imports:
//!
//! ```no_run
//! use skytable::{Connection, Query, Element, SkyResult};
//! fn main() -> SkyResult<()> {
//! let mut con = Connection::new("127.0.0.1", 2003)?;
//! Ok(())
//! }
//! ```
//!
//! Now let's run a `Query`! Change the previous code block to:
//!
//! ```no_run
//! use skytable::{error, Connection, Query, Element};
//! fn main() -> Result<(), error::Error> {
//! let mut con = Connection::new("127.0.0.1", 2003)?;
//! let query = Query::from("heya");
//! let res: String = con.run_query(&query)?;
//! assert_eq!(res, "HEY!");
//! Ok(())
//! }
//! ```
//!
//! ## Running actions
//!
//! As noted [below](#binary-data), the default table is a key/value table with a binary key
//! type and a binary value type. Let's go ahead and run some actions (we're assuming you're
//! using the sync API; for async, simply change the import to `use skytable::actions::AsyncActions`).
//!
//! ### `SET`ting a key
//!
//! ```no_run
//! use skytable::actions::Actions;
//! use skytable::sync::Connection;
//!
//! let mut con = Connection::new("127.0.0.1", 2003).unwrap();
//! con.set("hello", "world").unwrap();
//! ```
//!
//! This will set the value of the key `hello` to `world` in the `default:default` entity.
//!
//! ### `GET`ting a key
//!
//! ```no_run
//! use skytable::actions::Actions;
//! use skytable::sync::Connection;
//!
//! let mut con = Connection::new("127.0.0.1", 2003).unwrap();
//! let x: String = con.get("hello").unwrap();
//! assert_eq!(x, "world");
//! ```
//!
//! Way to go — you're all set! Now go ahead and run more advanced queries!
//!
//! ## Binary data
//!
//! The `default:default` keyspace has the following declaration:
//!
//! ```text
//! Keymap { data:(binstr,binstr), volatile:false }
//! ```
//!
//! This means that the default keyspace is ready to store binary data. Let's say
//! you wanted to `SET` the value of a key called `bindata` to some binary data stored
//! in a `Vec<u8>`. You can achieve this with the `RawString` type:
//!
//! ```no_run
//! use skytable::actions::Actions;
//! use skytable::sync::Connection;
//! use skytable::types::RawString;
//!
//! let mut con = Connection::new("127.0.0.1", 2003).unwrap();
//! let mybinarydata = RawString::from(vec![1, 2, 3, 4]);
//! assert!(con.set("bindata", mybinarydata).unwrap());
//! ```
//!
//! ## Going advanced
//!
//! Now that you know how you can run basic queries, check out:
//! - the [`actions`] module documentation for learning to use actions
//! - the [`Pipeline`] documentation for using pipelines
//! - the [`pool`] module documentation for using sync/async connection pools
//! - the [`types`] module documentation for implementing your own Skyhash serializable types.
//! - the [`ddl`] module for DDL queries like `create` and `drop`
//!
//! You can also find some [examples here](https://github.com/skytable/client-rust/tree/v0.7.0-alpha.4/examples).
//!
//! ## Pipelining
//!
//! Check out the documentation for [`Pipeline`].
//!
//! ## Connection pooling
//!
//! This library supports using sync/async connection pools. See the [`pool`] module-level documentation for examples
//! and information.
//!
//! ## Async API
//!
//! If you need to use an `async` API, just change your import to:
//!
//! ```toml
//! skytable = { version = "0.7.0-alpha.4", features=["aio"], default-features = false }
//! ```
//!
//! You can now establish a connection by using `skytable::AsyncConnection::new()`, adding `.await`s wherever
//! necessary. Do note that you'll the [Tokio runtime](https://tokio.rs).
//!
//! ## Using both `sync` and `async` APIs
//!
//! With this client driver, it is possible to use both sync and `async` APIs **at the same time**. To do
//! this, simply change your import to:
//!
//! ```toml
//! skytable = { version="0.7.0-alpha.4", features=["sync", "aio"] }
//! ```
//!
//! ## TLS
//!
//! If you need to use TLS features, this crate will let you do so with OpenSSL.
//!
//! ### Using TLS with sync interfaces
//!
//! ```toml
//! skytable = { version="0.7.0-alpha.4", features=["sync","ssl"] }
//! ```
//!
//! You can now use the async `sync::TlsConnection` object.
//!
//! ### Using TLS with async interfaces
//!
//! ```toml
//! skytable = { version="0.7.0-alpha.4", features=["aio","aio-ssl"], default-features=false }
//! ```
//!
//! You can now use the async `aio::TlsConnection` object.
//!
//! ### _Packed TLS_ setup
//!
//! If you want to pack OpenSSL with your crate, then for sync add `sslv` instead of `ssl` or
//! add `aio-sslv` instead of `aio-ssl` for async. Adding this will statically link OpenSSL
//! to your crate. Do note that you'll need a C compiler, GNU Make and Perl to compile OpenSSL
//! and statically link against it.
//!
//! ## MSRV
//!
//! The MSRV for this crate is Rust 1.39. Need const generics? Add the `const-gen` feature to your
//! dependency!
//!
//! ## Contributing
//!
//! Open-source, and contributions ... — they're always welcome! For ideas and suggestions,
//! [create an issue on GitHub](https://github.com/skytable/client-rust/issues/new) and for patches,
//! fork and open those pull requests [here](https://github.com/skytable/client-rust)!
//!
//! ## License
//!
//! This client library is distributed under the permissive
//! [Apache-2.0 License](https://github.com/skytable/client-rust/blob/next/LICENSE). Now go build great apps!
//!
/*
* Created on Wed May 05 2021
*
* Copyright (c) 2021 Sayan Nandan <nandansayan@outlook.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#![cfg_attr(docsrs, feature(doc_cfg))]
// macro mods
#[macro_use]
mod util;
// endof macro mods
// public mods
pub mod actions;
pub mod ddl;
pub mod error;
pub mod pool;
pub mod types;
// endof public mods
// private mods
mod deserializer;
mod respcode;
// endof private mods
use crate::types::GetIterator;
pub use deserializer::Element;
pub use respcode::RespCode;
pub(crate) use std::io::Result as IoResult;
use types::IntoSkyhashAction;
use types::IntoSkyhashBytes;
/// The default host address
pub const DEFAULT_HOSTADDR: &str = "127.0.0.1";
/// The default port
pub const DEFAULT_PORT: u16 = 2003;
/// The default entity
pub const DEFAULT_ENTITY: &str = "default:default";
cfg_async!(
use core::{future::Future, pin::Pin};
pub mod aio;
pub use aio::Connection as AsyncConnection;
use tokio::io::AsyncWriteExt;
/// A special result that is returned when running actions (async)
pub type AsyncResult<'s, T> = Pin<Box<dyn Future<Output = T> + Send + Sync + 's>>;
);
cfg_sync!(
pub mod sync;
pub use sync::Connection;
);
/// A generic result type
pub type SkyResult<T> = Result<T, self::error::Error>;
/// A result type for queries
pub type SkyQueryResult = SkyResult<Element>;
/// A connection builder for easily building connections
///
/// ## Example (sync)
/// ```no_run
/// use skytable::ConnectionBuilder;
/// let con =
/// ConnectionBuilder::new()
/// .set_host("127.0.0.1".to_string())
/// .set_port(2003)
/// .set_entity("mykeyspace:mytable".to_string())
/// .get_connection()
/// .unwrap();
/// ```
///
/// ## Example (async)
/// ```no_run
/// use skytable::ConnectionBuilder;
/// async fn run() {
/// let con =
/// ConnectionBuilder::new()
/// .set_host("127.0.0.1".to_string())
/// .set_port(2003)
/// .set_entity("mykeyspace:mytable".to_string())
/// .get_async_connection()
/// .await
/// .unwrap();
/// }
/// ```
#[derive(Debug, Clone)]
pub struct ConnectionBuilder {
port: u16,
host: String,
entity: String,
}
impl Default for ConnectionBuilder {
fn default() -> Self {
Self::new()
}
}
impl ConnectionBuilder {
/// Create an empty connection builder
pub fn new() -> Self {
Self::default()
}
/// Set the port (defaults to `2003`)
pub fn set_port(mut self, port: u16) -> Self {
self.port = port;
self
}
/// Set the host (defaults to `localhost`)
pub fn set_host(mut self, host: String) -> Self {
self.host = host;
self
}
/// Set the entity (defaults to `default:default`)
pub fn set_entity(mut self, entity: String) -> Self {
self.entity = entity;
self
}
cfg_sync! {
/// Get a [sync connection](sync::Connection) to the database
pub fn get_connection(&self) -> SkyResult<sync::Connection> {
use crate::ddl::Ddl;
let mut con =
sync::Connection::new(&self.host, self.port)?;
con.switch(&self.entity)?;
Ok(con)
}
cfg_sync_ssl_any! {
/// Get a [sync TLS connection](sync::TlsConnection) to the database
pub fn get_tls_connection(
&self,
sslcert: String,
) -> SkyResult<sync::TlsConnection> {
use crate::ddl::Ddl;
let mut con = sync::TlsConnection::new(
&self.host,
self.port,
&sslcert,
)?;
con.switch(&self.entity)?;
Ok(con)
}
}
}
cfg_async! {
/// Get an [async connection](aio::Connection) to the database
pub async fn get_async_connection(&self) -> SkyResult<aio::Connection> {
use crate::ddl::AsyncDdl;
let mut con = aio::Connection::new(&self.host, self.port)
.await?;
con.switch(&self.entity).await?;
Ok(con)
}
cfg_async_ssl_any! {
/// Get an [async TLS connection](aio::TlsConnection) to the database
pub async fn get_async_tls_connection(
&self,
sslcert: String,
) -> SkyResult<aio::TlsConnection> {
use crate::ddl::AsyncDdl;
let mut con = aio::TlsConnection::new(
&self.host,
self.port,
&sslcert,
)
.await?;
con.switch(&self.entity).await?;
Ok(con)
}
}
}
}
cfg_sync! {
trait WriteQuerySync {
fn write_sync(&self, b: &mut impl std::io::Write) -> IoResult<()>;
}
impl WriteQuerySync for Query {
fn write_sync(&self, stream: &mut impl std::io::Write) -> IoResult<()> {
// Write the metaframe
stream.write_all(b"*1\n")?;
// Add the dataframe
let number_of_items_in_datagroup = self.len().to_string().into_bytes();
stream.write_all(&[b'~'])?;
stream.write_all(&number_of_items_in_datagroup)?;
stream.write_all(&[b'\n'])?;
stream.write_all(self.get_holding_buffer())?;
stream.flush()?;
Ok(())
}
}
impl WriteQuerySync for Pipeline {
fn write_sync(&self, stream: &mut impl std::io::Write) -> IoResult<()> {
let len = self.len.to_string().into_bytes();
stream.write_all(b"*")?;
stream.write_all(&len)?;
stream.write_all(b"\n")?;
stream.write_all(&self.chain)
}
}
}
cfg_async! {
use tokio::io::AsyncWrite;
type FutureRet<'s> = Pin<Box<dyn Future<Output = IoResult<()>> + Send + Sync + 's>>;
trait WriteQueryAsync<T: AsyncWrite + Unpin + Send + Sync>: Unpin + Sync + Send {
fn write_async<'s>(&'s self, b: &'s mut T) -> FutureRet<'s>;
}
impl<T: AsyncWrite + Unpin + Send + Sync> WriteQueryAsync<T> for Query {
fn write_async<'s>(&'s self, stream: &'s mut T) -> FutureRet {
Box::pin(async move {
// Write the metaframe
stream.write_all(b"*1\n").await?;
// Add the dataframe
let number_of_items_in_datagroup = self.len().to_string().into_bytes();
stream.write_all(&[b'~']).await?;
stream.write_all(&number_of_items_in_datagroup).await?;
stream.write_all(&[b'\n']).await?;
stream.write_all(self.get_holding_buffer()).await?;
stream.flush().await?;
Ok(())
})
}
}
impl<T: AsyncWrite + Unpin + Send + Sync> WriteQueryAsync<T> for Pipeline {
fn write_async<'s>(&'s self, stream: &'s mut T) -> FutureRet {
Box::pin(async move {
let len = self.len.to_string().into_bytes();
stream.write_all(b"*").await?;
stream.write_all(&len).await?;
stream.write_all(b"\n").await?;
stream.write_all(&self.chain).await
})
}
}
}
#[macro_export]
/// A macro that can be used to easily create queries with _almost_ variadic properties.
/// Where you'd normally create queries like this:
/// ```
/// use skytable::Query;
/// let q = Query::new().arg("mset").arg("x").arg("100").arg("y").arg("200");
/// ```
/// with this macro, you can just do this:
/// ```
/// use skytable::query;
/// let q = query!("mset", "x", "100", "y", "200");
/// ```
macro_rules! query {
($($arg:expr),+) => {
$crate::Query::new()$(.arg($arg))*
};
}
#[derive(Debug, PartialEq)]
/// This struct represents a single simple query as defined by the Skyhash protocol
///
/// A simple query is serialized into a flat string array which is nothing but a Skyhash serialized equivalent
/// of an array of [`String`] items. To construct a query like `SET x 100`, one needs to:
/// ```
/// use skytable::Query;
/// let q = Query::new().arg("set").arg("x").arg("100");
///
/// ```
/// You can now run this with a [`Connection`] or an `AsyncConnection`. You can also created queries [`From`]
/// objects that can be turned into actions. For example, these are completely valid constructions:
/// ```
/// use skytable::Query;
///
/// let q1 = Query::from(["mget", "x", "y", "z"]);
/// let q2 = Query::from(vec!["mset", "x", "100", "y", "200", "z", "300"]);
/// let q3 = Query::from("get").arg("x");
/// ```
/// **Important:** You should use the [`RawString`](types::RawString) type if you're willing to directly add things like
/// `Vec<u8>` to your query.
///
/// Finally, queries can also be created by taking references. For example:
/// ```
/// use skytable::Query;
///
/// let my_keys = vec!["key1", "key2", "key3"];
/// let mut q = Query::new();
/// for key in my_keys {
/// q.push(key);
/// }
/// ```
///
#[derive(Default)]
pub struct Query {
size_count: usize,
data: Vec<u8>,
}
impl<T> From<T> for Query
where
T: IntoSkyhashAction,
{
fn from(action: T) -> Self {
Query::new().arg(action)
}
}
impl AsRef<Query> for Query {
fn as_ref(&self) -> &Query {
self
}
}
impl Query {
/// Create a new empty query with no arguments
pub fn new() -> Self {
Query::default()
}
/// Add an argument to a query returning a [`Query`]. This can be used for queries built using the
/// builder pattern. If you need to add items, by reference, consider using [`Query::push`]
///
/// ## Panics
/// This method will panic if the passed `arg` is empty
pub fn arg(mut self, arg: impl IntoSkyhashAction) -> Self {
arg.push_into_query(&mut self);
self
}
pub(in crate) fn _push_arg(&mut self, arg: Vec<u8>) {
// A data element will look like:
// `<bytes_in_next_line>\n<data>`
let bytes_in_next_line = arg.len().to_string().into_bytes();
self.data.extend(bytes_in_next_line);
// add the LF char
self.data.push(b'\n');
// Add the data itself, which is `arg`
self.data.extend(arg);
self.data.push(b'\n'); // add the LF char
self.size_count += 1;
}
/// Add an argument to a query taking a reference to it
///
/// This is useful if you are adding queries in a loop than building it using the builder
/// pattern (to use the builder-pattern, use [`Query::arg`])
///
/// ## Panics
/// This method will panic if the passed `arg` is empty
pub fn push(&mut self, arg: impl IntoSkyhashAction) {
arg.push_into_query(self);
}
pub(in crate) fn _push_alt_iter<T, U>(
mut self,
v1: impl GetIterator<T>,
v2: impl GetIterator<U>,
) -> Query
where
T: IntoSkyhashBytes,
U: IntoSkyhashBytes,
{
v1.get_iter().zip(v2.get_iter()).for_each(|(a, b)| {
self._push_arg(a.as_bytes());
self._push_arg(b.as_bytes());
});
self
}
/// Returns the number of arguments in this query
pub fn len(&self) -> usize {
self.size_count
}
/// Check if the query is empty
pub fn is_empty(&self) -> bool {
self.size_count == 0
}
fn get_holding_buffer(&self) -> &[u8] {
&self.data
}
fn write_query_to_writable(&self, buffer: &mut Vec<u8>) {
assert!(!self.is_empty(), "Query cannot be empty");
// Add the dataframe element
let number_of_items_in_datagroup = self.len().to_string().into_bytes();
buffer.extend([b'~']);
buffer.extend(&number_of_items_in_datagroup);
buffer.extend([b'\n']);
buffer.extend(self.get_holding_buffer());
}
cfg_dbg!(
/// Get the raw bytes of a query
///
/// This is a function that is **not intended for daily use** but is for developers working to improve/debug
/// or extend the Skyhash protocol. [Skytable](https://github.com/skytable/skytable) itself uses this function
/// to generate raw queries. Once you're done passing the arguments to a query, running this function will
/// return the raw query that would be written to the stream, serialized using the Skyhash serialization protocol
pub fn into_raw_query(self) -> Vec<u8> {
let mut v = Vec::with_capacity(self.data.len());
v.extend(b"*1\n~");
v.extend(self.len().to_string().into_bytes());
v.extend(b"\n");
v.extend(self.get_holding_buffer());
v
}
/// Returns the expected size of a packet for the given lengths of the query
/// This is not a _standard feature_ but is intended for developers working
/// on Skytable
pub fn array_packet_size_hint(element_lengths: impl AsRef<[usize]>) -> usize {
let element_lengths = element_lengths.as_ref();
let mut len = 0_usize;
// *1\n_
len += 4;
let dig_count = |dig| -> usize {
let dig_count = (dig as f32).log(10.0_f32).floor() + 1_f32;
dig_count as usize
};
// the array size byte count
len += dig_count(element_lengths.len());
// the newline
len += 1;
element_lengths.iter().for_each(|elem| {
// the digit length
len += dig_count(*elem);
// the newline
len += 1;
// the element's own length
len += elem;
// the final newline
len += 1;
});
len
}
);
}
cfg_dbg!(
#[test]
fn my_query() {
let q = vec!["set", "x", "100"];
let ma_query_len = Query::from(&q).into_raw_query().len();
let q_len =
Query::array_packet_size_hint(q.iter().map(|v| v.len()).collect::<Vec<usize>>());
assert_eq!(ma_query_len, q_len);
}
);
/// # Pipeline
///
/// A pipeline is a way of queing up multiple queries, sending them to the server at once instead of sending them individually, avoiding
/// round-trip-times while also simplifying usage in several places. Responses are returned in the order they are sent.
///
/// ## Example with the sync API
///
/// ```no_run
/// use skytable::{query, Pipeline, Element, RespCode};
/// use skytable::sync::Connection;
///
/// let mut con = Connection::new("127.0.0.1", 2003).unwrap();
/// let pipe = Pipeline::new()
/// .append(query!("set", "x", "100"))
/// .append(query!("heya", "echo me!"));
///
/// let ret = con.run_pipeline(pipe).unwrap();
/// assert_eq!(
/// ret,
/// vec![
/// Element::RespCode(RespCode::Okay),
/// Element::String("echo me!".to_owned())
/// ]
/// );
/// ```
///
/// ## Example with the async API
///
/// ```no_run
/// use skytable::{query, Pipeline, Element, RespCode};
/// use skytable::aio::Connection;
///
/// async fn run() {
/// let mut con = Connection::new("127.0.0.1", 2003).await.unwrap();
/// let pipe = Pipeline::new()
/// .append(query!("set", "x", "100"))
/// .append(query!("heya", "echo me!"));
///
/// let ret = con.run_pipeline(pipe).await.unwrap();
/// assert_eq!(
/// ret,
/// vec![
/// Element::RespCode(RespCode::Okay),
/// Element::String("echo me!".to_owned())
/// ]
/// );
/// }
/// ```
///
pub struct Pipeline {
len: usize,
chain: Vec<u8>,
}
impl Default for Pipeline {
fn default() -> Self {
Self::new()
}
}
impl Pipeline {
/// Initializes a new empty pipeline
pub fn new() -> Self {
Self {
len: 0usize,
chain: Vec::new(),
}
}
/// Append a query (builder pattern)
pub fn append(mut self, query: Query) -> Self {
self.len += 1;
query.write_query_to_writable(&mut self.chain);
self
}
/// Append a query by taking reference
pub fn push(&mut self, query: Query) {
self.len += 1;
query.write_query_to_writable(&mut self.chain);
}
/// Returns the number of queries in the pipeline
pub fn len(&self) -> usize {
self.len
}
/// Checks if the pipeline is empty or not
pub fn is_empty(&self) -> bool {
self.len == 0
}
cfg_dbg! {
/// Returns the query packet representation of this pipeline
///
/// ## Panics
///
/// This function will panic if the query is empty
pub fn into_raw_query(self) -> Vec<u8> {
if self.len == 0 {
panic!("The pipeline is empty")
} else {
let mut v = Vec::with_capacity(self.chain.len() + 4);
v.push(b'*');
v.extend(self.len.to_string().as_bytes());
v.push(b'\n');
v.extend(self.chain);
v
}
}
}
}
cfg_dbg! {
#[test]
fn test_pipeline_dbg() {
let bytes = b"*2\n~1\n5\nhello\n~1\n5\nworld\n";
let pipe = Pipeline::new().append(query!("hello")).append(query!("world"));
assert_eq!(pipe.into_raw_query(), bytes);
}
}