2022-05-24 18:03:31 +09:30
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
func (db *DB) InitIndices() {
|
|
|
|
wi := entity.WordIndex{}
|
2022-05-28 16:16:08 +09:30
|
|
|
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
|
2022-05-28 16:16:08 +09:30
|
|
|
txn, err := db.store.Bolt().Begin(true)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-06-01 16:33:08 +09:30
|
|
|
db.store.TxForEach(txn, &bolthold.Query{}, func(wi *entity.WordIndex) error {
|
2022-05-24 18:03:31 +09:30
|
|
|
delete(wi.Bitmap, id)
|
2022-08-20 23:44:47 +09:30
|
|
|
// if the index is now completely empty, nuke it entirely
|
|
|
|
empty := true
|
|
|
|
for _, v := range wi.Bitmap {
|
|
|
|
if v {
|
|
|
|
empty = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if empty {
|
|
|
|
db.store.TxDelete(txn, "word_index_"+wi.Word, wi)
|
|
|
|
} else {
|
|
|
|
db.store.TxUpdate(txn, "word_index_"+wi.Word, wi)
|
|
|
|
}
|
2022-06-01 16:33:08 +09:30
|
|
|
return nil
|
2022-05-24 18:03:31 +09:30
|
|
|
})
|
|
|
|
|
2022-05-26 12:19:29 +09:30
|
|
|
// adding
|
2022-05-24 18:03:31 +09:30
|
|
|
for i, word := range words {
|
|
|
|
// log.Printf("indexing %s", word)
|
|
|
|
thisWI := entity.WordIndex{Word: word}
|
|
|
|
err := db.store.TxGet(txn, "word_index_"+word, &thisWI)
|
2022-05-28 16:16:08 +09:30
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
thisWI.Bitmap[id] = true
|
|
|
|
err = db.store.TxUpsert(txn, "word_index_"+word, thisWI)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i > 0 && i%100 == 0 {
|
|
|
|
txn.Commit()
|
2022-05-28 16:16:08 +09:30
|
|
|
txn, err = db.store.Bolt().Begin(true)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-05-24 18:03:31 +09:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
txn.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) DumpIndex() {
|
|
|
|
|
|
|
|
// delete this id from all indices
|
2022-05-28 16:16:08 +09:30
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|