Skip to content

Commit

Permalink
refactor: Remove extra sort in favor of sorted insert
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Nov 7, 2022
1 parent 5b36b40 commit 5474504
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 33 deletions.
9 changes: 0 additions & 9 deletions pkg/provider/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,3 @@ func (s Share) CheckPassword(authorizationHeader string) error {
func (s Share) IsExpired(now time.Time) bool {
return s.Duration != 0 && s.Creation.Add(s.Duration).Before(now)
}

// ShareByID sort Share by ID
type ShareByID []Share

func (a ShareByID) Len() int { return len(a) }
func (a ShareByID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ShareByID) Less(i, j int) bool {
return a[i].ID < a[j].ID
}
8 changes: 0 additions & 8 deletions pkg/provider/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,3 @@ func (w Webhook) matchItem(item absto.Item) bool {

return itemDir == w.Pathname
}

type WebhookByID []Webhook

func (a WebhookByID) Len() int { return len(a) }
func (a WebhookByID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a WebhookByID) Less(i, j int) bool {
return a[i].ID < a[j].ID
}
18 changes: 10 additions & 8 deletions pkg/share/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,23 @@ func (a *App) generateID() (string, error) {
}

// List shares
func (a *App) List() (shares []provider.Share) {
func (a *App) List() (output []provider.Share) {
a.RLock()
defer a.RUnlock()

var i int64
shares = make([]provider.Share, len(a.shares))
output = make([]provider.Share, 0, len(a.shares))

for _, value := range a.shares {
shares[i] = value
i++
}
index := sort.Search(len(output), func(i int) bool {
return output[i].ID > value.ID
})

sort.Sort(provider.ShareByID(shares))
output = append(output, value)
copy(output[index+1:], output[index:])
output[index] = value
}

return shares
return output
}

// Create a share
Expand Down
18 changes: 10 additions & 8 deletions pkg/webhook/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,23 @@ func (a *App) generateID() (string, error) {
}
}

func (a *App) List() (webhooks []provider.Webhook) {
func (a *App) List() (output []provider.Webhook) {
a.RLock()
defer a.RUnlock()

var i int64
webhooks = make([]provider.Webhook, len(a.webhooks))
output = make([]provider.Webhook, len(a.webhooks))

for _, value := range a.webhooks {
webhooks[i] = value
i++
}
index := sort.Search(len(output), func(i int) bool {
return output[i].ID > value.ID
})

sort.Sort(provider.WebhookByID(webhooks))
output = append(output, value)
copy(output[index+1:], output[index:])
output[index] = value
}

return webhooks
return output
}

func (a *App) Create(ctx context.Context, pathname string, recursive bool, kind provider.WebhookKind, url string, types []provider.EventType) (string, error) {
Expand Down

0 comments on commit 5474504

Please sign in to comment.