This repository has been archived on 2025-05-15. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
synchronizator-go/pkg/collection.go

125 lines
3 KiB
Go

package synchronizator
import (
"fmt"
"slices"
"strings"
)
type default_collection struct {
platform_name string
}
func (collection *default_collection) GetClass() string {
platform_name := strings.ToUpper(collection.platform_name)
return platform_name + "_DEFAULT"
}
func (collection *default_collection) GetMetadata() []byte {
return nil
}
func (collection *default_collection) FromNode(_class string, name string, metadata []byte) error {
if _class != "DEFAULT" {
return fmt.Errorf("invalid class %s", _class)
}
return nil
}
// Utility struct to represent a collection of nodes, it's a [Node] itself so all
// the node's functionality is available.
type Collection struct {
Node // Underlaying node info
childs []*Node // Child nodes
is_default bool
}
// NewCollectionObject creates a new Collection instance without persisting it to the database.
// This is useful when you want to prepare a Collection for later storage.
func NewCollection(name string, metadata []byte) *Collection {
return &Collection{
Node: Node{
name: name,
_class: "COLLECTION",
metadata: metadata,
Id: -1, // Use -1 to indicate not persisted
},
childs: make([]*Node, 0),
is_default: false,
}
}
// Internal RelationshipClass to handle the collection to node relationship
type collection_has_node struct {
Relationship
}
func (col_has_node *collection_has_node) GetClass() string {
return "COLLECTION_HAS"
}
func (col_has_node *collection_has_node) GetMetadata() []byte {
return nil
}
func (col_has_node *collection_has_node) FromRelationship(_class string, metadata []byte) error {
if _class != "COLLECTION_HAS" {
return fmt.Errorf("invalid class %s", _class)
}
return nil
}
// Adds a new child to this collection. Use the underlaying node's AddRelation
// method.
func (collection *Collection) AddChild(node *Node) error {
_, err := collection.StoreRelation(&collection_has_node{}, node.Id)
if err != nil {
return err
}
collection.childs = append(collection.childs, node)
return nil
}
func (collection *Collection) IsDefault() bool {
return collection.is_default
}
type NodeFetcher = func(metadata []byte, pagination Pagination) ([]*Node, Pagination, error)
func (collection *Collection) FetchNodes(fetcher NodeFetcher, start_pagination Pagination) error {
nodes, pagination, err := fetcher(collection.GetMetadata(), start_pagination)
if err != nil {
return err
}
collection.childs = slices.Concat(collection.childs, nodes)
if pagination.HasMore {
return collection.FetchNodes(fetcher, pagination)
}
err = BulkCreateNode(collection._conn, collection.childs)
if err != nil {
return err
}
for _, item := range collection.childs {
err := collection.AddRelationship(
&Relationship{
_class: "COLLECTION_HAS_NODE",
From: collection.Id,
To: item.Id,
})
if err != nil {
return err
}
}
err = BulkCreateRelationships(collection._conn, collection._relationships)
if err != nil {
return err
}
return nil
}