package skl
import (
"sync/atomic"
"unsafe"
"github.com/dgraph-io/badger/v4/y"
)
const (
offsetSize = int (unsafe .Sizeof (uint32 (0 )))
nodeAlign = int (unsafe .Sizeof (uint64 (0 ))) - 1
)
type Arena struct {
n atomic .Uint32
buf []byte
}
func newArena(n int64 ) *Arena {
out := &Arena {buf : make ([]byte , n )}
out .n .Store (1 )
return out
}
func (s *Arena ) size () int64 {
return int64 (s .n .Load ())
}
func (s *Arena ) putNode (height int ) uint32 {
unusedSize := (maxHeight - height ) * offsetSize
l := uint32 (MaxNodeSize - unusedSize + nodeAlign )
n := s .n .Add (l )
y .AssertTruef (int (n ) <= len (s .buf ),
"Arena too small, toWrite:%d newTotal:%d limit:%d" ,
l , n , len (s .buf ))
m := (n - l + uint32 (nodeAlign )) & ^uint32 (nodeAlign )
return m
}
func (s *Arena ) putVal (v y .ValueStruct ) uint32 {
l := v .EncodedSize ()
n := s .n .Add (l )
y .AssertTruef (int (n ) <= len (s .buf ),
"Arena too small, toWrite:%d newTotal:%d limit:%d" ,
l , n , len (s .buf ))
m := n - l
v .Encode (s .buf [m :])
return m
}
func (s *Arena ) putKey (key []byte ) uint32 {
l := uint32 (len (key ))
n := s .n .Add (l )
y .AssertTruef (int (n ) <= len (s .buf ),
"Arena too small, toWrite:%d newTotal:%d limit:%d" ,
l , n , len (s .buf ))
m := n - l
y .AssertTrue (len (key ) == copy (s .buf [m :n ], key ))
return m
}
func (s *Arena ) getNode (offset uint32 ) *node {
if offset == 0 {
return nil
}
return (*node )(unsafe .Pointer (&s .buf [offset ]))
}
func (s *Arena ) getKey (offset uint32 , size uint16 ) []byte {
return s .buf [offset : offset +uint32 (size )]
}
func (s *Arena ) getVal (offset uint32 , size uint32 ) (ret y .ValueStruct ) {
ret .Decode (s .buf [offset : offset +size ])
return
}
func (s *Arena ) getNodeOffset (nd *node ) uint32 {
if nd == nil {
return 0
}
return uint32 (uintptr (unsafe .Pointer (nd )) - uintptr (unsafe .Pointer (&s .buf [0 ])))
}
The pages are generated with Golds v0.8.4 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .