Move wisski instance code to separate package
This commit is contained in:
parent
7c3c84e116
commit
063f3f9b7d
67 changed files with 533 additions and 409 deletions
|
|
@ -5,9 +5,12 @@ import (
|
|||
"path/filepath"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/component"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/component/meta"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/component/snapshotslog"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/component/sql"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/component/triplestore"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/models"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
|
||||
"github.com/tkw1536/goprogram/exit"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
|
|
@ -17,8 +20,10 @@ import (
|
|||
type Instances struct {
|
||||
component.ComponentBase
|
||||
|
||||
TS *triplestore.Triplestore
|
||||
SQL *sql.SQL
|
||||
TS *triplestore.Triplestore
|
||||
SQL *sql.SQL
|
||||
Meta *meta.Meta
|
||||
SnapshotsLog *snapshotslog.SnapshotsLog
|
||||
}
|
||||
|
||||
func (Instances) Name() string {
|
||||
|
|
@ -37,40 +42,53 @@ var errSQL = exit.Error{
|
|||
ExitCode: exit.ExitGeneric,
|
||||
}
|
||||
|
||||
// Instance is a convenience function to return an instance based on a model slug.
|
||||
// When the instance does not exist, returns nil.
|
||||
func (instances *Instances) Instance(instance models.Instance) *WissKI {
|
||||
i, err := instances.WissKI(instance.Slug)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return &i
|
||||
// use uses the non-nil wisski instance with this instances
|
||||
func (instances *Instances) use(wisski *wisski.WissKI) {
|
||||
wisski.Core = instances.Core
|
||||
wisski.SQL = instances.SQL
|
||||
wisski.TS = instances.TS
|
||||
wisski.Meta = instances.Meta
|
||||
wisski.SnapshotsLog = instances.SnapshotsLog
|
||||
}
|
||||
|
||||
// WissKI returns the WissKI with the provided slug, if it exists.
|
||||
// It the WissKI does not exist, returns ErrWissKINotFound.
|
||||
func (instances *Instances) WissKI(slug string) (i WissKI, err error) {
|
||||
func (instances *Instances) WissKI(slug string) (wissKI *wisski.WissKI, err error) {
|
||||
sql := instances.SQL
|
||||
if err := sql.WaitQueryTable(); err != nil {
|
||||
return i, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
table, err := sql.QueryTable(false, models.InstanceTable)
|
||||
if err != nil {
|
||||
return i, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// create a struct
|
||||
wissKI = new(wisski.WissKI)
|
||||
|
||||
// find the instance by slug
|
||||
query := table.Where(&models.Instance{Slug: slug}).Find(&i.Instance)
|
||||
query := table.Where(&models.Instance{Slug: slug}).Find(&wissKI.Instance)
|
||||
switch {
|
||||
case query.Error != nil:
|
||||
return i, errSQL.WithMessageF(query.Error)
|
||||
return nil, errSQL.WithMessageF(query.Error)
|
||||
case query.RowsAffected == 0:
|
||||
return i, ErrWissKINotFound
|
||||
default:
|
||||
i.instances = instances
|
||||
return i, nil
|
||||
return nil, ErrWissKINotFound
|
||||
}
|
||||
|
||||
// use the wissKI instance
|
||||
instances.use(wissKI)
|
||||
return wissKI, nil
|
||||
}
|
||||
|
||||
// Instance is a convenience function to return an instance based on a model slug.
|
||||
// When the instance does not exist, returns nil.
|
||||
func (instances *Instances) Instance(instance models.Instance) *wisski.WissKI {
|
||||
wissKI, err := instances.WissKI(instance.Slug)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return wissKI
|
||||
}
|
||||
|
||||
// Has checks if a WissKI with the provided slug exists inside the database.
|
||||
|
|
@ -96,7 +114,7 @@ func (instances *Instances) Has(slug string) (ok bool, err error) {
|
|||
// All returns all instances of the WissKI Distillery in consistent order.
|
||||
//
|
||||
// There is no guarantee that this order remains identical between different api releases; however subsequent invocations are guaranteed to return the same order.
|
||||
func (instances *Instances) All() ([]WissKI, error) {
|
||||
func (instances *Instances) All() ([]*wisski.WissKI, error) {
|
||||
return instances.find(true, func(table *gorm.DB) *gorm.DB {
|
||||
return table
|
||||
})
|
||||
|
|
@ -104,14 +122,14 @@ func (instances *Instances) All() ([]WissKI, error) {
|
|||
|
||||
// WissKIs returns the WissKI instances with the provides slugs.
|
||||
// If a slug does not exist, it is omitted from the result.
|
||||
func (instances *Instances) WissKIs(slugs ...string) ([]WissKI, error) {
|
||||
func (instances *Instances) WissKIs(slugs ...string) ([]*wisski.WissKI, error) {
|
||||
return instances.find(true, func(table *gorm.DB) *gorm.DB {
|
||||
return table.Where("slug IN ?", slugs)
|
||||
})
|
||||
}
|
||||
|
||||
// Load is like All, except that when no slugs are provided, it calls All.
|
||||
func (instances *Instances) Load(slugs ...string) ([]WissKI, error) {
|
||||
func (instances *Instances) Load(slugs ...string) ([]*wisski.WissKI, error) {
|
||||
if len(slugs) == 0 {
|
||||
return instances.All()
|
||||
}
|
||||
|
|
@ -119,7 +137,7 @@ func (instances *Instances) Load(slugs ...string) ([]WissKI, error) {
|
|||
}
|
||||
|
||||
// find finds instances based on the provided query
|
||||
func (instances *Instances) find(order bool, query func(table *gorm.DB) *gorm.DB) (results []WissKI, err error) {
|
||||
func (instances *Instances) find(order bool, query func(table *gorm.DB) *gorm.DB) (results []*wisski.WissKI, err error) {
|
||||
sql := instances.SQL
|
||||
if err := sql.WaitQueryTable(); err != nil {
|
||||
return nil, err
|
||||
|
|
@ -148,10 +166,11 @@ func (instances *Instances) find(order bool, query func(table *gorm.DB) *gorm.DB
|
|||
}
|
||||
|
||||
// make proper instances
|
||||
results = make([]WissKI, len(bks))
|
||||
results = make([]*wisski.WissKI, len(bks))
|
||||
for i, bk := range bks {
|
||||
results[i] = new(wisski.WissKI)
|
||||
results[i].Instance = bk
|
||||
results[i].instances = instances
|
||||
instances.use(results[i])
|
||||
}
|
||||
|
||||
return results, nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue