File.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. // LockWrites 锁定写入(禁止 Insert/Update/Delete)
  37. func (ff *File) LockWrites() {
  38. if ff.bp != nil {
  39. ff.bp.LockWrites()
  40. }
  41. }
  42. // UnlockWrites 解锁写入(允许 Insert/Update/Delete)
  43. func (ff *File) UnlockWrites() {
  44. if ff.bp != nil {
  45. ff.bp.UnlockWrites()
  46. }
  47. }
  48. // SetBufferPoolCapacity 设置 BufferPool 容量(页数)
  49. func (ff *File) SetBufferPoolCapacity(capacity int) {
  50. if ff.bp != nil {
  51. ff.bp.SetCapacity(capacity)
  52. }
  53. }
  54. // CollectDirty 收集所有脏页快照并清除脏标记 (配合 WAL 使用)
  55. func (ff *File) CollectDirty() []pool.DirtyPage {
  56. if ff.bp != nil {
  57. return ff.bp.CollectDirty()
  58. }
  59. return nil
  60. }
  61. // FlushBufferPool 刷盘 (仅在 Close 时使用)
  62. func (ff *File) FlushBufferPool() {
  63. if ff.bp != nil {
  64. ff.bp.Flush()
  65. }
  66. }
  67. func (ff *File) WritePage16K(pageID uint32, buf *[PageSize]byte) error {
  68. // 头占一个4K,所以 pageID 从 1 开始
  69. offset := int64(PageSize4K) + int64(pageID-1)*int64(PageSize)
  70. _, err := ff.f.WriteAt(buf[:], offset)
  71. return err
  72. }
  73. // -----------------------------------------------------------------------------------
  74. // ✅ 动态长度:索引页 — 缓冲池拦截层
  75. // -----------------------------------------------------------------------------------
  76. func (ff *File) WritePage(pageID uint32, buf []byte) error {
  77. if ff.bp != nil {
  78. return ff.bp.WritePage(pageID, buf) // 写缓存, 延迟落盘
  79. }
  80. return ff.writePageDisk(pageID, buf)
  81. }
  82. func (ff *File) ReadPage(pageID uint32, buf []byte) error {
  83. if ff.bp != nil {
  84. return ff.bp.ReadPage(pageID, buf) // 先查缓存
  85. }
  86. return ff.readPageDisk(pageID, buf)
  87. }
  88. // WritePageDirect 直接写盘 (绕过 BufferPool, 用于 WAL 回写)
  89. func (ff *File) WritePageDirect(pageID uint32, buf []byte) error {
  90. return ff.writePageDisk(pageID, buf)
  91. }
  92. // SyncCache 同步缓存页 (WAL 提交后调用, 保持缓存与磁盘一致)
  93. func (ff *File) SyncCache(pageID uint32, data []byte) {
  94. if ff.bp != nil {
  95. ff.bp.UpdatePage(pageID, data)
  96. }
  97. }
  98. // ClearCache 清空所有缓存 (WAL 提交后调用)
  99. func (ff *File) ClearCache() {
  100. if ff.bp != nil {
  101. ff.bp.ClearAll()
  102. }
  103. }
  104. // -----------------------------------------------------------------------------------
  105. // ✅ 底层磁盘 I/O (缓冲池回源)
  106. // -----------------------------------------------------------------------------------
  107. func (ff *File) writePageDisk(pageID uint32, buf []byte) error {
  108. offset := int64(PageSize4K) + int64(pageID-1)*int64(len(buf))
  109. _, err := ff.f.WriteAt(buf, offset)
  110. return err
  111. }
  112. func (ff *File) readPageDisk(pageID uint32, buf []byte) error {
  113. offset := int64(PageSize4K) + int64(pageID-1)*int64(len(buf))
  114. _, err := ff.f.ReadAt(buf, offset)
  115. return err
  116. }
  117. // -----------------------------------------------------------------------------------
  118. // ✅ 头读写(固定4K)
  119. // -----------------------------------------------------------------------------------
  120. func (ff *File) WriteHeader(h *HeaderPage) error {
  121. var buf [PageSize4K]byte
  122. copy(buf[:], (*[PageSize4K]byte)(unsafe.Pointer(h))[:])
  123. _, err := ff.f.WriteAt(buf[:], 0)
  124. return err
  125. }
  126. func (ff *File) ReadHeader() (*HeaderPage, error) {
  127. buf := make([]byte, PageSize4K)
  128. _, err := ff.f.ReadAt(buf, 0)
  129. if err != nil {
  130. return nil, err
  131. }
  132. h := &HeaderPage{}
  133. copy((*[PageSize4K]byte)(unsafe.Pointer(h))[:], buf)
  134. return h, nil
  135. }
  136. func (ff *File) TruncateSize(size int64) error {
  137. return ff.f.Truncate(size)
  138. }