added reindexing

This commit is contained in:
ston1th 2019-08-24 17:42:02 +02:00
commit bb22097b7b
17 changed files with 226 additions and 47 deletions

View file

@ -3,14 +3,72 @@
package db
import (
"errors"
"git.giftfish.de/ston1th/docstore/pkg/core"
"git.giftfish.de/ston1th/docstore/pkg/log"
"git.giftfish.de/ston1th/docstore/pkg/tags"
)
const (
idPrefix = "id/"
// schema:
// key: id/<path>
// value: <id>
idPrefix = "id/"
// schema:
// key: path/<id>
// value: <path>
pathPrefix = "path/"
)
func (db *DB) Reindex(path string, match []core.Tag) (err error) {
if match == nil {
match, err = db.GetAllTags()
if err != nil {
return
}
}
id, err := db.GetID(path)
if err != nil {
return
}
log.Debugf("db: reindexing: %s", path)
doc, err := db.Index.Get(id)
if err != nil {
return
}
doc.Tags = tags.Find(doc.Text, match)
err = db.Index.Reindex(id, doc)
if err != nil {
return
}
return db.SetTag(path, doc.Tags)
}
func (db *DB) ReindexAll() (err error) {
var paths []string
err = db.store.ForEachPrefix(idPrefix, func(k string, _ []byte) error {
paths = append(paths, k)
return nil
})
if err != nil {
return
}
match, err := db.GetAllTags()
if err != nil {
return
}
for _, p := range paths {
e := db.Reindex(p, match)
if e != nil {
if err == nil {
err = errors.New("reindexAll had errors")
}
log.Printf("db: error reindexing %s: %s", p, e)
}
}
return
}
func (db *DB) GetPaths(res []core.Result) {
for i, v := range res {
err := db.store.Get(pathPrefix+v.ID, &res[i].Path)
@ -49,7 +107,7 @@ func (db *DB) DeleteID(path string) error {
return db.store.Delete(idPrefix + path)
}
func (db *DB) NewFile(id, path string, tags []string) (err error) {
func (db *DB) NewFile(id, path string, tag []string) (err error) {
err = db.store.Set(idPrefix+path, id)
if err != nil {
return
@ -58,21 +116,21 @@ func (db *DB) NewFile(id, path string, tags []string) (err error) {
if err != nil {
return
}
return db.SetTag(path, tags)
return db.SetTag(path, tag)
}
func (db *DB) MoveFile(op, np string) (err error) {
if !db.IsIndexed(op) {
return nil
}
id, tags, err := db.DeleteFile(op)
id, tag, err := db.DeleteFile(op)
if err != nil {
return
}
return db.NewFile(id, np, tags)
return db.NewFile(id, np, tag)
}
func (db *DB) DeleteFile(path string) (id string, tags []string, err error) {
func (db *DB) DeleteFile(path string) (id string, tag []string, err error) {
if !db.IsIndexed(path) {
return
}
@ -84,7 +142,7 @@ func (db *DB) DeleteFile(path string) (id string, tags []string, err error) {
if err != nil {
return
}
tags, err = db.UnsetTag(path)
tag, err = db.UnsetTag(path)
if err != nil {
return
}