package store import ( "errors" "io" "os" ) 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) { if offset < 0 { return DocumentWindow{}, errors.New("negative Document offset") } if limit < 1 { limit = 64 << 10 } if limit > MaxDocumentWindowBytes { limit = MaxDocumentWindowBytes } if s.DocumentRoot != "" { path, err := s.DocumentPath(documentHash) if err != nil { return DocumentWindow{}, err } file, err := os.Open(path) if err != nil { return DocumentWindow{}, err } defer file.Close() info, err := file.Stat() if err != nil { return DocumentWindow{}, err } if offset > info.Size() { offset = info.Size() } data := make([]byte, minInt64(limit, info.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: info.Size(), Truncated: offset+int64(n) < info.Size()}, nil } var body string if err := s.DB.QueryRow(`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) { if s.DocumentRoot != "" { path, err := s.DocumentPath(documentHash) if err != nil { return 0, err } info, err := os.Stat(path) if err != nil { return 0, err } return info.Size(), nil } var size int64 err := s.DB.QueryRow(`SELECT length(COALESCE(body,'')) FROM revisions WHERE document_hash=? ORDER BY rowid DESC LIMIT 1`, documentHash).Scan(&size) return size, err }