Do not discard orphan volumes

This commit is contained in:
Alex Yatskov 2023-11-02 18:03:10 -07:00
parent 9adeac0a37
commit 7ef4853c40

View File

@ -75,7 +75,7 @@ type Page struct {
} }
func (self *Page) export(dir string, config ExportConfig) error { func (self *Page) export(dir string, config ExportConfig) error {
name, err := buildTemplatedName(config.PageTemplate, self.Node.Name, self.Index+1, len(self.Volume.Pages)) name, err := buildTemplatedName(config.PageTemplate, self.Node.Name, self.Index+1, len(self.Volume.Pages)-1)
if err != nil { if err != nil {
return err return err
} }
@ -108,7 +108,7 @@ func (self *Volume) AveragePageSize() int {
} }
func (self *Volume) export(path string, config ExportConfig, allocator *TempDirAllocator) error { func (self *Volume) export(path string, config ExportConfig, allocator *TempDirAllocator) error {
name, err := buildTemplatedName(config.VolumeTemplate, stripExt(self.Node.Name), self.Index, self.Book.MaxVolume) name, err := buildTemplatedName(config.VolumeTemplate, stripExt(self.Node.Name), self.Index, self.Book.VolumeCount-1)
if err != nil { if err != nil {
return err return err
} }
@ -162,7 +162,8 @@ func (self *Volume) supercedes(other *Volume) bool {
type Book struct { type Book struct {
Node *Node Node *Node
Volumes map[int]*Volume Volumes map[int]*Volume
MaxVolume int VolumeCount int
orphans []*Volume
} }
func (self *Book) Export(path string, config ExportConfig, allocator *TempDirAllocator) error { func (self *Book) Export(path string, config ExportConfig, allocator *TempDirAllocator) error {
@ -231,10 +232,26 @@ func (self *Book) Export(path string, config ExportConfig, allocator *TempDirAll
} }
func (self *Book) addVolume(volume *Volume) { func (self *Book) addVolume(volume *Volume) {
currVolume, _ := self.Volumes[volume.Index] insert := func(v *Volume) {
if currVolume == nil || volume.supercedes(currVolume) { self.Volumes[v.Index] = v
self.Volumes[volume.Index] = volume if v.Index >= self.VolumeCount {
self.VolumeCount = v.Index + 1
} }
}
currVolume, _ := self.Volumes[volume.Index]
if currVolume == nil {
insert(volume)
} else if volume.supercedes(currVolume) {
self.addOrphan(currVolume)
insert(volume)
} else {
self.addOrphan(volume)
}
}
func (self *Book) addOrphan(volume *Volume) {
self.orphans = append(self.orphans, volume)
} }
func (self *Book) parseVolumes(node *Node) { func (self *Book) parseVolumes(node *Node) {
@ -266,11 +283,9 @@ func (self *Book) parseVolumes(node *Node) {
} }
volume.Index = int(index) volume.Index = int(index)
if volume.Index > self.MaxVolume {
self.MaxVolume = volume.Index
}
self.addVolume(volume) self.addVolume(volume)
} else {
self.addOrphan(volume)
} }
} }
} }
@ -283,6 +298,15 @@ func ParseBook(node *Node) (*Book, error) {
book.parseVolumes(node) book.parseVolumes(node)
if len(book.orphans) > 0 {
for _, volume := range book.orphans {
volume.Index = book.VolumeCount
book.addVolume(volume)
}
book.orphans = nil
}
if len(book.Volumes) == 0 { if len(book.Volumes) == 0 {
return nil, errors.New("no volumes found") return nil, errors.New("no volumes found")
} }