Add forwarding gather() function to IOBufQueue
authorDaniel Sommermann <dcsommer@whatsapp.com>
Fri, 9 Sep 2016 20:50:08 +0000 (13:50 -0700)
committerFacebook Github Bot 2 <facebook-github-bot-2-bot@fb.com>
Fri, 9 Sep 2016 20:53:28 +0000 (13:53 -0700)
Summary:
I'm working with a parser that requires a certain number of
contiguous bytes to be able to make forward progress. I'm also using
IOBufQueue to receive data from an AsyncReader::ReadCallback with
pre/postallocate. So, I need to call gather() to ensure that the queue's
front IOBuf has the right number of contiguous bytes available.

Reviewed By: djwatson

Differential Revision: D3838079

fbshipit-source-id: 9f1ec5c86895eb1b2b109f9f145ca42d2dbba4c6

folly/io/IOBufQueue.cpp
folly/io/IOBufQueue.h
folly/io/test/IOBufQueueTest.cpp

index 491e99a804ea20e49037ef35a0ad20830dff865d..6faf5c79b2b67895d0788413071eccd98cb757c8 100644 (file)
@@ -286,4 +286,10 @@ void IOBufQueue::appendToString(std::string& out) const {
   }
 }
 
+void IOBufQueue::gather(uint64_t maxLength) {
+  if (head_ != nullptr) {
+    head_->gather(maxLength);
+  }
+}
+
 } // folly
index d421f917a0df2378cd06b8310d4dad0a0776bc66..bbf28fe9f9a7fee2a600ca27d67c05a9cea4d66d 100644 (file)
@@ -265,6 +265,11 @@ class IOBufQueue {
    */
   void appendToString(std::string& out) const;
 
+  /**
+   * Calls IOBuf::gather() on the head of the queue, if it exists.
+   */
+  void gather(uint64_t maxLength);
+
   /** Movable */
   IOBufQueue(IOBufQueue&&) noexcept;
   IOBufQueue& operator=(IOBufQueue&&);
index 8ba3d7b47c85aca0c8b4aed192ad7d6fd1333478..26062185da1f8a3e71445e415f5583ee9817bf7e 100644 (file)
@@ -385,3 +385,19 @@ TEST(IOBufQueue, AppendToString) {
   queue.appendToString(s);
   EXPECT_EQ("hello world", s);
 }
+
+TEST(IOBufQueue, Gather) {
+  IOBufQueue queue;
+
+  queue.append(stringToIOBuf(SCL("hello ")));
+  queue.append(stringToIOBuf(SCL("world")));
+
+  EXPECT_EQ(queue.front()->length(), 6);
+  queue.gather(11);
+  EXPECT_EQ(queue.front()->length(), 11);
+
+  StringPiece s(
+      reinterpret_cast<const char*>(queue.front()->data()),
+      queue.front()->length());
+  EXPECT_EQ("hello world", s);
+}