Skip to content

Commit

Permalink
query: Add pipeline builder
Browse files Browse the repository at this point in the history
  • Loading branch information
ohsayan committed Apr 5, 2024
1 parent afa3a66 commit fc9dd17
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All changes in this project will be noted in this file.

### 0.9.0

> **Minimum Supported Skytable Version**: 0.8.2
- Added support for pipelines

### 0.8.6

Reduced allocations in `Query`.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 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 2 Protocol](https://docs.skytable.io/protocol/overview). This version of the library was tested with the latest Skytable release (release [0.8.0-beta](https://github.com/skytable/skytable/releases/v0.8.0-beta)).
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 2 Protocol](https://docs.skytable.io/protocol/overview). This version of the library was tested with the latest Skytable release (release [0.8.1](https://github.com/skytable/skytable/releases/v0.8.1)).

## Definitive example

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub use {
aio::{self, ConnectionAsync, ConnectionTlsAsync},
sync::{self as syncio, Connection, ConnectionTls},
},
query::Query,
query::{Pipeline, Query},
};
// private
mod io;
Expand Down
4 changes: 2 additions & 2 deletions src/protocol/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use {
crate::response::Response,
};

const PIPELINE_EXCEPTION: u8 = 0xFF;
const ILLEGAL_PACKET_ESCAPE: u8 = 0xFF;

#[derive(Debug, PartialEq, Default)]
pub(crate) struct MRespState {
Expand All @@ -47,7 +47,7 @@ impl MRespState {
if decoder._cursor_eof() {
return PipelineResult::Pending(self);
}
if decoder._cursor_value() == PIPELINE_EXCEPTION {
if decoder._cursor_value() == ILLEGAL_PACKET_ESCAPE {
return Self::except();
}
match decoder.validate_response(RState(
Expand Down
25 changes: 20 additions & 5 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl Pipeline {
///
/// Note: It's not possible to get the query back from the pipeline since it's not indexed (and doing so would be an unnecessary
/// waste of space and time). That's why we take a reference which allows the caller to continue owning the [`Query`] item
pub fn add_query(&mut self, q: &Query) {
pub fn push(&mut self, q: &Query) {
// qlen
self.buf
.extend(itoa::Buffer::new().format(q.q_window).as_bytes());
Expand All @@ -173,6 +173,22 @@ impl Pipeline {
self.buf.extend(&q.buf);
self.cnt += 1;
}
/// Add a query to this pipeline (builder pattern)
///
/// This is intended to be used with the
/// ["builder pattern"](https://rust-unofficial.github.io/patterns/patterns/creational/builder.html). For example:
/// ```
/// use skytable::{query, Pipeline};
///
/// let pipeline = Pipeline::new()
/// .add(&query!("create space myspace"))
/// .add(&query!("drop space myspace"));
/// assert_eq!(pipeline.query_count(), 2);
/// ```
pub fn add(mut self, q: &Query) -> Self {
self.push(q);
self
}
}

impl<Q: AsRef<Query>, I> From<I> for Pipeline
Expand All @@ -181,22 +197,21 @@ where
{
fn from(iter: I) -> Self {
let mut pipeline = Pipeline::new();
iter.into_iter()
.for_each(|q| pipeline.add_query(q.as_ref()));
iter.into_iter().for_each(|q| pipeline.push(q.as_ref()));
pipeline
}
}

impl<Q: AsRef<Query>> Extend<Q> for Pipeline {
fn extend<T: IntoIterator<Item = Q>>(&mut self, iter: T) {
iter.into_iter().for_each(|q| self.add_query(q.as_ref()))
iter.into_iter().for_each(|q| self.push(q.as_ref()))
}
}

impl<Q: AsRef<Query>> FromIterator<Q> for Pipeline {
fn from_iter<T: IntoIterator<Item = Q>>(iter: T) -> Self {
let mut pipe = Pipeline::new();
iter.into_iter().for_each(|q| pipe.add_query(q.as_ref()));
iter.into_iter().for_each(|q| pipe.push(q.as_ref()));
pipe
}
}
Expand Down

0 comments on commit fc9dd17

Please sign in to comment.