The zfs backup tool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zfs_backup_tool/tree.go

98 lines
2.5 KiB

package main
import (
"errors"
"fmt"
)
/*
Frequency is a custom unit to represent the time intervals that snapshots are taken in.
*/
type frequency uint64
const yearly frequency = 0
const monthly frequency = 1
const weekly frequency = 2
const daily frequency = 3
const hourly frequency = 4
const frequent frequency = 5
/*
A snapshotTreeNode is a node in the tree representation of snapshots.
*/
type snapshotTreeNode struct {
Data Snapshot
Before []snapshotTreeNode
After []snapshotTreeNode
}
/*
addSnapshot adds a snapshot into a tree
*/
func (node *snapshotTreeNode) addSnapshot(s Snapshot) (err error) {
var tempNode snapshotTreeNode = snapshotTreeNode{Data: s}
if node == nil {
node = &tempNode
return nil
}
if node.frequency() > s.frequency() {
return fmt.Errorf("%s from a longer interval than %s", node.Data.String(), s.String())
}
if node.frequency() == s.frequency() {
return fmt.Errorf("%s is from the same interval as %s", node.Data.String(), s.String())
}
if node.frequency() < s.frequency() {
if node.frequency()+1 == s.frequency() {
if node.Data.TimeStamp.After(s.TimeStamp) {
if node.Before == nil {
node.Before = append(node.Before, tempNode)
return nil
}
var tSlice []snapshotTreeNode
for index := 0; index < len(node.Before); index++ {
if node.Before[index].Data.TimeStamp.Before(s.TimeStamp) {
tSlice = append(tSlice, tempNode)
copy(tSlice, node.Before[index:])
break
} else {
tSlice = append(tSlice, node.Before[index])
}
}
node.Before = tSlice
}
if node.Data.TimeStamp.Before(s.TimeStamp) {
if node.After == nil {
node.After = append(node.After, tempNode)
return nil
}
var tSlice []snapshotTreeNode
for index := 0; index < len(node.After); index++ {
if node.After[index].Data.TimeStamp.Before(s.TimeStamp) {
tSlice = append(tSlice, tempNode)
copy(tSlice, node.After[index:])
break
} else {
tSlice = append(tSlice, node.After[index])
}
}
node.After = tSlice
}
} else {
if node.Data.TimeStamp.After(s.TimeStamp) {
for index := 0; index < len(node.Before); index++ {
node.Before[index].Data.TimeStamp.Before(s.TimeStamp)
node.Before[index].addSnapshot(s)
break
}
}
}
}
return errors.New("Non implemented branch in func (node * snapshotTreeNode) addSnapshot(s Snapshot)")
}
/*
The frequency of this snapshot's interval in the tree?
*/
func (node snapshotTreeNode) frequency() frequency {
return node.Data.Interval
}