2022-05-24 18:03:31 +09:30
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2022-05-28 19:28:18 +09:30
|
|
|
"fmt"
|
2022-08-20 23:44:47 +09:30
|
|
|
"time"
|
2022-05-24 18:03:31 +09:30
|
|
|
|
2025-05-03 12:37:44 +09:30
|
|
|
"github.com/blevesearch/bleve/v2"
|
|
|
|
|
|
|
|
"github.com/blevesearch/bleve/v2/analysis/analyzer/keyword"
|
|
|
|
"github.com/blevesearch/bleve/v2/analysis/lang/en"
|
|
|
|
"github.com/blevesearch/bleve/v2/mapping"
|
2022-05-24 18:03:31 +09:30
|
|
|
"github.com/tardisx/linkwallet/entity"
|
2022-05-28 16:16:08 +09:30
|
|
|
bolthold "github.com/timshannon/bolthold"
|
2022-05-24 18:03:31 +09:30
|
|
|
)
|
|
|
|
|
|
|
|
type DB struct {
|
2022-05-28 16:16:08 +09:30
|
|
|
store *bolthold.Store
|
2022-08-21 13:24:52 +09:30
|
|
|
file string
|
2025-05-03 12:37:44 +09:30
|
|
|
bleve bleve.Index
|
2022-05-24 18:03:31 +09:30
|
|
|
}
|
|
|
|
|
2025-05-03 12:37:44 +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) {
|
2022-05-28 16:16:08 +09:30
|
|
|
// options := bolthold.DefaultOptions
|
|
|
|
// options.Dir = dir
|
|
|
|
// options.ValueDir = dir
|
2025-05-03 12:37:44 +09:30
|
|
|
rescrapeNeeded := false
|
2022-05-28 19:28:18 +09:30
|
|
|
store, err := bolthold.Open(path, 0666, nil)
|
2022-05-24 18:03:31 +09:30
|
|
|
if err != nil {
|
2025-05-03 12:37:44 +09:30
|
|
|
return false, fmt.Errorf("cannot open '%s' - %s", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
blevePath := path + ".bleve"
|
|
|
|
|
|
|
|
index, err := bleve.New(blevePath, createIndexMapping())
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false, fmt.Errorf("cannot open bleve '%s' - %s", path, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we just created an index, one didn't exist, so we need to queue
|
|
|
|
// all bookmarks to be scraped
|
|
|
|
rescrapeNeeded = true
|
2022-05-24 18:03:31 +09:30
|
|
|
}
|
2025-05-03 12:37:44 +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-05-03 12:37:44 +09:30
|
|
|
db.bleve = index
|
|
|
|
return rescrapeNeeded, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createIndexMapping() mapping.IndexMapping {
|
|
|
|
indexMapping := bleve.NewIndexMapping()
|
|
|
|
|
|
|
|
englishTextFieldMapping := bleve.NewTextFieldMapping()
|
|
|
|
englishTextFieldMapping.Analyzer = en.AnalyzerName
|
|
|
|
|
|
|
|
// a generic reusable mapping for keyword text
|
|
|
|
keywordFieldMapping := bleve.NewTextFieldMapping()
|
|
|
|
keywordFieldMapping.Analyzer = keyword.Name
|
|
|
|
|
|
|
|
pageInfoMapping := bleve.NewDocumentMapping()
|
|
|
|
pageInfoMapping.AddFieldMappingsAt("Title", englishTextFieldMapping)
|
|
|
|
pageInfoMapping.AddFieldMappingsAt("Size", bleve.NewNumericFieldMapping())
|
|
|
|
pageInfoMapping.AddFieldMappingsAt("RawText", englishTextFieldMapping)
|
|
|
|
|
|
|
|
bookmarkMapping := bleve.NewDocumentMapping()
|
|
|
|
bookmarkMapping.AddFieldMappingsAt("URL", bleve.NewTextFieldMapping())
|
|
|
|
bookmarkMapping.AddFieldMappingsAt("Tags", keywordFieldMapping)
|
|
|
|
bookmarkMapping.AddSubDocumentMapping("Info", pageInfoMapping)
|
|
|
|
|
|
|
|
indexMapping.AddDocumentMapping("bookmark", bookmarkMapping)
|
|
|
|
|
|
|
|
return indexMapping
|
2022-05-24 18:03:31 +09:30
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) Close() {
|
|
|
|
db.store.Close()
|
|
|
|
}
|
|
|
|
|
2022-08-20 23:44:47 +09:30
|
|
|
// 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-03 12:37:44 +09:30
|
|
|
stats.History[now] = entity.BookmarkInfo{Bookmarks: bookmarkCount}
|
2022-08-20 23:44:47 +09:30
|
|
|
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
|
|
|
}
|