File: autocomplete_object_name_cache.h

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (198 lines) | stat: -rw-r--r-- 8,388 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* 
 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the
 * License.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#pragma once

#include "base/threading.h"
#include "base/string_utilities.h"

#include "wbpublic_public_interface.h"
#include <sqlite/connection.hpp>
#include "grts/structs.db.mgmt.h"

#include "cppdbc.h"

class WBPUBLICBACKEND_PUBLIC_FUNC AutoCompleteCache
{
public:

  // Note: feedback can be called from the worker thread. Make the necessary arrangements.
  //       It comes with parameter true if the cache update is going on, otherwise false.
  AutoCompleteCache(const std::string &connection_id,
                    boost::function<base::RecMutexLock (sql::Dbc_connection_handler::Ref &)> get_connection,
                    const std::string &cache_dir,
                    boost::function<void (bool)> feedback);

  // Data retrieval functions.
  std::vector<std::string> get_matching_schema_names(const std::string &prefix = "");
  std::vector<std::string> get_matching_table_names(const std::string &schema = "",
                                                    const std::string &prefix = "");
  std::vector<std::string> get_matching_view_names(const std::string &schema = "",
                                                   const std::string &prefix = "");
  std::vector<std::string> get_matching_column_names(const std::string &schema,
                                                     const std::string &table,
                                                     const std::string &prefix = "");
  std::vector<std::string> get_matching_procedure_names(const std::string &schema = "",
                                                        const std::string &prefix = "");
  std::vector<std::string> get_matching_function_names(const std::string &schema = "",
                                                       const std::string &prefix = "");
  std::vector<std::string> get_matching_trigger_names(const std::string &schema = "",
                                                      const std::string &table = "",
                                                      const std::string &prefix = "");

  std::vector<std::string> get_matching_udf_names(const std::string &prefix = "");
  std::vector<std::string> get_matching_variables(const std::string &prefix = ""); // System vars only.
  std::vector<std::string> get_matching_engines(const std::string &prefix = "");
  
  std::vector<std::string> get_matching_logfile_groups(const std::string &prefix = ""); // Only useful for NDB cluster.
  std::vector<std::string> get_matching_tablespaces(const std::string &prefix = ""); // Only useful for NDB cluster.

  std::vector<std::string> get_matching_charsets(const std::string &prefix = "");
  std::vector<std::string> get_matching_collations(const std::string &prefix = "");

  std::vector<std::string> get_matching_events(const std::string &schema = "", const std::string &prefix = "");

  // Data refresh functions. To be called from outside when data objects are created or destroyed.
  void refresh_schema_list();
  bool refresh_schema_cache_if_needed(const std::string &schema);
  void refresh_tables(const std::string &schema);
  void refresh_views(const std::string &schema);
  void refresh_columns(const std::string &schema, const std::string &table);
  void refresh_triggers(const std::string &schema, const std::string &table);
  void refresh_udfs();
  void refresh_tablespaces();    // Logfile groups and tablespaces are unqualified,
  void refresh_logfile_groups(); // even though they belong to a specific table.
  void refresh_events();

  // Update functions that can also be called from outside.
  void update_schemas(const std::vector<std::string> &schemas);
  void update_tables(const std::string &schema, base::StringListPtr tables);
  void update_views(const std::string &schema, base::StringListPtr tables);
  void update_procedures(const std::string &schema, base::StringListPtr tables);
  void update_functions(const std::string &schema, base::StringListPtr tables);
  void update_events(const std::string &schema, base::StringListPtr events);

  // Status functions.
  bool is_schema_list_fetch_done();
  bool is_schema_tables_fetch_done(const std::string &schema);
  bool is_schema_table_columns_fetch_done(const std::string &schema, const std::string &table);
  bool is_schema_functions_fetch_done(const std::string &schema);
  bool is_schema_procedure_fetch_done(const std::string &schema);

  void shutdown();
  
  ~AutoCompleteCache();
private:
  struct RefreshTask {
    enum RefreshType {
      RefreshSchemas,
      RefreshTables,
      RefreshViews,
      RefreshProcedures,
      RefreshFunctions,
      RefreshColumns,
      RefreshTriggers,
      RefreshUDFs,
      RefreshVariables,
      RefreshEngines,
      RefreshLogfileGroups,
      RefreshTableSpaces,
      RefreshCharsets,
      RefreshCollations,
      RefreshEvents
    } type;
    std::string schema_name;
    std::string table_name;

    RefreshTask()
    {
      type = RefreshSchemas;
    }
    
    RefreshTask(RefreshType type_, const std::string &schema, const std::string &table)
    {
      type = type_;
      schema_name = schema;
      table_name = table;
    }
  };

  enum RetrievalType {
    RetrieveWithNoQualifier,
    RetrieveWithSchemaQualifier,
    RetrieveWithFullQualifier
  };

  void init_db();

  static void *_refresh_cache_thread(void *);
  void refresh_cache_thread();
  void refresh_schemas_w();
  void refresh_tables_w(const std::string &schema);
  void refresh_views_w(const std::string &schema);
  void refresh_functions_w(const std::string &schema);
  void refresh_procedures_w(const std::string &schema);
  void refresh_columns_w(const std::string &schema, const std::string &table);
  void refresh_triggers_w(const std::string &schema, const std::string &table);
  void refresh_udfs_w();
  void refreshCharsets_w();
  void refreshCollations_w();
  void refresh_variables_w();
  void refresh_engines_w();
  void refresh_logfile_groups_w();
  void refresh_tablespaces_w();
  void refreshEvents_w(const std::string &schema);

  void update_object_names(const std::string &cache, const std::vector<std::string> &objects);
  void update_object_names(const std::string &cache,
                           const std::string &schema,
                           base::StringListPtr objects);
  void update_object_names(const std::string &cache,
                           const std::string &schema,
                           const std::string &table,
                           const std::vector<std::string> &objects);

  std::vector<std::string> get_matching_objects(const std::string &cache,
                                                const std::string &schema,
                                                const std::string &table,
                                                const std::string &prefix,
                                                RetrievalType type);
  void touch_schema_record(const std::string &schema);
  bool is_fetch_done(const std::string &cache, const std::string &schema);

  bool get_pending_refresh(RefreshTask &task);
  void add_pending_refresh(RefreshTask::RefreshType type, const std::string &schema = "",
                           const std::string &table = "");
  void create_worker_thread();
  
  base::RecMutex _sqconn_mutex;
  sqlite::connection *_sqconn;

  GThread *_refresh_thread;
  base::Semaphore _cache_working;

  base::RecMutex _pending_mutex; // Protects the pending tasks.
  std::list<RefreshTask> _pending_tasks;

  std::string _connection_id;
  boost::function<base::RecMutexLock (sql::Dbc_connection_handler::Ref &)> _get_connection;
  boost::function<void (bool)> _feedback;

  bool _shutdown;
};