Skip to content

Commit

Permalink
implement database cache
Browse files Browse the repository at this point in the history
  • Loading branch information
MRtecno98 committed Jul 31, 2024
1 parent d7a991c commit a847a98
Show file tree
Hide file tree
Showing 10 changed files with 535 additions and 23 deletions.
89 changes: 89 additions & 0 deletions bucket/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package bucket

func (cp *CachedPlugin) GetName() string {
if cp.RemotePlugin != nil {
return cp.RemotePlugin.GetName()
}

return cp.name
}

func (cp *CachedPlugin) GetIdentifier() string {
if cp.RemotePlugin != nil {
return cp.RemotePlugin.GetIdentifier()
}

return cp.RemoteIdentifier
}

func (cp *CachedPlugin) GetAuthors() []string {
if cp.RemotePlugin != nil {
return cp.RemotePlugin.GetAuthors()
}

return cp.authors
}

func (cp *CachedPlugin) GetDescription() string {
if cp.RemotePlugin != nil {
return cp.RemotePlugin.GetDescription()
}

return cp.description
}

func (cp *CachedPlugin) GetWebsite() string {
if cp.RemotePlugin != nil {
return cp.RemotePlugin.GetWebsite()
}

return cp.website
}

func (cp *CachedPlugin) requestIfMissing() error {
if cp.RemotePlugin == nil {
return cp.Request()
} else {
return nil
}
}

func (cp *CachedPlugin) GetLatestVersion() (RemoteVersion, error) {
if err := cp.requestIfMissing(); err != nil {
return nil, err
}

return cp.RemotePlugin.GetLatestVersion()
}

func (cp *CachedPlugin) GetVersions() ([]RemoteVersion, error) {
if err := cp.requestIfMissing(); err != nil {
return nil, err
}

return cp.RemotePlugin.GetVersions()
}

func (cp *CachedPlugin) GetVersion(version string) (RemoteVersion, error) {
if err := cp.requestIfMissing(); err != nil {
return nil, err
}

return cp.RemotePlugin.GetVersion(version)
}

func (cp *CachedPlugin) GetVersionIdentifiers() ([]string, error) {
if err := cp.requestIfMissing(); err != nil {
return nil, err
}

return cp.RemotePlugin.GetVersionIdentifiers()
}

func (cp *CachedPlugin) GetLatestCompatible(plt PlatformType) (RemoteVersion, error) {
if err := cp.requestIfMissing(); err != nil {
return nil, err
}

return cp.RemotePlugin.GetLatestCompatible(plt)
}
24 changes: 22 additions & 2 deletions bucket/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bucket

import (
"context"
"fmt"
"io"
"log"
"os"
Expand All @@ -24,8 +26,26 @@ type Config struct {
}

type RepositoryConfig struct {
Name string `yaml:"name"`
Options map[string]string `yaml:"options"`
Name string `yaml:"name"`
Provider string `yaml:"provider"`
Options map[string]string `yaml:"options"`
}

func (rc *RepositoryConfig) GetName() string {
if rc.Name == "" {
return rc.Provider
} else {
return rc.Name
}
}

func (rc *RepositoryConfig) MakeRepository(oc *OpenContext) (*NamedRepository, error) {
if constr, ok := Repositories[rc.Provider]; ok {
return &NamedRepository{RepositoryConfig: *rc,
Repository: constr(context.TODO(), oc, rc.Options)}, nil
} else {
return nil, fmt.Errorf("unknown repository: %s", rc.Name)
}
}

func (c *Config) MakeWorkspace() (*Workspace, error) {
Expand Down
86 changes: 73 additions & 13 deletions bucket/context.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package bucket

import (
"context"
"database/sql"
"fmt"
"log"
"os"
Expand All @@ -15,6 +15,8 @@ import (

const SIMILARITY_THRESHOLD float64 = 0.51

var DEFAULT_REPOSITORIES = [...]string{"spigotmc", "modrinth"}

type Context struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
Expand All @@ -25,7 +27,11 @@ type OpenContext struct {
Fs afero.Afero
LocalConfig *Config
Platform Platform
Repositories []Repository
Repositories map[string]NamedRepository

Plugins *SymmetricBiMap[string, CachedPlugin]

Database *sql.DB
}

type Workspace struct {
Expand All @@ -34,6 +40,7 @@ type Workspace struct {

func (w *Workspace) RunWithContext(name string, action func(*OpenContext, *log.Logger) error) error {
var res error = &multierror.Error{Errors: []error{}}
var newline bool = false
for _, c := range w.Contexts {
fmt.Printf(":%s [%s]\n", name, c.Name)

Expand All @@ -51,14 +58,17 @@ func (w *Workspace) RunWithContext(name string, action func(*OpenContext, *log.L
logger.Println()
}
}

newline = true
}

if err != nil {
logger.Printf(":%s [%s] FAILED: %s\n\n", name, c.Name, err)
newline = true
}
}

if len(w.Contexts) > 1 {
if !newline && len(w.Contexts) > 1 {
fmt.Println()
}

Expand All @@ -71,7 +81,7 @@ func (w *Workspace) RunWithContext(name string, action func(*OpenContext, *log.L

func (w *Workspace) CloseWorkspace() {
w.RunWithContext("close", func(c *OpenContext, log *log.Logger) error {
c.Fs.Close()
c.CloseContext()
return nil
})
}
Expand All @@ -88,13 +98,24 @@ func (c Context) OpenContext() (*OpenContext, error) {
conf.Collapse(GlobalConfig) // Also add base options
}

ctx := &OpenContext{Context: c, Fs: afero.Afero{Fs: fs}, LocalConfig: conf}
ctx := &OpenContext{Context: c, Fs: afero.Afero{Fs: fs},
LocalConfig: conf,
Repositories: make(map[string]NamedRepository),
Plugins: NewPluginBiMap()}

if err := ctx.LoadRepositories(); err != nil {
return nil, err
return ctx, Parallelize(
ctx.LoadRepositories,
ctx.LoadPlatform,
ctx.InitialiazeDatabase)
}

func (c *OpenContext) CloseContext() {
if c.Database != nil {
// c.SavePluginDatabase() // Maybe not necessary
c.Database.Close()
}

return ctx, ctx.LoadPlatform()
c.Fs.Close()
}

func (c *OpenContext) PlatformName() string {
Expand Down Expand Up @@ -135,6 +156,10 @@ func (c *OpenContext) LoadPlatform() error {
func (c *OpenContext) ResolvePlugin(plugin Plugin) (RemotePlugin, error) {
var gerr error

if rem, ok := c.Plugins.GetAny(plugin.GetIdentifier()); ok {
return &rem, nil
}

for _, r := range c.Repositories {
if _, candidates, err := r.Resolve(plugin); err != nil {
gerr = multierror.Append(gerr, err)
Expand Down Expand Up @@ -171,19 +196,54 @@ func (c *OpenContext) ResolvePlugin(plugin Plugin) (RemotePlugin, error) {
continue
}

return scores[match], nil
res := CachedMatch(plugin, scores[match], r, match)
if err := c.SavePlugin(res); err != nil {
return nil, err
}

return &res, nil
}
}

return nil, gerr
}

func (c *OpenContext) RepositoryByNameOrProvider(name string) *NamedRepository {
if v, ok := c.Repositories[name]; ok {
return &v
}

if v := c.RepositoryByProvider(name); v != nil {
return v
}

return nil
}

func (c *OpenContext) RepositoryByProvider(provider string) *NamedRepository {
for _, v := range c.Repositories {
if v.Repository.Provider() == provider {
return &v
}
}

return nil
}

func (c *OpenContext) LoadRepositories() error {
for _, v := range c.Config().Repositories {
if constr, ok := Repositories[v.Name]; ok {
c.Repositories = append(c.Repositories, constr(context.Background(), c, v.Options)) // TODO: Use another context
repos := c.Config().Repositories
if len(repos) == 0 {
repos = make([]RepositoryConfig, len(DEFAULT_REPOSITORIES))
for i, v := range DEFAULT_REPOSITORIES {
repos[i] = RepositoryConfig{Provider: v}
}
}

for _, v := range repos {
if r, err := v.MakeRepository(c); err != nil {
return err
} else {
return fmt.Errorf("unknown repository: %s", v.Name)
c.Repositories[v.GetName()] = *r
}
}

Expand Down
Loading

0 comments on commit a847a98

Please sign in to comment.