Restructure data layer (#2532)

* Add new txn manager interface
* Add txn management to sqlite
* Rename get to getByID
* Add contexts to repository methods
* Update query builders
* Add context to reader writer interfaces
* Use repository in resolver
* Tighten interfaces
* Tighten interfaces in dlna
* Tighten interfaces in match package
* Tighten interfaces in scraper package
* Tighten interfaces in scan code
* Tighten interfaces on autotag package
* Remove ReaderWriter usage
* Merge database package into sqlite
This commit is contained in:
WithoutPants
2022-05-19 17:49:32 +10:00
parent 7b5bd80515
commit 964b559309
244 changed files with 7377 additions and 6699 deletions

42
pkg/sqlite/regex.go Normal file
View File

@@ -0,0 +1,42 @@
package sqlite
import (
"regexp"
lru "github.com/hashicorp/golang-lru"
)
// size of the regex LRU cache in elements.
// A small number number was chosen because it's most likely use is for a
// single query - this function gets called for every row in the (filtered)
// results. It's likely to only need no more than 1 or 2 in any given query.
// After that point, it's just sitting in the cache and is unlikely to be used
// again.
const regexCacheSize = 10
var regexCache *lru.Cache
func init() {
regexCache, _ = lru.New(regexCacheSize)
}
// regexFn is registered as an SQLite function as "regexp"
// It uses an LRU cache to cache recent regex patterns to reduce CPU load over
// identical patterns.
func regexFn(re, s string) (bool, error) {
entry, ok := regexCache.Get(re)
var compiled *regexp.Regexp
if !ok {
var err error
compiled, err = regexp.Compile(re)
if err != nil {
return false, err
}
regexCache.Add(re, compiled)
} else {
compiled = entry.(*regexp.Regexp)
}
return compiled.MatchString(s), nil
}