jldb/mdb/pfile/alloclist.go

58 lines
1.3 KiB
Go
Raw Normal View History

2023-10-13 09:43:27 +00:00
package pfile
import "slices"
type allocList map[[2]uint64][]uint64
func newAllocList() *allocList {
al := allocList(map[[2]uint64][]uint64{})
return &al
}
func (al allocList) Create(collectionID, itemID, page uint64) {
key := al.key(collectionID, itemID)
al[key] = []uint64{page}
}
// Push is used to add pages to the storage when loading. It will append
// pages to the appropriate list, or return false if the list isn't found.
func (al allocList) Push(collectionID, itemID, page uint64) bool {
key := al.key(collectionID, itemID)
if _, ok := al[key]; !ok {
return false
}
al[key] = append(al[key], page)
return true
}
func (al allocList) Store(collectionID, itemID uint64, pages []uint64) {
key := al.key(collectionID, itemID)
al[key] = slices.Clone(pages)
}
func (al allocList) Remove(collectionID, itemID uint64) []uint64 {
key := al.key(collectionID, itemID)
pages := al[key]
delete(al, key)
return pages
}
func (al allocList) Iterate(
each func(collectionID, itemID uint64, pages []uint64) error,
) error {
for key, pages := range al {
if err := each(key[0], key[1], pages); err != nil {
return err
}
}
return nil
}
func (al allocList) Len() int {
return len(al)
}
func (al allocList) key(collectionID, itemID uint64) [2]uint64 {
return [2]uint64{collectionID, itemID}
}