diff --git a/src/Ql/Cql/CqlConnection.php b/src/Ql/Cql/CqlConnection.php index 5e8b0bb..3db83ac 100644 --- a/src/Ql/Cql/CqlConnection.php +++ b/src/Ql/Cql/CqlConnection.php @@ -139,7 +139,7 @@ public function connect() (int)$this->_config()->getItem('send_timeout', 1000) ); - $this->_transport = new TFramedTransport($this->_socket); + $this->_transport = new DalFramedTransport($this->_socket); $this->_protocol = new TBinaryProtocolAccelerated($this->_transport); $this->_client = new CassandraClient($this->_protocol); diff --git a/src/Ql/Cql/DalFramedTransport.php b/src/Ql/Cql/DalFramedTransport.php new file mode 100644 index 0000000..578eaad --- /dev/null +++ b/src/Ql/Cql/DalFramedTransport.php @@ -0,0 +1,78 @@ +_transport = $transport; + $this->_readFramed = $read; + } + + public function read($len) + { + if(!$this->_readFramed) + { + return $this->_transport->read($len); + } + + if($this->_offset >= $this->_length) + { + $this->_readFrame(); + if($this->_length === 0) + { + return ''; + } + } + + $out = substr($this->_buffer, $this->_offset, $len); + $this->_offset += strlen($out); + if($this->_offset >= $this->_length) + { + $this->_setBuffer(''); + } + return $out; + } + + public function putBack($data) + { + $remaining = $this->_offset < $this->_length ? substr($this->_buffer, $this->_offset) : ''; + $this->_setBuffer($data . $remaining); + } + + private function _readFrame() + { + $size = unpack('N', $this->_transport->readAll(4))[1]; + $this->_setBuffer($size > 0 ? $this->_transport->readAll($size) : ''); + } + + private function _setBuffer(string $buffer) + { + $this->_buffer = $buffer; + $this->_offset = 0; + $this->_length = strlen($buffer); + } +} diff --git a/tests/Ql/Cql/DalFramedTransportTest.php b/tests/Ql/Cql/DalFramedTransportTest.php new file mode 100644 index 0000000..6d5b5bf --- /dev/null +++ b/tests/Ql/Cql/DalFramedTransportTest.php @@ -0,0 +1,85 @@ +_frame('abcdef') . $this->_frame('ghij')) + ); + $transport->open(); + + $this->assertSame('abcd', $transport->read(4)); + $this->assertSame('ef', $transport->read(4)); + $this->assertSame('gh', $transport->readAll(2)); + $this->assertSame('ij', $transport->read(10)); + } + + public function testReadAllSpansFrames() + { + $transport = new DalFramedTransport( + new TMemoryBuffer($this->_frame('abc') . $this->_frame('def')) + ); + $this->assertSame('abcdef', $transport->readAll(6)); + } + + public function testPutBackIsReadFirst() + { + $transport = new DalFramedTransport(new TMemoryBuffer($this->_frame('abcdef'))); + + $this->assertSame('ab', $transport->read(2)); + $transport->putBack('XY'); + $this->assertSame('XYcd', $transport->read(4)); + $this->assertSame('ef', $transport->read(4)); + + $transport->putBack('Z'); + $this->assertSame('Z', $transport->read(4)); + } + + public function testUnframedReadPassesThrough() + { + $transport = new DalFramedTransport(new TMemoryBuffer('raw-bytes'), false, false); + $this->assertSame('raw', $transport->read(3)); + } + + public function testWritesAreFramed() + { + $buffer = new TMemoryBuffer(); + $transport = new DalFramedTransport($buffer); + $transport->write('hello'); + $transport->flush(); + $this->assertSame($this->_frame('hello'), $buffer->getBuffer()); + } + + /** + * The binary protocol reads one field at a time, so a large CQL result is + * consumed as millions of 1-8 byte reads from a single frame. + */ + public function testLargeFrameReadInSmallFieldsIsLinear() + { + $size = 2 * 1024 * 1024; + $payload = str_repeat('0123456789abcdef', $size / 16); + $transport = new DalFramedTransport(new TMemoryBuffer($this->_frame($payload))); + + $start = microtime(true); + $out = ''; + while(strlen($out) < $size) + { + $out .= $transport->read(4); + } + $elapsed = microtime(true) - $start; + + $this->assertSame($payload, $out); + $this->assertLessThan(2.0, $elapsed); + } +}