MCPcopy Create free account
hub / github.com/ClickHouse/ClickHouse / convertHistoryFile

Function convertHistoryFile

src/Client/ReplxxLineReader.cpp:233–286  ·  view source on GitHub ↗

Convert from readline to replxx format. replxx requires each history line to prepended with time line: ### YYYY-MM-DD HH:MM:SS.SSS select 1 And w/o those service lines it will load all lines from history file as one history line for suggestion. And if there are lots of lines in file it will take lots of time (getline() + tons of reallocations). NOTE: this code uses std::ifstream/std::ofstream

Source from the content-addressed store, hash-verified

231///
232/// NOTE: this code uses std::ifstream/std::ofstream like original replxx code.
233void convertHistoryFile(const std::string & path, replxx::Replxx & rx)
234{
235 std::ifstream in(path);
236 if (!in)
237 {
238 rx.print("Cannot open %s reading (for conversion): %s\n",
239 path.c_str(), errnoToString().c_str());
240 return;
241 }
242
243 std::string line;
244 if (getline(in, line).bad())
245 {
246 rx.print("Cannot read from %s (for conversion): %s\n",
247 path.c_str(), errnoToString().c_str());
248 return;
249 }
250
251 /// This is the marker of the date, no need to convert.
252 static char const REPLXX_TIMESTAMP_PATTERN[] = "### dddd-dd-dd dd:dd:dd.ddd";
253 if (line.empty() || (line.starts_with("### ") && line.size() == strlen(REPLXX_TIMESTAMP_PATTERN)))
254 {
255 return;
256 }
257
258 std::vector<std::string> lines;
259 in.seekg(0);
260 while (getline(in, line).good())
261 {
262 lines.push_back(line);
263 }
264 in.close();
265
266 size_t lines_size = lines.size();
267 std::sort(lines.begin(), lines.end());
268 lines.erase(std::unique(lines.begin(), lines.end()), lines.end());
269 rx.print("The history file (%s) is in old format. %zu lines, %zu unique lines.\n",
270 path.c_str(), lines_size, lines.size());
271
272 std::ofstream out(path);
273 if (!out)
274 {
275 rx.print("Cannot open %s for writing (for conversion): %s\n",
276 path.c_str(), errnoToString().c_str());
277 return;
278 }
279
280 const std::string & timestamp = replxx_now_ms_str();
281 for (const auto & out_line : lines)
282 {
283 out << "### " << timestamp << "\n" << out_line << std::endl;
284 }
285 out.close();
286}
287
288}
289

Callers 1

ReplxxLineReaderMethod · 0.85

Calls 14

errnoToStringFunction · 0.85
uniqueFunction · 0.85
replxx_now_ms_strFunction · 0.85
sortFunction · 0.50
printMethod · 0.45
badMethod · 0.45
emptyMethod · 0.45
sizeMethod · 0.45
goodMethod · 0.45
push_backMethod · 0.45
closeMethod · 0.45
beginMethod · 0.45

Tested by

no test coverage detected