-
Notifications
You must be signed in to change notification settings - Fork 103
[raft/memstore] Add scd memstore #1542
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package utils | ||
|
|
||
| func ClonePtr[T any](v *T) *T { | ||
| if v == nil { | ||
| return nil | ||
| } | ||
| return new(*v) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package memstore | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| scdmodels "github.com/interuss/dss/pkg/scd/models" | ||
| "github.com/jackc/pgx/v5" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestUssAvailabilityUpsertGet(t *testing.T) { | ||
| ctx := writeCtx() | ||
| r := setUpStore(t) | ||
|
|
||
| got, err := r.UpsertUssAvailability(ctx, sampleAvailability()) | ||
| require.NoError(t, err) | ||
| require.Equal(t, manager, got.Uss) | ||
| require.Equal(t, scdmodels.UssAvailabilityStateNormal, got.Availability) | ||
| require.Equal(t, scdmodels.OVN("HXjEfPAc0lkinCf0ejtSGiGPE4o2Qogm-iXAGPG-QNo_"), got.Version) | ||
|
|
||
| fetched, err := r.GetUssAvailability(ctx, manager) | ||
| require.NoError(t, err) | ||
| require.Equal(t, got.Version, fetched.Version) | ||
| require.Equal(t, scdmodels.UssAvailabilityStateNormal, fetched.Availability) | ||
| } | ||
|
|
||
| func TestGetUssAvailabilityMissingReturnsErrNoRows(t *testing.T) { | ||
| r := setUpStore(t) | ||
| _, err := r.GetUssAvailability(writeCtx(), manager) | ||
| require.ErrorIs(t, err, pgx.ErrNoRows) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,29 +2,97 @@ package memstore | |
|
|
||
| import ( | ||
| "context" | ||
| "slices" | ||
|
|
||
| dsserr "github.com/interuss/dss/pkg/errors" | ||
| "github.com/interuss/dss/pkg/memstore/utils" | ||
| dssmodels "github.com/interuss/dss/pkg/models" | ||
| scdmodels "github.com/interuss/dss/pkg/scd/models" | ||
| dsssql "github.com/interuss/dss/pkg/sql" | ||
| "github.com/interuss/dss/pkg/timestamp" | ||
| "github.com/interuss/stacktrace" | ||
| "github.com/jackc/pgx/v5" | ||
| ) | ||
|
|
||
| func (rec *constraintRecord) toModel() *scdmodels.Constraint { | ||
| return &scdmodels.Constraint{ | ||
| ID: rec.ID, | ||
| Manager: rec.Manager, | ||
| Version: rec.Version, | ||
| OVN: scdmodels.NewOVNFromTime(rec.UpdatedAt, rec.ID.String()), | ||
| StartTime: utils.ClonePtr(rec.StartTime), | ||
| EndTime: utils.ClonePtr(rec.EndTime), | ||
| USSBaseURL: rec.USSBaseURL, | ||
| AltitudeLower: utils.ClonePtr(rec.AltitudeLower), | ||
| AltitudeUpper: utils.ClonePtr(rec.AltitudeUpper), | ||
| Cells: slices.Clone(rec.Cells), | ||
| } | ||
| } | ||
|
|
||
| func (r *repo) SearchConstraints(_ context.Context, v4d *dssmodels.Volume4D) ([]*scdmodels.Constraint, error) { | ||
| return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "SearchConstraints not implemented for memstore") | ||
| want, err := coveringSet(v4d) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if len(want) == 0 { | ||
| return []*scdmodels.Constraint{}, nil | ||
| } | ||
|
|
||
| var out []*scdmodels.Constraint | ||
| for _, rec := range r.state.Constraints { | ||
| if !overlaps(rec.Cells, want) { | ||
| continue | ||
| } | ||
| if !overlapsTime(rec.StartTime, rec.EndTime, v4d) { | ||
| continue | ||
| } | ||
| out = append(out, rec.toModel()) | ||
|
|
||
| if len(out) >= dssmodels.MaxResultLimit { // mirror SQL "LIMIT MaxResultLimit" | ||
| break | ||
| } | ||
| } | ||
| return out, nil | ||
| } | ||
|
|
||
| func (r *repo) GetConstraint(_ context.Context, id dssmodels.ID) (*scdmodels.Constraint, error) { | ||
| return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "GetConstraint not implemented for memstore") | ||
| rec, ok := r.state.Constraints[id] | ||
| if !ok { | ||
| return nil, pgx.ErrNoRows // TODO: #1608 | ||
| } | ||
| return rec.toModel(), nil | ||
| } | ||
|
|
||
| func (r *repo) UpsertConstraint(_ context.Context, constraint *scdmodels.Constraint) (*scdmodels.Constraint, error) { | ||
| return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "UpsertConstraint not implemented for memstore") | ||
| func (r *repo) UpsertConstraint(ctx context.Context, s *scdmodels.Constraint) (*scdmodels.Constraint, error) { | ||
| if _, err := dsssql.CellUnionToCellIdsWithValidation(s.Cells); err != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just validate with
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a small helper on the loop + convert, is it worth it to change it back?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'd factor away the loop in |
||
| return nil, stacktrace.Propagate(err, "Failed to convert array to jackc/pgtype") | ||
| } | ||
|
|
||
| now := timestamp.MustGetRequestTimestamp(ctx) | ||
|
|
||
| rec := &constraintRecord{ | ||
| ID: s.ID, | ||
| Manager: s.Manager, | ||
| Version: s.Version, | ||
| StartTime: utils.ClonePtr(s.StartTime), | ||
| EndTime: utils.ClonePtr(s.EndTime), | ||
| USSBaseURL: s.USSBaseURL, | ||
| AltitudeLower: utils.ClonePtr(s.AltitudeLower), | ||
| AltitudeUpper: utils.ClonePtr(s.AltitudeUpper), | ||
| Cells: slices.Clone(s.Cells), | ||
| UpdatedAt: now, | ||
| } | ||
| r.state.Constraints[s.ID] = rec | ||
| return rec.toModel(), nil | ||
| } | ||
|
|
||
| func (r *repo) DeleteConstraint(_ context.Context, id dssmodels.ID) error { | ||
| return stacktrace.NewErrorWithCode(dsserr.NotImplemented, "DeleteConstraint not implemented for memstore") | ||
| if _, ok := r.state.Constraints[id]; !ok { | ||
| return pgx.ErrNoRows // TODO: #1608 | ||
| } | ||
| delete(r.state.Constraints, id) | ||
| return nil | ||
| } | ||
|
|
||
| func (r *repo) CountConstraints(_ context.Context) (int64, error) { | ||
| return 0, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "CountConstraint not implemented for memstore") | ||
| return int64(len(r.state.Constraints)), nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package memstore | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/golang/geo/s2" | ||
| "github.com/interuss/dss/pkg/scd/models" | ||
| "github.com/jackc/pgx/v5" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestConstraintUpsertGetDelete(t *testing.T) { | ||
| ctx := writeCtx() | ||
| r := setUpStore(t) | ||
|
|
||
| got, err := r.UpsertConstraint(ctx, sampleConstraint()) | ||
| require.NoError(t, err) | ||
| require.Equal(t, constraintId, got.ID) | ||
| require.Equal(t, manager, got.Manager) | ||
| require.Equal(t, models.OVN("4Ne9uzrR5K9LYEyJ-c6rTI0r-FTuLQGMSBR1j.SaTvk_"), got.OVN) | ||
|
|
||
| fetched, err := r.GetConstraint(ctx, constraintId) | ||
| require.NoError(t, err) | ||
| require.Equal(t, got.OVN, fetched.OVN) | ||
| require.Equal(t, cells, fetched.Cells) | ||
|
|
||
| count, err := r.CountConstraints(ctx) | ||
| require.NoError(t, err) | ||
| require.Equal(t, int64(1), count) | ||
|
|
||
| require.NoError(t, r.DeleteConstraint(ctx, constraintId)) | ||
|
|
||
| _, err = r.GetConstraint(ctx, constraintId) | ||
| require.ErrorIs(t, err, pgx.ErrNoRows) | ||
| } | ||
|
|
||
| func TestConstraintGetMissingReturnsErrNoRows(t *testing.T) { | ||
| r := setUpStore(t) | ||
| _, err := r.GetConstraint(writeCtx(), constraintId) | ||
| require.ErrorIs(t, err, pgx.ErrNoRows) | ||
| } | ||
|
|
||
| func TestConstraintDeleteMissingReturnsErrNoRows(t *testing.T) { | ||
| r := setUpStore(t) | ||
| err := r.DeleteConstraint(writeCtx(), constraintId) | ||
| require.ErrorIs(t, err, pgx.ErrNoRows) | ||
| } | ||
|
|
||
| func TestSearchConstraints(t *testing.T) { | ||
| ctx := writeCtx() | ||
| r := setUpStore(t) | ||
| _, err := r.UpsertConstraint(ctx, sampleConstraint()) | ||
| require.NoError(t, err) | ||
|
|
||
| // Overlapping volume with no time bounds matches. | ||
| res, err := r.SearchConstraints(ctx, volume4D(cells, nil, nil, nil, nil)) | ||
| require.NoError(t, err) | ||
| require.Len(t, res, 1) | ||
|
|
||
| // Time window after the constraint's end excludes it. | ||
| afterStart := endTime.Add(time.Hour) | ||
| afterEnd := afterStart.Add(time.Hour) | ||
| res, err = r.SearchConstraints(ctx, volume4D(cells, &afterStart, &afterEnd, nil, nil)) | ||
| require.NoError(t, err) | ||
| require.Empty(t, res) | ||
|
|
||
| // No covering cells returns an empty (non-nil) slice. | ||
| res, err = r.SearchConstraints(ctx, volume4D(s2.CellUnion{}, nil, nil, nil, nil)) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, res) | ||
| require.Empty(t, res) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.