Skip to content

Commit

Permalink
Fixes #285: Unnecessary file upload
Browse files Browse the repository at this point in the history
This change attempts to fix #285 by only uploading the module/provider index when it has actually changed

Signed-off-by: AbstractionFactory <[email protected]>
  • Loading branch information
abstractionfactory committed Feb 12, 2025
1 parent 77976b9 commit aef1ced
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 10 deletions.
4 changes: 2 additions & 2 deletions backend/internal/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@ func (b backend) Generate(ctx context.Context, opts ...GenerateOpt) error {
eg.Go(func() error {
b.cfg.Logger.Info(ctx, "Cloning registry repository...")
if err := b.cloner.Clone(ctx); err != nil {
b.cfg.Logger.Error(ctx, "Clone failed (%v).", err)
b.cfg.Logger.Error(ctx, "DeepCopy failed (%v).", err)
return fmt.Errorf("failed to clone registry (%w)", err)
}
b.cfg.Logger.Info(ctx, "Clone complete.")
b.cfg.Logger.Info(ctx, "DeepCopy complete.")
return nil
})
if err := eg.Wait(); err != nil {
Expand Down
17 changes: 11 additions & 6 deletions backend/internal/moduleindex/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func (g generator) generate(ctx context.Context, moduleList []module.Addr, block

moduleIndexPath := path.Join(moduleAddr.Namespace, moduleAddr.Name, moduleAddr.TargetSystem, "index.json")
entry := modules.GetModule(moduleAddr.Addr)
var originalEntry *Module
needsAdd := false
if entry == nil {
entry = &Module{
Expand All @@ -239,6 +240,8 @@ func (g generator) generate(ctx context.Context, moduleList []module.Addr, block
BlockedReason: blockedReason,
}
needsAdd = true
} else {
originalEntry = entry.DeepCopy()
}

if entry.IsBlocked != blocked {
Expand Down Expand Up @@ -361,12 +364,14 @@ func (g generator) generate(ctx context.Context, moduleList []module.Addr, block
modulesToAdd = append(modulesToAdd, entry)
lock.Unlock()
}
versionListing, err := json.Marshal(entry)
if err != nil {
return fmt.Errorf("failed to marshal module index for %s (%w)", entry.Addr, err)
}
if err := g.storage.WriteFile(ctx, indexstorage.Path(moduleIndexPath), versionListing); err != nil {
return fmt.Errorf("failed to write the module index for %s (%w)", entry.Addr, err)
if originalEntry == nil || !originalEntry.Equals(entry) {
versionListing, err := json.Marshal(entry)
if err != nil {
return fmt.Errorf("failed to marshal module index for %s (%w)", entry.Addr, err)
}
if err := g.storage.WriteFile(ctx, indexstorage.Path(moduleIndexPath), versionListing); err != nil {
return fmt.Errorf("failed to write the module index for %s (%w)", entry.Addr, err)
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions backend/internal/moduleindex/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

// swagger:model Module
type Module struct {
// If you add a field here, update Equals() and DeepCopy() below.

// required: true
Addr ModuleAddr `json:"addr"`
// required: true
Expand All @@ -36,6 +38,41 @@ type Module struct {
UpstreamPopularity int `json:"upstream_popularity"`
// UpstreamForkCount contains the number of forks of the upstream repository.
UpstreamForkCount int `json:"upstream_fork_count"`

// If you add a field here, update Equals() and DeepCopy() below.
}

func (m *Module) Equals(other *Module) bool {
if m == other {
return true
}
if m == nil || other == nil {
return false
}
return m.Addr.Equals(other.Addr.Addr) && m.Description == other.Description &&
slices.Equal(m.Versions, other.Versions) && m.IsBlocked == other.IsBlocked &&
m.BlockedReason == other.BlockedReason && m.Popularity == other.Popularity && m.ForkCount == other.ForkCount &&
m.ForkOfLink == other.ForkOfLink && m.ForkOf.Equals(other.ForkOf.Addr) &&
m.UpstreamPopularity == other.UpstreamPopularity && m.UpstreamForkCount == other.UpstreamForkCount
}

func (m *Module) DeepCopy() *Module {
versions := make([]ModuleVersionDescriptor, len(m.Versions))
copy(versions, m.Versions)

return &Module{
Addr: m.Addr,
Description: m.Description,
Versions: versions,
IsBlocked: m.IsBlocked,
BlockedReason: m.BlockedReason,
Popularity: m.Popularity,
ForkCount: m.ForkCount,
ForkOfLink: m.ForkOfLink,
ForkOf: m.ForkOf,
UpstreamPopularity: m.UpstreamPopularity,
UpstreamForkCount: m.UpstreamForkCount,
}
}

func (m *Module) Validate() error {
Expand Down
9 changes: 7 additions & 2 deletions backend/internal/providerindex/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func (d *documentationGenerator) scrape(ctx context.Context, providers []provide
blocked, blockedReason := d.blocklist.IsProviderBlocked(addr)

providerEntry := existingProviders.GetProvider(addr)
var originalProviderEntry *providertypes.Provider
needsAdd := false
if providerEntry == nil {
providerEntry = &providertypes.Provider{
Expand All @@ -207,6 +208,8 @@ func (d *documentationGenerator) scrape(ctx context.Context, providers []provide
BlockedReason: blockedReason,
}
needsAdd = true
} else {
originalProviderEntry = providerEntry.DeepCopy()
}

// scrape the docs into their own directory
Expand All @@ -223,8 +226,10 @@ func (d *documentationGenerator) scrape(ctx context.Context, providers []provide
return err
}

if err := d.destination.StoreProvider(ctx, *providerEntry); err != nil {
return fmt.Errorf("failed to store provider %s (%w)", addr, err)
if originalProviderEntry == nil || !originalProviderEntry.Equals(providerEntry) {
if err := d.destination.StoreProvider(ctx, *providerEntry); err != nil {
return fmt.Errorf("failed to store provider %s (%w)", addr, err)
}
}

if needsAdd {
Expand Down
71 changes: 71 additions & 0 deletions backend/internal/providerindex/providertypes/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
//
// swagger:model Provider
type Provider struct {
// If you add something here, don't forget to update the Equals() and DeepCopy() functions below.

// Addr holds the address of a provider. It can be split by / to obtain a namespace and name.
//
// required: true
Expand Down Expand Up @@ -61,6 +63,75 @@ type Provider struct {
IsBlocked bool `json:"is_blocked"`
// required: false
BlockedReason string `json:"blocked_reason,omitempty"`

// If you add something here, don't forget to update the Equals() and DeepCopy() functions below.
}

// Equals returns true if and only of all parameters of the two providers are equal (with a deep comparison).
func (p *Provider) Equals(other *Provider) bool {
if p == other {
return true
}
if p == nil || other == nil {
return false
}
reverseAliasesEqual := true
if len(p.ReverseAliases) != len(other.ReverseAliases) {
reverseAliasesEqual = false
} else {
for i := range len(p.ReverseAliases) {
if !p.ReverseAliases[i].Equals(other.ReverseAliases[i].Addr) {
reverseAliasesEqual = false
break
}
}
}
return p.Addr.Equals(other.Addr.Addr) && slices.Equal(p.Warnings, other.Warnings) && p.Link == other.Link &&
(p.CanonicalAddr == other.CanonicalAddr || p.CanonicalAddr.Equals(other.CanonicalAddr.Addr)) &&
reverseAliasesEqual && p.Description == other.Description && p.Popularity == other.Popularity &&
p.ForkCount == other.ForkCount && p.ForkOfLink == other.ForkOfLink && p.ForkOf == other.ForkOf &&
p.UpstreamPopularity == other.UpstreamPopularity && p.UpstreamForkCount == other.UpstreamForkCount &&
slices.Equal(p.Versions, other.Versions) && p.BlockedReason == other.BlockedReason
}

// DeepCopy creates a deep copy of the Provider.
func (p *Provider) DeepCopy() *Provider {
warnings := make([]string, len(p.Warnings))
copy(warnings, p.Warnings)

var canonicalAddr *ProviderAddr
if p.CanonicalAddr != nil {
canonicalAddr = &ProviderAddr{
Addr: p.CanonicalAddr.Addr,
Display: p.CanonicalAddr.Display,
Namespace: p.CanonicalAddr.Namespace,
Name: p.CanonicalAddr.Name,
}
}

reverseAliases := make([]ProviderAddr, len(p.ReverseAliases))
copy(reverseAliases, p.ReverseAliases)

versions := make([]ProviderVersionDescriptor, len(p.Versions))
copy(versions, p.Versions)

return &Provider{
Addr: p.Addr,
Warnings: warnings,
Link: p.Link,
CanonicalAddr: canonicalAddr,
ReverseAliases: reverseAliases,
Description: p.Description,
Popularity: p.Popularity,
ForkCount: p.ForkCount,
ForkOfLink: p.ForkOfLink,
ForkOf: p.ForkOf,
UpstreamPopularity: p.UpstreamPopularity,
UpstreamForkCount: p.UpstreamForkCount,
Versions: versions,
IsBlocked: p.IsBlocked,
BlockedReason: p.BlockedReason,
}
}

func (p *Provider) Compare(other Provider) int {
Expand Down

0 comments on commit aef1ced

Please sign in to comment.