TEST_F(DataQueueTest, QueueTest) { const int numberOfSNPs = 3; std::vector<SNP*>* snpQueue = new std::vector<SNP*>(numberOfSNPs); std::vector<SNP*> snpStore(numberOfSNPs); for(int i = 0; i < numberOfSNPs; ++i){ std::ostringstream os; os << "snp" << i; Id id(os.str()); SNP* snp = new SNP(id, "allele1", "allele2", 1); (*snpQueue)[i] = snp; snpStore[i] = snp; } Task::DataQueue dataQueue(snpQueue); for(int i = numberOfSNPs - 1; i >= 0; --i){ SNP* snp = dataQueue.next(); ASSERT_TRUE(snp != nullptr); EXPECT_EQ(*(snpStore[i]), *snp); delete snp; } SNP* snp = dataQueue.next(); ASSERT_TRUE(snp == nullptr); }
void encode(const LuaObject& input, folly::io::CodecType codecType, LuaVersionInfo versionInfo, Writer&& writer, int maxVersion, uint64_t chunkLength) { folly::IOBufQueue dataQueue(folly::IOBufQueue::cacheChainLength()); apache::thrift::CompactSerializer::serialize(input, &dataQueue); auto codec = folly::io::getCodec(codecType); // Determine minimum version required for reading bool needChunking = false; uint64_t codecMaxLength = codec->maxUncompressedLength(); if (codecMaxLength < chunkLength) { chunkLength = codecMaxLength; } int version = 0; if (dataQueue.chainLength() > chunkLength) { needChunking = true; // Version 2: chunking version = 2; } else { // Version 1: specials, metatables for (auto& ref : input.refs) { if (ref.__isset.tableVal) { auto& table = ref.tableVal; if (table.__isset.specialKey || table.__isset.specialValue || table.__isset.metatable) { version = 1; break; } } } } DCHECK_LE(version, kMaxSupportedVersion); if (version > maxVersion) { throw std::invalid_argument(folly::to<std::string>( "Version ", version, " required (requested ", maxVersion, ")")); } ThriftHeader th; th.version = version; th.codec = static_cast<int32_t>(codecType); th.uncompressedLength = dataQueue.chainLength(); th.luaVersionInfo = std::move(versionInfo); auto uncompressed = dataQueue.move(); std::unique_ptr<folly::IOBuf> compressed; if (needChunking) { th.__isset.chunks = true; compressed = compressChunked( codec.get(), uncompressed.get(), chunkLength, th.chunks); } else { compressed = codec->compress(uncompressed.get()); } th.compressedLength = compressed->computeChainDataLength(); folly::IOBufQueue queue(folly::IOBufQueue::cacheChainLength()); apache::thrift::CompactSerializer::serialize(th, &queue); Header header; header.magic = folly::Endian::little(kMagic); header.thriftHeaderLength = folly::Endian::little(queue.chainLength()); writer(folly::IOBuf::copyBuffer(&header, sizeof(header))); writer(queue.move()); writer(std::move(compressed)); }