Rework to use bolthold/boltdb instead of badgerhold/badgerdb, much more memory efficient

This commit is contained in:
2022-05-28 16:16:08 +09:30
parent 10e205dd8e
commit 8e08329711
10 changed files with 47 additions and 208 deletions

View File

@@ -5,20 +5,22 @@ import (
"time"
"github.com/tardisx/linkwallet/entity"
badgerhold "github.com/timshannon/badgerhold/v4"
bolthold "github.com/timshannon/bolthold"
)
func (db *DB) InitIndices() {
wi := entity.WordIndex{}
db.store.DeleteMatching(wi, &badgerhold.Query{})
db.store.DeleteMatching(wi, &bolthold.Query{})
}
func (db *DB) UpdateIndexForWordsByID(words []string, id uint64) {
// delete this id from all indices
txn := db.store.Badger().NewTransaction(true)
txn, err := db.store.Bolt().Begin(true)
if err != nil {
panic(err)
}
db.store.TxForEach(txn, &badgerhold.Query{}, func(wi *entity.WordIndex) {
db.store.TxForEach(txn, &bolthold.Query{}, func(wi *entity.WordIndex) {
// log.Printf("considering this one: %s", wi.Word)
delete(wi.Bitmap, id)
})
@@ -30,8 +32,7 @@ func (db *DB) UpdateIndexForWordsByID(words []string, id uint64) {
tF := time.Now()
thisWI := entity.WordIndex{Word: word}
err := db.store.TxGet(txn, "word_index_"+word, &thisWI)
// err := db.store.TxFindOne(txn, &thisWI, badgerhold.Where("Word").Eq(word).Index("Word"))
if err == badgerhold.ErrNotFound {
if err == bolthold.ErrNotFound {
// create it
thisWI.Bitmap = map[uint64]bool{}
} else if err != nil {
@@ -52,7 +53,10 @@ func (db *DB) UpdateIndexForWordsByID(words []string, id uint64) {
if i > 0 && i%100 == 0 {
txn.Commit()
txn = db.store.Badger().NewTransaction(true)
txn, err = db.store.Bolt().Begin(true)
if err != nil {
panic(err)
}
}
}
@@ -64,7 +68,7 @@ func (db *DB) UpdateIndexForWordsByID(words []string, id uint64) {
func (db *DB) DumpIndex() {
// delete this id from all indices
err := db.store.ForEach(&badgerhold.Query{}, func(wi *entity.WordIndex) error {
err := db.store.ForEach(&bolthold.Query{}, func(wi *entity.WordIndex) error {
log.Printf("%10s: %v", wi.Word, wi.Bitmap)
return nil
})