github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/cmd/swarm/db.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:33</date> 10 //</624450070623817728> 11 12 13 package main 14 15 import ( 16 "fmt" 17 "io" 18 "os" 19 "path/filepath" 20 21 "github.com/ethereum/go-ethereum/cmd/utils" 22 "github.com/ethereum/go-ethereum/common" 23 "github.com/ethereum/go-ethereum/log" 24 "github.com/ethereum/go-ethereum/swarm/storage" 25 "gopkg.in/urfave/cli.v1" 26 ) 27 28 var dbCommand = cli.Command{ 29 Name: "db", 30 CustomHelpTemplate: helpTemplate, 31 Usage: "manage the local chunk database", 32 ArgsUsage: "db COMMAND", 33 Description: "Manage the local chunk database", 34 Subcommands: []cli.Command{ 35 { 36 Action: dbExport, 37 CustomHelpTemplate: helpTemplate, 38 Name: "export", 39 Usage: "export a local chunk database as a tar archive (use - to send to stdout)", 40 ArgsUsage: "<chunkdb> <file>", 41 Description: ` 42 Export a local chunk database as a tar archive (use - to send to stdout). 43 44 swarm db export ~/.ethereum/swarm/bzz-KEY/chunks chunks.tar 45 46 The export may be quite large, consider piping the output through the Unix 47 pv(1) tool to get a progress bar: 48 49 swarm db export ~/.ethereum/swarm/bzz-KEY/chunks - | pv > chunks.tar 50 `, 51 }, 52 { 53 Action: dbImport, 54 CustomHelpTemplate: helpTemplate, 55 Name: "import", 56 Usage: "import chunks from a tar archive into a local chunk database (use - to read from stdin)", 57 ArgsUsage: "<chunkdb> <file>", 58 Description: `Import chunks from a tar archive into a local chunk database (use - to read from stdin). 59 60 swarm db import ~/.ethereum/swarm/bzz-KEY/chunks chunks.tar 61 62 The import may be quite large, consider piping the input through the Unix 63 pv(1) tool to get a progress bar: 64 65 pv chunks.tar | swarm db import ~/.ethereum/swarm/bzz-KEY/chunks -`, 66 }, 67 }, 68 } 69 70 func dbExport(ctx *cli.Context) { 71 args := ctx.Args() 72 if len(args) != 3 { 73 utils.Fatalf("invalid arguments, please specify both <chunkdb> (path to a local chunk database), <file> (path to write the tar archive to, - for stdout) and the base key") 74 } 75 76 store, err := openLDBStore(args[0], common.Hex2Bytes(args[2])) 77 if err != nil { 78 utils.Fatalf("error opening local chunk database: %s", err) 79 } 80 defer store.Close() 81 82 var out io.Writer 83 if args[1] == "-" { 84 out = os.Stdout 85 } else { 86 f, err := os.Create(args[1]) 87 if err != nil { 88 utils.Fatalf("error opening output file: %s", err) 89 } 90 defer f.Close() 91 out = f 92 } 93 94 count, err := store.Export(out) 95 if err != nil { 96 utils.Fatalf("error exporting local chunk database: %s", err) 97 } 98 99 log.Info(fmt.Sprintf("successfully exported %d chunks", count)) 100 } 101 102 func dbImport(ctx *cli.Context) { 103 args := ctx.Args() 104 if len(args) != 3 { 105 utils.Fatalf("invalid arguments, please specify both <chunkdb> (path to a local chunk database), <file> (path to read the tar archive from, - for stdin) and the base key") 106 } 107 108 store, err := openLDBStore(args[0], common.Hex2Bytes(args[2])) 109 if err != nil { 110 utils.Fatalf("error opening local chunk database: %s", err) 111 } 112 defer store.Close() 113 114 var in io.Reader 115 if args[1] == "-" { 116 in = os.Stdin 117 } else { 118 f, err := os.Open(args[1]) 119 if err != nil { 120 utils.Fatalf("error opening input file: %s", err) 121 } 122 defer f.Close() 123 in = f 124 } 125 126 count, err := store.Import(in) 127 if err != nil { 128 utils.Fatalf("error importing local chunk database: %s", err) 129 } 130 131 log.Info(fmt.Sprintf("successfully imported %d chunks", count)) 132 } 133 134 func openLDBStore(path string, basekey []byte) (*storage.LDBStore, error) { 135 if _, err := os.Stat(filepath.Join(path, "CURRENT")); err != nil { 136 return nil, fmt.Errorf("invalid chunkdb path: %s", err) 137 } 138 139 storeparams := storage.NewDefaultStoreParams() 140 ldbparams := storage.NewLDBStoreParams(storeparams, path) 141 ldbparams.BaseKey = basekey 142 return storage.NewLDBStore(ldbparams) 143 } 144