mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 11:37:01 -07:00
feat: updated cache implementation and fixed bugs
This commit is contained in:
parent
dcfd926056
commit
f53829c6ee
7 changed files with 550 additions and 97 deletions
46
internal/cache/sqlite/sqlite.go
vendored
46
internal/cache/sqlite/sqlite.go
vendored
|
|
@ -11,7 +11,17 @@ import (
|
|||
|
||||
const TABLE_NAME = "magellan_scanned_assets"
|
||||
|
||||
func CreateScannedAssetIfNotExists(path string) (*sqlx.DB, error) {
|
||||
func GetColumns() []string {
|
||||
return []string{
|
||||
"host",
|
||||
"port",
|
||||
"protocol",
|
||||
"state",
|
||||
"timestamp",
|
||||
}
|
||||
}
|
||||
|
||||
func CreateRemoteAssetsIfNotExists(path string) (*sqlx.DB, error) {
|
||||
schema := fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
host TEXT NOT NULL,
|
||||
|
|
@ -34,13 +44,13 @@ func CreateScannedAssetIfNotExists(path string) (*sqlx.DB, error) {
|
|||
return db, nil
|
||||
}
|
||||
|
||||
func InsertScannedAssets(path string, assets ...magellan.RemoteAsset) error {
|
||||
func InsertRemoteAssets(path string, assets ...magellan.RemoteAsset) error {
|
||||
if assets == nil {
|
||||
return fmt.Errorf("states == nil")
|
||||
}
|
||||
|
||||
// create database if it doesn't already exist
|
||||
db, err := CreateScannedAssetIfNotExists(path)
|
||||
db, err := CreateRemoteAssetsIfNotExists(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -62,7 +72,33 @@ func InsertScannedAssets(path string, assets ...magellan.RemoteAsset) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func DeleteScannedAssets(path string, assets ...magellan.RemoteAsset) error {
|
||||
func DeleteRemoteAssetsByHost(path string, hosts ...string) error {
|
||||
var (
|
||||
db *sqlx.DB
|
||||
tx *sqlx.Tx
|
||||
err error
|
||||
)
|
||||
db, err = sqlx.Open("sqlite3", path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open database: %v", err)
|
||||
}
|
||||
tx = db.MustBegin()
|
||||
for _, host := range hosts {
|
||||
sql := fmt.Sprintf(`DELETE FROM %s WHERE host='%s'`, TABLE_NAME, host)
|
||||
_, err := tx.Exec(sql, &host)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to execute DELETE transaction: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to commit transaction: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteRemoteAssets(path string, assets ...magellan.RemoteAsset) error {
|
||||
var (
|
||||
db *sqlx.DB
|
||||
tx *sqlx.Tx
|
||||
|
|
@ -91,7 +127,7 @@ func DeleteScannedAssets(path string, assets ...magellan.RemoteAsset) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func GetScannedAssets(path string) ([]magellan.RemoteAsset, error) {
|
||||
func GetRemoteAssets(path string) ([]magellan.RemoteAsset, error) {
|
||||
// check if path exists first to prevent creating the database
|
||||
_, exists := util.PathExists(path)
|
||||
if !exists {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue