All checks were successful
check / check (push) Successful in 5s
Clears the final 80 golangci-lint findings under the canonical .golangci.yml (sha256 021cc83f4e6fc7c31b95b34b846723dfcf20b66b7baeea1dc40406e643346bcb), taking the repo from red to green: script/cibuild exits 0. - wsl_v5 (60): blank line above defer/go statements sharing no variable with the line above; blank-line-only diff. - sqlclosecheck (10): the package-local CloseRows helper hid the close from the analyzer. Helper removed; all 18 call sites now defer an inline rows.Close(), preserving the fatal-on-close-error path. No resource leak existed - the rows were always being closed. - prealloc (3): append targets given a starting capacity. - revive (3): package-name findings suppressed with per-site directives pending the naming decision tracked in #76. No gosec suppressions are needed under the pinned linter. .golangci.yml, Dockerfile, Makefile, .gitea/ and script/ are byte-identical to main. Verified with script/cibuild (digest-pinned golangci-lint v2.12.2), not make check - the latter resolves the linter from PATH and is not a trustworthy gate here; see #78. Closes #59.
323 lines
7.5 KiB
Go
323 lines
7.5 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"sneak.berlin/go/vaultik/internal/types"
|
|
)
|
|
|
|
// FileChunkRepository provides access to the file_chunks table, which maps
|
|
// files to their ordered constituent chunks.
|
|
type FileChunkRepository struct {
|
|
db *DB
|
|
}
|
|
|
|
// NewFileChunkRepository creates a FileChunkRepository backed by db.
|
|
func NewFileChunkRepository(db *DB) *FileChunkRepository {
|
|
return &FileChunkRepository{db: db}
|
|
}
|
|
|
|
// Create inserts a file_chunks row (idempotently), using tx when non-nil.
|
|
func (r *FileChunkRepository) Create(
|
|
ctx context.Context, tx *sql.Tx, fc *FileChunk,
|
|
) error {
|
|
query := `
|
|
INSERT INTO file_chunks (file_id, idx, chunk_hash)
|
|
VALUES (?, ?, ?)
|
|
ON CONFLICT(file_id, idx) DO NOTHING
|
|
`
|
|
|
|
var err error
|
|
if tx != nil {
|
|
_, err = tx.ExecContext(ctx, query, fc.FileID.String(), fc.Idx, fc.ChunkHash.String())
|
|
} else {
|
|
_, err = r.db.ExecWithLog(ctx, query,
|
|
fc.FileID.String(), fc.Idx, fc.ChunkHash.String())
|
|
}
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("inserting file_chunk: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetByPath returns the ordered chunks of the file at the given path.
|
|
func (r *FileChunkRepository) GetByPath(
|
|
ctx context.Context, path string,
|
|
) ([]*FileChunk, error) {
|
|
query := `
|
|
SELECT fc.file_id, fc.idx, fc.chunk_hash
|
|
FROM file_chunks fc
|
|
JOIN files f ON fc.file_id = f.id
|
|
WHERE f.path = ?
|
|
ORDER BY fc.idx
|
|
`
|
|
|
|
rows, err := r.db.conn.QueryContext(ctx, query, path)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("querying file chunks: %w", err)
|
|
}
|
|
|
|
defer func() {
|
|
err := rows.Close()
|
|
if err != nil {
|
|
Fatalf("failed to close rows: %v", err)
|
|
}
|
|
}()
|
|
|
|
return r.scanFileChunks(rows)
|
|
}
|
|
|
|
// GetByFileID retrieves file chunks by file ID
|
|
func (r *FileChunkRepository) GetByFileID(
|
|
ctx context.Context, fileID types.FileID,
|
|
) ([]*FileChunk, error) {
|
|
query := `
|
|
SELECT file_id, idx, chunk_hash
|
|
FROM file_chunks
|
|
WHERE file_id = ?
|
|
ORDER BY idx
|
|
`
|
|
|
|
rows, err := r.db.conn.QueryContext(ctx, query, fileID.String())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("querying file chunks: %w", err)
|
|
}
|
|
|
|
defer func() {
|
|
err := rows.Close()
|
|
if err != nil {
|
|
Fatalf("failed to close rows: %v", err)
|
|
}
|
|
}()
|
|
|
|
return r.scanFileChunks(rows)
|
|
}
|
|
|
|
// GetByPathTx retrieves file chunks within a transaction
|
|
func (r *FileChunkRepository) GetByPathTx(
|
|
ctx context.Context, tx *sql.Tx, path string,
|
|
) ([]*FileChunk, error) {
|
|
query := `
|
|
SELECT fc.file_id, fc.idx, fc.chunk_hash
|
|
FROM file_chunks fc
|
|
JOIN files f ON fc.file_id = f.id
|
|
WHERE f.path = ?
|
|
ORDER BY fc.idx
|
|
`
|
|
|
|
LogSQL("GetByPathTx", query, path)
|
|
|
|
rows, err := tx.QueryContext(ctx, query, path)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("querying file chunks: %w", err)
|
|
}
|
|
|
|
defer func() {
|
|
err := rows.Close()
|
|
if err != nil {
|
|
Fatalf("failed to close rows: %v", err)
|
|
}
|
|
}()
|
|
|
|
fileChunks, err := r.scanFileChunks(rows)
|
|
LogSQL("GetByPathTx", "Complete", path, "count", len(fileChunks))
|
|
|
|
return fileChunks, err
|
|
}
|
|
|
|
// DeleteByPath deletes all file_chunks rows for the file at the given path.
|
|
func (r *FileChunkRepository) DeleteByPath(
|
|
ctx context.Context, tx *sql.Tx, path string,
|
|
) error {
|
|
query := `
|
|
DELETE FROM file_chunks
|
|
WHERE file_id = (SELECT id FROM files WHERE path = ?)
|
|
`
|
|
|
|
var err error
|
|
if tx != nil {
|
|
_, err = tx.ExecContext(ctx, query, path)
|
|
} else {
|
|
_, err = r.db.ExecWithLog(ctx, query, path)
|
|
}
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("deleting file chunks: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// DeleteByFileID deletes all chunks for a file by its UUID
|
|
func (r *FileChunkRepository) DeleteByFileID(
|
|
ctx context.Context, tx *sql.Tx, fileID types.FileID,
|
|
) error {
|
|
query := `DELETE FROM file_chunks WHERE file_id = ?`
|
|
|
|
var err error
|
|
if tx != nil {
|
|
_, err = tx.ExecContext(ctx, query, fileID.String())
|
|
} else {
|
|
_, err = r.db.ExecWithLog(ctx, query, fileID.String())
|
|
}
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("deleting file chunks: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// DeleteByFileIDs deletes all chunks for multiple files in a single statement.
|
|
//
|
|
//nolint:dupl // symmetric implementation for a parallel association table
|
|
func (r *FileChunkRepository) DeleteByFileIDs(
|
|
ctx context.Context, tx *sql.Tx, fileIDs []types.FileID,
|
|
) error {
|
|
if len(fileIDs) == 0 {
|
|
return nil
|
|
}
|
|
|
|
// Batch at 500 to stay within SQLite's variable limit
|
|
const batchSize = 500
|
|
|
|
for i := 0; i < len(fileIDs); i += batchSize {
|
|
end := min(i+batchSize, len(fileIDs))
|
|
|
|
batch := fileIDs[i:end]
|
|
|
|
//nolint:gosec // G202: concatenates constant SQL and "?" placeholders only
|
|
query := "DELETE FROM file_chunks WHERE file_id IN (?" +
|
|
repeatPlaceholder(len(batch)-1) + ")"
|
|
|
|
args := make([]any, len(batch))
|
|
for j, id := range batch {
|
|
args[j] = id.String()
|
|
}
|
|
|
|
var err error
|
|
if tx != nil {
|
|
_, err = tx.ExecContext(ctx, query, args...)
|
|
} else {
|
|
_, err = r.db.ExecWithLog(ctx, query, args...)
|
|
}
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("batch deleting file_chunks: %w", err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// CreateBatch inserts multiple file_chunks in a single statement for efficiency.
|
|
// Batches are automatically split to stay within SQLite's variable limit.
|
|
func (r *FileChunkRepository) CreateBatch(
|
|
ctx context.Context, tx *sql.Tx, fcs []FileChunk,
|
|
) error {
|
|
if len(fcs) == 0 {
|
|
return nil
|
|
}
|
|
|
|
// Each file_chunks row binds this many SQL variables.
|
|
const fileChunkCols = 3
|
|
|
|
// SQLite has a limit on variables (typically 999 or 32766), so batch
|
|
// at 300 rows to be safe.
|
|
const batchSize = 300
|
|
|
|
for i := 0; i < len(fcs); i += batchSize {
|
|
end := min(i+batchSize, len(fcs))
|
|
|
|
batch := fcs[i:end]
|
|
|
|
// Build the query with multiple value sets
|
|
query := "INSERT INTO file_chunks (file_id, idx, chunk_hash) VALUES "
|
|
|
|
args := make([]any, 0, len(batch)*fileChunkCols)
|
|
|
|
var querySb211 strings.Builder
|
|
|
|
for j, fc := range batch {
|
|
if j > 0 {
|
|
querySb211.WriteString(", ")
|
|
}
|
|
|
|
querySb211.WriteString("(?, ?, ?)")
|
|
|
|
args = append(args, fc.FileID.String(), fc.Idx, fc.ChunkHash.String())
|
|
}
|
|
|
|
query += querySb211.String() //nolint:gosec // G202: appends "?" placeholders only
|
|
|
|
query += " ON CONFLICT(file_id, idx) DO NOTHING"
|
|
|
|
var err error
|
|
if tx != nil {
|
|
_, err = tx.ExecContext(ctx, query, args...)
|
|
} else {
|
|
_, err = r.db.ExecWithLog(ctx, query, args...)
|
|
}
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("batch inserting file_chunks: %w", err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetByFile is an alias for GetByPath for compatibility
|
|
func (r *FileChunkRepository) GetByFile(
|
|
ctx context.Context, path string,
|
|
) ([]*FileChunk, error) {
|
|
LogSQL("GetByFile", "Starting", path)
|
|
result, err := r.GetByPath(ctx, path)
|
|
LogSQL("GetByFile", "Complete", path, "count", len(result))
|
|
|
|
return result, err
|
|
}
|
|
|
|
// GetByFileTx retrieves file chunks within a transaction
|
|
func (r *FileChunkRepository) GetByFileTx(
|
|
ctx context.Context, tx *sql.Tx, path string,
|
|
) ([]*FileChunk, error) {
|
|
LogSQL("GetByFileTx", "Starting", path)
|
|
result, err := r.GetByPathTx(ctx, tx, path)
|
|
LogSQL("GetByFileTx", "Complete", path, "count", len(result))
|
|
|
|
return result, err
|
|
}
|
|
|
|
// scanFileChunks is a helper that scans file chunk rows
|
|
func (r *FileChunkRepository) scanFileChunks(rows *sql.Rows) ([]*FileChunk, error) {
|
|
var fileChunks []*FileChunk
|
|
|
|
for rows.Next() {
|
|
var (
|
|
fc FileChunk
|
|
fileIDStr, chunkHashStr string
|
|
)
|
|
|
|
err := rows.Scan(&fileIDStr, &fc.Idx, &chunkHashStr)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("scanning file chunk: %w", err)
|
|
}
|
|
|
|
fc.FileID, err = types.ParseFileID(fileIDStr)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parsing file ID: %w", err)
|
|
}
|
|
|
|
fc.ChunkHash = types.ChunkHash(chunkHashStr)
|
|
fileChunks = append(fileChunks, &fc)
|
|
}
|
|
|
|
return fileChunks, rows.Err()
|
|
}
|