db.go 3.0 KB

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