mirror of
https://github.com/Theodor-Springmann-Stiftung/kgpz_web.git
synced 2025-10-29 09:05:30 +00:00
Speed up startup
This commit is contained in:
@@ -11,11 +11,11 @@ type AgentProvider struct {
|
||||
|
||||
type Agent struct {
|
||||
XMLName xml.Name `xml:"akteur"`
|
||||
ID string `xml:"id,attr"`
|
||||
Names []string `xml:"name"`
|
||||
SortName string `xml:"sortiername"`
|
||||
Life string `xml:"lebensdaten"`
|
||||
GND string `xml:"gnd"`
|
||||
Identifier
|
||||
AnnotationNote
|
||||
}
|
||||
|
||||
|
||||
@@ -15,9 +15,10 @@ type Categories struct {
|
||||
}
|
||||
|
||||
type Category struct {
|
||||
ID string `xml:"id,attr"`
|
||||
XMLName xml.Name `xml:"kategorie"`
|
||||
Names []string `xml:"name"`
|
||||
SortName string `xml:"sortiername"`
|
||||
Identifier
|
||||
AnnotationNote
|
||||
}
|
||||
|
||||
|
||||
120
providers/git.go
120
providers/git.go
@@ -13,9 +13,12 @@ import (
|
||||
|
||||
var InvalidBranchError = errors.New("The currently checked out branch does not match the requested branch. Please checkout the correct branch first.")
|
||||
var InvalidStateError = errors.New("The GitProvider is not in a valid state. Fix the issues or continue without Git data.")
|
||||
var NoURLProvidedError = errors.New("No URL provided for GitProvider.")
|
||||
var NoPathProvidedError = errors.New("No path or branch provided for GitProvider.")
|
||||
|
||||
// NOTE: GitProvider does not open the files, it can only
|
||||
// - clone the repo, given an URL
|
||||
// NOTE: GitProvider does not open XML files, it can only
|
||||
// - read in information from the repo, given a path
|
||||
// - clone the repo, given an URL & a path
|
||||
// - pull the repo, given a path
|
||||
// In case of success in either case it updates the commit hash and date and closes the repo again.
|
||||
// The Files are opened and serialized by the FSProvider, which operates on the same file path.
|
||||
@@ -29,33 +32,67 @@ type GitProvider struct {
|
||||
Date time.Time
|
||||
}
|
||||
|
||||
func NewGitProvider(url string, path string, branch string) *GitProvider {
|
||||
if branch == "" || url == "" || path == "" {
|
||||
return nil
|
||||
func NewGitProvider(url string, path string, branch string) (*GitProvider, error) {
|
||||
// TODO: check if directory is empty
|
||||
// TODO: force clone
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return GitProviderFromPath(path, branch)
|
||||
}
|
||||
return &GitProvider{URL: url, Path: path, Branch: branch}
|
||||
|
||||
return GitProviderFromURL(url, path, branch)
|
||||
}
|
||||
|
||||
func (g *GitProvider) Pull() error {
|
||||
func GitProviderFromPath(path string, branch string) (*GitProvider, error) {
|
||||
if branch == "" || path == "" {
|
||||
return nil, NoPathProvidedError
|
||||
}
|
||||
|
||||
gp := GitProvider{Path: path, Branch: branch}
|
||||
if err := gp.Read(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &gp, nil
|
||||
}
|
||||
|
||||
func GitProviderFromURL(url string, path string, branch string) (*GitProvider, error) {
|
||||
if url == "" {
|
||||
return nil, NoURLProvidedError
|
||||
}
|
||||
|
||||
if branch == "" || path == "" {
|
||||
return nil, NoPathProvidedError
|
||||
}
|
||||
|
||||
gp := GitProvider{URL: url, Path: path, Branch: branch}
|
||||
if err := gp.Clone(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &gp, nil
|
||||
}
|
||||
|
||||
// Returs true if the repo was updated remotely, false otherwise
|
||||
func (g *GitProvider) Pull() (error, bool) {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
|
||||
branch := plumbing.NewBranchReferenceName(g.Branch)
|
||||
repo, err := git.PlainOpen(g.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
return err, false
|
||||
}
|
||||
|
||||
wt, err := repo.Worktree()
|
||||
if err != nil {
|
||||
return err
|
||||
return err, false
|
||||
}
|
||||
|
||||
if err := wt.Checkout(&git.CheckoutOptions{
|
||||
Branch: branch,
|
||||
Force: true,
|
||||
}); err != nil {
|
||||
return err
|
||||
return err, false
|
||||
}
|
||||
|
||||
if err := wt.Pull(&git.PullOptions{
|
||||
@@ -63,14 +100,20 @@ func (g *GitProvider) Pull() error {
|
||||
ReferenceName: branch,
|
||||
Progress: os.Stdout,
|
||||
}); err != nil && err != git.NoErrAlreadyUpToDate {
|
||||
return err
|
||||
return err, false
|
||||
} else if err == git.NoErrAlreadyUpToDate {
|
||||
return nil, false
|
||||
}
|
||||
defer wt.Clean(&git.CleanOptions{Dir: true})
|
||||
|
||||
return g.setValues(repo)
|
||||
return g.setValues(repo), true
|
||||
}
|
||||
|
||||
func (g *GitProvider) Clone() error {
|
||||
if g.URL == "" {
|
||||
return NoURLProvidedError
|
||||
}
|
||||
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
|
||||
@@ -106,7 +149,7 @@ func (g *GitProvider) Clone() error {
|
||||
|
||||
// Implement String Interface
|
||||
func (g *GitProvider) String() string {
|
||||
return fmt.Sprintf("GitProvider{URL: %s, Path: %s, Branch: %s, Commit: %s, Date: %s}", g.URL, g.Path, g.Branch, g.Commit, g.Date)
|
||||
return fmt.Sprintf("GitProvider\nURL: %s\nPath: %s\nBranch: %s\nCommit: %s\nDate: %s\n", g.URL, g.Path, g.Branch, g.Commit, g.Date)
|
||||
}
|
||||
|
||||
func (g *GitProvider) setValues(repo *git.Repository) error {
|
||||
@@ -124,10 +167,9 @@ func (g *GitProvider) setValues(repo *git.Repository) error {
|
||||
g.Commit = commit.Hash.String()
|
||||
g.Date = commit.Author.When
|
||||
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
// WARNING: this expects the repo to be in a certain state and is intended to be used in tests.
|
||||
func (g *GitProvider) Read() error {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
@@ -137,6 +179,47 @@ func (g *GitProvider) Read() error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.ValidateBranch(repo); err != nil {
|
||||
branch := plumbing.NewBranchReferenceName(g.Branch)
|
||||
wt, err := repo.Worktree()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer wt.Clean(&git.CleanOptions{Dir: true})
|
||||
|
||||
if err := wt.Checkout(&git.CheckoutOptions{
|
||||
Branch: branch,
|
||||
Force: true,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.ValidateBranch(repo); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return g.setValues(repo)
|
||||
}
|
||||
|
||||
func (g *GitProvider) Validate() error {
|
||||
repo, err := git.PlainOpen(g.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.ValidateBranch(repo); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.ValidateCommit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *GitProvider) ValidateBranch(repo *git.Repository) error {
|
||||
head, err := repo.Head()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -147,13 +230,10 @@ func (g *GitProvider) Read() error {
|
||||
return InvalidBranchError
|
||||
}
|
||||
|
||||
return g.setValues(repo)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *GitProvider) Validate() error {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
|
||||
func (g *GitProvider) ValidateCommit() error {
|
||||
if g.Commit == "" || g.Date.IsZero() {
|
||||
return InvalidStateError
|
||||
}
|
||||
|
||||
@@ -15,26 +15,30 @@ type Issues struct {
|
||||
}
|
||||
|
||||
type Issue struct {
|
||||
ID string `xml:"id,attr"`
|
||||
XMLName xml.Name `xml:"stueck"`
|
||||
Number IssueNumber `xml:"nummer"`
|
||||
Datum KGPZDate `xml:"datum"`
|
||||
Von string `xml:"von"`
|
||||
Bis string `xml:"bis"`
|
||||
Additionals []Additional `xml:"beilage"`
|
||||
Identifier
|
||||
AnnotationNote
|
||||
}
|
||||
|
||||
type IssueNumber struct {
|
||||
XMLName xml.Name `xml:"nummer"`
|
||||
Value string `xml:",chardata"`
|
||||
Corrected string `xml:"korrigiert,attr"`
|
||||
XMLName xml.Name `xml:"nummer"`
|
||||
Value
|
||||
Corrected string `xml:"korrigiert,attr"`
|
||||
}
|
||||
|
||||
type KGPZDate struct {
|
||||
When string `xml:"when,attr"`
|
||||
NotBefore string `xml:"notBefore,attr"`
|
||||
NotAfter string `xml:"notAfter,attr"`
|
||||
From string `xml:"from,attr"`
|
||||
To string `xml:"to,attr"`
|
||||
XMLName xml.Name `xml:"datum"`
|
||||
When string `xml:"when,attr"`
|
||||
NotBefore string `xml:"notBefore,attr"`
|
||||
NotAfter string `xml:"notAfter,attr"`
|
||||
From string `xml:"from,attr"`
|
||||
To string `xml:"to,attr"`
|
||||
Value
|
||||
}
|
||||
|
||||
type Additional struct {
|
||||
|
||||
@@ -15,7 +15,7 @@ type Pieces struct {
|
||||
}
|
||||
|
||||
type Piece struct {
|
||||
ID string `xml:"id,attr"`
|
||||
XMLName xml.Name `xml:"beitrag"`
|
||||
IssueRefs []IssueRef `xml:"stueck"`
|
||||
PlaceRefs []PlaceRef `xml:"ort"`
|
||||
CategoryRefs []CategoryRef `xml:"kategorie"`
|
||||
@@ -25,50 +25,10 @@ type Piece struct {
|
||||
AdditionalRef []AdditionalRef `xml:"beilage"`
|
||||
Incipit []string `xml:"incipit"`
|
||||
Title []string `xml:"titel"`
|
||||
Identifier
|
||||
AnnotationNote
|
||||
}
|
||||
|
||||
type AdditionalRef struct {
|
||||
Datum string `xml:"datum,attr"`
|
||||
Nr string `xml:"nr,attr"`
|
||||
Von string `xml:"von,attr"`
|
||||
Bis string `xml:"bis,attr"`
|
||||
Value string `xml:",chardata"`
|
||||
}
|
||||
|
||||
type IssueRef struct {
|
||||
Datum string `xml:"datum,attr"`
|
||||
Nr string `xml:"nr,attr"`
|
||||
Von string `xml:"von,attr"`
|
||||
Bis string `xml:"bis,attr"`
|
||||
Value string `xml:",chardata"`
|
||||
Category string `xml:"kat,attr"`
|
||||
}
|
||||
|
||||
type PlaceRef struct {
|
||||
Ref string `xml:"ref,attr"`
|
||||
Value string `xml:",chardata"`
|
||||
Category string `xml:"kat,attr"`
|
||||
}
|
||||
|
||||
type CategoryRef struct {
|
||||
Ref string `xml:"ref,attr"`
|
||||
Value string `xml:",chardata"`
|
||||
}
|
||||
|
||||
type WorkRef struct {
|
||||
Ref string `xml:"ref,attr"`
|
||||
Value string `xml:",chardata"`
|
||||
Category string `xml:"kat,attr"`
|
||||
Page string `xml:"s,attr"`
|
||||
}
|
||||
|
||||
type PieceRef struct {
|
||||
Ref string `xml:"ref,attr"`
|
||||
Category string `xml:"kat,attr"`
|
||||
Value string `xml:",chardata"`
|
||||
}
|
||||
|
||||
func (p Pieces) Append(data Pieces) Pieces {
|
||||
p.Piece = append(p.Piece, data.Piece...)
|
||||
return p
|
||||
|
||||
@@ -15,10 +15,11 @@ type Places struct {
|
||||
}
|
||||
|
||||
type Place struct {
|
||||
ID string `xml:"id,attr"`
|
||||
XMLName xml.Name `xml:"ort"`
|
||||
Names []string `xml:"name"`
|
||||
SortName string `xml:"sortiername"`
|
||||
Geo string `xml:"geonames"`
|
||||
Identifier
|
||||
AnnotationNote
|
||||
}
|
||||
|
||||
|
||||
@@ -15,24 +15,14 @@ type Works struct {
|
||||
}
|
||||
|
||||
type Work struct {
|
||||
ID string `xml:"id,attr"`
|
||||
XMLName xml.Name `xml:"werk"`
|
||||
URLs []URL `xml:"url"`
|
||||
Citation []string `xml:"zitation"`
|
||||
Akteur []AgentRef `xml:"akteur"`
|
||||
Identifier
|
||||
AnnotationNote
|
||||
}
|
||||
|
||||
type AgentRef struct {
|
||||
Ref string `xml:"ref,attr"`
|
||||
Category string `xml:"Kat,attr"`
|
||||
Value string `xml:",chardata"`
|
||||
}
|
||||
|
||||
type URL struct {
|
||||
Address string `xml:"address,attr"`
|
||||
Value string `xml:",chardata"`
|
||||
}
|
||||
|
||||
func (w Works) Append(data Works) Works {
|
||||
w.Work = append(w.Work, data.Work...)
|
||||
return w
|
||||
|
||||
@@ -1,48 +1,55 @@
|
||||
package providers
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
import "encoding/xml"
|
||||
|
||||
type KGPZXML[T any] interface {
|
||||
Append(data T) T
|
||||
fmt.Stringer
|
||||
type AgentRef struct {
|
||||
XMLName xml.Name `xml:"akteur"`
|
||||
Reference
|
||||
}
|
||||
|
||||
type XMLProvider[T KGPZXML[T]] struct {
|
||||
mu sync.Mutex
|
||||
paths []string
|
||||
Items T
|
||||
type URL struct {
|
||||
XMLName xml.Name `xml:"url"`
|
||||
Address string `xml:"address,attr"`
|
||||
Value
|
||||
}
|
||||
|
||||
func (p *XMLProvider[T]) Load() error {
|
||||
var wg sync.WaitGroup
|
||||
for _, path := range p.paths {
|
||||
wg.Add(1)
|
||||
go func(path string) {
|
||||
defer wg.Done()
|
||||
var data T
|
||||
if err := UnmarshalFile(path, &data); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
p.mu.Lock()
|
||||
p.Items = p.Items.Append(data)
|
||||
p.mu.Unlock()
|
||||
}(path)
|
||||
}
|
||||
wg.Wait()
|
||||
return nil
|
||||
type AdditionalRef struct {
|
||||
XMLName xml.Name `xml:"beilage"`
|
||||
Reference
|
||||
Datum string `xml:"datum,attr"`
|
||||
Nr string `xml:"nr,attr"`
|
||||
Von string `xml:"von,attr"`
|
||||
Bis string `xml:"bis,attr"`
|
||||
}
|
||||
|
||||
func (a *XMLProvider[T]) String() string {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
return fmt.Sprintf("Items: %s", a.Items)
|
||||
type IssueRef struct {
|
||||
XMLName xml.Name `xml:"stueck"`
|
||||
Reference
|
||||
Datum string `xml:"datum,attr"`
|
||||
Nr string `xml:"nr,attr"`
|
||||
Von string `xml:"von,attr"`
|
||||
Bis string `xml:"bis,attr"`
|
||||
}
|
||||
|
||||
type PlaceRef struct {
|
||||
XMLName xml.Name `xml:"ort"`
|
||||
Reference
|
||||
}
|
||||
|
||||
type CategoryRef struct {
|
||||
XMLName xml.Name `xml:"kategorie"`
|
||||
Reference
|
||||
}
|
||||
|
||||
type WorkRef struct {
|
||||
XMLName xml.Name `xml:"werk"`
|
||||
Reference
|
||||
Page string `xml:"s,attr"`
|
||||
}
|
||||
|
||||
type PieceRef struct {
|
||||
XMLName xml.Name `xml:"beitrag"`
|
||||
Reference
|
||||
}
|
||||
|
||||
type AnnotationNote struct {
|
||||
@@ -50,16 +57,17 @@ type AnnotationNote struct {
|
||||
Notes []string `xml:"vermerk"`
|
||||
}
|
||||
|
||||
func UnmarshalFile[T any](filename string, data *T) error {
|
||||
xmlFile, err := os.Open(filename)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
fmt.Println("Successfully opened " + filename)
|
||||
defer xmlFile.Close()
|
||||
byteValue, _ := io.ReadAll(xmlFile)
|
||||
xml.Unmarshal(byteValue, data)
|
||||
|
||||
return nil
|
||||
type Identifier struct {
|
||||
ID string `xml:"id,attr"`
|
||||
}
|
||||
|
||||
type Reference struct {
|
||||
Ref string `xml:"ref,attr"`
|
||||
Category string `xml:"kat,attr"`
|
||||
Unsicher bool `xml:"unsicher,attr"`
|
||||
Value
|
||||
}
|
||||
|
||||
type Value struct {
|
||||
Value string `xml:",chardata"`
|
||||
}
|
||||
|
||||
60
providers/xmlprovider.go
Normal file
60
providers/xmlprovider.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package providers
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type KGPZXML[T any] interface {
|
||||
Append(data T) T
|
||||
fmt.Stringer
|
||||
}
|
||||
|
||||
type XMLProvider[T KGPZXML[T]] struct {
|
||||
mu sync.Mutex
|
||||
paths []string
|
||||
Items T
|
||||
}
|
||||
|
||||
func (p *XMLProvider[T]) Load() error {
|
||||
var wg sync.WaitGroup
|
||||
for _, path := range p.paths {
|
||||
wg.Add(1)
|
||||
go func(path string) {
|
||||
defer wg.Done()
|
||||
var data T
|
||||
if err := UnmarshalFile(path, &data); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
p.mu.Lock()
|
||||
p.Items = p.Items.Append(data)
|
||||
p.mu.Unlock()
|
||||
}(path)
|
||||
}
|
||||
wg.Wait()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *XMLProvider[T]) String() string {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
return fmt.Sprintf("Items: %s", a.Items)
|
||||
}
|
||||
|
||||
func UnmarshalFile[T any](filename string, data *T) error {
|
||||
xmlFile, err := os.Open(filename)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
fmt.Println("Successfully opened " + filename)
|
||||
defer xmlFile.Close()
|
||||
byteValue, _ := io.ReadAll(xmlFile)
|
||||
xml.Unmarshal(byteValue, data)
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user