1 #ifndef REWRITER_INCLUDED
2 #define REWRITER_INCLUDED
3 /*  Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License, version 2.0,
7     as published by the Free Software Foundation.
8 
9     This program is also distributed with certain software (including
10     but not limited to OpenSSL) that is licensed under separate terms,
11     as designated in a particular file or component or in included license
12     documentation.  The authors of MySQL hereby grant you an additional
13     permission to link the program and your derivative works with the
14     separately licensed software that they have included with MySQL.
15 
16     This program is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19     GNU General Public License, version 2.0, for more details.
20 
21     You should have received a copy of the GNU General Public License
22     along with this program; if not, write to the Free Software
23     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24     02110-1301  USA */
25 
26 #include "my_config.h"
27 #include <hash.h>
28 #include "rule.h"
29 
30 /**
31   @file rewriter.h
32 
33   Facade for the query rewriter. Provides the Rewriter class which performs
34   all of the functionality. A query rewriter plugin needs only include this
35   file.
36 */
37 
38 
39 class Persisted_rule;
40 
41 
42 /**
43   Implementation of the post parse query rewriter. The public interface
44   consists of two operations: refresh(), which loads the rules from the disk
45   table, and rewrite_query(), which rewrites a query if applicable.
46 */
47 class Rewriter {
48 
49 public:
50 
51   enum Load_status {
52     REWRITER_OK,
53     REWRITER_ERROR_TABLE_MALFORMED,
54     REWRITER_ERROR_LOAD_FAILED,
55     REWRITER_ERROR_READ_FAILED
56   };
57 
58   Rewriter();
59 
60   /**
61     The number of rules currently loaded in the hash table. In case of rules
62     that fail to load, this number will be lower than the number of rows in
63     the database.
64   */
get_number_loaded_rules()65   int get_number_loaded_rules() const { return m_digests.records; }
66 
67   ~Rewriter();
68 
69   /**
70     Attempts to rewrite thd's current query with digest in 'key'.
71 
72     @return A Rewrite_result object.
73   */
74   Rewrite_result rewrite_query(MYSQL_THD thd, const uchar* key);
75 
76   /// Empty the hashtable and reload all rules from disk table.
77   Load_status refresh(MYSQL_THD thd);
78 
79   /**
80     Implementation of the loading procedure. The server doesn't handle
81     different sessions in the same thread, so we load the rules into the hash
82     table in this function, intended to be run in a new thread. The main
83     thread will do join().
84 
85     @param sessions_thd The session to be used for loading rules.
86   */
87   void do_refresh(MYSQL_THD session_thd);
88 
89 private:
90   Rewriter::Load_status m_refresh_status;
91 
92   /// The in-memory rules hash table.
93   HASH m_digests;
94 
95   /// Loads the rule retrieved from the database in the hash table.
96   bool load_rule(MYSQL_THD thd, Persisted_rule *diskrule);
97 
98 };
99 
100 #endif /* REWRITER_INCLUDED */
101