|
5 | 5 | "math" |
6 | 6 | "os" |
7 | 7 |
|
| 8 | + "github.com/codeharik/secretary/utils" |
8 | 9 | "github.com/dgraph-io/ristretto/v2" |
9 | 10 | ) |
10 | 11 |
|
@@ -59,15 +60,15 @@ func (tree *BTree) NewRecordPager(fileType string, level uint8) (*RecordPager, e |
59 | 60 |
|
60 | 61 | // Opens or creates a file and sets up the Pager |
61 | 62 | func NewPager[T PageItem[T]](tree *BTree, fileType string, level uint8) (*Pager[T], error) { |
62 | | - pageSize := int64(float64(tree.BaseSize) * math.Pow(float64(tree.Increment)/100, float64(level))) |
| 63 | + itemSize := int64(float64(tree.BaseSize) * math.Pow(float64(tree.Increment)/100, float64(level))) |
63 | 64 |
|
64 | 65 | var headerSize int64 = 0 |
65 | 66 |
|
66 | | - path := fmt.Sprintf("SECRETARY/%s/%s_%d_%d.bin", tree.CollectionName, fileType, level, pageSize) |
| 67 | + path := fmt.Sprintf("SECRETARY/%s/%s_%d_%d.bin", tree.CollectionName, fileType, level, itemSize) |
67 | 68 | if fileType == "index" { |
68 | 69 |
|
69 | 70 | headerSize = SECRETARY_HEADER_LENGTH |
70 | | - pageSize = 1024 * 1024 |
| 71 | + itemSize = int64(tree.nodeSize) |
71 | 72 |
|
72 | 73 | path = fmt.Sprintf("SECRETARY/%s/%s.bin", tree.CollectionName, fileType) |
73 | 74 | } |
@@ -104,7 +105,7 @@ func NewPager[T PageItem[T]](tree *BTree, fileType string, level uint8) (*Pager[ |
104 | 105 | file: file, |
105 | 106 | level: level, |
106 | 107 | headerSize: headerSize, |
107 | | - itemSize: pageSize, |
| 108 | + itemSize: itemSize, |
108 | 109 | dirtyPages: map[int64]bool{}, |
109 | 110 | } |
110 | 111 |
|
@@ -226,6 +227,17 @@ func (store *Pager[T]) WriteAt(data []byte, offset int64) error { |
226 | 227 |
|
227 | 228 | // ReadAt reads data from the specified offset in the file |
228 | 229 | func (store *Pager[T]) ReadAt(offset int64, size int32) ([]byte, error) { |
| 230 | + fileInfo, err := store.file.Stat() |
| 231 | + if err != nil { |
| 232 | + return nil, ErrorFileStat(err) |
| 233 | + } |
| 234 | + fileSize := fileInfo.Size() |
| 235 | + |
| 236 | + if offset+int64(size) > fileSize { |
| 237 | + utils.Log(store.itemSize) |
| 238 | + return nil, ErrorDataExceedPageSize(int(size), store.itemSize, offset) |
| 239 | + } |
| 240 | + |
229 | 241 | // Allocate a buffer to hold the data |
230 | 242 | data := make([]byte, size) |
231 | 243 |
|
|
0 commit comments