Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache fs/thunk paths to more useful filenames #204

Merged
merged 1 commit into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions pkg/bass/cache_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package bass

import (
"context"
"encoding/base64"
"encoding/binary"
"fmt"
"path/filepath"
"strings"
Expand Down Expand Up @@ -44,9 +42,7 @@ func (value CachePath) String() string {

// Hash returns a non-cryptographic hash of the cache path's ID.
func (value CachePath) Hash() string {
var sum [8]byte
binary.BigEndian.PutUint64(sum[:], xxh3.HashString(value.ID))
return base64.URLEncoding.EncodeToString(sum[:])
return b64(xxh3.HashString(value.ID))
}

func (value CachePath) Equal(other Value) bool {
Expand Down
3 changes: 2 additions & 1 deletion pkg/bass/fspath.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (combiner *FSPath) Call(ctx context.Context, val Value, scope *Scope, cont
var _ Path = (*FSPath)(nil)

func (path *FSPath) Name() string {
// TODO: should this special-case ./ to return the path hash?
return path.Path.FilesystemPath().Name()
}

Expand All @@ -131,7 +132,7 @@ func (fsp *FSPath) CachePath(ctx context.Context, dest string) (string, error) {
return "", err
}

return Cache(ctx, filepath.Join(dest, "fs", hash), fsp)
return Cache(ctx, filepath.Join(dest, "fs", hash, fsp.Path.FilesystemPath().FromSlash()), fsp)
}

func (fsp *FSPath) Open(ctx context.Context) (io.ReadCloser, error) {
Expand Down
6 changes: 1 addition & 5 deletions pkg/bass/host_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package bass

import (
"context"
"encoding/base64"
"encoding/binary"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -51,9 +49,7 @@ func (value HostPath) String() string {

// Hash returns a non-cryptographic hash of the host path's context dir.
func (value HostPath) Hash() string {
var sum [8]byte
binary.BigEndian.PutUint64(sum[:], xxh3.HashString(value.ContextDir))
return base64.URLEncoding.EncodeToString(sum[:])
return b64(xxh3.HashString(value.ContextDir))
}

func (value HostPath) Equal(other Value) bool {
Expand Down
47 changes: 26 additions & 21 deletions pkg/bass/thunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"context"
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"hash/fnv"
"io"
"log"
"math/rand"
Expand Down Expand Up @@ -490,46 +488,53 @@ func (thunk *Thunk) Platform() *Platform {

// Hash returns a stable, non-cryptographic hash derived from the thunk.
func (thunk Thunk) Hash() (string, error) {
msg, err := thunk.MarshalProto()
hash, err := thunk.hash()
if err != nil {
return "", err
}

payload, err := gproto.Marshal(msg)
if err != nil {
return "", err
}

var sum [8]byte
binary.BigEndian.PutUint64(sum[:], xxh3.Hash(payload))
return base64.URLEncoding.EncodeToString(sum[:]), nil
return b64(hash), nil
}

// Avatar returns an ASCII art avatar derived from the thunk.
func (wl Thunk) Avatar() (*invaders.Invader, error) {
payload, err := json.Marshal(wl)
if err != nil {
return nil, err
}

h := fnv.New64a()
_, err = h.Write(payload)
hash, err := wl.hash()
if err != nil {
return nil, err
}

invader := &invaders.Invader{}
invader.Set(rand.New(rand.NewSource(int64(h.Sum64()))))
invader.Set(rand.New(rand.NewSource(int64(hash))))
return invader, nil
}

var _ Readable = Thunk{}

func (thunk Thunk) CachePath(ctx context.Context, dest string) (string, error) {
digest, err := thunk.Hash()
hash, err := thunk.Hash()
if err != nil {
return "", err
}

return Cache(ctx, filepath.Join(dest, "thunk-outputs", digest), thunk)
return Cache(ctx, filepath.Join(dest, "thunk-outputs", hash), thunk)
}

func (thunk Thunk) hash() (uint64, error) {
msg, err := thunk.MarshalProto()
if err != nil {
return 0, err
}

payload, err := gproto.Marshal(msg)
if err != nil {
return 0, err
}

return xxh3.Hash(payload), nil
}

func b64(n uint64) string {
var sum [8]byte
binary.BigEndian.PutUint64(sum[:], n)
return base64.URLEncoding.EncodeToString(sum[:])
}
25 changes: 2 additions & 23 deletions pkg/bass/thunk_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ package bass
import (
"archive/tar"
"context"
"encoding/base64"
"encoding/binary"
"fmt"
"io"
"path/filepath"
"strings"

"github.com/vito/bass/pkg/proto"
"github.com/zeebo/xxh3"
"google.golang.org/protobuf/encoding/protojson"
gproto "google.golang.org/protobuf/proto"
)

// A path created by a thunk.
Expand All @@ -24,23 +20,6 @@ type ThunkPath struct {

var _ Value = ThunkPath{}

// Hash returns a non-cryptographic hash derived from the thunk path.
func (thunkPath ThunkPath) Hash() (string, error) {
msg, err := thunkPath.MarshalProto()
if err != nil {
return "", err
}

payload, err := gproto.Marshal(msg)
if err != nil {
return "", err
}

var sum [8]byte
binary.BigEndian.PutUint64(sum[:], xxh3.Hash(payload))
return base64.URLEncoding.EncodeToString(sum[:]), nil
}

func (value ThunkPath) String() string {
return fmt.Sprintf("%s/%s", value.Thunk, strings.TrimPrefix(value.Path.Slash(), "./"))
}
Expand Down Expand Up @@ -170,12 +149,12 @@ func (path ThunkPath) Dir() ThunkPath {
var _ Readable = ThunkPath{}

func (path ThunkPath) CachePath(ctx context.Context, dest string) (string, error) {
digest, err := path.Hash()
digest, err := path.Thunk.Hash()
if err != nil {
return "", err
}

return Cache(ctx, filepath.Join(dest, "thunk-paths", digest), path)
return Cache(ctx, filepath.Join(dest, "thunk-paths", digest, path.Path.FilesystemPath().FromSlash()), path)
}

func (path ThunkPath) Open(ctx context.Context) (io.ReadCloser, error) {
Expand Down