在序列化Map之前,按照key进行排序,构建数组,arr[i]记录应该在这个位置上的item在offsets上的偏移.
auto cmp = [](const StringRef& lhs, const StringRef& rhs){
return (strcmp(lhs.data, rhs.data) < 0);
};
std::vector<unsigned long> sorted_row_indexes(next_offset-offset, 0);
std::map<StringRef, size_t, decltype(cmp)> _map(cmp);
auto resort_columns_index_by_key = [&](){
for (size_t i = offset; i < next_offset; i++){
_map.insert(std::make_pair(nested_tuple.getColumn(0).getDataAt(i), i));
}
size_t idx = 0;
for (auto const & it: _map){
sorted_row_indexes[idx++] = it.second;
}
_map.clear();
};
resort_columns_index_by_key();
// --------------------------- 上面代码是增加逻辑
writeChar('{', ostr);
for (size_t i = offset; i < next_offset; ++i)
{
if (i != offset)
writeChar(',', ostr);
//key_writer(ostr, key, nested_tuple.getColumn(0), i);
key_writer(ostr, key, nested_tuple.getColumn(0), sorted_row_indexes[i-offset]); // sorted_row_indexes: 是修正后的offset
writeChar(':', ostr);
value_writer(ostr, value, nested_tuple.getColumn(1), sorted_row_indexes[i-offset]);
}
writeChar('}', ostr);