Various DAL codebase improvements and cleanups
This commit is contained in:
+6
-6
@@ -8,6 +8,11 @@ import (
|
||||
)
|
||||
|
||||
func (app *CortezaApp) initDAL(ctx context.Context, log *zap.Logger) (err error) {
|
||||
// no-op - if DAL is already initialized
|
||||
if dal.Initialized() {
|
||||
return
|
||||
}
|
||||
|
||||
// Verify that primary store is connected
|
||||
// or return error
|
||||
if app.Store == nil {
|
||||
@@ -15,12 +20,7 @@ func (app *CortezaApp) initDAL(ctx context.Context, log *zap.Logger) (err error)
|
||||
}
|
||||
|
||||
// Init DAL and prepare default connection
|
||||
svc, err := dal.New(log.Named("dal"), app.Opt.Environment.IsDevelopment())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dal.SetGlobal(svc)
|
||||
dal.SetGlobal(dal.New(log.Named("dal"), app.Opt.Environment.IsDevelopment()))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ type (
|
||||
|
||||
GetConnectionByID(uint64) *dal.ConnectionWrap
|
||||
|
||||
SearchModelIssues(connectionID, resourceID uint64) (out []error)
|
||||
SearchModelIssues(resourceID uint64) (out []error)
|
||||
}
|
||||
|
||||
dalDater interface {
|
||||
|
||||
@@ -77,7 +77,7 @@ type (
|
||||
ReplaceModel(context.Context, *dal.Model) error
|
||||
RemoveModel(ctx context.Context, connectionID, ID uint64) error
|
||||
ReplaceModelAttribute(ctx context.Context, model *dal.Model, old, new *dal.Attribute, trans ...dal.TransformationFunction) (err error)
|
||||
SearchModelIssues(connectionID, ID uint64) []error
|
||||
SearchModelIssues(ID uint64) []error
|
||||
}
|
||||
)
|
||||
|
||||
@@ -295,7 +295,7 @@ func (svc module) procDal(m *types.Module) {
|
||||
return
|
||||
}
|
||||
|
||||
ii := svc.dal.SearchModelIssues(m.Config.DAL.ConnectionID, m.ID)
|
||||
ii := svc.dal.SearchModelIssues(m.ID)
|
||||
if len(ii) == 0 {
|
||||
m.Issues = nil
|
||||
return
|
||||
@@ -567,7 +567,7 @@ func (svc module) updater(ctx context.Context, namespaceID, moduleID uint64, act
|
||||
|
||||
// @todo rethink how model issues and attempted module update with records should interact.
|
||||
// this is a temporary solution but should be re-thinked.
|
||||
modelIssues := svc.dal.SearchModelIssues(m.Config.DAL.ConnectionID, m.ID)
|
||||
modelIssues := svc.dal.SearchModelIssues(m.ID)
|
||||
if len(modelIssues) == 0 {
|
||||
if set, _, err = dalutils.ComposeRecordsList(ctx, svc.dal, m, types.RecordFilter{Paging: filter.Paging{Limit: 1}, Check: func(r *types.Record) (bool, error) { return true, nil }}); err != nil {
|
||||
return err
|
||||
@@ -1353,6 +1353,18 @@ func moduleSystemFieldsToAttributes(mod *types.Module) (out dal.AttributeSet, er
|
||||
// moduleFieldToAttribute converts the given module field to a DAL attribute
|
||||
func moduleFieldToAttribute(f *types.ModuleField, conn *dal.ConnectionWrap) (out *dal.Attribute, err error) {
|
||||
var (
|
||||
// ensure JSON encoded record values always have ident to use
|
||||
// either from the connection config or "values" as a failsafe
|
||||
attribIdent = func() string {
|
||||
var attribIdent = conn.Config.AttributeIdent
|
||||
|
||||
if len(attribIdent) > 0 {
|
||||
return attribIdent
|
||||
}
|
||||
|
||||
return "values"
|
||||
}()
|
||||
|
||||
// generate dal.Codec for each attribute
|
||||
// using encoding strategy for that attribute
|
||||
// with failsafe on JSON RVS.
|
||||
@@ -1372,7 +1384,7 @@ func moduleFieldToAttribute(f *types.ModuleField, conn *dal.ConnectionWrap) (out
|
||||
// defaulting to RecordValueSetJSON with
|
||||
// default attribute ident from connection
|
||||
return &dal.CodecRecordValueSetJSON{
|
||||
Ident: conn.Config.AttributeIdent,
|
||||
Ident: attribIdent,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-9
@@ -47,12 +47,7 @@ func (svc *service) SearchConnectionIssues(connectionID uint64) (out []error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (svc *service) SearchModelIssues(connectionID, resourceID uint64) (out []error) {
|
||||
// @todo index by connection as well
|
||||
// if _, ok := svc.modelIssues[connectionID]; !ok {
|
||||
// return
|
||||
// }
|
||||
|
||||
func (svc *service) SearchModelIssues(resourceID uint64) (out []error) {
|
||||
for _, issue := range svc.modelIssues[resourceID] {
|
||||
out = append(out, issue.err)
|
||||
}
|
||||
@@ -64,8 +59,8 @@ func (svc *service) hasConnectionIssues(connectionID uint64) bool {
|
||||
return len(svc.SearchConnectionIssues(connectionID)) > 0
|
||||
}
|
||||
|
||||
func (svc *service) hasModelIssues(connectionID, modelID uint64) bool {
|
||||
return len(svc.SearchModelIssues(connectionID, modelID)) > 0
|
||||
func (svc *service) hasModelIssues(modelID uint64) bool {
|
||||
return len(svc.SearchModelIssues(modelID)) > 0
|
||||
}
|
||||
|
||||
func (svc *service) updateIssues(issues *issueHelper) {
|
||||
@@ -126,7 +121,7 @@ func (svc *service) canOpData(connectionID, modelID uint64) (err error) {
|
||||
if svc.hasConnectionIssues(connectionID) {
|
||||
return errRecordOpProblematicConnection(connectionID)
|
||||
}
|
||||
if svc.hasModelIssues(connectionID, modelID) {
|
||||
if svc.hasModelIssues(modelID) {
|
||||
return errRecordOpProblematicModel(modelID)
|
||||
}
|
||||
|
||||
|
||||
+45
-7
@@ -29,16 +29,42 @@ type (
|
||||
connectionIssues dalIssueIndex
|
||||
modelIssues dalIssueIndex
|
||||
}
|
||||
|
||||
FullService interface {
|
||||
Drivers() (drivers []Driver)
|
||||
|
||||
ReplaceSensitivityLevel(levels ...SensitivityLevel) (err error)
|
||||
RemoveSensitivityLevel(levelIDs ...uint64) (err error)
|
||||
|
||||
GetConnectionByID(connectionID uint64) *ConnectionWrap
|
||||
|
||||
ReplaceConnection(ctx context.Context, cw *ConnectionWrap, isDefault bool) (err error)
|
||||
RemoveConnection(ctx context.Context, ID uint64) (err error)
|
||||
|
||||
SearchModels(ctx context.Context) (out ModelSet, err error)
|
||||
ReplaceModel(ctx context.Context, model *Model) (err error)
|
||||
RemoveModel(ctx context.Context, connectionID, ID uint64) (err error)
|
||||
ReplaceModelAttribute(ctx context.Context, model *Model, old, new *Attribute, trans ...TransformationFunction) (err error)
|
||||
FindModelByResourceID(connectionID uint64, resourceID uint64) *Model
|
||||
FindModelByResourceIdent(connectionID uint64, resourceType, resourceIdent string) *Model
|
||||
FindModelByIdent(connectionID uint64, ident string) *Model
|
||||
|
||||
Create(ctx context.Context, mf ModelRef, operations OperationSet, rr ...ValueGetter) (err error)
|
||||
Update(ctx context.Context, mf ModelRef, operations OperationSet, rr ...ValueGetter) (err error)
|
||||
Search(ctx context.Context, mf ModelRef, operations OperationSet, f filter.Filter) (iter Iterator, err error)
|
||||
Lookup(ctx context.Context, mf ModelRef, operations OperationSet, lookup ValueGetter, dst ValueSetter) (err error)
|
||||
Delete(ctx context.Context, mf ModelRef, operations OperationSet, vv ...ValueGetter) (err error)
|
||||
Truncate(ctx context.Context, mf ModelRef, operations OperationSet) (err error)
|
||||
|
||||
SearchConnectionIssues(connectionID uint64) (out []error)
|
||||
SearchModelIssues(resourceID uint64) (out []error)
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
gSvc *service
|
||||
)
|
||||
|
||||
func SetGlobal(svc *service) {
|
||||
gSvc = svc
|
||||
}
|
||||
|
||||
// New creates a DAL service with the primary connection
|
||||
//
|
||||
// It needs an established and working connection to the primary store
|
||||
@@ -58,6 +84,10 @@ func New(log *zap.Logger, inDev bool) (*service, error) {
|
||||
return svc, nil
|
||||
}
|
||||
|
||||
func Initialized() bool {
|
||||
return gSvc != nil
|
||||
}
|
||||
|
||||
// Service returns the global initialized DAL service
|
||||
//
|
||||
// Function will panic if DAL service is not set (via SetGlobal)
|
||||
@@ -69,6 +99,14 @@ func Service() *service {
|
||||
return gSvc
|
||||
}
|
||||
|
||||
func SetGlobal(svc *service, err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
gSvc = svc
|
||||
}
|
||||
|
||||
// Purge resets the service to the initial zero state
|
||||
// @todo will probably need to change but for now this is ok
|
||||
//
|
||||
@@ -515,7 +553,7 @@ func (svc *service) ReplaceModel(ctx context.Context, model *Model) (err error)
|
||||
|
||||
// Add to connection
|
||||
connectionIssues := svc.hasConnectionIssues(model.ConnectionID)
|
||||
modelIssues := svc.hasModelIssues(model.ConnectionID, model.ResourceID)
|
||||
modelIssues := svc.hasModelIssues(model.ResourceID)
|
||||
if connectionIssues {
|
||||
log.Warn("not adding to connection due to connection issues")
|
||||
}
|
||||
@@ -677,7 +715,7 @@ func (svc *service) ReplaceModelAttribute(ctx context.Context, model *Model, old
|
||||
defer svc.updateIssues(issues)
|
||||
|
||||
if model.ConnectionID == 0 {
|
||||
model.ConnectionID = gSvc.defConnID
|
||||
model.ConnectionID = svc.defConnID
|
||||
}
|
||||
|
||||
// Validation
|
||||
@@ -710,7 +748,7 @@ func (svc *service) ReplaceModelAttribute(ctx context.Context, model *Model, old
|
||||
// Update attribute
|
||||
// Update connection
|
||||
connectionIssues := svc.hasConnectionIssues(model.ConnectionID)
|
||||
modelIssues := svc.hasModelIssues(model.ConnectionID, model.ResourceID)
|
||||
modelIssues := svc.hasModelIssues(model.ResourceID)
|
||||
|
||||
if !modelIssues && !connectionIssues {
|
||||
svc.logger.Debug("updating model attribute", zap.Uint64("connection", model.ConnectionID), zap.Uint64("model", model.ResourceID))
|
||||
|
||||
@@ -14,9 +14,6 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/dal"
|
||||
"github.com/cortezaproject/corteza-server/pkg/filter"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/app"
|
||||
"github.com/cortezaproject/corteza-server/compose/rest"
|
||||
"github.com/cortezaproject/corteza-server/compose/service"
|
||||
@@ -24,6 +21,7 @@ import (
|
||||
"github.com/cortezaproject/corteza-server/pkg/api/server"
|
||||
"github.com/cortezaproject/corteza-server/pkg/auth"
|
||||
"github.com/cortezaproject/corteza-server/pkg/cli"
|
||||
"github.com/cortezaproject/corteza-server/pkg/dal"
|
||||
"github.com/cortezaproject/corteza-server/pkg/envoy"
|
||||
"github.com/cortezaproject/corteza-server/pkg/envoy/csv"
|
||||
"github.com/cortezaproject/corteza-server/pkg/envoy/directory"
|
||||
@@ -59,22 +57,9 @@ type (
|
||||
}
|
||||
|
||||
dalSvc interface {
|
||||
dal.FullService
|
||||
|
||||
Purge(ctx context.Context)
|
||||
|
||||
GetConnectionByID(uint64) *dal.ConnectionWrap
|
||||
|
||||
SearchModels(ctx context.Context) (out dal.ModelSet, err error)
|
||||
RemoveModel(ctx context.Context, connectionID, ID uint64) (err error)
|
||||
ReplaceModel(ctx context.Context, model *dal.Model) (err error)
|
||||
ReplaceModelAttribute(ctx context.Context, model *dal.Model, old, new *dal.Attribute, trans ...dal.TransformationFunction) (err error)
|
||||
SearchModelIssues(connectionID, resourceID uint64) (out []error)
|
||||
|
||||
Create(ctx context.Context, m dal.ModelRef, operations dal.OperationSet, vv ...dal.ValueGetter) error
|
||||
Update(ctx context.Context, m dal.ModelRef, operations dal.OperationSet, rr ...dal.ValueGetter) (err error)
|
||||
Search(ctx context.Context, m dal.ModelRef, operations dal.OperationSet, f filter.Filter) (dal.Iterator, error)
|
||||
Lookup(ctx context.Context, m dal.ModelRef, operations dal.OperationSet, lookup dal.ValueGetter, dst dal.ValueSetter) (err error)
|
||||
Delete(ctx context.Context, m dal.ModelRef, operations dal.OperationSet, pkv ...dal.ValueGetter) (err error)
|
||||
Truncate(ctx context.Context, m dal.ModelRef, operations dal.OperationSet) (err error)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ type (
|
||||
Truncate(ctx context.Context, mf dal.ModelRef, operations dal.OperationSet) (err error)
|
||||
|
||||
SearchConnectionIssues(connectionID uint64) (out []error)
|
||||
SearchModelIssues(connectionID, resourceID uint64) (out []error)
|
||||
SearchModelIssues(resourceID uint64) (out []error)
|
||||
}
|
||||
|
||||
dalConnectionRestRsp struct {
|
||||
|
||||
@@ -38,7 +38,7 @@ type (
|
||||
RemoveModel(ctx context.Context, connectionID, ID uint64) (err error)
|
||||
ReplaceModel(ctx context.Context, model *dal.Model) (err error)
|
||||
ReplaceModelAttribute(ctx context.Context, model *dal.Model, old, new *dal.Attribute, trans ...dal.TransformationFunction) (err error)
|
||||
SearchModelIssues(connectionID, resourceID uint64) (out []error)
|
||||
SearchModelIssues(resourceID uint64) (out []error)
|
||||
|
||||
Create(ctx context.Context, m dal.ModelRef, operations dal.OperationSet, vv ...dal.ValueGetter) error
|
||||
Update(ctx context.Context, m dal.ModelRef, operations dal.OperationSet, rr ...dal.ValueGetter) (err error)
|
||||
|
||||
Reference in New Issue
Block a user