diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2015-01-08 15:03:43 -0700 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2015-01-08 15:03:55 -0700 |
commit | ea7ace2f62a387cafbaa5b839753305315076208 (patch) | |
tree | 5a9125cfe35c7c2e571a86d99ef0842c806ad225 /cmd/bolt/import.go | |
parent | README (diff) | |
download | dedo-ea7ace2f62a387cafbaa5b839753305315076208.tar.gz dedo-ea7ace2f62a387cafbaa5b839753305315076208.tar.xz |
Remove 'import' and 'export' CLI commands.
The import and export commands are a relic of early Bolt when the file
format was not stable. If the file format changed then users could export
their old data and import it into a new database with a new format.
The Bolt DB file format is stable and will not change so this command is no
longer needed.
Thanks to Alejandro Gaviria for pointing this out.
Diffstat (limited to 'cmd/bolt/import.go')
-rw-r--r-- | cmd/bolt/import.go | 108 |
1 files changed, 0 insertions, 108 deletions
diff --git a/cmd/bolt/import.go b/cmd/bolt/import.go deleted file mode 100644 index 258a7da..0000000 --- a/cmd/bolt/import.go +++ /dev/null @@ -1,108 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "os" - - "github.com/boltdb/bolt" -) - -// Import converts an exported database dump into a new database. -func Import(path string, input string) { - f, err := os.Open(input) - if err != nil { - fatal(err) - return - } - defer f.Close() - - // Read in entire dump. - var root []*rawMessage - if err := json.NewDecoder(f).Decode(&root); err != nil { - fatal(err) - } - - // Import all of the buckets. - importBuckets(path, root) -} - -func importBuckets(path string, root []*rawMessage) { - // Open the database. - db, err := bolt.Open(path, 0600, nil) - if err != nil { - fatal(err) - return - } - defer db.Close() - - // Insert entire dump into database. - err = db.Update(func(tx *bolt.Tx) error { - // Loop over every message and create a bucket. - for _, message := range root { - // Validate that root messages are buckets. - if message.Type != "bucket" { - return fmt.Errorf("invalid root type: %q", message.Type) - } - - // Create the bucket if it doesn't exist. - b, err := tx.CreateBucketIfNotExists(message.Key) - if err != nil { - return fmt.Errorf("create bucket: %s", err) - } - - // Decode child messages. - var children []*rawMessage - if err := json.Unmarshal(message.Value, &children); err != nil { - return fmt.Errorf("decode children: %s", err) - } - - // Import all the values into the bucket. - if err := importBucket(b, children); err != nil { - return fmt.Errorf("import bucket: %s", err) - } - } - return nil - }) - if err != nil { - fatal("update: ", err) - } -} - -func importBucket(b *bolt.Bucket, children []*rawMessage) error { - // Decode each message into a key/value pair. - for _, child := range children { - // Bucket messages are handled recursively. - if child.Type == "bucket" { - // Create the bucket if it doesn't exist. - subbucket, err := b.CreateBucketIfNotExists(child.Key) - if err != nil { - return fmt.Errorf("create bucket: %s", err) - } - - // Decode child messages. - var subchildren []*rawMessage - if err := json.Unmarshal(child.Value, &subchildren); err != nil { - return fmt.Errorf("decode children: %s", err) - } - - // Import subbucket. - if err := importBucket(subbucket, subchildren); err != nil { - return fmt.Errorf("import bucket: %s", err) - } - continue - } - - // Non-bucket values are decoded from base64. - var value []byte - if err := json.Unmarshal(child.Value, &value); err != nil { - return fmt.Errorf("decode value: %s", err) - } - - // Insert key/value into bucket. - if err := b.Put(child.Key, value); err != nil { - return fmt.Errorf("put: %s", err) - } - } - return nil -} |