dbms: fixed error; improvement [#CONV-2944].

This commit is contained in:
Alexey Milovidov 2013-06-15 06:14:41 +00:00
parent 72b18df1ff
commit 3b1cb6ef77
2 changed files with 16 additions and 23 deletions

View File

@ -195,8 +195,6 @@ namespace ErrorCodes
COLLATION_COMPARISON_FAILED, COLLATION_COMPARISON_FAILED,
UNKNOWN_ACTION, UNKNOWN_ACTION,
MULTIPLE_ARRAY_JOIN, MULTIPLE_ARRAY_JOIN,
CANNOT_GETCWD,
CANNOT_CHDIR,
POCO_EXCEPTION = 1000, POCO_EXCEPTION = 1000,
STD_EXCEPTION, STD_EXCEPTION,

View File

@ -1,5 +1,3 @@
#include <unistd.h>
#include <Poco/DirectoryIterator.h> #include <Poco/DirectoryIterator.h>
#include <Poco/FileStream.h> #include <Poco/FileStream.h>
@ -13,6 +11,8 @@
#include <DB/IO/WriteBufferFromString.h> #include <DB/IO/WriteBufferFromString.h>
#include <DB/IO/copyData.h> #include <DB/IO/copyData.h>
#include <statdaemons/Stopwatch.h>
namespace DB namespace DB
{ {
@ -54,14 +54,6 @@ void loadMetadata(Context & context)
/// Здесь хранятся определения таблиц /// Здесь хранятся определения таблиц
String path = context.getPath() + "metadata"; String path = context.getPath() + "metadata";
/** Файлы открываются быстрее, если открывать их по относительному пути, находясь в директории, содержащей их.
* Поэтому, запомним текущую рабочую директорию. Затем будем менять её для каждой БД.
* В конце, поменяем обратно.
*/
std::vector<char> current_working_directory(PATH_MAX);
if (NULL == getcwd(&current_working_directory[0], current_working_directory.size()))
throwFromErrno("Cannot getcwd", ErrorCodes::CANNOT_GETCWD);
/// Цикл по базам данных /// Цикл по базам данных
Poco::DirectoryIterator dir_end; Poco::DirectoryIterator dir_end;
for (Poco::DirectoryIterator it(path); it != dir_end; ++it) for (Poco::DirectoryIterator it(path); it != dir_end; ++it)
@ -77,9 +69,6 @@ void loadMetadata(Context & context)
executeCreateQuery("ATTACH DATABASE " + it.name(), context, it.name(), it->path()); executeCreateQuery("ATTACH DATABASE " + it.name(), context, it.name(), it->path());
if (0 != chdir(it->path().c_str()))
throwFromErrno("Cannot chdir to " + it->path(), ErrorCodes::CANNOT_CHDIR);
/// Цикл по таблицам /// Цикл по таблицам
typedef std::vector<std::string> Tables; typedef std::vector<std::string> Tables;
Tables tables; Tables tables;
@ -93,7 +82,7 @@ void loadMetadata(Context & context)
if (jt.name().compare(jt.name().size() - 4, 4, ".sql")) if (jt.name().compare(jt.name().size() - 4, 4, ".sql"))
throw Exception("Incorrect file extension: " + jt.name() + " in metadata directory " + it->path(), ErrorCodes::INCORRECT_FILE_NAME); throw Exception("Incorrect file extension: " + jt.name() + " in metadata directory " + it->path(), ErrorCodes::INCORRECT_FILE_NAME);
tables.push_back(jt.name()); tables.push_back(jt->path());
} }
LOG_INFO(&Logger::get("loadMetadata"), "Found " << tables.size() << " tables."); LOG_INFO(&Logger::get("loadMetadata"), "Found " << tables.size() << " tables.");
@ -104,31 +93,37 @@ void loadMetadata(Context & context)
*/ */
std::sort(tables.begin(), tables.end()); std::sort(tables.begin(), tables.end());
for (Tables::const_iterator jt = tables.begin(); jt != tables.end(); ++jt) Stopwatch watch;
for (size_t j = 0, size = tables.size(); j < size; ++j)
{ {
/// Сообщения, чтобы было не скучно ждать, когда сервер долго загружается.
if (j % 256 == 0 && watch.elapsedSeconds() > 5)
{
LOG_INFO(&Logger::get("loadMetadata"), std::fixed << std::setprecision(2) << j * 100.0 / size << "%");
watch.restart();
}
String s; String s;
{ {
static const size_t in_buf_size = 32768; static const size_t in_buf_size = 32768;
char in_buf[in_buf_size]; char in_buf[in_buf_size];
ReadBufferFromFile in(*jt, 32768, in_buf); ReadBufferFromFile in(tables[j], 32768, in_buf);
WriteBufferFromString out(s); WriteBufferFromString out(s);
copyData(in, out); copyData(in, out);
} }
try try
{ {
executeCreateQuery(s, context, it.name(), *jt); executeCreateQuery(s, context, it.name(), tables[j]);
} }
catch (const DB::Exception & e) catch (const DB::Exception & e)
{ {
throw Exception("Cannot create table from metadata file " + *jt + ", error: " + e.displayText(), throw Exception("Cannot create table from metadata file " + tables[j] + ", error: " + e.displayText(),
ErrorCodes::CANNOT_CREATE_TABLE_FROM_METADATA); ErrorCodes::CANNOT_CREATE_TABLE_FROM_METADATA);
} }
} }
} }
if (0 != chdir(&current_working_directory[0]))
throwFromErrno("Cannot chdir to " + std::string(&current_working_directory[0]), ErrorCodes::CANNOT_CHDIR);
} }