Commit 33a4bac6 authored by Alessandro Salvatori's avatar Alessandro Salvatori Committed by Owen Yamauchi

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
parent 81af5c4c
......@@ -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
......@@ -204,6 +204,13 @@ class IOBufQueue {
return head_.get();
}
/**
* 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.
......
......@@ -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);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment