| 81 | } |
| 82 | |
| 83 | int DatabaseManager::open(const char *path, Database **db) { |
| 84 | MRN_DBUG_ENTER_METHOD(); |
| 85 | |
| 86 | int error = 0; |
| 87 | *db = NULL; |
| 88 | |
| 89 | mrn::PathMapper mapper(path); |
| 90 | mrn::Lock lock(mutex_); |
| 91 | |
| 92 | error = mrn::encoding::set(ctx_, system_charset_info); |
| 93 | if (error) { |
| 94 | DBUG_RETURN(error); |
| 95 | } |
| 96 | |
| 97 | grn_id id; |
| 98 | void *db_address; |
| 99 | id = grn_hash_get(ctx_, cache_, |
| 100 | mapper.db_name(), strlen(mapper.db_name()), |
| 101 | &db_address); |
| 102 | if (id == GRN_ID_NIL) { |
| 103 | grn_obj *grn_db; |
| 104 | struct stat db_stat; |
| 105 | if (stat(mapper.db_path(), &db_stat)) { |
| 106 | GRN_LOG(ctx_, GRN_LOG_INFO, |
| 107 | "database not found. creating...: <%s>", mapper.db_path()); |
| 108 | if (path[0] == FN_CURLIB && |
| 109 | mrn_is_directory_separator(path[1])) { |
| 110 | ensure_database_directory(); |
| 111 | } |
| 112 | grn_db = grn_db_create(ctx_, mapper.db_path(), NULL); |
| 113 | if (ctx_->rc) { |
| 114 | error = ER_CANT_CREATE_TABLE; |
| 115 | my_message(error, ctx_->errbuf, MYF(0)); |
| 116 | DBUG_RETURN(error); |
| 117 | } |
| 118 | } else { |
| 119 | grn_db = grn_db_open(ctx_, mapper.db_path()); |
| 120 | if (ctx_->rc) { |
| 121 | error = ER_CANT_OPEN_FILE; |
| 122 | my_message(error, ctx_->errbuf, MYF(0)); |
| 123 | DBUG_RETURN(error); |
| 124 | } |
| 125 | } |
| 126 | *db = new Database(ctx_, grn_db); |
| 127 | grn_hash_add(ctx_, cache_, |
| 128 | mapper.db_name(), strlen(mapper.db_name()), |
| 129 | &db_address, NULL); |
| 130 | memcpy(db_address, db, sizeof(Database *)); |
| 131 | error = ensure_normalizers_registered((*db)->get()); |
| 132 | if (!error) { |
| 133 | if ((*db)->is_broken()) { |
| 134 | error = ER_CANT_OPEN_FILE; |
| 135 | char error_message[MRN_MESSAGE_BUFFER_SIZE]; |
| 136 | snprintf(error_message, MRN_MESSAGE_BUFFER_SIZE, |
| 137 | "mroonga: database: open: " |
| 138 | "The database maybe broken. " |
| 139 | "We recommend you to recreate the database. " |
| 140 | "If the database isn't broken, " |
no test coverage detected