File.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package core
  2. import (
  3. "TsDB2/DB/core/pool"
  4. "os"
  5. "unsafe"
  6. )
  7. // File 只负责:打开、关闭、读、写 —— unsafe 全部收敛
  8. type File struct {
  9. f *os.File
  10. bp *pool.BufferPool // 页面缓冲池 (nil = 未启用)
  11. }
  12. func OpenFile(path string) (*File, error) {
  13. f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
  14. if err != nil {
  15. return nil, err
  16. }
  17. return &File{f: f}, nil
  18. }
  19. func (ff *File) Close() error {
  20. if ff.bp != nil {
  21. ff.bp.Close() // 先刷盘再关文件
  22. }
  23. return ff.f.Close()
  24. }
  25. // InitBufferPool 初始化页面缓冲池, 拦截 ReadPage/WritePage 实现内存缓存
  26. // capacity: 最大缓存页数 (建议 10000~50000)
  27. func (ff *File) InitBufferPool(capacity int) {
  28. ff.bp = pool.NewBufferPool(capacity, ff.readPageDisk, ff.writePageDisk)
  29. }
  30. // SetDirtyThreshold 设置脏页阈值, 超过时触发回调 (配合 WAL 自动 Commit)
  31. func (ff *File) SetDirtyThreshold(threshold int, onThreshold func()) {
  32. if ff.bp != nil {
  33. ff.bp.SetDirtyThreshold(threshold, onThreshold)
  34. }
  35. }
  36. // CollectDirty 收集所有脏页快照并清除脏标记 (配合 WAL 使用)
  37. func (ff *File) CollectDirty() []pool.DirtyPage {
  38. if ff.bp != nil {
  39. return ff.bp.CollectDirty()
  40. }
  41. return nil
  42. }
  43. // FlushBufferPool 刷盘 (仅在 Close 时使用)
  44. func (ff *File) FlushBufferPool() {
  45. if ff.bp != nil {
  46. ff.bp.Flush()
  47. }
  48. }
  49. func (ff *File) WritePage16K(pageID uint32, buf *[PageSize]byte) error {
  50. // 头占一个4K,所以 pageID 从 1 开始
  51. offset := int64(PageSize4K) + int64(pageID-1)*int64(PageSize)
  52. _, err := ff.f.WriteAt(buf[:], offset)
  53. return err
  54. }
  55. // -----------------------------------------------------------------------------------
  56. // ✅ 动态长度:索引页 — 缓冲池拦截层
  57. // -----------------------------------------------------------------------------------
  58. func (ff *File) WritePage(pageID uint32, buf []byte) error {
  59. if ff.bp != nil {
  60. return ff.bp.WritePage(pageID, buf) // 写缓存, 延迟落盘
  61. }
  62. return ff.writePageDisk(pageID, buf)
  63. }
  64. func (ff *File) ReadPage(pageID uint32, buf []byte) error {
  65. if ff.bp != nil {
  66. return ff.bp.ReadPage(pageID, buf) // 先查缓存
  67. }
  68. return ff.readPageDisk(pageID, buf)
  69. }
  70. // WritePageDirect 直接写盘 (绕过 BufferPool, 用于 WAL 回写)
  71. func (ff *File) WritePageDirect(pageID uint32, buf []byte) error {
  72. return ff.writePageDisk(pageID, buf)
  73. }
  74. // SyncCache 同步缓存页 (WAL 提交后调用, 保持缓存与磁盘一致)
  75. func (ff *File) SyncCache(pageID uint32, data []byte) {
  76. if ff.bp != nil {
  77. ff.bp.UpdatePage(pageID, data)
  78. }
  79. }
  80. // ClearCache 清空所有缓存 (WAL 提交后调用)
  81. func (ff *File) ClearCache() {
  82. if ff.bp != nil {
  83. ff.bp.ClearAll()
  84. }
  85. }
  86. // -----------------------------------------------------------------------------------
  87. // ✅ 底层磁盘 I/O (缓冲池回源)
  88. // -----------------------------------------------------------------------------------
  89. func (ff *File) writePageDisk(pageID uint32, buf []byte) error {
  90. offset := int64(PageSize4K) + int64(pageID-1)*int64(len(buf))
  91. _, err := ff.f.WriteAt(buf, offset)
  92. return err
  93. }
  94. func (ff *File) readPageDisk(pageID uint32, buf []byte) error {
  95. offset := int64(PageSize4K) + int64(pageID-1)*int64(len(buf))
  96. _, err := ff.f.ReadAt(buf, offset)
  97. return err
  98. }
  99. // -----------------------------------------------------------------------------------
  100. // ✅ 头读写(固定4K)
  101. // -----------------------------------------------------------------------------------
  102. func (ff *File) WriteHeader(h *HeaderPage) error {
  103. var buf [PageSize4K]byte
  104. copy(buf[:], (*[PageSize4K]byte)(unsafe.Pointer(h))[:])
  105. _, err := ff.f.WriteAt(buf[:], 0)
  106. return err
  107. }
  108. func (ff *File) ReadHeader() (*HeaderPage, error) {
  109. buf := make([]byte, PageSize4K)
  110. _, err := ff.f.ReadAt(buf, 0)
  111. if err != nil {
  112. return nil, err
  113. }
  114. h := &HeaderPage{}
  115. copy((*[PageSize4K]byte)(unsafe.Pointer(h))[:], buf)
  116. return h, nil
  117. }
  118. func (ff *File) TruncateSize(size int64) error {
  119. return ff.f.Truncate(size)
  120. }