From 23448b0322604e9584e75e1420599cb8984a8182 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 27 Jun 2019 15:53:55 +0200 Subject: [PATCH] Make RedisWriter#ChunkObjects()'s result type more explicit --- lib/redis/rediswriter-objects.cpp | 35 +++++++++++++++---------------- lib/redis/rediswriter.hpp | 2 +- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/lib/redis/rediswriter-objects.cpp b/lib/redis/rediswriter-objects.cpp index a8ce587d8..27d237940 100644 --- a/lib/redis/rediswriter-objects.cpp +++ b/lib/redis/rediswriter-objects.cpp @@ -43,6 +43,7 @@ #include "base/convert.hpp" #include "base/array.hpp" #include "base/exception.hpp" +#include #include #include #include @@ -127,13 +128,12 @@ void RedisWriter::UpdateAllConfigObjects() keys.insert(keys.end(), globalKeys.begin(), globalKeys.end()); - std::vector objectChunks = ChunkObjects(type.first->GetObjects(), 500); + auto objectChunks (ChunkObjects(type.first->GetObjects(), 500)); WorkQueue upqObjectType(25000, Configuration::Concurrency); upqObjectType.SetName("RedisWriter:ConfigDump:" + lcType); - upqObjectType.ParallelFor(objectChunks, [this, &type, &lcType, &keys](const Array::Ptr chunk) { - ObjectLock chunkLock(chunk); + upqObjectType.ParallelFor(objectChunks, [this, &type, &lcType, &keys](decltype(objectChunks)::const_reference chunk) { std::map > statements = GenerateHmsetStatements(keys); std::vector states = {"HMSET", m_PrefixStateObject + lcType}; std::vector > transaction = {{"MULTI"}}; @@ -222,25 +222,24 @@ void RedisWriter::UpdateAllConfigObjects() << "Initial config/status dump finished in " << Utility::GetTime() - startTime << " seconds."; } -std::vector RedisWriter::ChunkObjects(std::vector > objects, size_t chunkSize) { - std::vector chunks; - Array::Ptr currentChunk(new Array); - size_t currentChunkSize = 0; - for (auto object : objects) { - currentChunk->Add(object); - currentChunkSize++; - if (currentChunkSize >= chunkSize) { - chunks.push_back(currentChunk); - currentChunk = new Array(); - currentChunkSize = 0; - } +std::vector>> RedisWriter::ChunkObjects(std::vector> objects, size_t chunkSize) { + std::vector>> chunks; + auto offset (objects.begin()); + auto end (objects.end()); + + chunks.reserve((std::distance(offset, end) + chunkSize - 1) / chunkSize); + + while (std::distance(offset, end) >= chunkSize) { + auto until (offset + chunkSize); + chunks.emplace_back(offset, until); + offset = until; } - if (currentChunkSize > 0) { - chunks.push_back(currentChunk); + if (offset != end) { + chunks.emplace_back(offset, end); } - return chunks; + return std::move(chunks); } void RedisWriter::DeleteKeys(const std::vector& keys) { diff --git a/lib/redis/rediswriter.hpp b/lib/redis/rediswriter.hpp index 56ed9ce33..26d7fb647 100644 --- a/lib/redis/rediswriter.hpp +++ b/lib/redis/rediswriter.hpp @@ -70,7 +70,7 @@ private: /* config & status dump */ void UpdateAllConfigObjects(); - std::vector ChunkObjects(std::vector > objects, size_t chunkSize); + std::vector>> ChunkObjects(std::vector> objects, size_t chunkSize); void DeleteKeys(const std::vector& keys); std::map > GenerateHmsetStatements(const std::vector keys); std::vector GetTypeObjectKeys(const String& type);