| 1 | #include <Storages/MergeTree/MergeTreeDataPartTTLInfo.h> |
| 2 | #include <IO/ReadHelpers.h> |
| 3 | #include <IO/WriteHelpers.h> |
| 4 | #include <Common/quoteString.h> |
| 5 | |
| 6 | #include <common/JSON.h> |
| 7 | |
| 8 | namespace DB |
| 9 | { |
| 10 | |
| 11 | void MergeTreeDataPartTTLInfos::update(const MergeTreeDataPartTTLInfos & other_infos) |
| 12 | { |
| 13 | for (const auto & [name, ttl_info] : other_infos.columns_ttl) |
| 14 | { |
| 15 | columns_ttl[name].update(ttl_info); |
| 16 | updatePartMinMaxTTL(ttl_info.min, ttl_info.max); |
| 17 | } |
| 18 | |
| 19 | for (const auto & [expression, ttl_info] : other_infos.moves_ttl) |
| 20 | { |
| 21 | moves_ttl[expression].update(ttl_info); |
| 22 | } |
| 23 | |
| 24 | table_ttl.update(other_infos.table_ttl); |
| 25 | updatePartMinMaxTTL(table_ttl.min, table_ttl.max); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | void MergeTreeDataPartTTLInfos::read(ReadBuffer & in) |
| 30 | { |
| 31 | String json_str; |
| 32 | readString(json_str, in); |
| 33 | assertEOF(in); |
| 34 | |
| 35 | JSON json(json_str); |
| 36 | if (json.has("columns" )) |
| 37 | { |
| 38 | const JSON & columns = json["columns" ]; |
| 39 | for (auto col : columns) |
| 40 | { |
| 41 | MergeTreeDataPartTTLInfo ttl_info; |
| 42 | ttl_info.min = col["min" ].getUInt(); |
| 43 | ttl_info.max = col["max" ].getUInt(); |
| 44 | String name = col["name" ].getString(); |
| 45 | columns_ttl.emplace(name, ttl_info); |
| 46 | |
| 47 | updatePartMinMaxTTL(ttl_info.min, ttl_info.max); |
| 48 | } |
| 49 | } |
| 50 | if (json.has("table" )) |
| 51 | { |
| 52 | const JSON & table = json["table" ]; |
| 53 | table_ttl.min = table["min" ].getUInt(); |
| 54 | table_ttl.max = table["max" ].getUInt(); |
| 55 | |
| 56 | updatePartMinMaxTTL(table_ttl.min, table_ttl.max); |
| 57 | } |
| 58 | if (json.has("moves" )) |
| 59 | { |
| 60 | const JSON & moves = json["moves" ]; |
| 61 | for (auto move : moves) |
| 62 | { |
| 63 | MergeTreeDataPartTTLInfo ttl_info; |
| 64 | ttl_info.min = move["min" ].getUInt(); |
| 65 | ttl_info.max = move["max" ].getUInt(); |
| 66 | String expression = move["expression" ].getString(); |
| 67 | moves_ttl.emplace(expression, ttl_info); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
| 73 | void MergeTreeDataPartTTLInfos::write(WriteBuffer & out) const |
| 74 | { |
| 75 | writeString("ttl format version: 1\n" , out); |
| 76 | writeString("{" , out); |
| 77 | if (!columns_ttl.empty()) |
| 78 | { |
| 79 | writeString("\"columns\":[" , out); |
| 80 | for (auto it = columns_ttl.begin(); it != columns_ttl.end(); ++it) |
| 81 | { |
| 82 | if (it != columns_ttl.begin()) |
| 83 | writeString("," , out); |
| 84 | |
| 85 | writeString("{\"name\":" , out); |
| 86 | writeString(doubleQuoteString(it->first), out); |
| 87 | writeString(",\"min\":" , out); |
| 88 | writeIntText(it->second.min, out); |
| 89 | writeString(",\"max\":" , out); |
| 90 | writeIntText(it->second.max, out); |
| 91 | writeString("}" , out); |
| 92 | } |
| 93 | writeString("]" , out); |
| 94 | } |
| 95 | if (table_ttl.min) |
| 96 | { |
| 97 | if (!columns_ttl.empty()) |
| 98 | writeString("," , out); |
| 99 | writeString("\"table\":{\"min\":" , out); |
| 100 | writeIntText(table_ttl.min, out); |
| 101 | writeString(",\"max\":" , out); |
| 102 | writeIntText(table_ttl.max, out); |
| 103 | writeString("}" , out); |
| 104 | } |
| 105 | if (!moves_ttl.empty()) |
| 106 | { |
| 107 | if (!columns_ttl.empty() || table_ttl.min) |
| 108 | writeString("," , out); |
| 109 | writeString("\"moves\":[" , out); |
| 110 | for (auto it = moves_ttl.begin(); it != moves_ttl.end(); ++it) |
| 111 | { |
| 112 | if (it != moves_ttl.begin()) |
| 113 | writeString("," , out); |
| 114 | |
| 115 | writeString("{\"expression\":" , out); |
| 116 | writeString(doubleQuoteString(it->first), out); |
| 117 | writeString(",\"min\":" , out); |
| 118 | writeIntText(it->second.min, out); |
| 119 | writeString(",\"max\":" , out); |
| 120 | writeIntText(it->second.max, out); |
| 121 | writeString("}" , out); |
| 122 | } |
| 123 | writeString("]" , out); |
| 124 | } |
| 125 | writeString("}" , out); |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | |