(db/snapshot): fix DeleteEntity bug (#3165)

Signed-off-by: peng.xu <peng.xu@zilliz.com>
pull/3158/head^2
XuPeng-SH 2020-08-07 09:37:26 +08:00 committed by GitHub
parent ce1e528d1c
commit 596808a376
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 5 deletions

View File

@ -143,6 +143,9 @@ Segment::AddChunk(const DataChunkPtr& chunk_ptr, int64_t from, int64_t to) {
Status
Segment::DeleteEntity(std::vector<offset_t>& offsets) {
if (offsets.size() == 0) {
return Status::OK();
}
// sort offset in descendant
std::sort(offsets.begin(), offsets.end(), std::greater<offset_t>());
@ -153,13 +156,23 @@ Segment::DeleteEntity(std::vector<offset_t>& offsets) {
continue;
}
BinaryDataPtr& data = pair.second;
for (auto offset : offsets) {
if (offset >= 0 && offset < row_count_) {
auto step = offset * width;
data->data_.erase(data->data_.begin() + step, data->data_.begin() + step + width);
auto& data = pair.second->data_;
std::vector<bool> marked(data.size(), false);
std::vector<uint8_t> temp;
temp.reserve(data.size() - offsets.size() * width);
for (auto pos = offsets.begin(); pos != offsets.end(); pos++) {
for (auto i = 0; i < width; ++i) {
marked[(*pos) * width + i] = true;
}
}
for (size_t i = 0; i < data.size(); i++) {
if (!marked[i]) {
temp.push_back(data[i]);
}
}
data = std::move(temp);
}
// reset row count