compile tags regex once

This commit is contained in:
ston1th 2019-08-24 20:29:43 +02:00
commit 7686dfa474
5 changed files with 27 additions and 10 deletions

View file

@ -2,7 +2,10 @@
package core package core
import "html/template" import (
"html/template"
"regexp"
)
type User struct { type User struct {
Username string Username string
@ -25,6 +28,11 @@ type Tag struct {
Regex string Regex string
} }
type RTag struct {
Name string
Regex *regexp.Regexp
}
type Path struct { type Path struct {
Name string Name string
Abs string Abs string

View file

@ -20,9 +20,9 @@ const (
pathPrefix = "path/" pathPrefix = "path/"
) )
func (db *DB) Reindex(path string, match []core.Tag) (err error) { func (db *DB) Reindex(path string, match []core.RTag) (err error) {
if match == nil { if match == nil {
match, err = db.GetAllTags() match, err = db.GetAllRTags()
if err != nil { if err != nil {
return return
} }
@ -53,7 +53,7 @@ func (db *DB) ReindexAll() (err error) {
if err != nil { if err != nil {
return return
} }
match, err := db.GetAllTags() match, err := db.GetAllRTags()
if err != nil { if err != nil {
return return
} }

View file

@ -41,6 +41,17 @@ func (db *DB) GetAllTags() (tags []core.Tag, err error) {
return return
} }
func (db *DB) GetAllRTags() (rtags []core.RTag, err error) {
tags, err := db.GetAllTags()
if err != nil {
return
}
for _, v := range tags {
rtags = append(rtags, core.RTag{v.Name, regexp.MustCompile(v.Regex)})
}
return
}
func (db *DB) GetTags(name string) (t core.Tag, err error) { func (db *DB) GetTags(name string) (t core.Tag, err error) {
err = db.store.Get(tagsPrefix+name, &t) err = db.store.Get(tagsPrefix+name, &t)
return return

View file

@ -187,11 +187,11 @@ func scanFile(ctx *Context, path string) (err error) {
l.Printf("scanner: %s: %s", path, err) l.Printf("scanner: %s: %s", path, err)
return return
} }
t, err := ctx.Srv.DB.GetAllTags() rt, err := ctx.Srv.DB.GetAllRTags()
if err != nil { if err != nil {
l.Printf("getTags: %s: %s", path, err) l.Printf("getTags: %s: %s", path, err)
} }
found := tags.Find(txt, t) found := tags.Find(txt, rt)
id, err := ctx.Srv.DB.Index.Add(txt, found) id, err := ctx.Srv.DB.Index.Add(txt, found)
if err != nil { if err != nil {
l.Printf("index: %s: %s", path, err) l.Printf("index: %s: %s", path, err)

View file

@ -4,13 +4,11 @@ package tags
import ( import (
"git.giftfish.de/ston1th/docstore/pkg/core" "git.giftfish.de/ston1th/docstore/pkg/core"
"regexp"
) )
func Find(text string, tags []core.Tag) (found []string) { func Find(text string, tags []core.RTag) (found []string) {
for _, t := range tags { for _, t := range tags {
re := regexp.MustCompile(t.Regex) if t.Regex.MatchString(text) {
if re.MatchString(text) {
found = append(found, t.Name) found = append(found, t.Name)
} }
} }