Skip to content

Commit

Permalink
Fixed array hash
Browse files Browse the repository at this point in the history
  • Loading branch information
angelini committed Oct 18, 2022
1 parent fde7fca commit a3806e2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
20 changes: 10 additions & 10 deletions internal/db/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ import (
)

type Hash struct {
H1 []byte
H2 []byte
H1 [16]byte
H2 [16]byte
}

func HashContent(data []byte) Hash {
sha := sha256.Sum256(data)
return Hash{
H1: sha[0:16],
H2: sha[16:32],
H1: *(*[16]byte)(sha[0:16]),
H2: *(*[16]byte)(sha[16:32]),
}
}

func (h *Hash) Bytes() []byte {
hash := make([]byte, 32)
copy(hash[0:16], h.H1)
copy(hash[16:], h.H2)
var hash []byte
hash = append(hash, h.H1[:]...)
hash = append(hash, h.H2[:]...)
return hash
}

Expand Down Expand Up @@ -109,13 +109,13 @@ func RandomContents(ctx context.Context, tx pgx.Tx, sample float32) ([]Hash, err
var hashes []Hash

for rows.Next() {
var h1, h2 []byte
err = rows.Scan(&h1, &h2)
var hash Hash
err = rows.Scan(&hash.H1, &hash.H2)
if err != nil {
return nil, fmt.Errorf("random contents scan: %w", err)
}

hashes = append(hashes, Hash{H1: h1, H2: h2})
hashes = append(hashes, hash)
}

return hashes, nil
Expand Down
4 changes: 2 additions & 2 deletions internal/db/queryBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (qb *queryBuilder) updatedObjectsCTE() string {
ON h.hash = o.hash`
}

hashSelector := "null::hash as hash"
hashSelector := "'(00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000)'::hash as hash"
if qb.includeHashes {
hashSelector = "o.hash"
}
Expand All @@ -126,7 +126,7 @@ func (qb *queryBuilder) updatedObjectsCTE() string {

func (qb *queryBuilder) removedObjectsCTE() string {
template := `
SELECT o.path, o.mode, 0 AS size, ''::bytea AS bytes, o.packed, true AS deleted, null::hash AS hash
SELECT o.path, o.mode, 0 AS size, ''::bytea AS bytes, o.packed, true AS deleted, '(00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000)'::hash AS hash
FROM possible_objects o
WHERE o.project = __project__
AND o.start_version <= __start_version__
Expand Down
3 changes: 1 addition & 2 deletions internal/db/update.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package db

import (
"bytes"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -164,7 +163,7 @@ func UpdatePackedObjects(ctx context.Context, tx pgx.Tx, project int64, version

newHash := HashContent(updated)

if bytes.Equal(hash.H1, newHash.H1) && bytes.Equal(hash.H2, newHash.H2) {
if hash == newHash {
// content didn't change
return false, nil
}
Expand Down

0 comments on commit a3806e2

Please sign in to comment.