Files
athens/pkg/index/mem/mem.go
Marwan Sulaiman 52934cfa46 implement /index endpoint (#1630)
* implement /index endpoint

* rename to Module to Path
2020-06-24 14:29:30 -04:00

52 lines
927 B
Go

package mem
import (
"context"
"sync"
"time"
"github.com/gomods/athens/pkg/errors"
"github.com/gomods/athens/pkg/index"
)
// New returns a new in-memory indexer
func New() index.Indexer {
return &indexer{}
}
type indexer struct {
mu sync.RWMutex
lines []*index.Line
}
func (i *indexer) Index(ctx context.Context, mod, ver string) error {
const op errors.Op = "mem.Index"
i.mu.Lock()
i.lines = append(i.lines, &index.Line{
Path: mod,
Version: ver,
Timestamp: time.Now(),
})
i.mu.Unlock()
return nil
}
func (i *indexer) Lines(ctx context.Context, since time.Time, limit int) ([]*index.Line, error) {
const op errors.Op = "mem.Lines"
lines := []*index.Line{}
var count int
i.mu.RLock()
defer i.mu.RUnlock()
for _, line := range i.lines {
if count >= limit {
break
}
if since.After(line.Timestamp) {
continue
}
lines = append(lines, line)
count++
}
return lines, nil
}