feat(database): add file size metadata and range parameters
- Add byte_size and line_count columns to files table - Increment SchemaVersion to 2 (requires re-indexing) - Add DeclarationWithMetadata, FileRange, FileResult types - Add GetDeclarationsWithMetadata method for file metadata lookup - Add GetFileWithRange method for paginated file retrieval - Implement countLines and applyLineRange helpers Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -88,7 +88,9 @@ func (s *PostgresStore) Initialize(ctx context.Context) error {
|
||||
revision_id INTEGER NOT NULL REFERENCES revisions(id) ON DELETE CASCADE,
|
||||
file_path TEXT NOT NULL,
|
||||
extension TEXT,
|
||||
content TEXT NOT NULL
|
||||
content TEXT NOT NULL,
|
||||
byte_size INTEGER NOT NULL DEFAULT 0,
|
||||
line_count INTEGER NOT NULL DEFAULT 0
|
||||
)`,
|
||||
IndexOptionsRevisionName,
|
||||
IndexOptionsRevisionParent,
|
||||
@@ -432,11 +434,19 @@ func (s *PostgresStore) GetDeclarations(ctx context.Context, optionID int64) ([]
|
||||
|
||||
// CreateFile creates a new file record.
|
||||
func (s *PostgresStore) CreateFile(ctx context.Context, file *File) error {
|
||||
// Compute metadata if not already set
|
||||
if file.ByteSize == 0 {
|
||||
file.ByteSize = len(file.Content)
|
||||
}
|
||||
if file.LineCount == 0 {
|
||||
file.LineCount = countLines(file.Content)
|
||||
}
|
||||
|
||||
err := s.db.QueryRowContext(ctx, `
|
||||
INSERT INTO files (revision_id, file_path, extension, content)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
INSERT INTO files (revision_id, file_path, extension, content, byte_size, line_count)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING id`,
|
||||
file.RevisionID, file.FilePath, file.Extension, file.Content,
|
||||
file.RevisionID, file.FilePath, file.Extension, file.Content, file.ByteSize, file.LineCount,
|
||||
).Scan(&file.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create file: %w", err)
|
||||
@@ -453,8 +463,8 @@ func (s *PostgresStore) CreateFilesBatch(ctx context.Context, files []*File) err
|
||||
defer tx.Rollback()
|
||||
|
||||
stmt, err := tx.PrepareContext(ctx, `
|
||||
INSERT INTO files (revision_id, file_path, extension, content)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
INSERT INTO files (revision_id, file_path, extension, content, byte_size, line_count)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING id`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to prepare statement: %w", err)
|
||||
@@ -462,7 +472,15 @@ func (s *PostgresStore) CreateFilesBatch(ctx context.Context, files []*File) err
|
||||
defer stmt.Close()
|
||||
|
||||
for _, file := range files {
|
||||
err := stmt.QueryRowContext(ctx, file.RevisionID, file.FilePath, file.Extension, file.Content).Scan(&file.ID)
|
||||
// Compute metadata if not already set
|
||||
if file.ByteSize == 0 {
|
||||
file.ByteSize = len(file.Content)
|
||||
}
|
||||
if file.LineCount == 0 {
|
||||
file.LineCount = countLines(file.Content)
|
||||
}
|
||||
|
||||
err := stmt.QueryRowContext(ctx, file.RevisionID, file.FilePath, file.Extension, file.Content, file.ByteSize, file.LineCount).Scan(&file.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to insert file: %w", err)
|
||||
}
|
||||
@@ -475,9 +493,9 @@ func (s *PostgresStore) CreateFilesBatch(ctx context.Context, files []*File) err
|
||||
func (s *PostgresStore) GetFile(ctx context.Context, revisionID int64, path string) (*File, error) {
|
||||
file := &File{}
|
||||
err := s.db.QueryRowContext(ctx, `
|
||||
SELECT id, revision_id, file_path, extension, content
|
||||
SELECT id, revision_id, file_path, extension, content, byte_size, line_count
|
||||
FROM files WHERE revision_id = $1 AND file_path = $2`, revisionID, path,
|
||||
).Scan(&file.ID, &file.RevisionID, &file.FilePath, &file.Extension, &file.Content)
|
||||
).Scan(&file.ID, &file.RevisionID, &file.FilePath, &file.Extension, &file.Content, &file.ByteSize, &file.LineCount)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -486,3 +504,41 @@ func (s *PostgresStore) GetFile(ctx context.Context, revisionID int64, path stri
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
// GetDeclarationsWithMetadata retrieves declarations with file metadata for an option.
|
||||
func (s *PostgresStore) GetDeclarationsWithMetadata(ctx context.Context, revisionID, optionID int64) ([]*DeclarationWithMetadata, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT d.id, d.option_id, d.file_path, d.line,
|
||||
COALESCE(f.byte_size, 0), COALESCE(f.line_count, 0), (f.id IS NOT NULL)
|
||||
FROM declarations d
|
||||
LEFT JOIN files f ON f.revision_id = $1 AND f.file_path = d.file_path
|
||||
WHERE d.option_id = $2`, revisionID, optionID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get declarations with metadata: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var decls []*DeclarationWithMetadata
|
||||
for rows.Next() {
|
||||
decl := &DeclarationWithMetadata{}
|
||||
if err := rows.Scan(&decl.ID, &decl.OptionID, &decl.FilePath, &decl.Line,
|
||||
&decl.ByteSize, &decl.LineCount, &decl.HasFile); err != nil {
|
||||
return nil, fmt.Errorf("failed to scan declaration: %w", err)
|
||||
}
|
||||
decls = append(decls, decl)
|
||||
}
|
||||
return decls, rows.Err()
|
||||
}
|
||||
|
||||
// GetFileWithRange retrieves a file with a specified line range.
|
||||
func (s *PostgresStore) GetFileWithRange(ctx context.Context, revisionID int64, path string, r FileRange) (*FileResult, error) {
|
||||
file, err := s.GetFile(ctx, revisionID, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if file == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return applyLineRange(file, r), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user