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
import "html/template"
import (
"html/template"
"regexp"
)
type User struct {
Username string
@ -25,6 +28,11 @@ type Tag struct {
Regex string
}
type RTag struct {
Name string
Regex *regexp.Regexp
}
type Path struct {
Name string
Abs string

View file

@ -20,9 +20,9 @@ const (
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 {
match, err = db.GetAllTags()
match, err = db.GetAllRTags()
if err != nil {
return
}
@ -53,7 +53,7 @@ func (db *DB) ReindexAll() (err error) {
if err != nil {
return
}
match, err := db.GetAllTags()
match, err := db.GetAllRTags()
if err != nil {
return
}

View file

@ -41,6 +41,17 @@ func (db *DB) GetAllTags() (tags []core.Tag, err error) {
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) {
err = db.store.Get(tagsPrefix+name, &t)
return

View file

@ -187,11 +187,11 @@ func scanFile(ctx *Context, path string) (err error) {
l.Printf("scanner: %s: %s", path, err)
return
}
t, err := ctx.Srv.DB.GetAllTags()
rt, err := ctx.Srv.DB.GetAllRTags()
if err != nil {
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)
if err != nil {
l.Printf("index: %s: %s", path, err)

View file

@ -4,13 +4,11 @@ package tags
import (
"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 {
re := regexp.MustCompile(t.Regex)
if re.MatchString(text) {
if t.Regex.MatchString(text) {
found = append(found, t.Name)
}
}