mirror of
https://github.com/octoleo/restic.git
synced 2025-01-27 17:18:34 +00:00
index: cleanup SaveIndex method
This commit is contained in:
parent
2ca1c37721
commit
57d69aa640
@ -1,6 +1,7 @@
|
|||||||
package index
|
package index
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -358,6 +359,24 @@ func (idx *Index) Encode(w io.Writer) error {
|
|||||||
return enc.Encode(idxJSON)
|
return enc.Encode(idxJSON)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SaveIndex saves an index in the repository.
|
||||||
|
func (idx *Index) SaveIndex(ctx context.Context, repo restic.SaverUnpacked) (restic.ID, error) {
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
|
err := idx.Encode(buf)
|
||||||
|
if err != nil {
|
||||||
|
return restic.ID{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := repo.SaveUnpacked(ctx, restic.IndexFile, buf.Bytes())
|
||||||
|
ierr := idx.SetID(id)
|
||||||
|
if ierr != nil {
|
||||||
|
// logic bug
|
||||||
|
panic(ierr)
|
||||||
|
}
|
||||||
|
return id, err
|
||||||
|
}
|
||||||
|
|
||||||
// Finalize sets the index to final.
|
// Finalize sets the index to final.
|
||||||
func (idx *Index) Finalize() {
|
func (idx *Index) Finalize() {
|
||||||
debug.Log("finalizing index")
|
debug.Log("finalizing index")
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package index
|
package index
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -461,7 +460,7 @@ func (mi *MasterIndex) Rewrite(ctx context.Context, repo restic.Unpacked, exclud
|
|||||||
worker := func() error {
|
worker := func() error {
|
||||||
for idx := range saveCh {
|
for idx := range saveCh {
|
||||||
idx.Finalize()
|
idx.Finalize()
|
||||||
if _, err := SaveIndex(wgCtx, repo, idx); err != nil {
|
if _, err := idx.SaveIndex(wgCtx, repo); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -551,7 +550,7 @@ func (mi *MasterIndex) SaveFallback(ctx context.Context, repo restic.SaverRemove
|
|||||||
worker := func() error {
|
worker := func() error {
|
||||||
for idx := range ch {
|
for idx := range ch {
|
||||||
idx.Finalize()
|
idx.Finalize()
|
||||||
if _, err := SaveIndex(wgCtx, repo, idx); err != nil {
|
if _, err := idx.SaveIndex(wgCtx, repo); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -572,30 +571,12 @@ func (mi *MasterIndex) SaveFallback(ctx context.Context, repo restic.SaverRemove
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveIndex saves an index in the repository.
|
|
||||||
func SaveIndex(ctx context.Context, repo restic.SaverUnpacked, index *Index) (restic.ID, error) {
|
|
||||||
buf := bytes.NewBuffer(nil)
|
|
||||||
|
|
||||||
err := index.Encode(buf)
|
|
||||||
if err != nil {
|
|
||||||
return restic.ID{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
id, err := repo.SaveUnpacked(ctx, restic.IndexFile, buf.Bytes())
|
|
||||||
ierr := index.SetID(id)
|
|
||||||
if ierr != nil {
|
|
||||||
// logic bug
|
|
||||||
panic(ierr)
|
|
||||||
}
|
|
||||||
return id, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// saveIndex saves all indexes in the backend.
|
// saveIndex saves all indexes in the backend.
|
||||||
func (mi *MasterIndex) saveIndex(ctx context.Context, r restic.SaverUnpacked, indexes ...*Index) error {
|
func (mi *MasterIndex) saveIndex(ctx context.Context, r restic.SaverUnpacked, indexes ...*Index) error {
|
||||||
for i, idx := range indexes {
|
for i, idx := range indexes {
|
||||||
debug.Log("Saving index %d", i)
|
debug.Log("Saving index %d", i)
|
||||||
|
|
||||||
sid, err := SaveIndex(ctx, r, idx)
|
sid, err := idx.SaveIndex(ctx, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -336,7 +336,7 @@ func benchmarkLoadIndex(b *testing.B, version uint) {
|
|||||||
}
|
}
|
||||||
idx.Finalize()
|
idx.Finalize()
|
||||||
|
|
||||||
id, err := index.SaveIndex(context.TODO(), repo, idx)
|
id, err := idx.SaveIndex(context.TODO(), repo)
|
||||||
rtest.OK(b, err)
|
rtest.OK(b, err)
|
||||||
|
|
||||||
b.Logf("index saved as %v", id.Str())
|
b.Logf("index saved as %v", id.Str())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user