Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
96 lines
3.7 KiB
Go
96 lines
3.7 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 {
|
|
for _, statement := range []string{
|
|
`CREATE TABLE IF NOT EXISTS projection_metadata (
|
|
document_hash TEXT NOT NULL,kind TEXT NOT NULL,version INTEGER NOT NULL,
|
|
payload TEXT NOT NULL DEFAULT '{}',error TEXT NOT NULL DEFAULT '',
|
|
updated_at_ms INTEGER NOT NULL,retry_at_ms INTEGER NOT NULL DEFAULT 0,
|
|
PRIMARY KEY(document_hash,kind,version))`,
|
|
`CREATE INDEX IF NOT EXISTS projection_metadata_eviction ON projection_metadata(updated_at_ms DESC)`,
|
|
`CREATE INDEX IF NOT EXISTS idx_event_jobs_identity_status ON event_jobs(event_type,event_key,status)`,
|
|
} {
|
|
if _, err := s.DB.Exec(statement); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
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()
|
|
}
|