refactor
This commit is contained in:
blob42 2023-09-08 11:52:49 +02:00
parent 6f08b8db2d
commit c1682e5d18
6 changed files with 38 additions and 24 deletions

View File

@ -140,12 +140,12 @@ type DB struct {
func (db *DB) open() error { func (db *DB) open() error {
var err error var err error
err = db.SQLXOpener.Open(db.EngineMode, db.Path) err = db.Open(db.EngineMode, db.Path)
if err != nil { if err != nil {
return err return err
} }
db.Handle = db.SQLXOpener.Get() db.Handle = db.Get()
err = db.Handle.Ping() err = db.Handle.Ping()
if err != nil { if err != nil {
return err return err
@ -350,7 +350,7 @@ func (db *DB) CountRows(table string) int {
// The order in the struct respects the columns order // The order in the struct respects the columns order
type SBookmark struct { type SBookmark struct {
id int id int
Url string URL string
metadata string metadata string
tags string tags string
desc string desc string
@ -363,7 +363,7 @@ func ScanBookmarkRow(row *sql.Rows) (*SBookmark, error) {
scan := new(SBookmark) scan := new(SBookmark)
err := row.Scan( err := row.Scan(
&scan.id, &scan.id,
&scan.Url, &scan.URL,
&scan.metadata, &scan.metadata,
&scan.tags, &scan.tags,
&scan.desc, &scan.desc,

View File

@ -99,7 +99,7 @@ func (src *DB) SyncTo(dst *DB) {
// Try to insert to row in dst table // Try to insert to row in dst table
_, err = dstTx.Stmt(tryInsertDstRow).Exec( _, err = dstTx.Stmt(tryInsertDstRow).Exec(
scan.Url, scan.URL,
scan.metadata, scan.metadata,
scan.tags, scan.tags,
scan.desc, scan.desc,
@ -139,7 +139,7 @@ func (src *DB) SyncTo(dst *DB) {
//log.Debugf("updating existing %s", scan.Url) //log.Debugf("updating existing %s", scan.Url)
row := getDstTags.QueryRow( row := getDstTags.QueryRow(
scan.Url, scan.URL,
) )
row.Scan(&tags) row.Scan(&tags)
@ -165,11 +165,11 @@ func (src *DB) SyncTo(dst *DB) {
strings.Join(newTags, TagJoinSep), strings.Join(newTags, TagJoinSep),
scan.desc, scan.desc,
0, //flags 0, //flags
scan.Url, scan.URL,
) )
if err != nil { if err != nil {
log.Errorf("%s: %s", err, scan.Url) log.Errorf("%s: %s", err, scan.URL)
} }
} }
@ -192,23 +192,23 @@ func (src *DB) SyncToDisk(dbpath string) error {
log.Debugf("Syncing <%s> to <%s>", src.Name, dbpath) log.Debugf("Syncing <%s> to <%s>", src.Name, dbpath)
//log.Debugf("[flush] openeing <%s>", src.path) //log.Debugf("[flush] openeing <%s>", src.path)
srcDb, err := sqlx.Open(DriverBackupMode, src.Path) srcDB, err := sqlx.Open(DriverBackupMode, src.Path)
defer flushSqliteCon(srcDb) defer flushSqliteCon(srcDB)
if err != nil { if err != nil {
return err return err
} }
srcDb.Ping() srcDB.Ping()
//log.Debugf("[flush] opening <%s>", DB_FILENAME) //log.Debugf("[flush] opening <%s>", DB_FILENAME)
dbUri := fmt.Sprintf("file:%s", dbpath) dbURI := fmt.Sprintf("file:%s", dbpath)
bkDb, err := sqlx.Open(DriverBackupMode, dbUri) bkDB, err := sqlx.Open(DriverBackupMode, dbURI)
defer flushSqliteCon(bkDb) defer flushSqliteCon(bkDB)
if err != nil { if err != nil {
return err return err
} }
err = bkDb.Ping() err = bkDB.Ping()
if err != nil { if err != nil {
return err return err
} }

View File

@ -41,6 +41,9 @@ var (
}, },
Stats: &parsing.Stats{}, Stats: &parsing.Stats{},
UseFileWatcher: true, UseFileWatcher: true,
// NOTE: see parsing.Hook to add custom parsing logic for each
// parsed node
// UseHooks: []string,
}, },
// Default data source name query options for `places.sqlite` db // Default data source name query options for `places.sqlite` db

View File

@ -144,7 +144,7 @@ func (f *Firefox) loadBookmarksToTree(bookmarks []*MozBookmark) {
// Parent will be a folder or nothing? // Parent will be a folder or nothing?
tree.AddChild(f.tagMap[tagNode.Name], urlNode) tree.AddChild(f.tagMap[tagNode.Name], urlNode)
f.CurrentUrlCount++ f.CurrentURLCount++
} }
// Link this URL node to its corresponding folder node if it exists. // Link this URL node to its corresponding folder node if it exists.
@ -342,7 +342,7 @@ func (f *Firefox) Load() error {
f.lastRunAt = time.Now().UTC() f.lastRunAt = time.Now().UTC()
log.Debugf("parsed %d bookmarks and %d nodes in %s", log.Debugf("parsed %d bookmarks and %d nodes in %s",
f.CurrentUrlCount, f.CurrentURLCount,
f.CurrentNodeCount, f.CurrentNodeCount,
f.LastFullTreeParseTime) f.LastFullTreeParseTime)
f.Reset() f.Reset()
@ -401,6 +401,10 @@ func (ff *Firefox) Run() {
ff.loadBookmarksToTree(bookmarks) ff.loadBookmarksToTree(bookmarks)
// tree.PrintTree(ff.NodeTree) // tree.PrintTree(ff.NodeTree)
//NOTE: we don't rebuild the index from the tree here as the source of
// truth is the URLIndex and not the tree. The tree is only used for
// reprensenting the bookmark hierarchy in a conveniant way.
database.SyncURLIndexToBuffer(ff.URLIndexList, ff.URLIndex, ff.BufferDB) database.SyncURLIndexToBuffer(ff.URLIndexList, ff.URLIndex, ff.BufferDB)
ff.BufferDB.SyncTo(database.Cache.DB) ff.BufferDB.SyncTo(database.Cache.DB)
database.Cache.DB.SyncToDisk(database.GetDBFullPath()) database.Cache.DB.SyncToDisk(database.GetDBFullPath())
@ -421,7 +425,7 @@ func (f *Firefox) Shutdown() error {
return err return err
} }
// HACK: addUrl and addTag share a lot of code, find a way to reuse shared code // TODO: addUrl and addTag share a lot of code, find a way to reuse shared code
// and only pass extra details about tag/url along in some data structure // and only pass extra details about tag/url along in some data structure
// PROBLEM: tag nodes use IDs and URL nodes use URL as hashes // PROBLEM: tag nodes use IDs and URL nodes use URL as hashes
func (f *Firefox) addURLNode(url, title, desc string) (bool, *tree.Node) { func (f *Firefox) addURLNode(url, title, desc string) (bool, *tree.Node) {
@ -441,6 +445,14 @@ func (f *Firefox) addURLNode(url, title, desc string) (bool, *tree.Node) {
f.URLIndexList = append(f.URLIndexList, url) f.URLIndexList = append(f.URLIndexList, url)
f.CurrentNodeCount++ f.CurrentNodeCount++
// Call hooks
//TEST:
err := f.CallHooks(urlNode)
if err != nil {
log.Errorf("error calling hooks for <%s>: %s", url, err)
}
return true, urlNode return true, urlNode
} else { } else {
urlNode = iUrlNode.(*tree.Node) urlNode = iUrlNode.(*tree.Node)
@ -621,7 +633,7 @@ func loadBookmarks(f *Firefox) {
// Set tag as parent to urlnode // Set tag as parent to urlnode
tree.AddChild(f.tagMap[tagTitle], urlNode) tree.AddChild(f.tagMap[tagTitle], urlNode)
f.CurrentUrlCount++ f.CurrentURLCount++
} }
log.Debugf("root tree children len is %d", len(f.NodeTree.Children)) log.Debugf("root tree children len is %d", len(f.NodeTree.Children))
@ -654,7 +666,7 @@ func (f *Firefox) initPlacesCopy() (mozilla.PlaceCopyJob, error) {
// init is required to register the module as a plugin when it is imported // init is required to register the module as a plugin when it is imported
func init() { func init() {
modules.RegisterBrowser(Firefox{FirefoxConfig: FFConfig}) // modules.RegisterBrowser(Firefox{FirefoxConfig: FFConfig})
//TIP: cmd.RegisterModCommand(BrowserName, &cli.Command{ //TIP: cmd.RegisterModCommand(BrowserName, &cli.Command{
// Name: "test", // Name: "test",
// }) // })

View File

@ -7,7 +7,6 @@ import (
"github.com/sp4ke/hashmap" "github.com/sp4ke/hashmap"
) )
type Index = *hashmap.RBTree
type HashTree = *hashmap.RBTree type HashTree = *hashmap.RBTree
// In memory index used for fast lookup of url->node pairs // In memory index used for fast lookup of url->node pairs

View File

@ -3,7 +3,7 @@ package mozilla
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io"
"os" "os"
"regexp" "regexp"
) )
@ -29,7 +29,7 @@ var (
// Finds and returns a prefernce definition. // Finds and returns a prefernce definition.
// Returns empty string ("") if no pref found // Returns empty string ("") if no pref found
func FindPref(path string, name string) (string, error) { func FindPref(path string, name string) (string, error) {
text, err := ioutil.ReadFile(path) text, err := os.ReadFile(path)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -104,7 +104,7 @@ func SetPrefBool(path string, name string, val bool) error {
re := regexp.MustCompile(fmt.Sprintf(REFirefoxPrefs, name)) re := regexp.MustCompile(fmt.Sprintf(REFirefoxPrefs, name))
template := []byte(fmt.Sprintf("user_pref(\"$option\", %t) ;\n", val)) template := []byte(fmt.Sprintf("user_pref(\"$option\", %t) ;\n", val))
text, err := ioutil.ReadAll(f) text, err := io.ReadAll(f)
if err != nil { if err != nil {
return err return err
} }