| 1 | /* |
| 2 | Copyright (c) 2014, SkySQL Ab |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; version 2 of the License. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program; if not, write to the Free Software |
| 15 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | /* C++ system header files */ |
| 20 | #include <map> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | /* MySQL header files */ |
| 25 | #include "./sql_class.h" |
| 26 | |
| 27 | /* RocksDB header files */ |
| 28 | #include "rocksdb/db.h" |
| 29 | |
| 30 | /* MyRocks header files */ |
| 31 | #include "./rdb_cf_options.h" |
| 32 | |
| 33 | namespace myrocks { |
| 34 | |
| 35 | /* |
| 36 | We need a Column Family (CF) manager. Its functions: |
| 37 | - create column families (synchronized, don't create the same twice) |
| 38 | - keep count in each column family. |
| 39 | = the count is kept on-disk. |
| 40 | = there are no empty CFs. initially count=1. |
| 41 | = then, when doing DDL, we increase or decrease it. |
| 42 | (atomicity is maintained by being in the same WriteBatch with DDLs) |
| 43 | = if DROP discovers that now count=0, it removes the CF. |
| 44 | |
| 45 | Current state is: |
| 46 | - CFs are created in a synchronized way. We can't remove them, yet. |
| 47 | */ |
| 48 | |
| 49 | class Rdb_cf_manager { |
| 50 | std::map<std::string, rocksdb::ColumnFamilyHandle *> m_cf_name_map; |
| 51 | std::map<uint32_t, rocksdb::ColumnFamilyHandle *> m_cf_id_map; |
| 52 | |
| 53 | mutable mysql_mutex_t m_mutex; |
| 54 | |
| 55 | std::unique_ptr<Rdb_cf_options> m_cf_options = nullptr; |
| 56 | |
| 57 | public: |
| 58 | Rdb_cf_manager(const Rdb_cf_manager &) = delete; |
| 59 | Rdb_cf_manager &operator=(const Rdb_cf_manager &) = delete; |
| 60 | Rdb_cf_manager() = default; |
| 61 | |
| 62 | static bool is_cf_name_reverse(const char *const name); |
| 63 | |
| 64 | /* |
| 65 | This is called right after the DB::Open() call. The parameters describe |
| 66 | column |
| 67 | families that are present in the database. The first CF is the default CF. |
| 68 | */ |
| 69 | void init(std::unique_ptr<Rdb_cf_options> cf_options, |
| 70 | std::vector<rocksdb::ColumnFamilyHandle *> *const handles); |
| 71 | void cleanup(); |
| 72 | |
| 73 | /* |
| 74 | Used by CREATE TABLE. |
| 75 | - cf_name=nullptr means use default column family |
| 76 | */ |
| 77 | rocksdb::ColumnFamilyHandle *get_or_create_cf(rocksdb::DB *const rdb, |
| 78 | const std::string &cf_name); |
| 79 | |
| 80 | /* Used by table open */ |
| 81 | rocksdb::ColumnFamilyHandle *get_cf(const std::string &cf_name) const; |
| 82 | |
| 83 | /* Look up cf by id; used by datadic */ |
| 84 | rocksdb::ColumnFamilyHandle *get_cf(const uint32_t &id) const; |
| 85 | |
| 86 | /* Used to iterate over column families for show status */ |
| 87 | std::vector<std::string> get_cf_names(void) const; |
| 88 | |
| 89 | /* Used to iterate over column families */ |
| 90 | std::vector<rocksdb::ColumnFamilyHandle *> get_all_cf(void) const; |
| 91 | |
| 92 | // void drop_cf(); -- not implemented so far. |
| 93 | |
| 94 | void get_cf_options(const std::string &cf_name, |
| 95 | rocksdb::ColumnFamilyOptions *const opts) |
| 96 | MY_ATTRIBUTE((__nonnull__)) { |
| 97 | m_cf_options->get_cf_options(cf_name, opts); |
| 98 | } |
| 99 | |
| 100 | void update_options_map(const std::string &cf_name, |
| 101 | const std::string &updated_options) { |
| 102 | m_cf_options->update(cf_name, updated_options); |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | } // namespace myrocks |
| 107 | |