mirror of
https://github.com/gomods/athens
synced 2026-02-03 11:00:32 +00:00
* Changed mongo.go to use new driver * Modified mongo cataloger * More new driver related changes * Change lister.go * Change saver.go * Change imports * Remove unnecessary Count query * Use IndexView for indexing * Rename ModuleStore fields * Use map of key:sorting-order for creating the index * Minor changes * Use client options to configure mongo client * Use method chaining * gofmt changes * Change imports * Fix some build errors * Use new GridFS API * Fix more build errors * Add Go Mongo driver to dependency modules * Use multierror * Leave download stream open * Remove mgo error handling * Copy zip instead of loading all in memory * Use context.WithTimeout() wherever possible * Raise KindNotFound when mod@ver isn't found * NopCloser not needed * Fix IndexView error * Fix build errors * Remove another mgo error usage * Fix build error * Changes according to review * Formatting changes as per gofmt * Modify gofmt argument to show the expected formatting (diff) * Handle ErrNoDocument error and error arising from query execution * Fix kind of returned error * Minor changes * Bug fixes * gofmt related changes * Minor change * Use Insecure from MongoConfig, remove Insecure from global Config * Remove stray print statement
68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
package mongo
|
|
|
|
import (
|
|
"context"
|
|
"github.com/gomods/athens/pkg/errors"
|
|
"github.com/gomods/athens/pkg/paths"
|
|
"github.com/gomods/athens/pkg/storage"
|
|
"github.com/hashicorp/go-multierror"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
// Catalog implements the (./pkg/storage).Cataloger interface
|
|
// It returns a list of modules and versions contained in the storage
|
|
func (s *ModuleStore) Catalog(ctx context.Context, token string, pageSize int) ([]paths.AllPathParams, string, error) {
|
|
const op errors.Op = "mongo.Catalog"
|
|
q := bson.M{}
|
|
if token != "" {
|
|
t, err := primitive.ObjectIDFromHex(token)
|
|
if err == nil {
|
|
q = bson.M{"_id": bson.M{"$gt": t}}
|
|
}
|
|
}
|
|
|
|
projection := bson.M{"module": 1, "version": 1}
|
|
sort := bson.M{"_id": 1}
|
|
|
|
c := s.client.Database(s.db).Collection(s.coll)
|
|
|
|
tctx, cancel := context.WithTimeout(ctx, s.timeout)
|
|
defer cancel()
|
|
modules := make([]storage.Module, 0)
|
|
findOptions := options.Find().SetProjection(projection).SetSort(sort).SetLimit(int64(pageSize))
|
|
cursor, err := c.Find(tctx, q, findOptions)
|
|
|
|
if err != nil {
|
|
return nil, "", errors.E(op, err)
|
|
}
|
|
|
|
var errs error
|
|
for cursor.Next(ctx) {
|
|
var module storage.Module
|
|
if err := cursor.Decode(&module); err != nil {
|
|
errs = multierror.Append(errs, err)
|
|
} else {
|
|
modules = append(modules, module)
|
|
}
|
|
}
|
|
|
|
// If there are 0 results, return empty results without an error
|
|
if len(modules) == 0 {
|
|
return nil, "", nil
|
|
}
|
|
|
|
var versions = make([]paths.AllPathParams, len(modules))
|
|
for i := range modules {
|
|
versions[i].Module = modules[i].Module
|
|
versions[i].Version = modules[i].Version
|
|
}
|
|
|
|
var next = modules[len(modules)-1].ID.Hex()
|
|
if len(modules) < pageSize {
|
|
return versions, "", nil
|
|
}
|
|
return versions, next, nil
|
|
}
|