Skip to content

Commit f392d6c

Browse files
committed
Use buffered IO
Closes #17
1 parent 5654fa5 commit f392d6c

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

src/lib.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use extra::url::{UserInfo, Url};
1212
use std::cell::Cell;
1313
use std::hashmap::HashMap;
1414
use std::rt::io::{Writer, io_error, Decorator};
15-
use std::rt::io::mem::MemWriter;
15+
use std::rt::io::buffered::BufferedStream;
1616
use std::rt::io::net;
1717
use std::rt::io::net::ip;
1818
use std::rt::io::net::ip::SocketAddr;
@@ -131,15 +131,15 @@ impl PostgresDbError {
131131
}
132132

133133
pub struct PostgresConnection {
134-
priv stream: Cell<TcpStream>,
134+
priv stream: Cell<BufferedStream<TcpStream>>,
135135
priv next_stmt_id: Cell<int>,
136136
priv notice_handler: Cell<~PostgresNoticeHandler>
137137
}
138138

139139
impl Drop for PostgresConnection {
140140
fn drop(&self) {
141141
do io_error::cond.trap(|_| {}).inside {
142-
self.write_message(&Terminate);
142+
self.write_messages([&Terminate]);
143143
}
144144
}
145145
}
@@ -182,7 +182,7 @@ impl PostgresConnection {
182182
};
183183

184184
let conn = PostgresConnection {
185-
stream: Cell::new(stream),
185+
stream: Cell::new(BufferedStream::new(stream)),
186186
next_stmt_id: Cell::new(0),
187187
notice_handler: Cell::new(~DefaultNoticeHandler
188188
as ~PostgresNoticeHandler)
@@ -197,10 +197,10 @@ impl PostgresConnection {
197197
if !path.is_empty() {
198198
args.push((~"database", path));
199199
}
200-
conn.write_message(&StartupMessage {
200+
conn.write_messages([&StartupMessage {
201201
version: message::PROTOCOL_VERSION,
202202
parameters: args.as_slice()
203-
});
203+
}]);
204204

205205
match conn.handle_auth(user) {
206206
Some(err) => return Err(err),
@@ -242,18 +242,11 @@ impl PostgresConnection {
242242
}
243243

244244
fn write_messages(&self, messages: &[&FrontendMessage]) {
245-
let mut buf = MemWriter::new();
246-
for &message in messages.iter() {
247-
buf.write_message(message);
248-
}
249-
do self.stream.with_mut_ref |s| {
250-
s.write(buf.inner_ref().as_slice());
251-
}
252-
}
253-
254-
fn write_message(&self, message: &FrontendMessage) {
255245
do self.stream.with_mut_ref |s| {
256-
s.write_message(message);
246+
for &message in messages.iter() {
247+
s.write_message(message);
248+
}
249+
s.flush();
257250
}
258251
}
259252

@@ -284,7 +277,7 @@ impl PostgresConnection {
284277
Some(pass) => pass,
285278
None => return Some(MissingPassword)
286279
};
287-
self.write_message(&PasswordMessage { password: pass });
280+
self.write_messages([&PasswordMessage { password: pass }]);
288281
}
289282
AuthenticationMD5Password { salt } => {
290283
let UserInfo { user, pass } = user;
@@ -300,9 +293,9 @@ impl PostgresConnection {
300293
md5.input_str(output);
301294
md5.input(salt);
302295
let output = "md5" + md5.result_str();
303-
self.write_message(&PasswordMessage {
296+
self.write_messages([&PasswordMessage {
304297
password: output.as_slice()
305-
});
298+
}]);
306299
}
307300
AuthenticationKerberosV5
308301
| AuthenticationSCMCredential
@@ -422,13 +415,13 @@ impl PostgresConnection {
422415

423416
pub fn try_update(&self, query: &str, params: &[&ToSql])
424417
-> Result<uint, PostgresDbError> {
425-
do self.try_prepare(query).chain |stmt| {
418+
do self.try_prepare(query).and_then |stmt| {
426419
stmt.try_update(params)
427420
}
428421
}
429422

430423
fn quick_query(&self, query: &str) {
431-
self.write_message(&Query { query: query });
424+
self.write_messages([&Query { query: query }]);
432425

433426
loop {
434427
match self.read_message() {

0 commit comments

Comments
 (0)