Skip to content

Commit

Permalink
Move code around and drop not needed pieces (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
outofforest authored Dec 12, 2024
1 parent 6f40c10 commit ed97cd7
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 139 deletions.
2 changes: 1 addition & 1 deletion alloc/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (s *State) VolatileSize() uint64 {

// Node returns node bytes.
func (s *State) Node(nodeAddress types.VolatileAddress) unsafe.Pointer {
return unsafe.Add(s.dataP, nodeAddress*types.NodeLength)
return unsafe.Add(s.dataP, nodeAddress.Naked()*types.NodeLength)
}

// Bytes returns byte slice of a node.
Expand Down
6 changes: 0 additions & 6 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,13 @@ func (db *DB) deleteSnapshot(
},
)

//nolint:nestif
if nextSnapshotInfo.DeallocationRoot.VolatileAddress != types.FreeAddress {
var err error
for nextDeallocSnapshot := range space.IteratorAndDeallocator(
nextSnapshotInfo.DeallocationRoot,
db.config.State,
deallocationNodeAssistant,
volatileDeallocator,
persistentDeallocator,
&err,
) {
if nextDeallocSnapshot.Key.SnapshotID > snapshotInfo.PreviousSnapshotID &&
nextDeallocSnapshot.Key.SnapshotID <= snapshotID {
Expand All @@ -287,9 +284,6 @@ func (db *DB) deleteSnapshot(
return err
}
}
if err != nil {
return err
}
}

nextSnapshotInfo.DeallocationRoot = snapshotInfo.DeallocationRoot
Expand Down
25 changes: 25 additions & 0 deletions space/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package space

import (
"github.com/outofforest/quantum/types"
)

const (
// flagPointerNode says that this is pointer node.
flagPointerNode = types.FlagNaked + 1

// flagHashMod says that key hash must be recalculated.
flagHashMod = flagPointerNode << 1
)

func isFree(address types.VolatileAddress) bool {
return address == types.FreeAddress
}

func isPointer(address types.VolatileAddress) bool {
return address.IsSet(flagPointerNode)
}

func isData(address types.VolatileAddress) bool {
return !isFree(address) && !isPointer(address)
}
27 changes: 9 additions & 18 deletions space/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ import (
"github.com/outofforest/quantum/types"
)

// DataItem stores single key-value pair.
type DataItem[K, V comparable] struct {
Key K
Value V
}

// NewDataNodeAssistant creates new space data node assistant.
func NewDataNodeAssistant[K, V comparable]() (*DataNodeAssistant[K, V], error) {
itemSize := uint64(unsafe.Sizeof(types.DataItem[K, V]{})+types.UInt64Length-1) /
itemSize := uint64(unsafe.Sizeof(DataItem[K, V]{})+types.UInt64Length-1) /
types.UInt64Length * types.UInt64Length

numOfItems := types.NodeLength / (itemSize + types.UInt64Length) // Uint64Length is for key hash.
Expand Down Expand Up @@ -45,8 +51,8 @@ func (na *DataNodeAssistant[K, V]) ItemOffset(index uint64) uint64 {
}

// Item maps the memory address given by the node address and offset to an item.
func (na *DataNodeAssistant[K, V]) Item(n unsafe.Pointer, offset uint64) *types.DataItem[K, V] {
return (*types.DataItem[K, V])(unsafe.Add(n, offset))
func (na *DataNodeAssistant[K, V]) Item(n unsafe.Pointer, offset uint64) *DataItem[K, V] {
return (*DataItem[K, V])(unsafe.Add(n, offset))
}

// KeyHashes returns slice of key hashes stored in the node.
Expand All @@ -55,18 +61,3 @@ func (na *DataNodeAssistant[K, V]) KeyHashes(n unsafe.Pointer) []types.KeyHash {
// alignment to compare them.
return unsafe.Slice((*types.KeyHash)(n), na.numOfItems)
}

// Iterator iterates over items.
func (na *DataNodeAssistant[K, V]) Iterator(n unsafe.Pointer) func(func(uint64, *types.DataItem[K, V]) bool) {
return func(yield func(uint64, *types.DataItem[K, V]) bool) {
keyHashP := n
itemP := unsafe.Add(n, na.itemOffset)
for i := range na.numOfItems {
if *(*types.KeyHash)(keyHashP) != 0 && !yield(i, (*types.DataItem[K, V])(itemP)) {
return
}
keyHashP = unsafe.Add(keyHashP, types.UInt64Length)
itemP = unsafe.Add(itemP, na.itemSize)
}
}
}
Loading

0 comments on commit ed97cd7

Please sign in to comment.