DetermineID in piecestore (#71)

* added spawn scripts

* Determine random id for storing

* Moved determine id to rpc example

* Added tests

* Better test

* goimports

* Updated tests

* Fix typos
This commit is contained in:
Alexander Leitner 2018-06-04 12:35:31 -04:00 committed by GitHub
parent 0e8391c30f
commit 040edbb7f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 38 deletions

View File

@ -36,18 +36,14 @@ func run(ctx context.Context) error {
ArgsUsage: "[hash] [dataPath] [storeDir]",
Action: func(c *cli.Context) error {
if c.Args().Get(0) == "" {
return argError.New("Missing data Hash")
}
if c.Args().Get(1) == "" {
return argError.New("No input file specified")
}
if c.Args().Get(2) == "" {
if c.Args().Get(1) == "" {
return argError.New("No output directory specified")
}
file, err := os.Open(c.Args().Get(1))
file, err := os.Open(c.Args().Get(0))
if err != nil {
return err
}
@ -55,16 +51,18 @@ func run(ctx context.Context) error {
// Close the file when we are done
defer file.Close()
fileInfo, err := os.Stat(c.Args().Get(1))
fileInfo, err := os.Stat(c.Args().Get(0))
if err != nil {
return err
}
if fileInfo.IsDir() {
return argError.New(fmt.Sprintf("Path (%s) is a directory, not a file", c.Args().Get(1)))
return argError.New(fmt.Sprintf("Path (%s) is a directory, not a file", c.Args().Get(0)))
}
dataFileChunk, err := pstore.StoreWriter(c.Args().Get(0), int64(fileInfo.Size()), 0, c.Args().Get(2))
id := pstore.DetermineID()
dataFileChunk, err := pstore.StoreWriter(id, int64(fileInfo.Size()), 0, c.Args().Get(1))
if err != nil {
return err
}

View File

@ -18,7 +18,7 @@ import (
"google.golang.org/grpc"
"storj.io/storj/examples/piecestore/rpc/client/utils"
"storj.io/storj/pkg/piecestore"
"storj.io/storj/pkg/piecestore/rpc/client"
)
@ -67,14 +67,11 @@ func main() {
var length = fileInfo.Size()
var ttl = time.Now().Unix() + 86400
id, err := utils.DetermineID(file, fileOffset, length)
if err != nil {
return err
}
// Created a section reader so that we can concurrently retrieve the same file.
dataSection := io.NewSectionReader(file, fileOffset, length)
id := pstore.DetermineID()
writer, err := routeClient.StorePieceRequest(id, fileOffset, length, ttl, storeOffset)
if err != nil {
fmt.Printf("Failed to send meta data to server to store file of id: %s\n", id)

View File

@ -1,23 +0,0 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package utils
import (
"crypto/sha256"
"fmt"
"io"
"os"
)
// DetermineID -- Get the id for a section of data
func DetermineID(f *os.File, offset int64, length int64) (string, error) {
h := sha256.New()
fSection := io.NewSectionReader(f, offset, length)
if _, err := io.Copy(h, fSection); err != nil {
return "", err
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}

View File

@ -4,6 +4,8 @@
package pstore
import (
"crypto/rand"
"encoding/base64"
"os"
"path"
"path/filepath"
@ -38,6 +40,18 @@ func PathByID(id, dir string) (string, error) {
return path.Join(dir, folder1, folder2, fileName), nil
}
// DetermineID creates random id
func DetermineID() string {
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
panic(err)
}
return base64.URLEncoding.EncodeToString(b)
}
// StoreWriter stores data into piece store in multiple writes
// id is the id of the data to be stored
// dir is the pstore directory containing all other data stored

View File

@ -306,6 +306,19 @@ func TestDelete(t *testing.T) {
}
}
func TestDetermineID(t *testing.T) {
t.Run("should return an id string", func(t *testing.T) {
assert := assert.New(t)
id := DetermineID()
assert.Equal(len(id) >= IDLength, true)
})
t.Run("should return a different string on each call", func(t *testing.T) {
assert := assert.New(t)
assert.NotEqual(DetermineID(), DetermineID())
})
}
func TestMain(m *testing.M) {
m.Run()
}