Files
pages/internal/store/document_window.go
T
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
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.
2026-11-01 15:23:31 +09:00

111 lines
3.2 KiB
Go

package store
import (
"context"
"errors"
"io"
)
const MaxDocumentWindowBytes int64 = 256 << 10
type DocumentWindow struct {
Data []byte
Offset int64
NextOffset int64
TotalBytes int64
Truncated bool
}
func (s *Store) ReadDocumentWindow(documentHash string, offset, limit int64) (DocumentWindow, error) {
return s.ReadDocumentWindowContext(context.Background(), documentHash, offset, limit)
}
func (s *Store) ReadDocumentWindowContext(ctx context.Context, documentHash string, offset, limit int64) (DocumentWindow, error) {
if err := ctx.Err(); err != nil {
return DocumentWindow{}, err
}
if offset < 0 {
return DocumentWindow{}, errors.New("negative Document offset")
}
if limit < 1 {
limit = 64 << 10
}
if limit > MaxDocumentWindowBytes {
limit = MaxDocumentWindowBytes
}
if s.DocumentRoot != "" {
backend, err := s.documentStorage()
if err != nil {
return DocumentWindow{}, err
}
file, err := backend.Open(ctx, documentHash)
if err != nil {
return DocumentWindow{}, err
}
defer file.Close()
size, err := file.Seek(0, io.SeekEnd)
if err != nil {
return DocumentWindow{}, err
}
if offset > size {
offset = size
}
data := make([]byte, minInt64(limit, size-offset))
n, err := file.ReadAt(data, offset)
if err != nil && !errors.Is(err, io.EOF) {
return DocumentWindow{}, err
}
data = data[:n]
return DocumentWindow{Data: data, Offset: offset, NextOffset: offset + int64(n), TotalBytes: size, Truncated: offset+int64(n) < size}, nil
}
var body string
if err := s.DB.QueryRowContext(ctx, `SELECT COALESCE(body,'') FROM revisions WHERE document_hash=? ORDER BY rowid DESC LIMIT 1`, documentHash).Scan(&body); err != nil {
return DocumentWindow{}, err
}
total := int64(len(body))
if offset > total {
offset = total
}
end := offset + limit
if end > total {
end = total
}
return DocumentWindow{Data: []byte(body[offset:end]), Offset: offset, NextOffset: end, TotalBytes: total, Truncated: end < total}, nil
}
func minInt64(a, b int64) int64 {
if a < b {
return a
}
return b
}
func (s *Store) GetRevisionMetadata(revisionUUID string) (*Revision, error) {
row := s.DB.QueryRow(`SELECT uuid,object_uuid,COALESCE(document_hash,''),COALESCE(NULLIF(lang,''),'und'),COALESCE(NULLIF(content_type,''),'text/html; charset=utf-8'),COALESCE(filename,''),'',COALESCE(message,''),COALESCE(created_at,0),COALESCE(created_at_ms,COALESCE(created_at,0)*1000) FROM revisions WHERE uuid=?`, revisionUUID)
return scanRevision(row)
}
func (s *Store) DocumentSize(documentHash string) (int64, error) {
return s.DocumentSizeContext(context.Background(), documentHash)
}
func (s *Store) DocumentSizeContext(ctx context.Context, documentHash string) (int64, error) {
if err := ctx.Err(); err != nil {
return 0, err
}
if s.DocumentRoot != "" {
backend, err := s.documentStorage()
if err != nil {
return 0, err
}
info, err := backend.Stat(ctx, documentHash)
if err != nil {
return 0, err
}
return info.Size, nil
}
var size int64
err := s.DB.QueryRowContext(ctx, `SELECT length(CAST(COALESCE(body,'') AS BLOB)) FROM revisions WHERE document_hash=? ORDER BY rowid DESC LIMIT 1`, documentHash).Scan(&size)
return size, err
}