Skip to content

Commit

Permalink
Optimize Mongo bulk delete
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewpeterkort committed Aug 8, 2024
1 parent 9bf7ca1 commit ab51dee
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions mongo/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,30 @@ func (mg *Graph) BulkAdd(stream <-chan *gdbi.GraphElement) error {
}

func (mg *Graph) BulkDel(Data *gdbi.DeleteData) error {
for _, v := range Data.Edges {
if err := mg.DelEdge(v); err != nil {
return err
}
eCol := mg.ar.EdgeCollection(mg.graph)
_, err := eCol.DeleteMany(context.TODO(), bson.M{"_id": bson.M{"$in": Data.Edges}})
if err != nil {
return fmt.Errorf("failed to delete edge(s): %s", err)
}
for _, v := range Data.Vertices {
if err := mg.DelVertex(v); err != nil {
return err
}
mg.ts.Touch(mg.graph)

vCol := mg.ar.VertexCollection(mg.graph)
_, err = vCol.DeleteMany(context.TODO(), bson.M{"_id": bson.M{"$in": Data.Vertices}})
if err != nil {
return fmt.Errorf("failed to delete list of vertices: %s", err)
}
mg.ts.Touch(mg.graph)

_, err = eCol.DeleteMany(context.TODO(), bson.M{
"$or": []bson.M{
{FIELD_FROM: bson.M{"$in": Data.Vertices}},
{FIELD_TO: bson.M{"$in": Data.Vertices}},
}})
if err != nil {
return fmt.Errorf("failed to delete connected edge(s): %s", err)
}
mg.ts.Touch(mg.graph)

return nil
}

Expand Down

0 comments on commit ab51dee

Please sign in to comment.