components/resolver: Use database instead of file approach
This commit is contained in:
parent
f6b38f055d
commit
32107265d4
10 changed files with 165 additions and 103 deletions
|
|
@ -35,9 +35,9 @@ type MetaStorage interface {
|
|||
// Any other metadata with the same key is deleted.
|
||||
Set(key MetaKey, value any) error
|
||||
|
||||
// Add serializes values and stores each as associated with the provided key.
|
||||
// Already existing metadata is left intact.
|
||||
Add(key MetaKey, values ...any) error
|
||||
// Set serializes values and stores them with the provided key.
|
||||
// Any other metadata with the same key is deleted.
|
||||
SetAll(key MetaKey, values ...any) error
|
||||
|
||||
// Purge removes all metadata, regardless of key.
|
||||
Purge() error
|
||||
|
|
@ -163,13 +163,19 @@ func (s *storage) Set(key MetaKey, value any) error {
|
|||
})
|
||||
}
|
||||
|
||||
func (s *storage) Add(key MetaKey, values ...any) error {
|
||||
func (s *storage) SetAll(key MetaKey, values ...any) error {
|
||||
table, err := s.SQL.QueryTable(true, models.MetadataTable)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return table.Transaction(func(tx *gorm.DB) error {
|
||||
// delete the old values
|
||||
status := tx.Where(&models.Metadatum{Slug: s.Slug, Key: string(key)}).Delete(&models.Metadatum{})
|
||||
if err := status.Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, value := range values {
|
||||
bytes, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -82,6 +82,29 @@ func hasAnyPrefix(candidate string, prefixes []string) bool {
|
|||
)
|
||||
}
|
||||
|
||||
var PrefixConfigKey MetaKey = "prefix"
|
||||
|
||||
// Prefixes returns the prefixes for the instance
|
||||
func (wisski *WissKI) PrefixesCached() (results []string, err error) {
|
||||
err = wisski.Metadata().GetAll(PrefixConfigKey, func(index, total int) any {
|
||||
if results == nil {
|
||||
results = make([]string, total)
|
||||
}
|
||||
return &results[index]
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// UpdatePrefixes updates the cached prefixes of this instance
|
||||
func (wisski *WissKI) UpdatePrefixes() error {
|
||||
prefixes, err := wisski.Prefixes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return wisski.Metadata().SetAll(PrefixConfigKey, slicesx.AsAny(prefixes)...)
|
||||
}
|
||||
|
||||
// PrefixConfig returns the prefix config belonging to this instance.
|
||||
func (wisski *WissKI) PrefixConfig() (config string, err error) {
|
||||
// if the user requested to skip the prefix, then don't do anything with it!
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue