Minor code cleanup

This commit is contained in:
stavrolia 2019-09-06 21:29:41 +03:00
parent 2ffc99b893
commit f533485d19
2 changed files with 13 additions and 17 deletions

View File

@ -93,22 +93,19 @@ std::vector<std::string> LSWithRegexpMatching(const std::string & path_for_ls, c
}
return result;
}
}
static std::string getTablePath(const std::string & db_dir_path, const std::string & table_name, const std::string & format_name)
std::string getTablePath(const std::string & db_dir_path, const std::string & table_name, const std::string & format_name)
{
return db_dir_path + escapeForFileName(table_name) + "/data." + escapeForFileName(format_name);
}
/// Both db_dir_path and table_path must be converted to absolute paths (in particular, path cannot contain '..').
static void checkCreationIsAllowed(Context & context_global, const std::string & db_dir_path, const std::string & table_path, int table_fd)
void checkCreationIsAllowed(Context & context_global, const std::string & db_dir_path, const std::string & table_path)
{
if (context_global.getApplicationType() != Context::ApplicationType::SERVER)
return;
if (table_fd >= 0)
throw Exception("Using file descriptor as source of storage isn't allowed for server daemons", ErrorCodes::DATABASE_ACCESS_DENIED);
else if (!startsWith(table_path, db_dir_path))
if (!startsWith(table_path, db_dir_path))
throw Exception("Part path " + table_path + " is not inside " + db_dir_path, ErrorCodes::DATABASE_ACCESS_DENIED);
Poco::File table_path_poco_file = Poco::File(table_path);
@ -117,7 +114,7 @@ static void checkCreationIsAllowed(Context & context_global, const std::string &
else if (table_path_poco_file.isDirectory())
throw Exception("File " + table_path + " must not be a directory", ErrorCodes::INCORRECT_FILE_NAME);
}
}
StorageFile::StorageFile(
const std::string & table_path_,
@ -148,7 +145,7 @@ StorageFile::StorageFile(
const std::string path = poco_path.absolute().toString();
paths = LSWithRegexpMatching("/", path);
for (const auto & cur_path : paths)
checkCreationIsAllowed(context_global, db_dir_path, cur_path, table_fd);
checkCreationIsAllowed(context_global, db_dir_path, cur_path);
is_db_table = false;
}
else /// Is DB's file
@ -163,11 +160,8 @@ StorageFile::StorageFile(
}
else /// Will use FD
{
if (paths.size() != 1)
throw Exception("Table '" + table_name + "' is in readonly mode", ErrorCodes::DATABASE_ACCESS_DENIED);
checkCreationIsAllowed(context_global, db_dir_path, paths[0], table_fd);
if (context_global.getApplicationType() == Context::ApplicationType::SERVER)
throw Exception("Using file descriptor as source of storage isn't allowed for server daemons", ErrorCodes::DATABASE_ACCESS_DENIED);
is_db_table = false;
use_table_fd = true;
@ -258,6 +252,8 @@ BlockInputStreams StorageFile::read(
const ColumnsDescription & columns_ = getColumns();
auto column_defaults = columns_.getDefaults();
BlockInputStreams blocks_input;
if (use_table_fd) /// need to call ctr BlockInputStream
paths = {""}; /// when use fd, paths are empty
blocks_input.reserve(paths.size());
for (const auto & file_path : paths)
{
@ -274,8 +270,6 @@ public:
explicit StorageFileBlockOutputStream(StorageFile & storage_)
: storage(storage_), lock(storage.rwlock)
{
if (storage.paths.size() != 1)
throw Exception("Table '" + storage.table_name + "' is in readonly mode", ErrorCodes::DATABASE_ACCESS_DENIED);
if (storage.use_table_fd)
{
/** NOTE: Using real file binded to FD may be misleading:
@ -287,6 +281,8 @@ public:
}
else
{
if (storage.paths.size() != 1)
throw Exception("Table '" + storage.table_name + "' is in readonly mode", ErrorCodes::DATABASE_ACCESS_DENIED);
write_buf = std::make_unique<WriteBufferFromFile>(storage.paths[0], DBMS_DEFAULT_BUFFER_SIZE, O_WRONLY | O_APPEND | O_CREAT);
}

View File

@ -70,10 +70,10 @@ private:
int table_fd = -1;
std::vector<std::string> paths{""};
std::vector<std::string> paths;
bool is_db_table = true; /// Table is stored in real database, not user's file
bool use_table_fd = false; /// Use table_fd insted of path
bool use_table_fd = false; /// Use table_fd instead of path
std::atomic<bool> table_fd_was_used{false}; /// To detect repeating reads from stdin
off_t table_fd_init_offset = -1; /// Initial position of fd, used for repeating reads