Refactor architecture into eight components and consolidate reliability work
Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
This commit is contained in:
1 parent
9dd425f765
commit
d5d21b7841
302 files changed
+20037
-3492
No files matched your search
@@ -2,28 +2,50 @@ package store
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"pageswiki/internal/geduldh"
|
||||
)
|
||||
|
||||
type memoryDocumentReader struct {
|
||||
*bytes.Reader
|
||||
reader *bytes.Reader
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func (memoryDocumentReader) Close() error { return nil }
|
||||
|
||||
func (r memoryDocumentReader) Read(p []byte) (int, error) {
|
||||
if err := r.ctx.Err(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return r.reader.Read(p)
|
||||
}
|
||||
|
||||
func (r memoryDocumentReader) ReadAt(p []byte, offset int64) (int, error) {
|
||||
if err := r.ctx.Err(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return r.reader.ReadAt(p, offset)
|
||||
}
|
||||
|
||||
func (r memoryDocumentReader) Seek(offset int64, whence int) (int64, error) {
|
||||
if err := r.ctx.Err(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return r.reader.Seek(offset, whence)
|
||||
}
|
||||
|
||||
var (
|
||||
ErrInvalidDocumentHash = errors.New("invalid Document SHA-256")
|
||||
ErrDocumentHashMismatch = errors.New(
|
||||
"Document payload does not match SHA-256",
|
||||
)
|
||||
ErrInvalidDocumentHash = geduldh.ErrInvalidHash
|
||||
ErrDocumentHashMismatch = geduldh.ErrHashMismatch
|
||||
)
|
||||
|
||||
func MigrateLegacyDocumentRoot(
|
||||
@@ -131,18 +153,38 @@ func (s *Store) DocumentPath(hash string) (string, error) {
|
||||
// OpenDocument opens an immutable content-addressed Document. Callers must
|
||||
// perform authorization through a referencing object before invoking it.
|
||||
func (s *Store) OpenDocument(hash string) (io.ReadSeekCloser, error) {
|
||||
path, err := s.documentPath(hash)
|
||||
if err != nil {
|
||||
return s.OpenDocumentContext(context.Background(), hash)
|
||||
}
|
||||
|
||||
// OpenDocumentContext supports bounded ReaderAt access without hydrating a
|
||||
// revision body. A request/job context must be used by new storage consumers.
|
||||
func (s *Store) OpenDocumentContext(ctx context.Context, hash string) (geduldh.Reader, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if path != "" {
|
||||
return os.Open(path)
|
||||
if _, err := geduldh.Key(hash); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s.DocumentRoot != "" {
|
||||
backend, err := s.documentStorage()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return backend.Open(ctx, hash)
|
||||
}
|
||||
var body string
|
||||
if err := s.DB.QueryRow(`SELECT COALESCE(body,'') FROM revisions WHERE document_hash=? ORDER BY rowid DESC LIMIT 1`, strings.ToLower(strings.TrimSpace(hash))).Scan(&body); err != nil {
|
||||
if err := s.DB.QueryRowContext(ctx, `SELECT COALESCE(body,'') FROM revisions WHERE document_hash=? ORDER BY rowid DESC LIMIT 1`, strings.ToLower(strings.TrimSpace(hash))).Scan(&body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return memoryDocumentReader{bytes.NewReader([]byte(body))}, nil
|
||||
return memoryDocumentReader{reader: bytes.NewReader([]byte(body)), ctx: ctx}, nil
|
||||
}
|
||||
|
||||
// documentStorage is the payload-only backend boundary. DocumentRoot still
|
||||
// anchors legacy calendars and instance paths; it must not become an S3 URL.
|
||||
// Remote selection is intentionally not exposed until canonical state, CLI,
|
||||
// deployment profiles and backend contract tests have been migrated.
|
||||
func (s *Store) documentStorage() (geduldh.Store, error) {
|
||||
return geduldh.NewLocal(s.DocumentRoot)
|
||||
}
|
||||
|
||||
type DocumentReference struct {
|
||||
@@ -205,191 +247,57 @@ func (s *Store) DocumentReferencesInDomain(domainUUID, hash string) ([]DocumentR
|
||||
return references, nil
|
||||
}
|
||||
|
||||
func (s *Store) persistDocument(
|
||||
expectedHash string,
|
||||
body string,
|
||||
) error {
|
||||
func (s *Store) persistDocument(expectedHash, body string) error {
|
||||
if s.ReadOnly {
|
||||
return ErrReadOnly
|
||||
}
|
||||
if s.DocumentRoot == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
actualHash :=
|
||||
revisionHash(
|
||||
body,
|
||||
)
|
||||
|
||||
if !strings.EqualFold(expectedHash, actualHash) {
|
||||
return fmt.Errorf(
|
||||
"%w: expected %s, got %s",
|
||||
ErrDocumentHashMismatch,
|
||||
expectedHash,
|
||||
actualHash,
|
||||
)
|
||||
}
|
||||
|
||||
path, err :=
|
||||
s.documentPath(
|
||||
actualHash,
|
||||
)
|
||||
|
||||
backend, err := s.documentStorage()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if existing, err := os.ReadFile(path); err == nil {
|
||||
if revisionHash(string(existing)) != actualHash {
|
||||
return fmt.Errorf(
|
||||
"%w: corrupt existing file %s",
|
||||
ErrDocumentHashMismatch,
|
||||
path,
|
||||
)
|
||||
}
|
||||
|
||||
return nil
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
|
||||
dir :=
|
||||
filepath.Dir(
|
||||
path,
|
||||
)
|
||||
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
temp, err :=
|
||||
os.CreateTemp(
|
||||
dir,
|
||||
".document-*",
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tempPath :=
|
||||
temp.Name()
|
||||
|
||||
defer os.Remove(tempPath)
|
||||
|
||||
if err := temp.Chmod(0644); err != nil {
|
||||
temp.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := temp.WriteString(body); err != nil {
|
||||
temp.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
if err := temp.Sync(); err != nil {
|
||||
temp.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
if err := temp.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.Rename(tempPath, path); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return backend.PutIfAbsent(context.Background(), expectedHash, int64(len(body)), strings.NewReader(body))
|
||||
}
|
||||
|
||||
func (s *Store) readDocument(
|
||||
hash string,
|
||||
) (string, error) {
|
||||
func (s *Store) readDocument(hash string) (string, error) {
|
||||
if s.DocumentRoot == "" {
|
||||
return "", os.ErrNotExist
|
||||
}
|
||||
|
||||
path, err :=
|
||||
s.documentPath(
|
||||
hash,
|
||||
)
|
||||
|
||||
backend, err := s.documentStorage()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
payload, err :=
|
||||
os.ReadFile(
|
||||
path,
|
||||
)
|
||||
|
||||
reader, err := backend.Open(context.Background(), hash)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
body :=
|
||||
string(
|
||||
payload,
|
||||
)
|
||||
|
||||
if revisionHash(body) != strings.ToLower(hash) {
|
||||
return "", fmt.Errorf(
|
||||
"%w: corrupt file %s",
|
||||
ErrDocumentHashMismatch,
|
||||
path,
|
||||
)
|
||||
defer reader.Close()
|
||||
payload, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return body, nil
|
||||
if revisionHash(string(payload)) != strings.ToLower(strings.TrimSpace(hash)) {
|
||||
return "", ErrDocumentHashMismatch
|
||||
}
|
||||
return string(payload), nil
|
||||
}
|
||||
|
||||
func (s *Store) EnsureDocumentStore() error {
|
||||
if s.DocumentRoot == "" {
|
||||
return nil
|
||||
}
|
||||
return s.EnsureDocumentStoreContext(context.Background())
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(
|
||||
filepath.Join(
|
||||
s.DocumentRoot,
|
||||
"sha256",
|
||||
),
|
||||
0755,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rows, err := s.DB.Query(`
|
||||
SELECT
|
||||
document_hash,
|
||||
COALESCE(body, '')
|
||||
FROM revisions
|
||||
WHERE document_hash IS NOT NULL
|
||||
AND document_hash != ''
|
||||
`)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
var hash string
|
||||
var body string
|
||||
|
||||
if err := rows.Scan(&hash, &body); err != nil {
|
||||
rows.Close()
|
||||
func (s *Store) EnsureDocumentStoreContext(ctx context.Context) error {
|
||||
for {
|
||||
progress, err := s.BackfillDocuments(ctx, 128)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.persistDocument(hash, body); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
if progress.Completed {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
return rows.Close()
|
||||
}
|
||||
|
||||
func (s *Store) hydrateRevision(
|
||||
|
||||
Reference in new issue
Block a user