linkwallet/db/index.go

80 lines
1.6 KiB
Go
Raw Normal View History

2022-05-24 18:03:31 +09:30
package db
import (
"log"
"time"
"github.com/tardisx/linkwallet/entity"
bolthold "github.com/timshannon/bolthold"
2022-05-24 18:03:31 +09:30
)
func (db *DB) InitIndices() {
wi := entity.WordIndex{}
db.store.DeleteMatching(wi, &bolthold.Query{})
2022-05-24 18:03:31 +09:30
}
func (db *DB) UpdateIndexForWordsByID(words []string, id uint64) {
// delete this id from all indices
txn, err := db.store.Bolt().Begin(true)
if err != nil {
panic(err)
}
2022-05-24 18:03:31 +09:30
db.store.TxForEach(txn, &bolthold.Query{}, func(wi *entity.WordIndex) {
2022-05-24 18:03:31 +09:30
// log.Printf("considering this one: %s", wi.Word)
delete(wi.Bitmap, id)
})
// adding
2022-05-24 18:03:31 +09:30
var find, store time.Duration
for i, word := range words {
// log.Printf("indexing %s", word)
tF := time.Now()
thisWI := entity.WordIndex{Word: word}
err := db.store.TxGet(txn, "word_index_"+word, &thisWI)
if err == bolthold.ErrNotFound {
2022-05-24 18:03:31 +09:30
// create it
thisWI.Bitmap = map[uint64]bool{}
} else if err != nil {
panic(err)
}
findT := time.Since(tF)
tS := time.Now()
thisWI.Bitmap[id] = true
// log.Printf("BM: %v", thisWI.Bitmap)
err = db.store.TxUpsert(txn, "word_index_"+word, thisWI)
if err != nil {
panic(err)
}
findS := time.Since(tS)
find += findT
store += findS
if i > 0 && i%100 == 0 {
txn.Commit()
txn, err = db.store.Bolt().Begin(true)
if err != nil {
panic(err)
}
2022-05-24 18:03:31 +09:30
}
}
//log.Printf("find %s store %s", find, store)
txn.Commit()
}
func (db *DB) DumpIndex() {
// delete this id from all indices
err := db.store.ForEach(&bolthold.Query{}, func(wi *entity.WordIndex) error {
2022-05-24 18:03:31 +09:30
log.Printf("%10s: %v", wi.Word, wi.Bitmap)
return nil
})
if err != nil {
panic(err)
}
}