feat: update getNodes from collection to use worker pool

This commit is contained in:
Alexander Navarro 2024-12-01 12:26:28 -03:00
parent 26b678b348
commit f58db2ecaa
2 changed files with 86 additions and 41 deletions

View file

@ -1,6 +1,7 @@
package synchronizator
import (
"context"
"fmt"
"slices"
"strings"
@ -86,33 +87,57 @@ func (collection *Collection) IsDefault() bool {
return collection.is_default
}
type NodeFetcher = func(metadata []byte, pagination Pagination) ([]*Node, Pagination, error)
// Is a type alias for FetchResponse containing a slice of Collection pointers.
type FetchNodesResponse = FetchResponse[[]*Node]
func (collection *Collection) FetchNodes(fetcher NodeFetcher, start_pagination Pagination) error {
nodes, _, err := fetcher(collection.GetMetadata(), start_pagination)
if err != nil {
return err
}
collection.childs = slices.Concat(collection.childs, nodes)
err = BulkCreateNode(collection._conn, collection.childs)
// Fetches collections using the provided fetcher and pagination settings.
// It updates the platform's collections and creates relationships between the platform and the collections.
//
// Parameters:
// - ctx: The context to control cancellation.
// - fetcher: The fetcher function to execute the work.
// - start_pagination: The initial pagination settings.
// - pool_config: The configuration for the worker pool.
//
// Returns:
// - error: The error if any occurred.
func (collection *Collection) FetchNodes(
ctx context.Context,
fetcher Work[Pagination, FetchNodesResponse],
startPagination Pagination,
poolConfig *WorkConfig,
) error {
values, err := fetchWithPagination(ctx, poolConfig, fetcher, startPagination)
if err != nil {
return err
}
for _, item := range collection.childs {
err := collection.AddRelationship(
&Relationship{
_class: "COLLECTION_HAS_NODE",
From: collection.Id,
To: item.Id,
})
collection.childs = slices.Concat(collection.childs, values)
fmt.Printf("Nodes: %v\n", len(collection.childs))
err = BulkCreateNode(collection._conn, values)
if err != nil {
return err
}
relationships := make([]*Relationship, 0, len(values))
for _, item := range values {
relation := &Relationship{
_class: "COLLECTION_HAS_NODE",
From: collection.Id,
To: item.Id,
}
err := collection.AddRelationship(relation)
if err != nil {
return err
}
relationships = append(relationships, relation)
}
err = BulkCreateRelationships(collection._conn, collection._relationships)
err = BulkCreateRelationships(collection._conn, relationships)
if err != nil {
return err
}