Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions db/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,19 @@ func (c *mongoCollection) FindOne(ctx context.Context, key any, data any) error

// Find multiple entries from the store collection for the given filter, where the data
// value is returned as a list based on the object type passed to it
func (c *mongoCollection) FindMany(ctx context.Context, filter any, data any) error {
func (c *mongoCollection) FindMany(ctx context.Context, filter any, data any, opts ...any) error {
if filter == nil {
filter = bson.D{}
}
cursor, err := c.col.Find(ctx, filter)
var findOpts []options.Lister[options.FindOptions]
for _, opt := range opts {
val, ok := opt.(options.Lister[options.FindOptions])
if !ok {
return errors.Wrapf(errors.InvalidArgument, "Invalid option type %T passed to FindMany", opt)
}
findOpts = append(findOpts, val)
}
cursor, err := c.col.Find(ctx, filter, findOpts...)
if err != nil {
return interpretMongoError(err)
}
Expand Down
99 changes: 99 additions & 0 deletions db/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ package db

import (
"context"
"fmt"
"reflect"
"testing"
"time"

"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)

type MyKey struct {
Expand Down Expand Up @@ -106,6 +108,103 @@ func Test_ClientConnection(t *testing.T) {
}
})

t.Run("Find_many_with_offset_limits", func(t *testing.T) {
config := &MongoConfig{
Host: "localhost",
Port: "27017",
Username: "root",
Password: "password",
}

client, err := NewMongoClient(config)

if err != nil {
t.Errorf("failed to connect to mongo DB Error: %s", err)
return
}

s := client.GetDataStore("test")

col := s.GetCollection("collection1")

key := &MyKey{
Name: "test-key",
}
data := &MyData{
Desc: "sample-description",
Val: &InternaData{
Test: "abc",
},
}

key1 := &MyKey{
Name: "test-key-1",
}

key2 := &MyKey{
Name: "test-key-2",
}

err = col.InsertOne(context.Background(), key, data)
if err != nil {
t.Errorf("failed to insert an entry to collection Error: %s", err)
}

err = col.InsertOne(context.Background(), key1, data)
if err != nil {
t.Errorf("failed to insert an entry to collection Error: %s", err)
}

err = col.InsertOne(context.Background(), key2, data)
if err != nil {
t.Errorf("failed to insert an entry to collection Error: %s", err)
}

val := &MyData{}
err = col.FindOne(context.Background(), key2, val)
if err != nil {
t.Errorf("failed to find the entry Error: %s", err)
}
fmt.Printf("found entry :%v", *val)

list := []*MyData{}
opts := options.Find().SetLimit(2).SetSkip(2)
err = col.FindMany(context.Background(), nil, &list, opts)
if err != nil {
t.Errorf("failed to find the entry Error: %s", err)
}

if len(list) != 1 {
t.Errorf("Expected 1 entries in table but got %d", len(list))
}

list = []*MyData{}
opts = options.Find().SetLimit(2).SetSkip(0)
err = col.FindMany(context.Background(), nil, &list, opts)
if err != nil {
t.Errorf("failed to find the entry Error: %s", err)
}

if len(list) != 2 {
t.Errorf("Expected 2 entries in table but got %d", len(list))
}

err = col.DeleteOne(context.Background(), key)
if err != nil {
t.Errorf("failed to delete entry using key Error: %s", err)
}

err = col.DeleteOne(context.Background(), key1)
if err != nil {
t.Errorf("failed to delete entry using key Error: %s", err)
}

err = col.DeleteOne(context.Background(), key2)
if err != nil {
t.Errorf("failed to delete entry using key Error: %s", err)
}
})

t.Run("InValid_Port", func(t *testing.T) {
config := &MongoConfig{
Host: "localhost",
Expand Down
2 changes: 1 addition & 1 deletion db/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type StoreCollection interface {

// Find multiple entries from the store collection for the given filter, where the data
// value is returned as a list based on the object type passed to it
FindMany(ctx context.Context, filter any, data any) error
FindMany(ctx context.Context, filter any, data any, opts ...any) error

// Return count of entries matching the provided filter
Count(ctx context.Context, filter any) (int64, error)
Expand Down
7 changes: 5 additions & 2 deletions table/cached_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"reflect"
"sync"

"go.mongodb.org/mongo-driver/v2/mongo/options"

"github.com/go-core-stack/core/db"
"github.com/go-core-stack/core/errors"
"github.com/go-core-stack/core/reconciler"
Expand Down Expand Up @@ -194,12 +196,13 @@ func (t *CachedTable[K, E]) DBFind(ctx context.Context, key *K) (*E, error) {

// DBFindMany retrieves multiple entries matching the provided filter from database.
// Returns a slice of entries and error if none found or if the table is not initialized.
func (t *CachedTable[K, E]) DBFindMany(ctx context.Context, filter any) ([]*E, error) {
func (t *CachedTable[K, E]) DBFindMany(ctx context.Context, filter any, offset, limit int32) ([]*E, error) {
if t.col == nil {
return nil, errors.Wrapf(errors.InvalidArgument, "Table not initialized")
}
var data []*E
err := t.col.FindMany(ctx, filter, &data)
opts := options.Find().SetLimit(int64(limit)).SetSkip(int64(offset))
err := t.col.FindMany(ctx, filter, &data, opts)
if err != nil {
return nil, errors.Wrapf(errors.NotFound, "failed to find any entry: %s", err)
}
Expand Down
7 changes: 5 additions & 2 deletions table/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"log"
"reflect"

"go.mongodb.org/mongo-driver/v2/mongo/options"

"github.com/go-core-stack/core/db"
"github.com/go-core-stack/core/errors"
"github.com/go-core-stack/core/reconciler"
Expand Down Expand Up @@ -193,12 +195,13 @@ func (t *Table[K, E]) Find(ctx context.Context, key *K) (*E, error) {

// FindMany retrieves multiple entries matching the provided filter.
// Returns a slice of entries and error if none found or if the table is not initialized.
func (t *Table[K, E]) FindMany(ctx context.Context, filter any) ([]*E, error) {
func (t *Table[K, E]) FindMany(ctx context.Context, filter any, offset, limit int32) ([]*E, error) {
if t.col == nil {
return nil, errors.Wrapf(errors.InvalidArgument, "Table not initialized")
}
var data []*E
err := t.col.FindMany(ctx, filter, &data)
opts := options.Find().SetLimit(int64(limit)).SetSkip(int64(offset))
err := t.col.FindMany(ctx, filter, &data, opts)
if err != nil {
return nil, errors.Wrapf(errors.NotFound, "failed to find any entry: %s", err)
}
Expand Down