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

View File

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

View File

@ -41,6 +41,9 @@ var (
},
Stats: &parsing.Stats{},
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

View File

@ -144,7 +144,7 @@ func (f *Firefox) loadBookmarksToTree(bookmarks []*MozBookmark) {
// Parent will be a folder or nothing?
tree.AddChild(f.tagMap[tagNode.Name], urlNode)
f.CurrentUrlCount++
f.CurrentURLCount++
}
// 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()
log.Debugf("parsed %d bookmarks and %d nodes in %s",
f.CurrentUrlCount,
f.CurrentURLCount,
f.CurrentNodeCount,
f.LastFullTreeParseTime)
f.Reset()
@ -401,6 +401,10 @@ func (ff *Firefox) Run() {
ff.loadBookmarksToTree(bookmarks)
// 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)
ff.BufferDB.SyncTo(database.Cache.DB)
database.Cache.DB.SyncToDisk(database.GetDBFullPath())
@ -421,7 +425,7 @@ func (f *Firefox) Shutdown() error {
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
// PROBLEM: tag nodes use IDs and URL nodes use URL as hashes
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.CurrentNodeCount++
// Call hooks
//TEST:
err := f.CallHooks(urlNode)
if err != nil {
log.Errorf("error calling hooks for <%s>: %s", url, err)
}
return true, urlNode
} else {
urlNode = iUrlNode.(*tree.Node)
@ -621,7 +633,7 @@ func loadBookmarks(f *Firefox) {
// Set tag as parent to urlnode
tree.AddChild(f.tagMap[tagTitle], urlNode)
f.CurrentUrlCount++
f.CurrentURLCount++
}
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
func init() {
modules.RegisterBrowser(Firefox{FirefoxConfig: FFConfig})
// modules.RegisterBrowser(Firefox{FirefoxConfig: FFConfig})
//TIP: cmd.RegisterModCommand(BrowserName, &cli.Command{
// Name: "test",
// })

View File

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

View File

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