feat(storage): add native PostgreSQL runtime and verified offline application import
Build and publish container images / images (push) Failing after 12m29s
Build and publish container images / images (push) Failing after 12m29s
This commit is contained in:
1 parent
96065d1e89
commit
0ba1dcfef0
243 files changed
+21441
-3090
No files matched your search
@@ -0,0 +1,115 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime"
|
||||
|
||||
"pageswiki/internal/documentrefs"
|
||||
"pageswiki/internal/geduldh"
|
||||
)
|
||||
|
||||
type SavedDocument struct {
|
||||
Revision Revision
|
||||
Payload []byte
|
||||
}
|
||||
|
||||
// RestoreDocument preserves the historical immutable embedded references; it
|
||||
// must not parse Asset provenance and rebind it to today's mutable Asset HEAD.
|
||||
func (r *Repository) RestoreDocument(ctx context.Context, documents geduldh.Store, domain, revision, expected, message, actor string) (Revision, error) {
|
||||
if documents == nil {
|
||||
return Revision{}, ErrInvalid
|
||||
}
|
||||
v, err := r.GetRevision(ctx, domain, revision)
|
||||
if err != nil {
|
||||
return Revision{}, err
|
||||
}
|
||||
refs, err := r.revisionReferences(ctx, domain, revision)
|
||||
if err != nil {
|
||||
return Revision{}, err
|
||||
}
|
||||
for _, hash := range append([]string{v.DocumentHash}, refs...) {
|
||||
if err := documents.Verify(ctx, hash); err != nil {
|
||||
return Revision{}, fmt.Errorf("verify restored Document: %w", err)
|
||||
}
|
||||
}
|
||||
v.Message = message
|
||||
return r.appendRevision(ctx, domain, v, expected, refs, actor)
|
||||
}
|
||||
|
||||
// SaveDocument freezes Domain-local Asset HEADs, verifies embedded payloads,
|
||||
// persists the immutable Document, then atomically publishes Revision, refs and
|
||||
// outbox. No SQL transaction/lock is held during filesystem or remote I/O.
|
||||
// A failed metadata commit may leave an unreferenced immutable blob; it must
|
||||
// never delete that blob (another writer may already reference the same hash).
|
||||
// Concurrent GC needs a separate pin protocol before this pipeline is enabled
|
||||
// in a deployment with background deletion. This is not an authorization API.
|
||||
func (r *Repository) SaveDocument(ctx context.Context, documents geduldh.Store, domain string, v Revision, payload []byte, expected, actor string) (SavedDocument, error) {
|
||||
return r.saveDocument(ctx, documents, domain, v, payload, &expected, actor)
|
||||
}
|
||||
|
||||
// SaveDocumentUnconditionally preserves append-without-CAS semantics for import
|
||||
// and legacy clients. Concurrent saves still append immutable revisions with
|
||||
// monotonic timestamps; it must not be used to silently retry a failed CAS.
|
||||
func (r *Repository) SaveDocumentUnconditionally(ctx context.Context, documents geduldh.Store, domain string, v Revision, payload []byte, actor string) (SavedDocument, error) {
|
||||
return r.saveDocument(ctx, documents, domain, v, payload, nil, actor)
|
||||
}
|
||||
|
||||
func (r *Repository) saveDocument(ctx context.Context, documents geduldh.Store, domain string, v Revision, payload []byte, expected *string, actor string) (SavedDocument, error) {
|
||||
if documents == nil {
|
||||
return SavedDocument{}, ErrInvalid
|
||||
}
|
||||
lang, err := normalizedLanguage(v.Language)
|
||||
if err != nil {
|
||||
return SavedDocument{}, err
|
||||
}
|
||||
v.Language = lang
|
||||
if _, _, err := mime.ParseMediaType(v.ContentType); err != nil {
|
||||
return SavedDocument{}, ErrInvalid
|
||||
}
|
||||
var bound bool
|
||||
if err := r.database.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM objects o WHERE o.uuid=`+r.p(1)+` AND (`+r.objectDomainScope("o.uuid", r.p(2))+`))`, v.ObjectUUID, domain).Scan(&bound); err != nil {
|
||||
return SavedDocument{}, r.failure(err)
|
||||
}
|
||||
if !bound {
|
||||
return SavedDocument{}, ErrNotFound
|
||||
}
|
||||
// Fail stale saves before expensive payload work; repeat CAS in the commit
|
||||
// transaction because a different writer may commit after this preflight.
|
||||
head, err := r.Head(ctx, domain, v.ObjectUUID, v.Language)
|
||||
if err != nil && !errors.Is(err, ErrNotFound) {
|
||||
return SavedDocument{}, err
|
||||
}
|
||||
if expected != nil && head.UUID != *expected {
|
||||
return SavedDocument{}, ErrConflict
|
||||
}
|
||||
frozen, refs, err := documentrefs.Freeze(ctx, payload, v.ContentType, func(ctx context.Context, address string) (string, error) {
|
||||
a, err := r.GetAsset(ctx, domain, address)
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
return "", documentrefs.ErrUnavailable
|
||||
}
|
||||
return a.HeadDocumentHash, err
|
||||
})
|
||||
if err != nil {
|
||||
return SavedDocument{}, err
|
||||
}
|
||||
for _, hash := range refs {
|
||||
if err := documents.Verify(ctx, hash); err != nil {
|
||||
return SavedDocument{}, fmt.Errorf("verify embedded Document: %w", err)
|
||||
}
|
||||
}
|
||||
sum := sha256.Sum256(frozen)
|
||||
v.DocumentHash = hex.EncodeToString(sum[:])
|
||||
if err := documents.PutIfAbsent(ctx, v.DocumentHash, int64(len(frozen)), bytes.NewReader(frozen)); err != nil {
|
||||
return SavedDocument{}, fmt.Errorf("persist Document: %w", err)
|
||||
}
|
||||
created, err := r.appendRevisionExpected(ctx, domain, v, expected, refs, actor)
|
||||
if err != nil {
|
||||
return SavedDocument{}, err
|
||||
}
|
||||
return SavedDocument{Revision: created, Payload: frozen}, nil
|
||||
}
|
||||
Reference in new issue
Block a user