navidrome/persistence/db_sql/sql_repository.go

141 lines
3.0 KiB
Go
Raw Normal View History

2020-01-12 23:32:06 +01:00
package db_sql
import (
"github.com/astaxie/beego/orm"
2020-01-13 00:36:19 +01:00
"github.com/cloudsonic/sonic-server/domain"
2020-01-13 06:04:11 +01:00
"github.com/cloudsonic/sonic-server/log"
2020-01-12 23:32:06 +01:00
"github.com/cloudsonic/sonic-server/persistence"
)
type sqlRepository struct {
2020-01-13 06:04:11 +01:00
tableName string
2020-01-12 23:32:06 +01:00
}
2020-01-13 00:36:19 +01:00
func (r *sqlRepository) newQuery(o orm.Ormer, options ...domain.QueryOptions) orm.QuerySeter {
2020-01-13 06:04:11 +01:00
q := o.QueryTable(r.tableName)
2020-01-13 00:36:19 +01:00
if len(options) > 0 {
opts := options[0]
q = q.Offset(opts.Offset)
if opts.Size > 0 {
q = q.Limit(opts.Size)
}
if opts.SortBy != "" {
if opts.Desc {
q = q.OrderBy("-" + opts.SortBy)
} else {
q = q.OrderBy(opts.SortBy)
}
}
}
return q
2020-01-12 23:32:06 +01:00
}
func (r *sqlRepository) CountAll() (int64, error) {
return r.newQuery(Db()).Count()
}
func (r *sqlRepository) Exists(id string) (bool, error) {
c, err := r.newQuery(Db()).Filter("id", id).Count()
return c == 1, err
}
2020-01-13 00:55:55 +01:00
// TODO This is used to generate random lists. Can be optimized in SQL: https://stackoverflow.com/a/19419
func (r *sqlRepository) GetAllIds() ([]string, error) {
qs := r.newQuery(Db())
var values []orm.Params
num, err := qs.Values(&values, "id")
if num == 0 {
return nil, err
}
result := persistence.CollectValue(values, func(item interface{}) string {
return item.(orm.Params)["ID"].(string)
})
return result, nil
}
2020-01-13 00:36:19 +01:00
func (r *sqlRepository) put(id string, a interface{}) error {
2020-01-12 23:32:06 +01:00
return WithTx(func(o orm.Ormer) error {
2020-01-13 00:36:19 +01:00
c, err := r.newQuery(o).Filter("id", id).Count()
2020-01-12 23:32:06 +01:00
if err != nil {
return err
}
if c == 0 {
_, err = o.Insert(a)
return err
}
_, err = o.Update(a)
return err
})
}
2020-01-13 06:04:11 +01:00
func paginateSlice(slice []string, skip int, size int) []string {
if skip > len(slice) {
skip = len(slice)
}
end := skip + size
if end > len(slice) {
end = len(slice)
}
return slice[skip:end]
}
func difference(slice1 []string, slice2 []string) []string {
var diffStr []string
m := map[string]int{}
for _, s1Val := range slice1 {
m[s1Val] = 1
}
for _, s2Val := range slice2 {
m[s2Val] = m[s2Val] + 1
}
for mKey, mVal := range m {
if mVal == 1 {
diffStr = append(diffStr, mKey)
2020-01-12 23:32:06 +01:00
}
2020-01-13 06:04:11 +01:00
}
return diffStr
}
2020-01-13 15:06:11 +01:00
func (r *sqlRepository) DeleteAll() error {
_, err := r.newQuery(Db()).Filter("id__isnull", false).Delete()
return err
}
2020-01-13 06:04:11 +01:00
func (r *sqlRepository) purgeInactive(activeList interface{}, getId func(item interface{}) string) ([]string, error) {
allIds, err := r.GetAllIds()
2020-01-12 23:32:06 +01:00
if err != nil {
return nil, err
}
2020-01-13 06:04:11 +01:00
activeIds := persistence.CollectValue(activeList, getId)
idsToDelete := difference(allIds, activeIds)
if len(idsToDelete) == 0 {
return nil, nil
2020-01-12 23:32:06 +01:00
}
2020-01-13 06:04:11 +01:00
log.Debug("Purging inactive records", "table", r.tableName, "total", len(idsToDelete))
err = WithTx(func(o orm.Ormer) error {
var offset int
for {
var subset = paginateSlice(idsToDelete, offset, 100)
if len(subset) == 0 {
break
}
log.Trace("-- Purging inactive records", "table", r.tableName, "num", len(subset), "from", offset)
offset += len(subset)
_, err := r.newQuery(o).Filter("id__in", subset).Delete()
if err != nil {
return err
}
}
return nil
})
return idsToDelete, err
2020-01-12 23:32:06 +01:00
}