fix(database/gdb): refactor gdb schema caching with type-safe tableRegistry#4711
Open
LanceAdd wants to merge 4 commits intogogf:masterfrom
Open
fix(database/gdb): refactor gdb schema caching with type-safe tableRegistry#4711LanceAdd wants to merge 4 commits intogogf:masterfrom
LanceAdd wants to merge 4 commits intogogf:masterfrom
Conversation
- 将Core结构体中的innerMemCache替换为tableRegistry以统一管理表结构元数据 - HasTable函数优化为通过tableRegistry完成表存在性判断,提高查询效率 - SetTableFields改为通过registry缓存表字段信息,移除直接操作缓存的逻辑 - GetTablesWithCache改用registry缓存表名列表以避免重复数据库查询 - ClearTableFields及ClearTableFieldsAll改为操作registry,实现缓存清理 - TableFields获取及软时间字段缓存逻辑改为访问registry,去除冗余缓存层 - 删除与innerMemCache相关的缓存键生成及无用缓存代码 - 新增tableRegistry类型,实现线程安全的三维(group-schema-table)元数据缓存管理 - 修改相关调用逻辑以适配registry缓存体系,提升缓存一致性及并发性能
- 修改 GetLoadedSchemaTables 方法以跟踪完整表名列表是否已从数据库加载 - 添加 schemaKey 类型用于标识 (group, schema) 对以跟踪表列表加载状态 - 新增 loadedSchemas 映射跟踪哪些 (group, schema) 对的完整表名列表已加载 - 更新 Sets 方法记录完整表名列表已从数据库加载的信息 - 修改 ClearTableFields 方法保留表存在标记以避免数据库往返查询 - 更新 ClearAll 方法清除已加载模式标记确保下次访问重新查询数据库 - 在 gdb_func.go 中为 HasTable 调用添加模式参数支持
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the database schema metadata caching mechanism in the database/gdb module by introducing a type-safe tableRegistry to replace the generic innerMemCache. The refactoring addresses correctness issues with multi-schema support, cache consistency, concurrent safety, and performance.
Changes:
- Introduced
tableRegistrywith three-dimensional addressing (group, schema, table) to replaceinnerMemCachefor schema metadata storage - Enhanced
HasTableandGetTablesWithCachewith optional schema parameters while maintaining backward compatibility - Refactored soft delete field caching to derive directly from the table fields registry, eliminating a separate cache layer
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| gdb_schema_table_registry.go | New file implementing type-safe registry for schema metadata with concurrent-safe operations |
| gdb.go | Updated Core struct to use registry instead of innerMemCache, removed obsolete cache prefix constant |
| gdb_core.go | Refactored HasTable to use O(1) registry lookup, GetTablesWithCache to support schema parameter, removed GetInnerMemCache method |
| gdb_core_utility.go | Updated ClearTableFields and ClearTableFieldsAll to work with registry, simplified ClearCacheAll |
| gdb_driver_wrapper_db.go | Refactored TableFields to use registry's GetOrSet with double-checked locking, removed cache imports |
| gdb_func.go | Updated HasTable calls to pass schema parameter, removed obsolete cache key generation functions |
| gdb_model_utility.go | Updated HasTable call to include schema parameter for correct table existence checking |
| gdb_model_soft_time.go | Refactored to derive soft time fields directly from registry, removed separate cache layer and related structures |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
变更概述
本次PR对
database/gdb模块的schema元数据缓存机制进行了全面重构,用类型安全的tableRegistry替代原有的innerMemCache,解决了多个正确性、一致性、并发安全和性能问题。主要改进
🎯 核心重构
tableRegistry作为schema元数据的唯一存储源(group, schema, table)三维寻址,彻底消除跨schema缓存混淆sync.RWMutex + map的组合,提供类型安全的并发访问🔧 问题修复
正确性问题:
HasTable和GetTablesWithCache添加schema参数支持一致性问题:
ClearTableFields现在会清除表的所有相关缓存ClearTableFieldsAll现在真正清除所有schema元数据并发安全问题:
性能优化:
HasTable从O(n)线性扫描优化为O(1)哈希查找🏗️ 架构优化
接口变更
向后兼容的增强
内部实现优化
SetTableFields签名保持不变,生成代码无需调整GetInnerMemCache()方法迁移说明
本次变更对现有用户代码完全向后兼容,无需任何代码修改即可享受性能提升和问题修复。