- Add pure Go SQLite driver (modernc.org/sqlite) to avoid CGO dependency - Implement database connection management with WAL mode - Add write mutex for serializing concurrent writes - Create schema for all tables matching DESIGN.md specifications - Implement repository pattern for all database entities: - Files, FileChunks, Chunks, Blobs, BlobChunks, ChunkFiles, Snapshots - Add transaction support with proper rollback handling - Add fatal error handling for database integrity issues - Add snapshot fields for tracking file sizes and compression ratios - Make index path configurable via VAULTIK_INDEX_PATH environment variable - Add comprehensive test coverage for all repositories - Add format check to Makefile to ensure code formatting
89 lines
2.1 KiB
Go
89 lines
2.1 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
)
|
|
|
|
type ChunkFileRepository struct {
|
|
db *DB
|
|
}
|
|
|
|
func NewChunkFileRepository(db *DB) *ChunkFileRepository {
|
|
return &ChunkFileRepository{db: db}
|
|
}
|
|
|
|
func (r *ChunkFileRepository) Create(ctx context.Context, tx *sql.Tx, cf *ChunkFile) error {
|
|
query := `
|
|
INSERT INTO chunk_files (chunk_hash, file_path, file_offset, length)
|
|
VALUES (?, ?, ?, ?)
|
|
ON CONFLICT(chunk_hash, file_path) DO NOTHING
|
|
`
|
|
|
|
var err error
|
|
if tx != nil {
|
|
_, err = tx.ExecContext(ctx, query, cf.ChunkHash, cf.FilePath, cf.FileOffset, cf.Length)
|
|
} else {
|
|
_, err = r.db.ExecWithLock(ctx, query, cf.ChunkHash, cf.FilePath, cf.FileOffset, cf.Length)
|
|
}
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("inserting chunk_file: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *ChunkFileRepository) GetByChunkHash(ctx context.Context, chunkHash string) ([]*ChunkFile, error) {
|
|
query := `
|
|
SELECT chunk_hash, file_path, file_offset, length
|
|
FROM chunk_files
|
|
WHERE chunk_hash = ?
|
|
`
|
|
|
|
rows, err := r.db.conn.QueryContext(ctx, query, chunkHash)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("querying chunk files: %w", err)
|
|
}
|
|
defer CloseRows(rows)
|
|
|
|
var chunkFiles []*ChunkFile
|
|
for rows.Next() {
|
|
var cf ChunkFile
|
|
err := rows.Scan(&cf.ChunkHash, &cf.FilePath, &cf.FileOffset, &cf.Length)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("scanning chunk file: %w", err)
|
|
}
|
|
chunkFiles = append(chunkFiles, &cf)
|
|
}
|
|
|
|
return chunkFiles, rows.Err()
|
|
}
|
|
|
|
func (r *ChunkFileRepository) GetByFilePath(ctx context.Context, filePath string) ([]*ChunkFile, error) {
|
|
query := `
|
|
SELECT chunk_hash, file_path, file_offset, length
|
|
FROM chunk_files
|
|
WHERE file_path = ?
|
|
`
|
|
|
|
rows, err := r.db.conn.QueryContext(ctx, query, filePath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("querying chunk files: %w", err)
|
|
}
|
|
defer CloseRows(rows)
|
|
|
|
var chunkFiles []*ChunkFile
|
|
for rows.Next() {
|
|
var cf ChunkFile
|
|
err := rows.Scan(&cf.ChunkHash, &cf.FilePath, &cf.FileOffset, &cf.Length)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("scanning chunk file: %w", err)
|
|
}
|
|
chunkFiles = append(chunkFiles, &cf)
|
|
}
|
|
|
|
return chunkFiles, rows.Err()
|
|
}
|