Skip to content
Browse files

allow to dequeue the first IOBuf in an IOBufQueue

Summary: allow to dequeue the first IOBuf in an IOBufQueue

Test Plan: throughly tested with some dependent code in proxygen

Reviewed By: tudorb@fb.com

FB internal diff: D732484
  • Loading branch information...
1 parent 81af5c4 commit 33a4bac623aab0fa4ea5e980fbb08b3766c40c98 Alessandro Salvatori committed with Owen Yamauchi Mar 8, 2013
Showing with 58 additions and 0 deletions.
  1. +12 −0 folly/io/IOBufQueue.cpp
  2. +7 −0 folly/io/IOBufQueue.h
  3. +39 −0 folly/io/test/IOBufQueueTest.cpp
View
12 folly/io/IOBufQueue.cpp
@@ -285,4 +285,16 @@ void IOBufQueue::trimEnd(size_t amount) {
}
}
+std::unique_ptr<folly::IOBuf> IOBufQueue::pop_front() {
+ if (!head_) {
+ return nullptr;
+ }
+ if (options_.cacheChainLength) {
+ chainLength_ -= head_->length();
+ }
+ std::unique_ptr<folly::IOBuf> retBuf = std::move(head_);
+ head_ = retBuf->pop();
+ return retBuf;
+}
+
} // folly
View
7 folly/io/IOBufQueue.h
@@ -205,6 +205,13 @@ class IOBufQueue {
}
/**
+ * returns the first IOBuf in the chain and removes it from the chain
+ *
+ * @return first IOBuf in the chain or nullptr if none.
+ */
+ std::unique_ptr<folly::IOBuf> pop_front();
+
+ /**
* Total chain length, only valid if cacheLength was specified in the
* constructor.
*/
View
39 folly/io/test/IOBufQueueTest.cpp
@@ -324,6 +324,45 @@ TEST(IOBufQueue, Prepend) {
out->length()));
}
+TEST(IOBufQueue, PopFirst) {
+ IOBufQueue queue(IOBufQueue::cacheChainLength());
+ const char * strings[] = {
+ "Hello",
+ ",",
+ " ",
+ "",
+ "World"
+ };
+
+ const size_t numStrings=sizeof(strings)/sizeof(*strings);
+ size_t chainLength = 0;
+ for(ssize_t i=0; i<numStrings; ++i) {
+ queue.append(stringToIOBuf(strings[i], strlen(strings[i])));
+ checkConsistency(queue);
+ chainLength += strlen(strings[i]);
+ }
+
+ unique_ptr<IOBuf> first;
+ for(ssize_t i=0; i<numStrings; ++i) {
+ checkConsistency(queue);
+ EXPECT_EQ(chainLength, queue.front()->computeChainDataLength());
+ EXPECT_EQ(chainLength, queue.chainLength());
+ first = queue.pop_front();
+ chainLength-=strlen(strings[i]);
+ EXPECT_EQ(strlen(strings[i]), first->computeChainDataLength());
+ }
+ checkConsistency(queue);
+ EXPECT_EQ(chainLength, queue.chainLength());
+
+ EXPECT_EQ((IOBuf*)NULL, queue.front());
+ first = queue.pop_front();
+ EXPECT_EQ((IOBuf*)NULL, first.get());
+
+ checkConsistency(queue);
+ EXPECT_EQ((IOBuf*)NULL, queue.front());
+ EXPECT_EQ(0, queue.chainLength());
+}
+
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
google::ParseCommandLineFlags(&argc, &argv, true);

0 comments on commit 33a4bac

Please sign in to comment.
Something went wrong with that request. Please try again.