164 lines
4.5 KiB
Go
Raw Permalink Normal View History

2022-05-24 18:03:31 +09:30
package db
import (
"fmt"
"time"
2022-05-24 18:03:31 +09:30
2025-04-25 23:57:04 +09:30
"github.com/blevesearch/bleve/v2"
2025-05-01 23:39:51 +09:30
"github.com/blevesearch/bleve/v2/analysis/analyzer/keyword"
"github.com/blevesearch/bleve/v2/analysis/lang/en"
2025-04-27 20:21:33 +09:30
"github.com/blevesearch/bleve/v2/mapping"
2022-05-24 18:03:31 +09:30
"github.com/tardisx/linkwallet/entity"
bolthold "github.com/timshannon/bolthold"
2022-05-24 18:03:31 +09:30
)
type DB struct {
store *bolthold.Store
2022-08-21 13:24:52 +09:30
file string
2025-04-25 23:57:04 +09:30
bleve bleve.Index
2022-05-24 18:03:31 +09:30
}
// Open opens the bookmark boltdb, and the bleve index. It returns
// true if the index was newly created, so the caller knows all bookmarks
// need to be re-scraped
func (db *DB) Open(path string) (bool, error) {
// options := bolthold.DefaultOptions
// options.Dir = dir
// options.ValueDir = dir
rescrapeNeeded := false
store, err := bolthold.Open(path, 0666, nil)
2022-05-24 18:03:31 +09:30
if err != nil {
return false, fmt.Errorf("cannot open '%s' - %s", path, err)
2022-05-24 18:03:31 +09:30
}
2025-04-25 23:57:04 +09:30
blevePath := path + ".bleve"
2025-04-27 20:21:33 +09:30
index, err := bleve.New(blevePath, createIndexMapping())
2025-04-25 23:57:04 +09:30
if err != nil {
if err == bleve.ErrorIndexPathExists {
index, err = bleve.Open(blevePath)
if err != nil {
return false, fmt.Errorf("cannot open bleve '%s' - %s", path, err)
2025-04-25 23:57:04 +09:30
}
} else {
return false, fmt.Errorf("cannot open bleve '%s' - %s", path, err)
2025-04-25 23:57:04 +09:30
}
} else {
// we just created an index, one didn't exist, so we need to queue
// all bookmarks to be scraped
rescrapeNeeded = true
2025-04-25 23:57:04 +09:30
}
2022-05-24 18:03:31 +09:30
db.store = store
2022-08-21 13:24:52 +09:30
db.file = path
2025-04-25 23:57:04 +09:30
db.bleve = index
return rescrapeNeeded, nil
2022-05-24 18:03:31 +09:30
}
2025-04-27 20:21:33 +09:30
func createIndexMapping() mapping.IndexMapping {
indexMapping := bleve.NewIndexMapping()
2025-05-01 23:39:51 +09:30
englishTextFieldMapping := bleve.NewTextFieldMapping()
englishTextFieldMapping.Analyzer = en.AnalyzerName
// a generic reusable mapping for keyword text
keywordFieldMapping := bleve.NewTextFieldMapping()
keywordFieldMapping.Analyzer = keyword.Name
2025-04-27 20:21:33 +09:30
pageInfoMapping := bleve.NewDocumentMapping()
2025-05-01 23:39:51 +09:30
pageInfoMapping.AddFieldMappingsAt("Title", englishTextFieldMapping)
2025-04-27 20:21:33 +09:30
pageInfoMapping.AddFieldMappingsAt("Size", bleve.NewNumericFieldMapping())
2025-05-01 23:39:51 +09:30
pageInfoMapping.AddFieldMappingsAt("RawText", englishTextFieldMapping)
2025-04-27 20:21:33 +09:30
bookmarkMapping := bleve.NewDocumentMapping()
bookmarkMapping.AddFieldMappingsAt("URL", bleve.NewTextFieldMapping())
2025-05-01 23:39:51 +09:30
bookmarkMapping.AddFieldMappingsAt("Tags", keywordFieldMapping)
2025-04-27 20:21:33 +09:30
bookmarkMapping.AddSubDocumentMapping("Info", pageInfoMapping)
indexMapping.AddDocumentMapping("bookmark", bookmarkMapping)
2025-05-01 23:39:51 +09:30
2025-04-27 20:21:33 +09:30
return indexMapping
}
2022-05-24 18:03:31 +09:30
func (db *DB) Close() {
db.store.Close()
}
// func (db *DB) Dumpy() {
// res := make([]entity.Bookmark, 0, 0)
// db.store.Find(&res, &bolthold.Query{})
// log.Printf("%v", res)
// }
// IncrementSearches increments the number of searches we have ever performed by one.
func (db *DB) IncrementSearches() error {
txn, err := db.store.Bolt().Begin(true)
if err != nil {
return fmt.Errorf("could not start transaction for increment searches: %s", err)
}
stats := entity.DBStats{}
err = db.store.TxGet(txn, "stats", &stats)
if err != nil && err != bolthold.ErrNotFound {
txn.Rollback()
return fmt.Errorf("could not get stats for incrementing searches: %s", err)
}
stats.Searches += 1
err = db.store.TxUpsert(txn, "stats", &stats)
if err != nil {
txn.Rollback()
return fmt.Errorf("could not upsert stats for incrementing searches: %s", err)
}
err = txn.Commit()
if err != nil {
return fmt.Errorf("could not commit increment searches transaction: %s", err)
}
return nil
}
// UpdateBookmarkStats updates the history on the number of bookmarks and words indexed.
func (db *DB) UpdateBookmarkStats() error {
txn, err := db.store.Bolt().Begin(true)
if err != nil {
return fmt.Errorf("could not start transaction for update stats: %s", err)
}
// count bookmarks and words indexed
bmI := entity.Bookmark{}
bookmarkCount, err := db.store.TxCount(txn, &bmI, &bolthold.Query{})
if err != nil {
txn.Rollback()
return fmt.Errorf("could not get bookmark count: %s", err)
}
// bucket these stats by day
now := time.Now().Truncate(time.Hour * 24)
stats := entity.DBStats{}
err = db.store.TxGet(txn, "stats", &stats)
if err != nil && err != bolthold.ErrNotFound {
txn.Rollback()
return fmt.Errorf("could not get stats: %s", err)
}
if stats.History == nil {
stats.History = make(map[time.Time]entity.BookmarkInfo)
}
2025-05-01 23:39:51 +09:30
stats.History[now] = entity.BookmarkInfo{Bookmarks: bookmarkCount}
err = db.store.TxUpsert(txn, "stats", &stats)
if err != nil {
txn.Rollback()
return fmt.Errorf("could not upsert stats: %s", err)
}
err = txn.Commit()
if err != nil {
return fmt.Errorf("could not commit stats transaction: %s", err)
}
return nil
2022-05-24 18:03:31 +09:30
}