diff --git a/pkg/scd/store/raftstore/constraints.go b/pkg/scd/store/raftstore/constraints.go index 2cec24039..b6c6a327d 100644 --- a/pkg/scd/store/raftstore/constraints.go +++ b/pkg/scd/store/raftstore/constraints.go @@ -2,29 +2,59 @@ package raftstore import ( "context" + "encoding/json" - dsserr "github.com/interuss/dss/pkg/errors" dssmodels "github.com/interuss/dss/pkg/models" + "github.com/interuss/dss/pkg/raftstore/consensus" scdmodels "github.com/interuss/dss/pkg/scd/models" "github.com/interuss/stacktrace" ) -func (r *repo) SearchConstraints(_ context.Context, cellsVolume *dssmodels.CellsVolume4D) ([]*scdmodels.Constraint, error) { - return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "SearchConstraints not implemented for raftstore") +const ( + searchConstraints consensus.RequestType[[]*scdmodels.Constraint] = "searchConstraints" + getConstraint consensus.RequestType[*scdmodels.Constraint] = "getConstraint" + upsertConstraint consensus.RequestType[*scdmodels.Constraint] = "upsertConstraint" + deleteConstraint consensus.RequestType[any] = "deleteConstraint" + countConstraints consensus.RequestType[int64] = "countConstraints" +) + +func (r *repo) SearchConstraints(ctx context.Context, cellsVolume *dssmodels.CellsVolume4D) ([]*scdmodels.Constraint, error) { + buf, err := json.Marshal(cellsVolume) + if err != nil { + return nil, stacktrace.Propagate(err, "failed to marshal payload") + } + + return r.consensus.HandleClientRequest(ctx, searchConstraints, buf, true) } -func (r *repo) GetConstraint(_ context.Context, id dssmodels.ID) (*scdmodels.Constraint, error) { - return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "GetConstraint not implemented for raftstore") +func (r *repo) GetConstraint(ctx context.Context, id dssmodels.ID) (*scdmodels.Constraint, error) { + buf, err := json.Marshal(id) + if err != nil { + return nil, stacktrace.Propagate(err, "failed to marshal payload") + } + + return r.consensus.HandleClientRequest(ctx, getConstraint, buf, true) } -func (r *repo) UpsertConstraint(_ context.Context, constraint *scdmodels.Constraint) (*scdmodels.Constraint, error) { - return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "UpsertConstraint not implemented for raftstore") +func (r *repo) UpsertConstraint(ctx context.Context, constraint *scdmodels.Constraint) (*scdmodels.Constraint, error) { + buf, err := json.Marshal(constraint) + if err != nil { + return nil, stacktrace.Propagate(err, "failed to marshal payload") + } + + return r.consensus.HandleClientRequest(ctx, upsertConstraint, buf, false) } -func (r *repo) DeleteConstraint(_ context.Context, id dssmodels.ID) error { - return stacktrace.NewErrorWithCode(dsserr.NotImplemented, "DeleteConstraint not implemented for raftstore") +func (r *repo) DeleteConstraint(ctx context.Context, id dssmodels.ID) error { + buf, err := json.Marshal(id) + if err != nil { + return stacktrace.Propagate(err, "failed to marshal payload") + } + + _, err = r.consensus.HandleClientRequest(ctx, deleteConstraint, buf, false) + return err } -func (r *repo) CountConstraints(_ context.Context) (int64, error) { - return 0, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "CountConstraint not implemented for raftstore") +func (r *repo) CountConstraints(ctx context.Context) (int64, error) { + return r.consensus.HandleClientRequest(ctx, countConstraints, nil, true) } diff --git a/pkg/scd/store/raftstore/store.go b/pkg/scd/store/raftstore/store.go index 44f8b407d..0317d538f 100644 --- a/pkg/scd/store/raftstore/store.go +++ b/pkg/scd/store/raftstore/store.go @@ -2,10 +2,13 @@ package raftstore import ( "context" + "encoding/json" "github.com/interuss/dss/pkg/memstore" + dssmodels "github.com/interuss/dss/pkg/models" "github.com/interuss/dss/pkg/raftstore" "github.com/interuss/dss/pkg/raftstore/consensus" + scdmodels "github.com/interuss/dss/pkg/scd/models" "github.com/interuss/dss/pkg/scd/operations" "github.com/interuss/dss/pkg/scd/repos" scdmemstore "github.com/interuss/dss/pkg/scd/store/memstore" @@ -46,6 +49,36 @@ func (r *repo) GetRepo() repos.Repository { return r } func (r *repo) Apply(ctx context.Context, proposal consensus.Proposal) (any, error) { switch proposal.RequestType { + case string(searchConstraints): + var cellsVolume dssmodels.CellsVolume4D + if err := json.Unmarshal(proposal.Value, &cellsVolume); err != nil { + return nil, stacktrace.Propagate(err, "failed to unmarshal %s payload", searchConstraints) + } + return r.Store.GetRepo().SearchConstraints(ctx, &cellsVolume) + + case string(getConstraint): + var id dssmodels.ID + if err := json.Unmarshal(proposal.Value, &id); err != nil { + return nil, stacktrace.Propagate(err, "failed to unmarshal %s payload", getConstraint) + } + return r.Store.GetRepo().GetConstraint(ctx, id) + + case string(upsertConstraint): + var constraint scdmodels.Constraint + if err := json.Unmarshal(proposal.Value, &constraint); err != nil { + return nil, stacktrace.Propagate(err, "failed to unmarshal %s payload", upsertConstraint) + } + return r.Store.GetRepo().UpsertConstraint(ctx, &constraint) + + case string(deleteConstraint): + var id dssmodels.ID + if err := json.Unmarshal(proposal.Value, &id); err != nil { + return nil, stacktrace.Propagate(err, "failed to unmarshal %s payload", deleteConstraint) + } + return nil, r.Store.GetRepo().DeleteConstraint(ctx, id) + + case string(countConstraints): + return r.Store.GetRepo().CountConstraints(ctx) default: handler, ok := operations.Registry[proposal.RequestType]