droboticflow/internal/store/annotation_current.go

95 lines
3.3 KiB
Go

package store
import (
"context"
"fmt"
"droboticflow/internal/domain"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Called only after the new authoritative data has been published. Strictly
// older records are removed; a concurrently prepared newer submission survives.
func (s *MemoryStore) PruneAnnotationDataBefore(_ context.Context, org, task string, current int) error {
if org == "" || task == "" || current <= 0 {
return fmt.Errorf("current annotation scope is required")
}
s.mu.Lock()
defer s.mu.Unlock()
for id, r := range s.annotationRevisions {
if annotationOrganizationMatches(r.OrganizationID, org) && r.TaskID == task && r.Revision < current {
delete(s.annotationRevisions, id)
}
}
for id, r := range s.sliceRevisions {
if annotationOrganizationMatches(r.OrganizationID, org) && r.TaskID == task && r.Revision < current {
delete(s.sliceRevisions, id)
}
}
return nil
}
func (s *MongoStore) PruneAnnotationDataBefore(ctx context.Context, org, task string, current int) error {
if org == "" || task == "" || current <= 0 {
return fmt.Errorf("current annotation scope is required")
}
f := bson.M{"organizationId": normalizedOrganizationID(org), "taskId": task, "revision": bson.M{"$lt": current}}
if _, err := s.annotationRevisions.DeleteMany(ctx, f); err != nil {
return err
}
_, err := s.sliceRevisions.DeleteMany(ctx, f)
return err
}
func withoutArtifacts(items []domain.ExecutionArtifact, ids map[string]bool) []domain.ExecutionArtifact {
out := make([]domain.ExecutionArtifact, 0, len(items))
for _, item := range items {
if !ids[item.ID] {
out = append(out, item)
}
}
return out
}
// Use a field-level update rather than overwriting worker progress with a stale Run.
func (s *MemoryStore) RemoveRunArtifacts(_ context.Context, org, runID string, ids []string) error {
if org == "" || runID == "" {
return fmt.Errorf("run artifact scope is required")
}
s.mu.Lock()
defer s.mu.Unlock()
run, ok := s.runs[runID]
if !ok || !annotationOrganizationMatches(run.OrganizationID, org) {
return nil
}
set := map[string]bool{}
for _, id := range ids {
set[id] = true
}
run.Artifacts = withoutArtifacts(run.Artifacts, set)
for i := range run.NodeExecutions {
run.NodeExecutions[i].Artifacts = withoutArtifacts(run.NodeExecutions[i].Artifacts, set)
}
s.runs[runID] = run
return nil
}
func (s *MongoStore) RemoveRunArtifacts(ctx context.Context, org, runID string, ids []string) error {
if org == "" || runID == "" {
return fmt.Errorf("run artifact scope is required")
}
if len(ids) == 0 {
return nil
}
// Compacted Runs intentionally store top-level artifacts as null and
// hydrate them from node outputs on reads. Do not $pull from that null.
f := bson.M{"organizationId": normalizedOrganizationID(org), "id": runID, "artifacts": bson.M{"$type": "array"}}
if _, err := s.runs.UpdateOne(ctx, f, bson.M{"$pull": bson.M{"artifacts": bson.M{"id": bson.M{"$in": ids}}}}); err != nil {
return err
}
f = bson.M{"organizationId": normalizedOrganizationID(org), "id": runID, "nodeExecutions": bson.M{"$type": "array"}}
_, err := s.runs.UpdateOne(ctx, f, bson.M{"$pull": bson.M{"nodeExecutions.$[node].artifacts": bson.M{"id": bson.M{"$in": ids}}}}, options.Update().SetArrayFilters(options.ArrayFilters{Filters: []interface{}{bson.M{"node.artifacts": bson.M{"$type": "array"}}}}))
return err
}