Skip to content

Commit

Permalink
Store only one global list of snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
outofforest committed Oct 2, 2024
1 parent f908ab2 commit faef9ee
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 53 deletions.
75 changes: 25 additions & 50 deletions snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func NewSnapshot(config SnapshotConfig) (Snapshot, error) {
}

singularityNode := *photon.FromBytes[SingularityNode](config.Allocator.Node(0))
if config.SnapshotID > 0 && singularityNode.SnapshotID < config.SnapshotID-1 {
if config.SnapshotID > 0 &&
(singularityNode.SnapshotRoot.State == stateFree || singularityNode.SnapshotID < config.SnapshotID-1) {
return Snapshot{}, errors.Errorf("snapshot %d does not exist", config.SnapshotID)
}

Expand All @@ -42,26 +43,25 @@ func NewSnapshot(config SnapshotConfig) (Snapshot, error) {
return Snapshot{}, err
}

var snapshotInfo SnapshotInfo
if singularityNode.SnapshotRoot.State != stateFree {
snapshots, err := NewSpace[uint64, SnapshotInfo](SpaceConfig[uint64, SnapshotInfo]{
SnapshotID: config.SnapshotID,
HashMod: &singularityNode.SnapshotRoot.HashMod,
SpaceRoot: ParentInfo{
State: lo.ToPtr(singularityNode.SnapshotRoot.State),
Item: lo.ToPtr[uint64](singularityNode.SnapshotRoot.Node),
},
PointerNodeAllocator: pointerNodeAllocator,
DataNodeAllocator: snapshotInfoNodeAllocator,
})
snapshots, err := NewSpace[uint64, SnapshotInfo](SpaceConfig[uint64, SnapshotInfo]{
SnapshotID: config.SnapshotID,
HashMod: &singularityNode.SnapshotRoot.HashMod,
SpaceRoot: ParentInfo{
State: lo.ToPtr(singularityNode.SnapshotRoot.State),
Item: lo.ToPtr[uint64](singularityNode.SnapshotRoot.Node),
},
PointerNodeAllocator: pointerNodeAllocator,
DataNodeAllocator: snapshotInfoNodeAllocator,
})

if err != nil {
return Snapshot{}, err
}
if err != nil {
return Snapshot{}, err
}

var snapshotInfo SnapshotInfo
if singularityNode.SnapshotRoot.State != stateFree {
snapshotID := config.SnapshotID
if singularityNode.SnapshotID == snapshotID-1 {
// FIXME (wojciech): In other cases snapshot should be read-only.
snapshotID = singularityNode.SnapshotID
}

Expand All @@ -72,21 +72,6 @@ func NewSnapshot(config SnapshotConfig) (Snapshot, error) {
}
}

snapshots, err := NewSpace[uint64, SnapshotInfo](SpaceConfig[uint64, SnapshotInfo]{
SnapshotID: config.SnapshotID,
HashMod: &snapshotInfo.SnapshotRoot.HashMod,
SpaceRoot: ParentInfo{
State: lo.ToPtr(snapshotInfo.SnapshotRoot.State),
Item: lo.ToPtr(snapshotInfo.SnapshotRoot.Node),
},
PointerNodeAllocator: pointerNodeAllocator,
DataNodeAllocator: snapshotInfoNodeAllocator,
})

if err != nil {
return Snapshot{}, err
}

spaceInfoNodeAllocator, err := NewNodeAllocator[DataItem[uint64, SpaceInfo]](config.Allocator)
if err != nil {
return Snapshot{}, err
Expand Down Expand Up @@ -157,32 +142,22 @@ func (s Snapshot) Commit() (Snapshot, error) {

clear(s.spacesToCommit)

snapshotInfo := SnapshotInfo{
s.snapshots.Set(s.config.SnapshotID, SnapshotInfo{
SnapshotID: s.config.SnapshotID,
SnapshotRoot: SpaceInfo{
HashMod: *s.snapshots.config.HashMod,
State: *s.snapshots.config.SpaceRoot.State,
Node: *s.snapshots.config.SpaceRoot.Item,
},
SpaceRoot: SpaceInfo{
HashMod: *s.spaces.config.HashMod,
State: *s.spaces.config.SpaceRoot.State,
Node: *s.spaces.config.SpaceRoot.Item,
},
}

// FIXME (wojciech): Chicken and egg issue
s.snapshots.Set(s.config.SnapshotID, snapshotInfo)
snapshotInfo.SnapshotRoot = SpaceInfo{
HashMod: *s.snapshots.config.HashMod,
State: *s.snapshots.config.SpaceRoot.State,
Node: *s.snapshots.config.SpaceRoot.Item,
}
s.snapshots.Set(s.config.SnapshotID, snapshotInfo)
})

*photon.FromBytes[SingularityNode](s.config.Allocator.Node(0)) = SingularityNode{
SnapshotID: snapshotInfo.SnapshotID,
SnapshotRoot: snapshotInfo.SnapshotRoot,
SnapshotID: s.config.SnapshotID,
SnapshotRoot: SpaceInfo{
HashMod: *s.snapshots.config.HashMod,
State: *s.snapshots.config.SpaceRoot.State,
Node: *s.snapshots.config.SpaceRoot.Item,
},
}

config := s.config
Expand Down
5 changes: 2 additions & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ type SpaceInfo struct {

// SnapshotInfo stores information required to retrieve snapshot.
type SnapshotInfo struct {
SnapshotID uint64
SnapshotRoot SpaceInfo
SpaceRoot SpaceInfo
SnapshotID uint64
SpaceRoot SpaceInfo
}

// SingularityNode is the root of the store.
Expand Down

0 comments on commit faef9ee

Please sign in to comment.