forked from mtusi2026/mtusi2026lab5
24 lines
655 B
Go
24 lines
655 B
Go
package attachment
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/binary"
|
|
"encoding/hex"
|
|
"errors"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
// LockHash serializes CAS publication and collection until the transaction ends.
|
|
// A collision in the signed first eight digest bytes only adds serialization.
|
|
func LockHash(ctx context.Context, tx pgx.Tx, hash []byte) error {
|
|
if len(hash) != sha256.Size {
|
|
return errors.New("attachment: lock requires a SHA256 hash")
|
|
}
|
|
_, err := tx.Exec(ctx, `SELECT pg_advisory_xact_lock($1)`, int64(binary.BigEndian.Uint64(hash[:8])))
|
|
return err
|
|
}
|
|
|
|
func BlobKey(hash []byte) string { return "blobs/" + hex.EncodeToString(hash) }
|