github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/cmd/swarm/db.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package main 13 14 import ( 15 "fmt" 16 "io" 17 "os" 18 "path/filepath" 19 20 "github.com/Sberex/go-sberex/cmd/utils" 21 "github.com/Sberex/go-sberex/log" 22 "github.com/Sberex/go-sberex/swarm/storage" 23 "gopkg.in/urfave/cli.v1" 24 ) 25 26 func dbExport(ctx *cli.Context) { 27 args := ctx.Args() 28 if len(args) != 2 { 29 utils.Fatalf("invalid arguments, please specify both <chunkdb> (path to a local chunk database) and <file> (path to write the tar archive to, - for stdout)") 30 } 31 32 store, err := openDbStore(args[0]) 33 if err != nil { 34 utils.Fatalf("error opening local chunk database: %s", err) 35 } 36 defer store.Close() 37 38 var out io.Writer 39 if args[1] == "-" { 40 out = os.Stdout 41 } else { 42 f, err := os.Create(args[1]) 43 if err != nil { 44 utils.Fatalf("error opening output file: %s", err) 45 } 46 defer f.Close() 47 out = f 48 } 49 50 count, err := store.Export(out) 51 if err != nil { 52 utils.Fatalf("error exporting local chunk database: %s", err) 53 } 54 55 log.Info(fmt.Sprintf("successfully exported %d chunks", count)) 56 } 57 58 func dbImport(ctx *cli.Context) { 59 args := ctx.Args() 60 if len(args) != 2 { 61 utils.Fatalf("invalid arguments, please specify both <chunkdb> (path to a local chunk database) and <file> (path to read the tar archive from, - for stdin)") 62 } 63 64 store, err := openDbStore(args[0]) 65 if err != nil { 66 utils.Fatalf("error opening local chunk database: %s", err) 67 } 68 defer store.Close() 69 70 var in io.Reader 71 if args[1] == "-" { 72 in = os.Stdin 73 } else { 74 f, err := os.Open(args[1]) 75 if err != nil { 76 utils.Fatalf("error opening input file: %s", err) 77 } 78 defer f.Close() 79 in = f 80 } 81 82 count, err := store.Import(in) 83 if err != nil { 84 utils.Fatalf("error importing local chunk database: %s", err) 85 } 86 87 log.Info(fmt.Sprintf("successfully imported %d chunks", count)) 88 } 89 90 func dbClean(ctx *cli.Context) { 91 args := ctx.Args() 92 if len(args) != 1 { 93 utils.Fatalf("invalid arguments, please specify <chunkdb> (path to a local chunk database)") 94 } 95 96 store, err := openDbStore(args[0]) 97 if err != nil { 98 utils.Fatalf("error opening local chunk database: %s", err) 99 } 100 defer store.Close() 101 102 store.Cleanup() 103 } 104 105 func openDbStore(path string) (*storage.DbStore, error) { 106 if _, err := os.Stat(filepath.Join(path, "CURRENT")); err != nil { 107 return nil, fmt.Errorf("invalid chunkdb path: %s", err) 108 } 109 hash := storage.MakeHashFunc("SHA3") 110 return storage.NewDbStore(path, hash, 10000000, 0) 111 }