forked from TrueCloudLab/restic
Add index.Dump()
This commit is contained in:
parent
f773feeb04
commit
65c3aead3e
1 changed files with 59 additions and 12 deletions
|
@ -173,28 +173,24 @@ type blobJSON struct {
|
||||||
Length uint `json:"length"`
|
Length uint `json:"length"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encode writes the JSON serialization of the index to the writer w. This
|
// generatePackList returns a list of packs containing only the index entries
|
||||||
// serialization only contains new blobs added via idx.Store(), not old ones
|
// that selsectFn returned true for. If selectFn is nil, the list contains all
|
||||||
// introduced via DecodeIndex().
|
// blobs in the index.
|
||||||
func (idx *Index) Encode(w io.Writer) error {
|
func (idx *Index) generatePackList(selectFn func(indexEntry) bool) ([]*packJSON, error) {
|
||||||
debug.Log("Index.Encode", "encoding index")
|
|
||||||
idx.m.Lock()
|
|
||||||
defer idx.m.Unlock()
|
|
||||||
|
|
||||||
list := []*packJSON{}
|
list := []*packJSON{}
|
||||||
packs := make(map[string]*packJSON)
|
packs := make(map[string]*packJSON)
|
||||||
|
|
||||||
for id, blob := range idx.pack {
|
for id, blob := range idx.pack {
|
||||||
if blob.old {
|
if selectFn != nil && !selectFn(blob) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
debug.Log("Index.Encode", "handle blob %q", id[:8])
|
debug.Log("Index.generatePackList", "handle blob %q", id[:8])
|
||||||
|
|
||||||
if blob.packID == nil {
|
if blob.packID == nil {
|
||||||
debug.Log("Index.Encode", "blob %q has no packID! (type %v, offset %v, length %v)",
|
debug.Log("Index.generatePackList", "blob %q has no packID! (type %v, offset %v, length %v)",
|
||||||
id[:8], blob.tpe, blob.offset, blob.length)
|
id[:8], blob.tpe, blob.offset, blob.length)
|
||||||
return fmt.Errorf("unable to serialize index: pack for blob %v hasn't been written yet", id)
|
return nil, fmt.Errorf("unable to serialize index: pack for blob %v hasn't been written yet", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// see if pack is already in map
|
// see if pack is already in map
|
||||||
|
@ -217,12 +213,63 @@ func (idx *Index) Encode(w io.Writer) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
debug.Log("Index.generatePackList", "done")
|
||||||
|
|
||||||
|
return list, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// encode writes the JSON serialization of the index filtered by selectFn to enc.
|
||||||
|
func (idx *Index) encode(w io.Writer, selectFn func(indexEntry) bool) error {
|
||||||
|
list, err := idx.generatePackList(func(entry indexEntry) bool {
|
||||||
|
return !entry.old
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
debug.Log("Index.Encode", "done")
|
debug.Log("Index.Encode", "done")
|
||||||
|
|
||||||
enc := json.NewEncoder(w)
|
enc := json.NewEncoder(w)
|
||||||
return enc.Encode(list)
|
return enc.Encode(list)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Encode writes the JSON serialization of the index to the writer w. This
|
||||||
|
// serialization only contains new blobs added via idx.Store(), not old ones
|
||||||
|
// introduced via DecodeIndex().
|
||||||
|
func (idx *Index) Encode(w io.Writer) error {
|
||||||
|
debug.Log("Index.Encode", "encoding index")
|
||||||
|
idx.m.Lock()
|
||||||
|
defer idx.m.Unlock()
|
||||||
|
|
||||||
|
return idx.encode(w, func(e indexEntry) bool { return !e.old })
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dump writes the pretty-printed JSON representation of the index to w.
|
||||||
|
func (idx *Index) Dump(w io.Writer) error {
|
||||||
|
debug.Log("Index.Dump", "dumping index")
|
||||||
|
idx.m.Lock()
|
||||||
|
defer idx.m.Unlock()
|
||||||
|
|
||||||
|
list, err := idx.generatePackList(nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
buf, err := json.MarshalIndent(list, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = w.Write(append(buf, '\n'))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
debug.Log("Index.Dump", "done")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// DecodeIndex loads and unserializes an index from rd.
|
// DecodeIndex loads and unserializes an index from rd.
|
||||||
func DecodeIndex(rd io.Reader) (*Index, error) {
|
func DecodeIndex(rd io.Reader) (*Index, error) {
|
||||||
debug.Log("Index.DecodeIndex", "Start decoding index")
|
debug.Log("Index.DecodeIndex", "Start decoding index")
|
||||||
|
|
Loading…
Reference in a new issue