-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from suharev7/feature/streaming
streaming (for #32)
- Loading branch information
Showing
12 changed files
with
185 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
pub(crate) use self::{io_future::{BoxFuture, BoxStream}, transport::ClickhouseTransport}; | ||
pub(crate) use self::{ | ||
io_future::{BoxFuture, BoxStream}, | ||
transport::ClickhouseTransport, | ||
}; | ||
|
||
mod io_future; | ||
mod transport; | ||
pub(crate) mod transport; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,3 @@ where | |
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use futures::{Async, Poll, Stream}; | ||
|
||
use crate::{ | ||
errors::{DriverError, Error}, | ||
io::transport::PacketStream, | ||
pool::PoolBinding, | ||
types::{Block, Context, Packet}, | ||
ClientHandle, | ||
}; | ||
|
||
pub(crate) struct BlockStream { | ||
inner: PacketStream, | ||
rest: Option<(Context, PoolBinding)>, | ||
eof: bool, | ||
block_index: usize, | ||
} | ||
|
||
impl BlockStream { | ||
pub(crate) fn new(inner: PacketStream, context: Context, pool: PoolBinding) -> BlockStream { | ||
BlockStream { | ||
inner, | ||
rest: Some((context, pool)), | ||
eof: false, | ||
block_index: 0, | ||
} | ||
} | ||
} | ||
|
||
impl Stream for BlockStream { | ||
type Item = Block; | ||
type Error = Error; | ||
|
||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> { | ||
loop { | ||
if self.eof { | ||
return Ok(Async::Ready(None)); | ||
} | ||
|
||
let packet = match self.inner.poll() { | ||
Err(err) => return Err(err), | ||
Ok(Async::NotReady) => return Ok(Async::NotReady), | ||
Ok(Async::Ready(None)) => { | ||
self.eof = true; | ||
continue; | ||
} | ||
Ok(Async::Ready(Some(packet))) => packet, | ||
}; | ||
|
||
match packet { | ||
Packet::Eof(inner) => { | ||
let (context, pool) = self.rest.take().unwrap(); | ||
let mut client = ClientHandle { | ||
inner: Some(inner), | ||
context, | ||
pool, | ||
}; | ||
if !client.pool.is_attached() { | ||
client.pool.attach(); | ||
} | ||
self.eof = true; | ||
} | ||
Packet::ProfileInfo(_) | Packet::Progress(_) => {} | ||
Packet::Exception(exception) => return Err(Error::Server(exception)), | ||
Packet::Block(block) => { | ||
self.block_index += 1; | ||
if self.block_index > 1 && !block.is_empty() { | ||
return Ok(Async::Ready(Some(block))); | ||
} | ||
} | ||
_ => return Err(Error::Driver(DriverError::UnexpectedPacket)), | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters