forked from starlinglab/integrity-v2
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[preprocessor/folder] add file status and database related functions
- Loading branch information
1 parent
c621246
commit a2c85c7
Showing
4 changed files
with
181 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package preprocessor_folder | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/jackc/pgx/v5" | ||
"github.com/jackc/pgx/v5/pgxpool" | ||
db "github.com/starlinglab/integrity-v2/database" | ||
) | ||
|
||
func initFileStatusTableIfNotExists(connPool *pgxpool.Pool) error { | ||
_, err := connPool.Exec( | ||
db.GetDatabaseContext(), | ||
FILE_STATUS_TABLE, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func initDbTableIfNotExists(connPool *pgxpool.Pool) error { | ||
err := initFileStatusTableIfNotExists(connPool) | ||
return err | ||
} | ||
|
||
type FileQueryResult struct { | ||
Status *string | ||
Cid *string | ||
ErrorMessage *string | ||
} | ||
|
||
func queryIfFileExists(connPool *pgxpool.Pool, filePath string) (*FileQueryResult, error) { | ||
var result FileQueryResult | ||
err := connPool.QueryRow( | ||
db.GetDatabaseContext(), | ||
"SELECT status, cid, error FROM file_status WHERE file_path = $1;", | ||
filePath, | ||
).Scan(&result.Status, &result.Cid, &result.ErrorMessage) | ||
if err != nil { | ||
if err == pgx.ErrNoRows { | ||
return nil, nil | ||
} | ||
return nil, err | ||
} | ||
return &result, nil | ||
} | ||
|
||
func setFileStatusFound(connPool *pgxpool.Pool, filePath string) error { | ||
_, err := connPool.Exec( | ||
db.GetDatabaseContext(), | ||
"INSERT INTO file_status (file_path, status, created_at, updated_at) VALUES ($1, $2, $3, $4);", | ||
filePath, | ||
FileStatusFound, | ||
time.Now().UTC(), | ||
time.Now().UTC(), | ||
) | ||
return err | ||
} | ||
|
||
func setFileStatusUploading(connPool *pgxpool.Pool, filePath string, sha256 string) error { | ||
_, err := connPool.Exec( | ||
db.GetDatabaseContext(), | ||
"UPDATE file_status SET status = $1, sha256 = $2, updated_at = $3 WHERE file_path = $4;", | ||
FileStatusUploading, | ||
sha256, | ||
time.Now().UTC(), | ||
filePath, | ||
) | ||
return err | ||
} | ||
|
||
func setFileStatusDone(connPool *pgxpool.Pool, filePath string, cid string) error { | ||
_, err := connPool.Exec( | ||
db.GetDatabaseContext(), | ||
"UPDATE file_status SET status = $1, cid = $2, updated_at = $3 WHERE file_path = $4;", | ||
FileStatusSuccess, | ||
cid, | ||
time.Now().UTC(), | ||
filePath, | ||
) | ||
return err | ||
} | ||
|
||
func setFileStatusError(connPool *pgxpool.Pool, filePath string, errorMessage string) error { | ||
_, err := connPool.Exec( | ||
db.GetDatabaseContext(), | ||
"UPDATE file_status SET status = $1, error = $2, updated_at = $3 WHERE file_path = $4;", | ||
FileStatusError, | ||
errorMessage, | ||
time.Now().UTC(), | ||
filePath, | ||
) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package preprocessor_folder | ||
|
||
var FILE_STATUS_TABLE = `CREATE TABLE IF NOT EXISTS file_status ( | ||
id BIGSERIAL PRIMARY KEY, | ||
file_path TEXT UNIQUE NOT NULL, | ||
sha256 TEXT, | ||
status TEXT NOT NULL, | ||
error TEXT, | ||
cid TEXT, | ||
created_at TIMESTAMP NOT NULL, | ||
updated_at TIMESTAMP NOT NULL | ||
); | ||
CREATE INDEX IF NOT EXISTS idx_file_status_file_path ON file_status (file_path); | ||
CREATE INDEX IF NOT EXISTS idx_file_status_sha256 ON file_status (sha256); | ||
CREATE INDEX IF NOT EXISTS idx_file_status_status ON file_status (status); | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters