Skip to content

Commit

Permalink
refactor: Using xid and xxhash for identifier and hash
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Aug 6, 2023
1 parent ba67d29 commit 8bda00e
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 28 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ require (
github.com/prometheus/client_golang v1.16.0
github.com/rabbitmq/amqp091-go v1.8.1
github.com/redis/go-redis/v9 v9.0.5
github.com/rs/xid v1.5.0
github.com/zeebo/xxh3 v1.0.2
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/trace v1.16.0
golang.org/x/crypto v0.12.0
Expand Down Expand Up @@ -46,11 +48,9 @@ require (
github.com/prometheus/procfs v0.10.1 // indirect
github.com/redis/go-redis/extra/rediscmd/v9 v9.0.5 // indirect
github.com/redis/go-redis/extra/redisotel/v9 v9.0.5 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/tdewolff/minify/v2 v2.12.8 // indirect
github.com/tdewolff/parse/v2 v2.6.7 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.42.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.16.0 // indirect
Expand Down
3 changes: 1 addition & 2 deletions pkg/crud/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/httputils/v4/pkg/model"
"github.com/ViBiOh/httputils/v4/pkg/renderer"
"github.com/ViBiOh/httputils/v4/pkg/sha"
)

func (a App) Create(w http.ResponseWriter, r *http.Request, request provider.Request) {
Expand Down Expand Up @@ -68,7 +67,7 @@ func (a App) CreateSavedSearch(w http.ResponseWriter, r *http.Request, request p
}

if err = a.searchApp.Add(ctx, item, provider.Search{
ID: sha.New(name),
ID: provider.Hash(name),
Name: name,
Query: r.URL.RawQuery,
}); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/crud/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (a App) serveFile(w http.ResponseWriter, r *http.Request, item absto.Item)
ctx, end := tracer.StartSpan(r.Context(), a.tracer, "file", trace.WithSpanKind(trace.SpanKindInternal))
defer end(&err)

etag, ok := provider.EtagMatch(w, r, sha.New(item))
etag, ok := provider.EtagMatch(w, r, provider.Hash(item.String()))
if ok {
return nil
}
Expand Down Expand Up @@ -191,7 +191,7 @@ func (a App) serveGeoJSON(w http.ResponseWriter, r *http.Request, request provid
} else if exifs, err := a.metadataApp.ListDir(ctx, item); err != nil {
logger.WithField("item", item.Pathname).Error("list exifs: %s", err)
} else {
hash = sha.New(exifs)
hash = provider.RawHash(exifs)
}

etag, ok := provider.EtagMatch(w, r, hash)
Expand Down
5 changes: 2 additions & 3 deletions pkg/crud/upload_chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ViBiOh/httputils/v4/pkg/cntxt"
"github.com/ViBiOh/httputils/v4/pkg/logger"
"github.com/ViBiOh/httputils/v4/pkg/model"
"github.com/ViBiOh/httputils/v4/pkg/sha"
"github.com/ViBiOh/httputils/v4/pkg/tracer"
)

Expand All @@ -31,7 +30,7 @@ func (a App) uploadChunk(w http.ResponseWriter, r *http.Request, request provide
return
}

tempDestination := filepath.Join(a.temporaryFolder, sha.New(fileName))
tempDestination := filepath.Join(a.temporaryFolder, provider.Hash(fileName))
tempFile := filepath.Join(tempDestination, chunkNumber)

if err = os.MkdirAll(tempDestination, absto.DirectoryPerm); err != nil {
Expand Down Expand Up @@ -77,7 +76,7 @@ func (a App) mergeChunk(w http.ResponseWriter, r *http.Request, request provider
return
}

tempFolder := filepath.Join(a.temporaryFolder, sha.New(fileName))
tempFolder := filepath.Join(a.temporaryFolder, provider.Hash(fileName))
tempFile := filepath.Join(tempFolder, fileName)

if err := a.mergeChunkFiles(tempFolder, tempFile); err != nil {
Expand Down
20 changes: 20 additions & 0 deletions pkg/provider/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package provider
import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"io"
"net/http"
"path"
"regexp"
"strconv"
"strings"
"sync"
"time"
Expand All @@ -17,6 +19,8 @@ import (
absto "github.com/ViBiOh/absto/pkg/model"
"github.com/ViBiOh/httputils/v4/pkg/logger"
"github.com/ViBiOh/httputils/v4/pkg/request"
"github.com/rs/xid"
"github.com/zeebo/xxh3"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
Expand Down Expand Up @@ -193,3 +197,19 @@ func EtagMatch(w http.ResponseWriter, r *http.Request, hash string) (etag string

return
}

func Identifier() string {
return xid.New().String()
}

func Hash(value string) string {
return strconv.FormatUint(xxh3.HashString(value), 16)
}

func RawHash(content any) string {
hasher := xxh3.New()

fmt.Fprintf(hasher, "%v", content)

return hex.EncodeToString(hasher.Sum(nil))
}
8 changes: 1 addition & 7 deletions pkg/share/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@ import (

"github.com/ViBiOh/fibr/pkg/exclusive"
"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/httputils/v4/pkg/sha"
"github.com/ViBiOh/httputils/v4/pkg/uuid"
)

func (a *App) generateID() (string, error) {
for {
id, err := uuid.New()
if err != nil {
return "", err
}
idSha := sha.New(id)[:8]
idSha := provider.Hash(provider.Identifier())[:8]

if _, ok := a.shares[idSha]; !ok {
return idSha, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/thumbnail/thumbnail.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (a App) List(w http.ResponseWriter, r *http.Request, item absto.Item, items
} else if thumbnails, err := a.ListDir(ctx, item); err != nil {
logger.WithField("item", item.Pathname).Error("list thumbnails: %s", err)
} else {
hash = sha.New(thumbnails)
hash = provider.RawHash(thumbnails)
}

etag, ok := provider.EtagMatch(w, r, hash)
Expand Down
3 changes: 1 addition & 2 deletions pkg/thumbnail/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
absto "github.com/ViBiOh/absto/pkg/model"
"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/fibr/pkg/version"
"github.com/ViBiOh/httputils/v4/pkg/sha"
"github.com/ViBiOh/httputils/v4/pkg/tracer"
"github.com/ViBiOh/vith/pkg/model"
)
Expand Down Expand Up @@ -81,7 +80,7 @@ func typeOfItem(item absto.Item) model.ItemType {
}

func redisKey(filename string) string {
return version.Redis("thumbnail:" + sha.New(filename))
return version.Redis("thumbnail:" + provider.Hash(filename))
}

func (a App) Info(ctx context.Context, pathname string) (item absto.Item, err error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package version
import (
"fmt"

"github.com/ViBiOh/httputils/v4/pkg/sha"
"github.com/ViBiOh/fibr/pkg/provider"
)

var (
cacheVersion = sha.New("vibioh/fibr/1")[:8]
cacheVersion = provider.Hash("vibioh/fibr/1")[:8]
cachePrefix = "fibr:" + cacheVersion
)

Expand Down
8 changes: 1 addition & 7 deletions pkg/webhook/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ import (
"sort"

"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/httputils/v4/pkg/sha"
"github.com/ViBiOh/httputils/v4/pkg/uuid"
)

func (a *App) generateID() (string, error) {
for {
id, err := uuid.New()
if err != nil {
return "", err
}
idSha := sha.New(id)[:8]
idSha := provider.Hash(provider.Identifier())[:8]

if _, ok := a.webhooks[idSha]; !ok {
return idSha, nil
Expand Down

0 comments on commit 8bda00e

Please sign in to comment.