|
| 1 | +use crate::client::InnerClient; |
| 2 | +use crate::codec::FrontendMessage; |
| 3 | +use crate::connection::RequestMessages; |
| 4 | +use crate::Error; |
| 5 | +use bytes::{Buf, BufMut, BytesMut, IntoBuf}; |
| 6 | +use futures::channel::mpsc; |
| 7 | +use futures::ready; |
| 8 | +use futures::{SinkExt, Stream, StreamExt, TryStream, TryStreamExt}; |
| 9 | +use pin_utils::pin_mut; |
| 10 | +use postgres_protocol::message::backend::Message; |
| 11 | +use postgres_protocol::message::frontend; |
| 12 | +use std::error; |
| 13 | +use std::pin::Pin; |
| 14 | +use std::sync::Arc; |
| 15 | +use std::task::{Context, Poll}; |
| 16 | +use postgres_protocol::message::frontend::CopyData; |
| 17 | + |
| 18 | +enum CopyInMessage { |
| 19 | + Message(FrontendMessage), |
| 20 | + Done, |
| 21 | +} |
| 22 | + |
| 23 | +pub struct CopyInReceiver { |
| 24 | + receiver: mpsc::Receiver<CopyInMessage>, |
| 25 | + done: bool, |
| 26 | +} |
| 27 | + |
| 28 | +impl CopyInReceiver { |
| 29 | + fn new(receiver: mpsc::Receiver<CopyInMessage>) -> CopyInReceiver { |
| 30 | + CopyInReceiver { |
| 31 | + receiver, |
| 32 | + done: false, |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl Stream for CopyInReceiver { |
| 38 | + type Item = FrontendMessage; |
| 39 | + |
| 40 | + fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<FrontendMessage>> { |
| 41 | + if self.done { |
| 42 | + return Poll::Ready(None); |
| 43 | + } |
| 44 | + |
| 45 | + match ready!(self.receiver.poll_next_unpin(cx)) { |
| 46 | + Some(CopyInMessage::Message(message)) => Poll::Ready(Some(message)), |
| 47 | + Some(CopyInMessage::Done) => { |
| 48 | + self.done = true; |
| 49 | + let mut buf = vec![]; |
| 50 | + frontend::copy_done(&mut buf); |
| 51 | + frontend::sync(&mut buf); |
| 52 | + Poll::Ready(Some(FrontendMessage::Raw(buf))) |
| 53 | + } |
| 54 | + None => { |
| 55 | + self.done = true; |
| 56 | + let mut buf = vec![]; |
| 57 | + frontend::copy_fail("", &mut buf).unwrap(); |
| 58 | + frontend::sync(&mut buf); |
| 59 | + Poll::Ready(Some(FrontendMessage::Raw(buf))) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +pub async fn copy_in<S>( |
| 66 | + client: Arc<InnerClient>, |
| 67 | + buf: Result<Vec<u8>, Error>, |
| 68 | + stream: S, |
| 69 | +) -> Result<u64, Error> |
| 70 | +where |
| 71 | + S: TryStream, |
| 72 | + S::Ok: IntoBuf, |
| 73 | + <S::Ok as IntoBuf>::Buf: 'static + Send, |
| 74 | + S::Error: Into<Box<dyn error::Error + Sync + Send>>, |
| 75 | +{ |
| 76 | + let buf = buf?; |
| 77 | + |
| 78 | + let (mut sender, receiver) = mpsc::channel(1); |
| 79 | + let receiver = CopyInReceiver::new(receiver); |
| 80 | + let mut responses = client.send(RequestMessages::CopyIn(receiver))?; |
| 81 | + |
| 82 | + sender |
| 83 | + .send(CopyInMessage::Message(FrontendMessage::Raw(buf))) |
| 84 | + .await |
| 85 | + .map_err(|_| Error::closed())?; |
| 86 | + |
| 87 | + match responses.next().await? { |
| 88 | + Message::BindComplete => {} |
| 89 | + _ => return Err(Error::unexpected_message()), |
| 90 | + } |
| 91 | + |
| 92 | + match responses.next().await? { |
| 93 | + Message::CopyInResponse(_) => {} |
| 94 | + _ => return Err(Error::unexpected_message()), |
| 95 | + } |
| 96 | + |
| 97 | + let mut bytes = BytesMut::new(); |
| 98 | + let stream = stream.into_stream(); |
| 99 | + pin_mut!(stream); |
| 100 | + |
| 101 | + while let Some(buf) = stream.try_next().await.map_err(Error::copy_in_stream)? { |
| 102 | + let buf = buf.into_buf(); |
| 103 | + |
| 104 | + let data: Box<dyn Buf + Send> = if buf.remaining() > 4096 { |
| 105 | + if bytes.is_empty() { |
| 106 | + Box::new(buf) |
| 107 | + } else { |
| 108 | + Box::new(bytes.take().freeze().into_buf().chain(buf)) |
| 109 | + } |
| 110 | + } else { |
| 111 | + bytes.reserve(buf.remaining()); |
| 112 | + bytes.put(buf); |
| 113 | + if bytes.len() > 4096 { |
| 114 | + Box::new(bytes.take().freeze().into_buf()) |
| 115 | + } else { |
| 116 | + continue; |
| 117 | + } |
| 118 | + }; |
| 119 | + |
| 120 | + let data = CopyData::new(data).map_err(Error::encode)?; |
| 121 | + sender |
| 122 | + .send(CopyInMessage::Message(FrontendMessage::CopyData(data))) |
| 123 | + .await |
| 124 | + .map_err(|_| Error::closed())?; |
| 125 | + } |
| 126 | + |
| 127 | + if !bytes.is_empty() { |
| 128 | + let data: Box<dyn Buf + Send> = Box::new(bytes.freeze().into_buf()); |
| 129 | + let data = CopyData::new(data).map_err(Error::encode)?; |
| 130 | + sender |
| 131 | + .send(CopyInMessage::Message(FrontendMessage::CopyData(data))) |
| 132 | + .await |
| 133 | + .map_err(|_| Error::closed())?; |
| 134 | + } |
| 135 | + |
| 136 | + sender |
| 137 | + .send(CopyInMessage::Done) |
| 138 | + .await |
| 139 | + .map_err(|_| Error::closed())?; |
| 140 | + |
| 141 | + match responses.next().await? { |
| 142 | + Message::CommandComplete(body) => { |
| 143 | + let rows = body |
| 144 | + .tag() |
| 145 | + .map_err(Error::parse)? |
| 146 | + .rsplit(' ') |
| 147 | + .next() |
| 148 | + .unwrap() |
| 149 | + .parse() |
| 150 | + .unwrap_or(0); |
| 151 | + Ok(rows) |
| 152 | + } |
| 153 | + _ => Err(Error::unexpected_message()), |
| 154 | + } |
| 155 | +} |
0 commit comments