This commit is contained in:
2026-05-21 23:38:52 +03:00
parent 793df29e11
commit e7ec6083cb
21 changed files with 1234 additions and 1017 deletions

View File

@@ -3,12 +3,12 @@ package atree
import (
"errors"
"fmt"
"hash/crc32"
"os"
"path/filepath"
"sync"
"gordenko.dev/dima/qb/bin"
"gordenko.dev/dima/qb/util"
)
const (
@@ -52,25 +52,12 @@ const (
FlagNewRoot byte = 2 // новая страница
)
var (
castagnoliTable = crc32.MakeTable(crc32.Castagnoli)
)
func calcChecksum(page []byte) uint32 {
return crc32.Checksum(page, castagnoliTable)
}
type PageToWrite struct {
PageNo uint32
Data []byte
IsReused bool
}
type FreeList interface {
// використовується в allocPage
ReservePage() uint32
}
type _page struct {
PageNo uint32
Buf []byte
@@ -80,7 +67,6 @@ type _page struct {
type Atree struct {
file *os.File
mutex sync.Mutex
freelist FreeList
allocatedPagesQty uint32
pages map[uint32]*_page
pageWaits map[uint32][]chan readResult
@@ -93,7 +79,6 @@ type Atree struct {
type Options struct {
Dir string
DatabaseName string
FreeList FreeList
}
func New(opt Options) (*Atree, error) {
@@ -106,9 +91,7 @@ func New(opt Options) (*Atree, error) {
if opt.DatabaseName == "" {
return nil, errors.New("DatabaseName option is required")
}
if opt.FreeList == nil {
return nil, errors.New("FreeList option is required")
}
// открываю или создаю dbName.data и dbName.index файлы
var (
fileName = filepath.Join(opt.Dir, opt.DatabaseName+".db")
@@ -136,7 +119,6 @@ func New(opt Options) (*Atree, error) {
}
tree := &Atree{
freelist: opt.FreeList,
file: file,
allocatedPagesQty: allocatedPagesQty,
pages: make(map[uint32]*_page),
@@ -261,7 +243,7 @@ type NotLinkedDataPage struct {
func (s NotLinkedDataPage) SetPrevPageNo(prevPageNo uint32) {
bin.PutUint32(s.Data[prevPageIdx:], prevPageNo)
bin.PutUint32(s.Data[crc32Idx:], calcChecksum(s.Data[:crc32Idx]))
bin.PutUint32(s.Data[crc32Idx:], util.CalcChecksum(s.Data[:crc32Idx]))
}
type AppendDataPagesReq struct {