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.
83 lines
3.1 KiB
Go
83 lines
3.1 KiB
Go
package store
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
MaxProjectionMetadataBytes = 1 << 20
|
|
maxProjectionCacheBytes = 64 << 20
|
|
maxProjectionCacheRecords = 2048
|
|
)
|
|
|
|
// ProjectionMetadata is non-authoritative, immutable-Document-derived data.
|
|
// Authorization must always resolve a live Object/Revision before cache access.
|
|
// Paths, credentials, and per-user visibility must never be stored in Payload.
|
|
type ProjectionMetadata struct {
|
|
DocumentHash string
|
|
Kind string
|
|
Version int
|
|
Payload json.RawMessage
|
|
Error string
|
|
UpdatedAtMS int64
|
|
RetryAtMS int64
|
|
}
|
|
|
|
func (s *Store) EnsureProjectionMetadataSchema() error {
|
|
return s.ensureSchemaComponent("projection-metadata")
|
|
}
|
|
|
|
func (s *Store) GetProjectionMetadata(hash, kind string, version int) (*ProjectionMetadata, error) {
|
|
if !validDocumentHash(hash) || version < 1 || (kind != "pdf" && kind != "archive") {
|
|
return nil, errors.New("invalid projection metadata key")
|
|
}
|
|
entry := &ProjectionMetadata{DocumentHash: hash, Kind: kind, Version: version}
|
|
var payload string
|
|
err := s.DB.QueryRow(`SELECT payload,error,updated_at_ms,retry_at_ms FROM projection_metadata
|
|
WHERE document_hash=? AND kind=? AND version=?`, hash, kind, version).
|
|
Scan(&payload, &entry.Error, &entry.UpdatedAtMS, &entry.RetryAtMS)
|
|
entry.Payload = json.RawMessage(payload)
|
|
return entry, err
|
|
}
|
|
|
|
func (s *Store) SaveProjectionMetadata(entry ProjectionMetadata) error {
|
|
if !validDocumentHash(entry.DocumentHash) || entry.Version < 1 ||
|
|
(entry.Kind != "pdf" && entry.Kind != "archive") ||
|
|
len(entry.Payload) > MaxProjectionMetadataBytes || !json.Valid(entry.Payload) {
|
|
return errors.New("invalid or oversized projection metadata")
|
|
}
|
|
if len(entry.Error) > 1024 {
|
|
entry.Error = strings.ToValidUTF8(entry.Error[:1024], "")
|
|
}
|
|
if entry.UpdatedAtMS == 0 {
|
|
entry.UpdatedAtMS = time.Now().UnixMilli()
|
|
}
|
|
tx, err := s.DB.Begin()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
if _, err = tx.Exec(`INSERT INTO projection_metadata
|
|
(document_hash,kind,version,payload,error,updated_at_ms,retry_at_ms)
|
|
VALUES(?,?,?,?,?,?,?) ON CONFLICT(document_hash,kind,version) DO UPDATE SET
|
|
payload=excluded.payload,error=excluded.error,updated_at_ms=excluded.updated_at_ms,retry_at_ms=excluded.retry_at_ms`,
|
|
entry.DocumentHash, entry.Kind, entry.Version, string(entry.Payload), entry.Error, entry.UpdatedAtMS, entry.RetryAtMS); err != nil {
|
|
return err
|
|
}
|
|
// Bound both artifact size and aggregate storage. Eviction happens only on
|
|
// background writes, not on foreground cache reads. Cache loss is recoverable.
|
|
if _, err = tx.Exec(`DELETE FROM projection_metadata WHERE (document_hash,kind,version) IN (
|
|
SELECT document_hash,kind,version FROM (
|
|
SELECT document_hash,kind,version,
|
|
ROW_NUMBER() OVER(ORDER BY updated_at_ms DESC,document_hash,kind,version) AS rank,
|
|
SUM(length(CAST(payload AS BLOB))+length(CAST(error AS BLOB))+128)
|
|
OVER(ORDER BY updated_at_ms DESC,document_hash,kind,version) AS total_bytes
|
|
FROM projection_metadata) WHERE rank>? OR total_bytes>?)`, maxProjectionCacheRecords, maxProjectionCacheBytes); err != nil {
|
|
return err
|
|
}
|
|
return tx.Commit()
|
|
}
|