Skip to content

Commit 1790f3a

Browse files
committed
WIP cache schema function
1 parent 1c21a73 commit 1790f3a

11 files changed

+144
-4
lines changed

duckdb

Submodule duckdb updated 280 files

src/include/mysql_scanner.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class MySQLScanFunction : public TableFunction {
3939
MySQLScanFunction();
4040
};
4141

42+
class MySQLCacheSchemaFunction : public TableFunction {
43+
public:
44+
MySQLCacheSchemaFunction();
45+
};
46+
4247
class MySQLClearCacheFunction : public TableFunction {
4348
public:
4449
MySQLClearCacheFunction();

src/include/storage/mysql_catalog.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class MySQLCatalog : public Catalog {
5555
bool InMemory() override;
5656
string GetDBPath() override;
5757

58+
void CacheSchema(ClientContext &context);
5859
void ClearCache();
5960

6061
private:

src/include/storage/mysql_catalog_set.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class MySQLCatalogSet {
2424
virtual void DropEntry(ClientContext &context, DropInfo &info);
2525
void Scan(ClientContext &context, const std::function<void(CatalogEntry &)> &callback);
2626
optional_ptr<CatalogEntry> CreateEntry(unique_ptr<CatalogEntry> entry);
27+
void CacheEntries(ClientContext &context);
2728
void ClearEntries();
2829

2930
protected:
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
3+
#include "duckdb.hpp"
4+
#ifndef DUCKDB_AMALGAMATION
5+
#include "duckdb/storage/object_cache.hpp"
6+
#endif
7+
//#include "mysql_schema_entry.hpp"
8+
9+
namespace duckdb {
10+
11+
//! MysqlSchemaEntriesCache
12+
class MysqlSchemaEntriesCache : public ObjectCacheEntry {
13+
public:
14+
MysqlSchemaEntriesCache() : entries(nullptr) {
15+
}
16+
MysqlSchemaEntriesCache(unique_ptr<vector<string>> mysql_schema_entries, time_t r_time)
17+
: entries(std::move(mysql_schema_entries)), read_time(r_time) {
18+
}
19+
20+
~MysqlSchemaEntriesCache() override = default;
21+
22+
//! Mysql schema entries
23+
unique_ptr<vector<string>> entries;
24+
25+
//! read time
26+
time_t read_time;
27+
28+
public:
29+
static string ObjectType() {
30+
return "mysql_schema_entries";
31+
}
32+
33+
string GetObjectType() override {
34+
return ObjectType();
35+
}
36+
};
37+
} // namespace duckdb

src/include/storage/mysql_schema_set.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ class MySQLSchemaSet : public MySQLCatalogSet {
2323

2424
protected:
2525
void LoadEntries(ClientContext &context) override;
26+
void PutInCache(ClientContext &context, vector<string> &schemas);
27+
vector<string> LoadSchemasFromMysqlInformationSchema(ClientContext &context);
2628
};
2729

30+
extern const string MYSQL_SCHEMA_ENTRIES_CACHE_KEY;
31+
2832
} // namespace duckdb

src/storage/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_library(
22
mysql_ext_storage OBJECT
3+
mysql_cache_schema.cpp
34
mysql_catalog.cpp
45
mysql_catalog_set.cpp
56
mysql_clear_cache.cpp

src/storage/mysql_cache_schema.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "duckdb.hpp"
2+
3+
#include "duckdb/parser/parsed_data/create_table_function_info.hpp"
4+
#include "mysql_scanner.hpp"
5+
#include "duckdb/main/database_manager.hpp"
6+
#include "duckdb/main/attached_database.hpp"
7+
#include "storage/mysql_catalog.hpp"
8+
9+
namespace duckdb {
10+
11+
struct CacheSchemaFunctionData : public TableFunctionData {
12+
bool finished = false;
13+
};
14+
15+
static unique_ptr<FunctionData> CacheSchemaBind(ClientContext &context, TableFunctionBindInput &input,
16+
vector<LogicalType> &return_types, vector<string> &names) {
17+
18+
auto result = make_uniq<CacheSchemaFunctionData>();
19+
return_types.push_back(LogicalType::BOOLEAN);
20+
names.emplace_back("Success");
21+
return std::move(result);
22+
}
23+
24+
static void CacheSchemaFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) {
25+
auto &data = data_p.bind_data->CastNoConst<CacheSchemaFunctionData>();
26+
if (data.finished) {
27+
return;
28+
}
29+
auto databases = DatabaseManager::Get(context).GetDatabases(context);
30+
for (auto &db_ref : databases) {
31+
auto &db = db_ref.get();
32+
auto &catalog = db.GetCatalog();
33+
if (catalog.GetCatalogType() != "mysql") {
34+
continue;
35+
}
36+
catalog.Cast<MySQLCatalog>().CacheSchema(context);
37+
}
38+
data.finished = true;
39+
}
40+
41+
MySQLCacheSchemaFunction::MySQLCacheSchemaFunction()
42+
: TableFunction("mysql_cache_schema", {}, CacheSchemaFunction, CacheSchemaBind) {
43+
}
44+
} // namespace duckdb

src/storage/mysql_catalog.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ WHERE table_schema = ${SCHEMA_NAME};
9292
return size;
9393
}
9494

95+
void MySQLCatalog::CacheSchema(ClientContext &context) {
96+
schemas.CacheEntries(context);
97+
}
98+
9599
void MySQLCatalog::ClearCache() {
96100
schemas.ClearEntries();
97101
}

src/storage/mysql_catalog_set.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ optional_ptr<CatalogEntry> MySQLCatalogSet::CreateEntry(unique_ptr<CatalogEntry>
6464
return result;
6565
}
6666

67+
void MySQLCatalogSet::CacheEntries(ClientContext &context) {
68+
if (!is_loaded) {
69+
is_loaded = true;
70+
LoadEntries(context);
71+
}
72+
}
73+
6774
void MySQLCatalogSet::ClearEntries() {
6875
entries.clear();
6976
is_loaded = false;

src/storage/mysql_schema_set.cpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,61 @@
11
#include "storage/mysql_schema_set.hpp"
22
#include "storage/mysql_transaction.hpp"
33
#include "duckdb/parser/parsed_data/create_schema_info.hpp"
4+
#include "duckdb/storage/object_cache.hpp"
5+
#include "storage/mysql_schema_entries_cache.hpp"
6+
#include <iostream>
47

58
namespace duckdb {
69

10+
const std::string MYSQL_SCHEMA_ENTRIES_CACHE_KEY = "mysql_schema_entries_cache";
11+
712
MySQLSchemaSet::MySQLSchemaSet(Catalog &catalog) : MySQLCatalogSet(catalog) {
813
}
914

1015
void MySQLSchemaSet::LoadEntries(ClientContext &context) {
16+
auto db = context.db;
17+
auto& object_cache = db->GetObjectCache();
18+
auto schema_cache = object_cache.Get<MysqlSchemaEntriesCache>(MYSQL_SCHEMA_ENTRIES_CACHE_KEY);
19+
if(schema_cache != nullptr) {
20+
//std::cout << "load from cache" << std::endl;
21+
auto mysql_schema_entries = std::move(schema_cache->entries);
22+
for(auto& schema_name : *mysql_schema_entries) {
23+
auto schema = make_uniq<MySQLSchemaEntry>(catalog, schema_name);
24+
CreateEntry(std::move(schema));
25+
}
26+
} else {
27+
//std::cout << "load from mysql" << std::endl;
28+
auto schemas = LoadSchemasFromMysqlInformationSchema(context);
29+
for(auto& schema : schemas) {
30+
auto schema_entry = make_uniq<MySQLSchemaEntry>(catalog, schema);
31+
CreateEntry(std::move(schema_entry));
32+
}
33+
PutInCache(context, schemas);
34+
}
35+
}
36+
37+
void MySQLSchemaSet::PutInCache(ClientContext &context, vector<string> &schemas) {
38+
auto db = context.db;
39+
auto& object_cache = db->GetObjectCache();
40+
auto mysql_schema_entries = make_shared<MysqlSchemaEntriesCache>(make_uniq<vector<string>>(schemas), time(nullptr));
41+
object_cache.Put(MYSQL_SCHEMA_ENTRIES_CACHE_KEY, mysql_schema_entries);
42+
}
43+
44+
vector<string> MySQLSchemaSet::LoadSchemasFromMysqlInformationSchema(ClientContext &context) {
1145
auto query = R"(
1246
SELECT schema_name
1347
FROM information_schema.schemata;
1448
)";
15-
49+
1650
auto &transaction = MySQLTransaction::Get(context, catalog);
1751
auto result = transaction.Query(query);
52+
53+
auto schemas = vector<string>();
1854
while (result->Next()) {
1955
auto schema_name = result->GetString(0);
20-
auto schema = make_uniq<MySQLSchemaEntry>(catalog, schema_name);
21-
CreateEntry(std::move(schema));
56+
schemas.push_back(schema_name);
2257
}
58+
return schemas;
2359
}
2460

2561
optional_ptr<CatalogEntry> MySQLSchemaSet::CreateSchema(ClientContext &context, CreateSchemaInfo &info) {

0 commit comments

Comments
 (0)