Skip to content

fix(database/gdb): refactor gdb schema caching with type-safe tableRegistry#4711

Open
LanceAdd wants to merge 4 commits intogogf:masterfrom
LanceAdd:feature/20260224
Open

fix(database/gdb): refactor gdb schema caching with type-safe tableRegistry#4711
LanceAdd wants to merge 4 commits intogogf:masterfrom
LanceAdd:feature/20260224

Conversation

@LanceAdd
Copy link
Copy Markdown
Member

变更概述

本次PR对 database/gdb 模块的schema元数据缓存机制进行了全面重构,用类型安全的 tableRegistry 替代原有的 innerMemCache,解决了多个正确性、一致性、并发安全和性能问题。

主要改进

🎯 核心重构

  • 引入 tableRegistry 作为schema元数据的唯一存储源
  • 采用 (group, schema, table) 三维寻址,彻底消除跨schema缓存混淆
  • 使用 sync.RWMutex + map 的组合,提供类型安全的并发访问

🔧 问题修复

正确性问题:

  • ✅ 修复多schema场景下的表名缓存混淆
  • ✅ 为 HasTableGetTablesWithCache 添加schema参数支持
  • ✅ 解决不同group间同名表的软删除字段缓存污染
  • ✅ 修正字段映射时的表存在性判断错误

一致性问题:

  • ClearTableFields 现在会清除表的所有相关缓存
  • ClearTableFieldsAll 现在真正清除所有schema元数据
  • ✅ 表名缓存现在可以被主动清除

并发安全问题:

  • ✅ 软删除字段查询不再出现cold start并发穿透

性能优化:

  • HasTable 从O(n)线性扫描优化为O(1)哈希查找
  • ✅ 消除了全局写锁导致的性能瓶颈
  • ✅ 表名批量注册从O(N)次锁操作优化为O(1)次

🏗️ 架构优化

  • 消除了冗余的独立缓存(tablenames和软删除字段)
  • 统一了缓存清除逻辑,提供完整的"按表清除所有相关缓存"能力
  • 简化了代码路径,提高了可维护性

接口变更

向后兼容的增强

// 新增可选schema参数,现有调用无需修改
HasTable(name string, schema ...string) (bool, error)
GetTablesWithCache(schema ...string) ([]string, error)

内部实现优化

  • SetTableFields 签名保持不变,生成代码无需调整
  • 移除了已废弃的 GetInnerMemCache() 方法
  • 软删除字段查询直接从TableFields派生,无需独立缓存

迁移说明

本次变更对现有用户代码完全向后兼容,无需任何代码修改即可享受性能提升和问题修复。

- 将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 调用添加模式参数支持
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 tableRegistry with three-dimensional addressing (group, schema, table) to replace innerMemCache for schema metadata storage
  • Enhanced HasTable and GetTablesWithCache with 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants