Commit 0231080f authored by lash's avatar lash Committed by GitHub

Add join from chunks on disk (symmetric to bee-split) (#294)

parent 363dcd69
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
"github.com/ethersphere/bee/pkg/file" "github.com/ethersphere/bee/pkg/file"
"github.com/ethersphere/bee/pkg/file/joiner" "github.com/ethersphere/bee/pkg/file/joiner"
"github.com/ethersphere/bee/pkg/logging" "github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm" "github.com/ethersphere/bee/pkg/swarm"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -23,6 +24,7 @@ var ( ...@@ -23,6 +24,7 @@ var (
host string // flag variable, http api host host string // flag variable, http api host
port int // flag variable, http api port port int // flag variable, http api port
ssl bool // flag variable, uses https for api if set ssl bool // flag variable, uses https for api if set
indir string // flag variable, directory to retrieve chunks from
verbosity string // flag variable, debug level verbosity string // flag variable, debug level
logger logging.Logger logger logging.Logger
) )
...@@ -70,8 +72,14 @@ func Join(cmd *cobra.Command, args []string) (err error) { ...@@ -70,8 +72,14 @@ func Join(cmd *cobra.Command, args []string) (err error) {
return err return err
} }
// initialize interface with HTTP API // initialize interface with backend store
store := cmdfile.NewApiStore(host, port, ssl) // either from directory if set, or HTTP API if not set
var store storage.Getter
if indir != "" {
store = cmdfile.NewFsStore(indir)
} else {
store = cmdfile.NewApiStore(host, port, ssl)
}
// create the join and get its data reader // create the join and get its data reader
j := joiner.NewSimpleJoiner(store) j := joiner.NewSimpleJoiner(store)
...@@ -95,6 +103,7 @@ Will output retrieved data to stdout.`, ...@@ -95,6 +103,7 @@ Will output retrieved data to stdout.`,
c.Flags().StringVar(&host, "host", "127.0.0.1", "api host") c.Flags().StringVar(&host, "host", "127.0.0.1", "api host")
c.Flags().IntVar(&port, "port", 8080, "api port") c.Flags().IntVar(&port, "port", 8080, "api port")
c.Flags().BoolVar(&ssl, "ssl", false, "use ssl") c.Flags().BoolVar(&ssl, "ssl", false, "use ssl")
c.Flags().StringVarP(&indir, "input-dir", "i", "", "retrieve chunks from directory")
c.Flags().StringVar(&verbosity, "info", "0", "log verbosity level 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=trace") c.Flags().StringVar(&verbosity, "info", "0", "log verbosity level 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=trace")
c.SetOutput(c.OutOrStdout()) c.SetOutput(c.OutOrStdout())
......
...@@ -42,8 +42,8 @@ func (n *nopWriteCloser) Close() error { ...@@ -42,8 +42,8 @@ func (n *nopWriteCloser) Close() error {
return nil return nil
} }
// putGetter wraps both storage.Putter and storage.Getter interfaces // PutGetter wraps both storage.Putter and storage.Getter interfaces
type putGetter interface { type PutGetter interface {
storage.Putter storage.Putter
storage.Getter storage.Getter
} }
...@@ -82,7 +82,7 @@ type FsStore struct { ...@@ -82,7 +82,7 @@ type FsStore struct {
} }
// NewFsStore creates a new FsStore. // NewFsStore creates a new FsStore.
func NewFsStore(path string) storage.Putter { func NewFsStore(path string) PutGetter {
return &FsStore{ return &FsStore{
path: path, path: path,
} }
...@@ -101,6 +101,16 @@ func (f *FsStore) Put(ctx context.Context, mode storage.ModePut, chs ...swarm.Ch ...@@ -101,6 +101,16 @@ func (f *FsStore) Put(ctx context.Context, mode storage.ModePut, chs ...swarm.Ch
return exist, nil return exist, nil
} }
// Get implements storage.Getter.
func (f *FsStore) Get(ctx context.Context, mode storage.ModeGet, address swarm.Address) (ch swarm.Chunk, err error) {
chunkPath := filepath.Join(f.path, address.String())
data, err := ioutil.ReadFile(chunkPath)
if err != nil {
return nil, err
}
return swarm.NewChunk(address, data), nil
}
// ApiStore provies a storage.Putter that adds chunks to swarm through the HTTP chunk API. // ApiStore provies a storage.Putter that adds chunks to swarm through the HTTP chunk API.
type ApiStore struct { type ApiStore struct {
Client *http.Client Client *http.Client
...@@ -108,7 +118,7 @@ type ApiStore struct { ...@@ -108,7 +118,7 @@ type ApiStore struct {
} }
// NewApiStore creates a new ApiStore. // NewApiStore creates a new ApiStore.
func NewApiStore(host string, port int, ssl bool) putGetter { func NewApiStore(host string, port int, ssl bool) PutGetter {
scheme := "http" scheme := "http"
if ssl { if ssl {
scheme += "s" scheme += "s"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment