Skip to content

Commit

Permalink
feat: Change GetCollectionBySchemaFoo funcs to return many (#1984)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves #1963

## Description

Changes GetCollectionBySchemaFoo function signatures to return many
Collections, instead of single values.

Conceptually these functions were broken and relied on the fact that we
cannot yet link multiple collections to the same schema(version)(s).
This changes their signatures and the code that calls them to reflect
what they should do long term.

The code that calls these functions now hosts the 'broken' code. These
will still need to change once we add global IDs.

The implementation of these functions will change later in
#1964.
  • Loading branch information
AndrewSisley authored Oct 19, 2023
1 parent 9e1ad62 commit e0e1779
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 119 deletions.
38 changes: 36 additions & 2 deletions cli/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ func MakeCollectionCommand(cfg *config.Config) *cobra.Command {
store := mustGetStoreContext(cmd)

var col client.Collection
var cols []client.Collection
switch {
case versionID != "":
col, err = store.GetCollectionByVersionID(cmd.Context(), versionID)
cols, err = store.GetCollectionsByVersionID(cmd.Context(), versionID)

case schemaID != "":
col, err = store.GetCollectionBySchemaID(cmd.Context(), schemaID)
cols, err = store.GetCollectionsBySchemaID(cmd.Context(), schemaID)

case name != "":
col, err = store.GetCollectionByName(cmd.Context(), name)
cols = []client.Collection{col}

default:
return nil
Expand All @@ -60,6 +62,38 @@ func MakeCollectionCommand(cfg *config.Config) *cobra.Command {
if err != nil {
return err
}

if schemaID != "" && versionID != "" && len(cols) > 0 {
if cols[0].SchemaID() != schemaID {
// If the a versionID has been provided that does not pair up with the given schemaID
// we should error and let the user know they have provided impossible params.
// We only need to check the first item - they will all be the same.
return NewErrSchemaVersionNotOfSchema(schemaID, versionID)
}
}

if name != "" {
// Multiple params may have been specified, and in some cases both are needed.
// For example if a schema version and a collection name have been provided,
// we need to ensure that a collection at the requested version is returned.
// Likewise we need to ensure that if a collection name and schema id are provided,
// but there are none matching both, that nothing is returned.
fetchedCols := cols
cols = nil
for _, c := range fetchedCols {
if c.Name() == name {
cols = append(cols, c)
break
}
}
}

if len(cols) != 1 {
// If more than one collection matches the given criteria we cannot set the context collection
return nil
}
col = cols[0]

if tx, ok := cmd.Context().Value(txContextKey).(datastore.Txn); ok {
col = col.WithTxn(tx)
}
Expand Down
26 changes: 19 additions & 7 deletions cli/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,29 @@ import (
"github.com/sourcenetwork/defradb/errors"
)

const errInvalidLensConfig = "invalid lens configuration"
const (
errInvalidLensConfig string = "invalid lens configuration"
errSchemaVersionNotOfSchema string = "the given schema version is from a different schema"
)

var (
ErrNoDocOrFile = errors.New("document or file must be defined")
ErrInvalidDocument = errors.New("invalid document")
ErrNoDocKeyOrFilter = errors.New("document key or filter must be defined")
ErrInvalidExportFormat = errors.New("invalid export format")
ErrNoLensConfig = errors.New("lens config cannot be empty")
ErrInvalidLensConfig = errors.New("invalid lens configuration")
ErrNoDocOrFile = errors.New("document or file must be defined")
ErrInvalidDocument = errors.New("invalid document")
ErrNoDocKeyOrFilter = errors.New("document key or filter must be defined")
ErrInvalidExportFormat = errors.New("invalid export format")
ErrNoLensConfig = errors.New("lens config cannot be empty")
ErrInvalidLensConfig = errors.New("invalid lens configuration")
ErrSchemaVersionNotOfSchema = errors.New(errSchemaVersionNotOfSchema)
)

func NewErrInvalidLensConfig(inner error) error {
return errors.Wrap(errInvalidLensConfig, inner)
}

func NewErrSchemaVersionNotOfSchema(schemaID string, schemaVersionID string) error {
return errors.New(
errSchemaVersionNotOfSchema,
errors.NewKV("SchemaID", schemaID),
errors.NewKV("SchemaVersionID", schemaVersionID),
)
}
12 changes: 6 additions & 6 deletions client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ type Store interface {
// If no matching collection is found an error will be returned.
GetCollectionByName(context.Context, CollectionName) (Collection, error)

// GetCollectionBySchemaID attempts to retrieve a collection matching the given schema ID.
// GetCollectionsBySchemaID attempts to retrieve all collections using the given schema ID.
//
// If no matching collection is found an error will be returned.
GetCollectionBySchemaID(context.Context, string) (Collection, error)
// If no matching collection is found an empty set will be returned.
GetCollectionsBySchemaID(context.Context, string) ([]Collection, error)

// GetCollectionBySchemaID attempts to retrieve a collection matching the given schema version ID.
// GetCollectionsByVersionID attempts to retrieve all collections using the given schema version ID.
//
// If no matching collection is found an error will be returned.
GetCollectionByVersionID(context.Context, string) (Collection, error)
// If no matching collections are found an empty set will be returned.
GetCollectionsByVersionID(context.Context, string) ([]Collection, error)

// GetAllCollections returns all the collections and their descriptions that currently exist within
// this [Store].
Expand Down
16 changes: 16 additions & 0 deletions client/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
errUninitializeProperty string = "invalid state, required property is uninitialized"
errMaxTxnRetries string = "reached maximum transaction reties"
errRelationOneSided string = "relation must be defined on both schemas"
errCollectionNotFound string = "collection not found"
)

// Errors returnable from this package.
Expand All @@ -45,6 +46,7 @@ var (
ErrInvalidDocKeyVersion = errors.New("invalid DocKey version")
ErrMaxTxnRetries = errors.New(errMaxTxnRetries)
ErrRelationOneSided = errors.New(errRelationOneSided)
ErrCollectionNotFound = errors.New(errCollectionNotFound)
)

// NewErrFieldNotExist returns an error indicating that the given field does not exist.
Expand Down Expand Up @@ -107,3 +109,17 @@ func NewErrRelationOneSided(fieldName string, typeName string) error {
errors.NewKV("Type", typeName),
)
}

func NewErrCollectionNotFoundForSchemaVersion(schemaVersionID string) error {
return errors.New(
errCollectionNotFound,
errors.NewKV("SchemaVersionID", schemaVersionID),
)
}

func NewErrCollectionNotFoundForSchema(schemaID string) error {
return errors.New(
errCollectionNotFound,
errors.NewKV("SchemaID", schemaID),
)
}
56 changes: 28 additions & 28 deletions client/mocks/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e0e1779

Please sign in to comment.