Skip to content

Commit c44105e

Browse files
committed
[raft] Add base memstore
1 parent 4e0c128 commit c44105e

18 files changed

Lines changed: 387 additions & 0 deletions

File tree

pkg/aux_/store/memstore/doc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Package aux_.store.memstore provides a full implementation of store.Store[aux_.repos.Repository]
2+
// storing data in memory. It is meant to be used by raftstore.
3+
package memstore

pkg/aux_/store/memstore/dss.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package memstore
2+
3+
import (
4+
"context"
5+
6+
auxmodels "github.com/interuss/dss/pkg/aux_/models"
7+
dsserr "github.com/interuss/dss/pkg/errors"
8+
"github.com/interuss/stacktrace"
9+
)
10+
11+
func (r *repo) SaveOwnMetadata(_ context.Context, locality string, publicEndpoint string) error {
12+
return stacktrace.NewErrorWithCode(dsserr.NotImplemented, "SaveOwnMetadata not implemented for memstore")
13+
}
14+
15+
func (r *repo) GetDSSMetadata(_ context.Context) ([]*auxmodels.DSSMetadata, error) {
16+
return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "GetDSSMetadata not implemented for memstore")
17+
}
18+
19+
func (r *repo) RecordHeartbeat(_ context.Context, heartbeat auxmodels.Heartbeat) error {
20+
return stacktrace.NewErrorWithCode(dsserr.NotImplemented, "RecordHeartbeat not implemented for memstore")
21+
}
22+
23+
func (r *repo) GetDSSAirspaceRepresentationID(_ context.Context) (string, error) {
24+
return "", stacktrace.NewErrorWithCode(dsserr.NotImplemented, "GetDSSAirspaceRepresentationID not implemented for memstore")
25+
}

pkg/aux_/store/memstore/store.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package memstore
2+
3+
import (
4+
"context"
5+
6+
"github.com/interuss/dss/pkg/aux_/repos"
7+
"github.com/interuss/dss/pkg/memstore"
8+
"go.uber.org/zap"
9+
)
10+
11+
// repo is a full implementation of aux_.repos.Repository for memory-based storage.
12+
type repo struct{}
13+
14+
func Init(ctx context.Context, logger *zap.Logger) (*memstore.Store[repos.Repository], error) {
15+
return memstore.Init(ctx, logger, "aux_", &repo{})
16+
}
17+
18+
func (r *repo) GetRepo() repos.Repository { return r }

pkg/aux_/store/store.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55

66
"github.com/interuss/dss/pkg/aux_/repos"
7+
auxmemstore "github.com/interuss/dss/pkg/aux_/store/memstore"
78
auxraftstore "github.com/interuss/dss/pkg/aux_/store/raftstore"
89
auxsqlstore "github.com/interuss/dss/pkg/aux_/store/sqlstore"
910
dssstore "github.com/interuss/dss/pkg/store"
@@ -24,6 +25,8 @@ func Init(ctx context.Context, logger *zap.Logger, withCheckCron bool) (Store, e
2425
return auxsqlstore.Init(ctx, logger, withCheckCron)
2526
case params.RaftStoreType:
2627
return auxraftstore.Init(ctx, logger)
28+
case params.MemStoreType:
29+
return auxmemstore.Init(ctx, logger)
2730
default:
2831
return nil, stacktrace.NewError("Unsupported store type %q for aux", storeType)
2932
}

pkg/memstore/store.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package memstore
2+
3+
// Memstore are a special kind of store:
4+
// Store instances store data in memory. There is no persistent storage.
5+
// Store instances are a singleton.
6+
// Repository usage is not thread-safe.
7+
//
8+
// As of now, they are made to be used by raftstorage.
9+
// Adaptations could be done to use them directly in the future.
10+
11+
import (
12+
"context"
13+
"sync"
14+
15+
dsserr "github.com/interuss/dss/pkg/errors"
16+
"github.com/interuss/dss/pkg/logging"
17+
"github.com/interuss/stacktrace"
18+
"go.uber.org/zap"
19+
)
20+
21+
type MemRepo[R any] interface {
22+
GetRepo() R
23+
}
24+
25+
type Store[R any] struct {
26+
logger *zap.Logger
27+
28+
name string
29+
memRepo MemRepo[R]
30+
}
31+
32+
var (
33+
stores = map[string]any{}
34+
storesMu sync.Mutex
35+
)
36+
37+
func Init[R any](ctx context.Context, logger *zap.Logger, name string, r MemRepo[R]) (*Store[R], error) {
38+
39+
storesMu.Lock()
40+
defer storesMu.Unlock()
41+
if s, ok := stores[name]; ok {
42+
return s.(*Store[R]), nil
43+
}
44+
45+
store := &Store[R]{
46+
name: name,
47+
logger: logging.WithValuesFromContext(ctx, logger),
48+
memRepo: r,
49+
}
50+
51+
stores[name] = store
52+
return store, nil
53+
}
54+
55+
func (s *Store[R]) Transact(ctx context.Context, requestType string, payload any, _ func(context.Context, R) error) (any, error) {
56+
return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "Transact not implemented for memstore")
57+
}
58+
59+
func (s *Store[R]) Interact(_ context.Context) (R, error) {
60+
return s.memRepo.GetRepo(), nil
61+
}
62+
63+
func (s *Store[R]) Close() error {
64+
return nil
65+
}

pkg/rid/store/memstore/doc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Package rid.store.memstore provides a full implementation of store.Store[rid.repos.Repository]
2+
// storing data in memory. It as meant to be used by raftstore.
3+
package memstore
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package memstore
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/golang/geo/s2"
8+
dsserr "github.com/interuss/dss/pkg/errors"
9+
dssmodels "github.com/interuss/dss/pkg/models"
10+
ridmodels "github.com/interuss/dss/pkg/rid/models"
11+
"github.com/interuss/stacktrace"
12+
)
13+
14+
func (r *repo) GetISA(_ context.Context, id dssmodels.ID, forUpdate bool) (*ridmodels.IdentificationServiceArea, error) {
15+
return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "GetISA not implemented for memstore")
16+
}
17+
18+
func (r *repo) DeleteISA(_ context.Context, isa *ridmodels.IdentificationServiceArea) (*ridmodels.IdentificationServiceArea, error) {
19+
return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "DeleteISA not implemented for memstore")
20+
}
21+
22+
func (r *repo) InsertISA(_ context.Context, isa *ridmodels.IdentificationServiceArea) (*ridmodels.IdentificationServiceArea, error) {
23+
return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "InsertISA not implemented for memstore")
24+
}
25+
26+
func (r *repo) UpdateISA(_ context.Context, isa *ridmodels.IdentificationServiceArea) (*ridmodels.IdentificationServiceArea, error) {
27+
return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "UpdateISA not implemented for memstore")
28+
}
29+
30+
func (r *repo) SearchISAs(_ context.Context, cells s2.CellUnion, earliest *time.Time, latest *time.Time) ([]*ridmodels.IdentificationServiceArea, error) {
31+
return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "SearchISAs not implemented for memstore")
32+
}
33+
34+
func (r *repo) ListExpiredISAs(_ context.Context, writer string, threshold time.Time) ([]*ridmodels.IdentificationServiceArea, error) {
35+
return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "ListExpiredISAs not implemented for memstore")
36+
}
37+
38+
func (r *repo) CountISAs(_ context.Context) (int64, error) {
39+
return 0, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "CountISAs not implemented for memstore")
40+
}

pkg/rid/store/memstore/store.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package memstore
2+
3+
import (
4+
"context"
5+
6+
"github.com/interuss/dss/pkg/memstore"
7+
"github.com/interuss/dss/pkg/rid/repos"
8+
"go.uber.org/zap"
9+
)
10+
11+
// repo is a full implementation of rid.repos.Repository for memory-based storage.
12+
type repo struct{}
13+
14+
func Init(ctx context.Context, logger *zap.Logger) (*memstore.Store[repos.Repository], error) {
15+
return memstore.Init(ctx, logger, "rid", &repo{})
16+
}
17+
18+
func (r *repo) GetRepo() repos.Repository { return r }
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package memstore
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/golang/geo/s2"
8+
dsserr "github.com/interuss/dss/pkg/errors"
9+
dssmodels "github.com/interuss/dss/pkg/models"
10+
ridmodels "github.com/interuss/dss/pkg/rid/models"
11+
"github.com/interuss/stacktrace"
12+
)
13+
14+
func (r *repo) GetSubscription(_ context.Context, id dssmodels.ID) (*ridmodels.Subscription, error) {
15+
return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "GetSubscription not implemented for memstore")
16+
}
17+
18+
func (r *repo) DeleteSubscription(_ context.Context, sub *ridmodels.Subscription) (*ridmodels.Subscription, error) {
19+
return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "DeleteSubscription not implemented for memstore")
20+
}
21+
22+
func (r *repo) InsertSubscription(_ context.Context, sub *ridmodels.Subscription) (*ridmodels.Subscription, error) {
23+
return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "InsertSubscription not implemented for memstore")
24+
}
25+
26+
func (r *repo) UpdateSubscription(_ context.Context, sub *ridmodels.Subscription) (*ridmodels.Subscription, error) {
27+
return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "UpdateSubscription not implemented for memstore")
28+
}
29+
30+
func (r *repo) SearchSubscriptions(_ context.Context, cells s2.CellUnion) ([]*ridmodels.Subscription, error) {
31+
return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "SearchSubscriptions not implemented for memstore")
32+
}
33+
34+
func (r *repo) SearchSubscriptionsByOwner(_ context.Context, cells s2.CellUnion, owner dssmodels.Owner) ([]*ridmodels.Subscription, error) {
35+
return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "SearchSubscriptionsByOwner not implemented for memstore")
36+
}
37+
38+
func (r *repo) UpdateNotificationIdxsInCells(_ context.Context, cells s2.CellUnion) ([]*ridmodels.Subscription, error) {
39+
return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "UpdateNotificationIdxsInCells not implemented for memstore")
40+
}
41+
42+
func (r *repo) MaxSubscriptionCountInCellsByOwner(_ context.Context, cells s2.CellUnion, owner dssmodels.Owner) (int, error) {
43+
return 0, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "MaxSubscriptionCountInCellsByOwner not implemented for memstore")
44+
}
45+
46+
func (r *repo) ListExpiredSubscriptions(_ context.Context, writer string, threshold time.Time) ([]*ridmodels.Subscription, error) {
47+
return nil, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "ListExpiredSubscriptions not implemented for memstore")
48+
}
49+
50+
func (r *repo) CountSubscriptions(_ context.Context) (int64, error) {
51+
return 0, stacktrace.NewErrorWithCode(dsserr.NotImplemented, "CountSubscriptions not implemented for memstore")
52+
}

pkg/rid/store/store.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55

66
"github.com/interuss/dss/pkg/rid/repos"
7+
ridmemstore "github.com/interuss/dss/pkg/rid/store/memstore"
78
ridraftstore "github.com/interuss/dss/pkg/rid/store/raftstore"
89
ridsqlstore "github.com/interuss/dss/pkg/rid/store/sqlstore"
910
dssstore "github.com/interuss/dss/pkg/store"
@@ -24,6 +25,8 @@ func Init(ctx context.Context, logger *zap.Logger, withCheckCron bool) (Store, e
2425
return ridsqlstore.Init(ctx, logger, withCheckCron)
2526
case params.RaftStoreType:
2627
return ridraftstore.Init(ctx, logger)
28+
case params.MemStoreType:
29+
return ridmemstore.Init(ctx, logger)
2730
default:
2831
return nil, stacktrace.NewError("Unsupported store type %q for rid", storeType)
2932
}

0 commit comments

Comments
 (0)