github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/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 12:09:31</date> 10 //</624342605349523456> 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 func dbExport(ctx *cli.Context) { 29 args := ctx.Args() 30 if len(args) != 3 { 31 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") 32 } 33 34 store, err := openLDBStore(args[0], common.Hex2Bytes(args[2])) 35 if err != nil { 36 utils.Fatalf("error opening local chunk database: %s", err) 37 } 38 defer store.Close() 39 40 var out io.Writer 41 if args[1] == "-" { 42 out = os.Stdout 43 } else { 44 f, err := os.Create(args[1]) 45 if err != nil { 46 utils.Fatalf("error opening output file: %s", err) 47 } 48 defer f.Close() 49 out = f 50 } 51 52 count, err := store.Export(out) 53 if err != nil { 54 utils.Fatalf("error exporting local chunk database: %s", err) 55 } 56 57 log.Info(fmt.Sprintf("successfully exported %d chunks", count)) 58 } 59 60 func dbImport(ctx *cli.Context) { 61 args := ctx.Args() 62 if len(args) != 3 { 63 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") 64 } 65 66 store, err := openLDBStore(args[0], common.Hex2Bytes(args[2])) 67 if err != nil { 68 utils.Fatalf("error opening local chunk database: %s", err) 69 } 70 defer store.Close() 71 72 var in io.Reader 73 if args[1] == "-" { 74 in = os.Stdin 75 } else { 76 f, err := os.Open(args[1]) 77 if err != nil { 78 utils.Fatalf("error opening input file: %s", err) 79 } 80 defer f.Close() 81 in = f 82 } 83 84 count, err := store.Import(in) 85 if err != nil { 86 utils.Fatalf("error importing local chunk database: %s", err) 87 } 88 89 log.Info(fmt.Sprintf("successfully imported %d chunks", count)) 90 } 91 92 func dbClean(ctx *cli.Context) { 93 args := ctx.Args() 94 if len(args) != 2 { 95 utils.Fatalf("invalid arguments, please specify <chunkdb> (path to a local chunk database) and the base key") 96 } 97 98 store, err := openLDBStore(args[0], common.Hex2Bytes(args[1])) 99 if err != nil { 100 utils.Fatalf("error opening local chunk database: %s", err) 101 } 102 defer store.Close() 103 104 store.Cleanup() 105 } 106 107 func openLDBStore(path string, basekey []byte) (*storage.LDBStore, error) { 108 if _, err := os.Stat(filepath.Join(path, "CURRENT")); err != nil { 109 return nil, fmt.Errorf("invalid chunkdb path: %s", err) 110 } 111 112 storeparams := storage.NewDefaultStoreParams() 113 ldbparams := storage.NewLDBStoreParams(storeparams, path) 114 ldbparams.BaseKey = basekey 115 return storage.NewLDBStore(ldbparams) 116 } 117