diff options
author | Alexander Barkov <bar@mnogosearch.org> | 2014-10-02 15:48:20 +0400 |
---|---|---|
committer | Alexander Barkov <bar@mnogosearch.org> | 2014-10-02 15:48:20 +0400 |
commit | 665ea7796f64e1bb1fcbdf7f9cb933e525b44c99 (patch) | |
tree | 7b68ac23bfd500bc1395f2c8d8fdf20cdb7dcfd8 /storage/mroonga/lib/mrn_path_mapper.cpp | |
parent | 44428d397cdb0da5456e885d8c3ebc559eebfe82 (diff) | |
parent | 8dbe3d81d44ed5c9df540e02ef18cbbc7d971c12 (diff) | |
download | mariadb-git-665ea7796f64e1bb1fcbdf7f9cb933e525b44c99.tar.gz |
Merge 10.0-mroonga -> 10.0
Diffstat (limited to 'storage/mroonga/lib/mrn_path_mapper.cpp')
-rw-r--r-- | storage/mroonga/lib/mrn_path_mapper.cpp | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/storage/mroonga/lib/mrn_path_mapper.cpp b/storage/mroonga/lib/mrn_path_mapper.cpp new file mode 100644 index 00000000000..8856e6f6082 --- /dev/null +++ b/storage/mroonga/lib/mrn_path_mapper.cpp @@ -0,0 +1,196 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2010 Tetsuro IKEDA + Copyright(C) 2011-2013 Kentoku SHIBA + Copyright(C) 2011-2012 Kouhei Sutou <kou@clear-code.com> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "mrn_path_mapper.hpp" + +#include <string.h> + +#include <mrn_mysql.h> + +namespace mrn { + char *PathMapper::default_path_prefix = NULL; + char *PathMapper::default_mysql_data_home_path = NULL; + + PathMapper::PathMapper(const char *mysql_path, + const char *path_prefix, + const char *mysql_data_home_path) + : mysql_path_(mysql_path), + path_prefix_(path_prefix), + mysql_data_home_path_(mysql_data_home_path) { + db_path_[0] = '\0'; + db_name_[0] = '\0'; + table_name_[0] = '\0'; + mysql_table_name_[0] = '\0'; + } + + /** + * "./${db}/${table}" ==> "${db}.mrn" + * "./${db}/" ==> "${db}.mrn" + * "/tmp/mysql-test/var/tmp/mysqld.1/#sql27c5_1_0" ==> + * "/tmp/mysql-test/var/tmp/mysqld.1/#sql27c5_1_0.mrn" + */ + const char *PathMapper::db_path() { + if (db_path_[0] != '\0') { + return db_path_; + } + + if (mysql_path_[0] == FN_CURLIB && mysql_path_[1] == FN_LIBCHAR) { + if (path_prefix_) { + strcpy(db_path_, path_prefix_); + } + + int i = 2, j = strlen(db_path_), len; + len = strlen(mysql_path_); + while (mysql_path_[i] != FN_LIBCHAR && i < len) { + db_path_[j++] = mysql_path_[i++]; + } + db_path_[j] = '\0'; + } else if (mysql_data_home_path_) { + int len = strlen(mysql_path_); + int mysql_data_home_len = strlen(mysql_data_home_path_); + if (len > mysql_data_home_len && + !strncmp(mysql_path_, mysql_data_home_path_, mysql_data_home_len)) { + int i = mysql_data_home_len, j; + if (path_prefix_ && path_prefix_[0] == FN_LIBCHAR) { + strcpy(db_path_, path_prefix_); + j = strlen(db_path_); + } else { + memcpy(db_path_, mysql_data_home_path_, mysql_data_home_len); + if (path_prefix_) { + if (path_prefix_[0] == FN_CURLIB && + path_prefix_[1] == FN_LIBCHAR) { + strcpy(&db_path_[mysql_data_home_len], &path_prefix_[2]); + } else { + strcpy(&db_path_[mysql_data_home_len], path_prefix_); + } + j = strlen(db_path_); + } else { + j = mysql_data_home_len; + } + } + + while (mysql_path_[i] != FN_LIBCHAR && i < len) { + db_path_[j++] = mysql_path_[i++]; + } + if (i == len) { + memcpy(db_path_, mysql_path_, len); + } else { + db_path_[j] = '\0'; + } + } else { + strcpy(db_path_, mysql_path_); + } + } else { + strcpy(db_path_, mysql_path_); + } + strcat(db_path_, MRN_DB_FILE_SUFFIX); + return db_path_; + } + + /** + * "./${db}/${table}" ==> "${db}" + * "./${db}/" ==> "${db}" + * "/tmp/mysql-test/var/tmp/mysqld.1/#sql27c5_1_0" ==> + * "/tmp/mysql-test/var/tmp/mysqld.1/#sql27c5_1_0" + */ + const char *PathMapper::db_name() { + if (db_name_[0] != '\0') { + return db_name_; + } + + if (mysql_path_[0] == FN_CURLIB && mysql_path_[1] == FN_LIBCHAR) { + int i = 2, j = 0, len; + len = strlen(mysql_path_); + while (mysql_path_[i] != FN_LIBCHAR && i < len) { + db_name_[j++] = mysql_path_[i++]; + } + db_name_[j] = '\0'; + } else if (mysql_data_home_path_) { + int len = strlen(mysql_path_); + int mysql_data_home_len = strlen(mysql_data_home_path_); + if (len > mysql_data_home_len && + !strncmp(mysql_path_, mysql_data_home_path_, mysql_data_home_len)) { + int i = mysql_data_home_len, j = 0; + while (mysql_path_[i] != FN_LIBCHAR && i < len) { + db_name_[j++] = mysql_path_[i++]; + } + if (i == len) { + memcpy(db_name_, mysql_path_, len); + } else { + db_name_[j] = '\0'; + } + } else { + strcpy(db_name_, mysql_path_); + } + } else { + strcpy(db_name_, mysql_path_); + } + return db_name_; + } + + /** + * "./${db}/${table}" ==> "${table}" (with encoding first '_') + */ + const char *PathMapper::table_name() { + if (table_name_[0] != '\0') { + return table_name_; + } + + int len = strlen(mysql_path_); + int i = len, j = 0; + for (; mysql_path_[--i] != FN_LIBCHAR ;) {} + if (mysql_path_[i + 1] == '_') { + table_name_[j++] = '@'; + table_name_[j++] = '0'; + table_name_[j++] = '0'; + table_name_[j++] = '5'; + table_name_[j++] = 'f'; + i++; + } + for (; i < len ;) { + table_name_[j++] = mysql_path_[++i]; + } + table_name_[j] = '\0'; + return table_name_; + } + + /** + * "./${db}/${table}" ==> "${table}" (without encoding first '_') + */ + const char *PathMapper::mysql_table_name() { + if (mysql_table_name_[0] != '\0') { + return mysql_table_name_; + } + + int len = strlen(mysql_path_); + int i = len, j = 0; + for (; mysql_path_[--i] != FN_LIBCHAR ;) {} + for (; i < len ;) { + mysql_table_name_[j++] = mysql_path_[++i]; + } + mysql_table_name_[j] = '\0'; + return mysql_table_name_; + } +} |