Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

importer delete support #64

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 64 additions & 8 deletions pkg/directory/v3/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,34 @@ func (s *Importer) Import(stream dsi3.Importer_ImportServer) error {

func (s *Importer) handleImportRequest(ctx context.Context, tx *bolt.Tx, req *dsi3.ImportRequest, res *dsi3.ImportResponse) (err error) {

if obj := req.GetObject(); obj != nil {
err = s.objectHandler(ctx, tx, obj)
res.Object = updateCounter(res.Object, req.OpCode, err)
} else if rel := req.GetRelation(); rel != nil {
err = s.relationHandler(ctx, tx, rel)
res.Relation = updateCounter(res.Relation, req.OpCode, err)
switch m := req.Msg.(type) {
case *dsi3.ImportRequest_Object:
if req.OpCode == dsi3.Opcode_OPCODE_SET {
err = s.objectSetHandler(ctx, tx, m.Object)
res.Object = updateCounter(res.Object, req.OpCode, err)
}

if req.OpCode == dsi3.Opcode_OPCODE_DELETE {
err = s.objectDeleteHandler(ctx, tx, m.Object)
res.Object = updateCounter(res.Object, req.OpCode, err)
}

case *dsi3.ImportRequest_Relation:
if req.OpCode == dsi3.Opcode_OPCODE_SET {
err = s.relationSetHandler(ctx, tx, m.Relation)
res.Relation = updateCounter(res.Relation, req.OpCode, err)
}

if req.OpCode == dsi3.Opcode_OPCODE_DELETE {
err = s.relationDeleteHandler(ctx, tx, m.Relation)
res.Relation = updateCounter(res.Relation, req.OpCode, err)
}
}

return err
}

func (s *Importer) objectHandler(ctx context.Context, tx *bolt.Tx, req *dsc3.Object) error {
func (s *Importer) objectSetHandler(ctx context.Context, tx *bolt.Tx, req *dsc3.Object) error {
s.logger.Debug().Interface("object", req).Msg("ImportObject")

if req == nil {
Expand Down Expand Up @@ -108,7 +124,25 @@ func (s *Importer) objectHandler(ctx context.Context, tx *bolt.Tx, req *dsc3.Obj
return nil
}

func (s *Importer) relationHandler(ctx context.Context, tx *bolt.Tx, req *dsc3.Relation) error {
func (s *Importer) objectDeleteHandler(ctx context.Context, tx *bolt.Tx, req *dsc3.Object) error {
s.logger.Debug().Interface("object", req).Msg("ImportObject")

if req == nil {
return derr.ErrInvalidObject.Msg("nil")
}

if err := s.v.Validate(req); err != nil {
return derr.ErrProtoValidate.Msg(err.Error())
}

if err := bdb.Delete(ctx, tx, bdb.ObjectsPath, ds.Object(req).Key()); err != nil {
return derr.ErrInvalidObject.Msg("delete")
}

return nil
}

func (s *Importer) relationSetHandler(ctx context.Context, tx *bolt.Tx, req *dsc3.Relation) error {
s.logger.Debug().Interface("relation", req).Msg("ImportRelation")

if req == nil {
Expand Down Expand Up @@ -144,6 +178,28 @@ func (s *Importer) relationHandler(ctx context.Context, tx *bolt.Tx, req *dsc3.R
return nil
}

func (s *Importer) relationDeleteHandler(ctx context.Context, tx *bolt.Tx, req *dsc3.Relation) error {
s.logger.Debug().Interface("relation", req).Msg("ImportRelation")

if req == nil {
return derr.ErrInvalidRelation.Msg("nil")
}

if err := s.v.Validate(req); err != nil {
return derr.ErrProtoValidate.Msg(err.Error())
}

if err := bdb.Delete(ctx, tx, bdb.RelationsObjPath, ds.Relation(req).ObjKey()); err != nil {
return derr.ErrInvalidRelation.Msg("delete")
}

if err := bdb.Delete(ctx, tx, bdb.RelationsSubPath, ds.Relation(req).SubKey()); err != nil {
return derr.ErrInvalidRelation.Msg("delete")
}

return nil
}

func updateCounter(c *dsi3.ImportCounter, opCode dsi3.Opcode, err error) *dsi3.ImportCounter {
c.Recv++
if opCode == dsi3.Opcode_OPCODE_SET {
Expand Down