1 /*
2    Copyright (c) 2015, Facebook, Inc.
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 /* The C++ file's header */
18 #include "./event_listener.h"
19 
20 /* C++ standard header files */
21 #include <string>
22 #include <vector>
23 
24 /* MySQL includes */
25 #include <my_global.h>
26 #include <mysql/plugin.h>
27 
28 /* MyRocks includes */
29 #include "./ha_rocksdb.h"
30 #include "./properties_collector.h"
31 #include "./rdb_datadic.h"
32 
33 namespace myrocks {
34 
35 static std::vector<Rdb_index_stats>
extract_index_stats(const std::vector<std::string> & files,const rocksdb::TablePropertiesCollection & props)36 extract_index_stats(const std::vector<std::string> &files,
37                     const rocksdb::TablePropertiesCollection &props) {
38   std::vector<Rdb_index_stats> ret;
39   for (auto fn : files) {
40     const auto it = props.find(fn);
41     DBUG_ASSERT(it != props.end());
42     std::vector<Rdb_index_stats> stats;
43     Rdb_tbl_prop_coll::read_stats_from_tbl_props(it->second, &stats);
44     ret.insert(ret.end(), stats.begin(), stats.end());
45   }
46   return ret;
47 }
48 
update_index_stats(const rocksdb::TableProperties & props)49 void Rdb_event_listener::update_index_stats(
50     const rocksdb::TableProperties &props) {
51   DBUG_ASSERT(m_ddl_manager != nullptr);
52   const auto tbl_props =
53       std::make_shared<const rocksdb::TableProperties>(props);
54 
55   std::vector<Rdb_index_stats> stats;
56   Rdb_tbl_prop_coll::read_stats_from_tbl_props(tbl_props, &stats);
57 
58   m_ddl_manager->adjust_stats(stats);
59 }
60 
OnCompactionCompleted(rocksdb::DB * db,const rocksdb::CompactionJobInfo & ci)61 void Rdb_event_listener::OnCompactionCompleted(
62     rocksdb::DB *db, const rocksdb::CompactionJobInfo &ci) {
63   DBUG_ASSERT(db != nullptr);
64   DBUG_ASSERT(m_ddl_manager != nullptr);
65 
66   if (ci.status.ok()) {
67     m_ddl_manager->adjust_stats(
68         extract_index_stats(ci.output_files, ci.table_properties),
69         extract_index_stats(ci.input_files, ci.table_properties));
70   }
71 }
72 
OnFlushCompleted(rocksdb::DB * db,const rocksdb::FlushJobInfo & flush_job_info)73 void Rdb_event_listener::OnFlushCompleted(
74     rocksdb::DB *db, const rocksdb::FlushJobInfo &flush_job_info) {
75   DBUG_ASSERT(db != nullptr);
76   update_index_stats(flush_job_info.table_properties);
77 }
78 
OnExternalFileIngested(rocksdb::DB * db,const rocksdb::ExternalFileIngestionInfo & info)79 void Rdb_event_listener::OnExternalFileIngested(
80     rocksdb::DB *db, const rocksdb::ExternalFileIngestionInfo &info) {
81   DBUG_ASSERT(db != nullptr);
82   update_index_stats(info.table_properties);
83 }
84 } // namespace myrocks
85