Skip to content

Commit

Permalink
interface: Add algorithm to Put(...)
Browse files Browse the repository at this point in the history
To allow per-put algorithm selection.  Touching an engine-level config
for that sort of thing would probably lead to all sorts of racy bugs
allow them to set it per-put.

Signed-off-by: W. Trevor King <wking@tremily.us>
  • Loading branch information
wking committed Oct 17, 2017
1 parent a94b57c commit b1ec590
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
7 changes: 5 additions & 2 deletions dir/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,11 @@ func (engine *Engine) Digests(ctx context.Context, algorithm digest.Algorithm, p
}

// Put implements Writer.Put.
func (engine *Engine) Put(ctx context.Context, reader io.Reader) (dig digest.Digest, err error) {
digester := engine.Algorithm.Digester()
func (engine *Engine) Put(ctx context.Context, algorithm digest.Algorithm, reader io.Reader) (dig digest.Digest, err error) {
if algorithm.String() == "" {
algorithm = engine.Algorithm
}
digester := algorithm.Digester()

file, err := ioutil.TempFile(engine.temp, "blob-")
if err != nil {
Expand Down
16 changes: 3 additions & 13 deletions dir/dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,10 @@ func TestGood(t *testing.T) {
}
defer engine.Close(ctx)

dirEngine, ok := engine.(*Engine)
if !ok {
t.Fatalf("template.New() did not return a *template.Engine")
}

var digestSha256 digest.Digest
bodyIn := "Hello, World!"
t.Run("put default algorithm", func(t *testing.T) {
digestSha256, err = engine.Put(ctx, strings.NewReader(bodyIn))
digestSha256, err = engine.Put(ctx, "", strings.NewReader(bodyIn))
if err != nil {
t.Fatal(err)
}
Expand All @@ -107,12 +102,7 @@ func TestGood(t *testing.T) {

var digestSha512 digest.Digest
t.Run("put SHA-512", func(t *testing.T) {
dirEngine.Algorithm = digest.SHA512
defer func() {
dirEngine.Algorithm = digest.SHA256
}()

digestSha512, err = engine.Put(ctx, strings.NewReader(bodyIn))
digestSha512, err = engine.Put(ctx, digest.SHA512, strings.NewReader(bodyIn))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -215,7 +205,7 @@ func TestGood(t *testing.T) {

t.Run("digests", func(t *testing.T) {
// Inject sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
_, err = engine.Put(ctx, strings.NewReader(""))
_, err = engine.Put(ctx, "", strings.NewReader(""))
if err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 6 additions & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,12 @@ type Writer interface {
// Put adds a new blob to the store. The action is idempotent; a
// nil return means "that content is stored at DIGEST" without
// implying "because of your Put()".
Put(ctx context.Context, reader io.Reader) (digest digest.Digest, err error)
//

// The algorithm argument allows you to require a particular digest
// algorithm. Set to the empty string to allow the Writer to use
// its preferred algorithm.
Put(ctx context.Context, algorithm digest.Algorithm, reader io.Reader) (digest digest.Digest, err error)
}

// Deleter represents a content-addressable storage engine deleter.
Expand Down

0 comments on commit b1ec590

Please sign in to comment.