Merge pull request #59 from tonistiigi/metadata-rawmessage
metadata: switch value encoding to json.RawMessagedocker-18.09
commit
9a862b9a28
|
@ -261,8 +261,8 @@ func (s *StorageItem) SetValue(b *bolt.Bucket, key string, v Value) error {
|
|||
}
|
||||
|
||||
type Value struct {
|
||||
Data []byte
|
||||
Index string
|
||||
Value json.RawMessage `json:"value,omitempty"`
|
||||
Index string `json:"index,omitempty"`
|
||||
// External bool
|
||||
}
|
||||
|
||||
|
@ -271,11 +271,11 @@ func NewValue(v interface{}) (*Value, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Value{Data: dt}, nil
|
||||
return &Value{Value: json.RawMessage(dt)}, nil
|
||||
}
|
||||
|
||||
func (v *Value) Unmarshal(target interface{}) error {
|
||||
err := json.Unmarshal(v.Data, target)
|
||||
err := json.Unmarshal(v.Value, target)
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -11,6 +11,5 @@ var debugCommand = cli.Command{
|
|||
Subcommands: []cli.Command{
|
||||
debug.DumpLLBCommand,
|
||||
debug.DumpMetadataCommand,
|
||||
debug.DumpBoltCommand,
|
||||
},
|
||||
}
|
||||
|
|
|
@ -1,56 +0,0 @@
|
|||
package debug
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/boltdb/bolt"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var DumpBoltCommand = cli.Command{
|
||||
Name: "dump-bolt",
|
||||
Usage: "dump an arbitrary bolt db file in human-readable format. This command does not need the daemon to be running.",
|
||||
// unlike dumpLLB(), dumpBolt() does not support reading from stdin
|
||||
ArgsUsage: "<dbfile>",
|
||||
Action: func(clicontext *cli.Context) error {
|
||||
dbFile := clicontext.Args().First()
|
||||
return dumpBolt(dbFile, func(k, v []byte) string {
|
||||
return fmt.Sprintf("%q: %q", string(k), string(v))
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
func dumpBolt(dbFile string, stringifier func(k, v []byte) string) error {
|
||||
if dbFile == "" {
|
||||
return errors.New("dbfile not specified")
|
||||
}
|
||||
if dbFile == "-" {
|
||||
// user could still specify "/dev/stdin" but unlikely to work
|
||||
return errors.New("stdin unsupported")
|
||||
}
|
||||
db, err := bolt.Open(dbFile, 0400, &bolt.Options{ReadOnly: true, Timeout: 3 * time.Second})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
return db.View(func(tx *bolt.Tx) error {
|
||||
// TODO: JSON format?
|
||||
return tx.ForEach(func(name []byte, b *bolt.Bucket) error {
|
||||
return dumpBucket(name, b, "", stringifier)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func dumpBucket(name []byte, b *bolt.Bucket, indent string, stringifier func(k, v []byte) string) error {
|
||||
fmt.Printf("%sbucket %q:\n", indent, string(name))
|
||||
childrenIndent := indent + " "
|
||||
return b.ForEach(func(k, v []byte) error {
|
||||
if bb := b.Bucket(k); bb != nil {
|
||||
return dumpBucket(k, bb, childrenIndent, stringifier)
|
||||
}
|
||||
fmt.Printf("%s%s\n", childrenIndent, stringifier(k, v))
|
||||
return nil
|
||||
})
|
||||
}
|
|
@ -1,11 +1,12 @@
|
|||
package debug
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/moby/buildkit/cache/metadata"
|
||||
"github.com/boltdb/bolt"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
|
@ -22,15 +23,40 @@ var DumpMetadataCommand = cli.Command{
|
|||
Action: func(clicontext *cli.Context) error {
|
||||
dbFile := filepath.Join(clicontext.String("root"), "metadata.db")
|
||||
return dumpBolt(dbFile, func(k, v []byte) string {
|
||||
raw := fmt.Sprintf("%q: %q", string(k), string(v))
|
||||
if len(v) == 0 {
|
||||
return raw
|
||||
}
|
||||
var mv metadata.Value
|
||||
if err := json.Unmarshal(v, &mv); err != nil {
|
||||
return raw
|
||||
}
|
||||
return fmt.Sprintf("%q: {\"Data\":%q, \"Index\":%q}", string(k), string(mv.Data), mv.Index)
|
||||
return fmt.Sprintf("%q: %s", string(k), string(v))
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
func dumpBolt(dbFile string, stringifier func(k, v []byte) string) error {
|
||||
if dbFile == "" {
|
||||
return errors.New("dbfile not specified")
|
||||
}
|
||||
if dbFile == "-" {
|
||||
// user could still specify "/dev/stdin" but unlikely to work
|
||||
return errors.New("stdin unsupported")
|
||||
}
|
||||
db, err := bolt.Open(dbFile, 0400, &bolt.Options{ReadOnly: true, Timeout: 3 * time.Second})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
return db.View(func(tx *bolt.Tx) error {
|
||||
// TODO: JSON format?
|
||||
return tx.ForEach(func(name []byte, b *bolt.Bucket) error {
|
||||
return dumpBucket(name, b, "", stringifier)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func dumpBucket(name []byte, b *bolt.Bucket, indent string, stringifier func(k, v []byte) string) error {
|
||||
fmt.Printf("%sbucket %q:\n", indent, string(name))
|
||||
childrenIndent := indent + " "
|
||||
return b.ForEach(func(k, v []byte) error {
|
||||
if bb := b.Bucket(k); bb != nil {
|
||||
return dumpBucket(k, bb, childrenIndent, stringifier)
|
||||
}
|
||||
fmt.Printf("%s%s\n", childrenIndent, stringifier(k, v))
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue