Skip to content

Commit

Permalink
✨ feat(map.go): add 1:N operations to create, delete, retrieve and up…
Browse files Browse the repository at this point in the history
…date documents concurrently against all DALs in the map

🔥 chore(map_test.go): rename tests to match new function names and add tests for new functions
  • Loading branch information
thalesfsp committed Jun 13, 2023
1 parent 491f534 commit b28a7d7
Show file tree
Hide file tree
Showing 2 changed files with 238 additions and 25 deletions.
129 changes: 116 additions & 13 deletions storage/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
// Map is a map of strgs
type Map map[string]IStorage

//////
// Methods.
//////

// String implements the Stringer interface.
func (m Map) String() string {
// Iterate over the map and print the storage name.
Expand Down Expand Up @@ -50,8 +54,12 @@ func (m Map) ToSlice() []IStorage {
return s
}

// CountMany returns a slice of int64 with the count of all DALs.
func CountMany(
//////
// 1:N Operations.
//////

// CountFromMany counts documents concurrently against all DALs in the map.
func CountFromMany(
ctx context.Context,
m Map,
target string,
Expand All @@ -69,8 +77,8 @@ func CountMany(
return r, nil
}

// CreateMany returns a slice of string with the id of all documents inserted.
func CreateMany[T any](
// CreateIntoMany creates one document concurrently against all DALs in the map.
func CreateIntoMany[T any](
ctx context.Context,
m Map,
id, target string,
Expand All @@ -89,8 +97,8 @@ func CreateMany[T any](
return r, nil
}

// DeleteMany deletes all documents concurrently.
func DeleteMany(
// DeleteFromMany deletes one documents concurrently against all DALs in the map.
func DeleteFromMany(
ctx context.Context,
m Map,
id, target string,
Expand All @@ -112,9 +120,10 @@ func DeleteMany(
return r, nil
}

// ListMany returns a slice of T with all documents. The result of each DAL is
// flattened into a single slice.
func ListMany[T any](
// ListFromMany lists documents concurrently against all DALs in the map.
//
// NOTE: The results are flattened into a single slice.
func ListFromMany[T any](
ctx context.Context,
m Map,
target string,
Expand All @@ -132,8 +141,9 @@ func ListMany[T any](
return Flatten2D(r), nil
}

// RetrieveMany returns a slice of T from all DALs.
func RetrieveMany[T any](
// RetrieveFromMany retrieves one document concurrently against all DALs in the
// map.
func RetrieveFromMany[T any](
ctx context.Context,
m Map,
id, target string,
Expand All @@ -151,8 +161,8 @@ func RetrieveMany[T any](
return r, nil
}

// UpdateMany updates all documents concurrently.
func UpdateMany(
// UpdateIntoMany updates one document concurrently against all DALs in the map.
func UpdateIntoMany(
ctx context.Context,
m Map,
id, target string,
Expand All @@ -174,3 +184,96 @@ func UpdateMany(

return r, nil
}

//////
// N:1 Operations.
//////

// CreateMany creates many documents concurrently against the same DAL.
func CreateMany[T any](
ctx context.Context,
str IStorage,
target string,
prm *create.Create,
itemsMap map[string]T,
) ([]string, error) {
r, errs := concurrentloop.MapM(ctx, itemsMap, func(ctx context.Context, key string, item T) (string, error) {
id, err := Create(ctx, str, key, target, item, prm)
if err != nil {
return "", err
}

return id, nil
})

if len(errs) > 0 {
return nil, errs[0]
}

return r, nil
}

// DeleteMany deletes many documents concurrently against the same DAL.
func DeleteMany(
ctx context.Context,
str IStorage,
target string,
prm *delete.Delete,
ids ...string,
) ([]bool, error) {
r, errs := concurrentloop.Map(ctx, ids, func(ctx context.Context, id string) (bool, error) {
if err := Delete(ctx, str, id, target, prm); err != nil {
return false, err
}

return true, nil
})

if len(errs) > 0 {
return nil, errs[0]
}

return r, nil
}

// RetrieveMany retrieves many documents concurrently against the same DAL.
func RetrieveMany[T any](
ctx context.Context,
str IStorage,
target string,
prm *retrieve.Retrieve,
ids ...string,
) ([]T, error) {
r, errs := concurrentloop.Map(ctx, ids, func(ctx context.Context, id string) (T, error) {
return Retrieve[T](ctx, str, id, target, prm)
})

if len(errs) > 0 {
return nil, errs[0]
}

return r, nil
}

// UpdateMany updates many documents concurrently against the same DAL.
func UpdateMany[T any](
ctx context.Context,
str IStorage,
target string,
prm *update.Update,
itemsMap map[string]T,
) ([]bool, error) {
r, errs := concurrentloop.MapM(ctx, itemsMap, func(ctx context.Context, key string, item T) (bool, error) {
if err := Update(ctx, str, key, target, item, prm); err != nil {
return false, err
}

return true, nil
})

if len(errs) > 0 {
return nil, errs[0]
}

return r, nil
}
Loading

0 comments on commit b28a7d7

Please sign in to comment.