Skip to content

Commit

Permalink
feat: Rework GetCollection/SchemaByFoo funcs into single (#2319)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves #2008

## Description

Reworks the GetCollection/SchemaByFoo funcs into single GetCollections
and GetSchemas funcs.
  • Loading branch information
AndrewSisley authored Feb 20, 2024
1 parent cdb8ff8 commit 392bd96
Show file tree
Hide file tree
Showing 26 changed files with 451 additions and 881 deletions.
58 changes: 17 additions & 41 deletions cli/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package cli
import (
"context"

"github.com/sourcenetwork/immutable"
"github.com/spf13/cobra"

"github.com/sourcenetwork/defradb/client"
Expand All @@ -24,6 +25,7 @@ func MakeCollectionCommand() *cobra.Command {
var name string
var schemaRoot string
var versionID string
var getInactive bool
var cmd = &cobra.Command{
Use: "collection [--name <name> --schema <schemaRoot> --version <versionID>]",
Short: "Interact with a collection.",
Expand All @@ -44,57 +46,30 @@ func MakeCollectionCommand() *cobra.Command {
}
store := mustGetContextStore(cmd)

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

case schemaRoot != "":
cols, err = store.GetCollectionsBySchemaRoot(cmd.Context(), schemaRoot)

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

default:
return nil
options := client.CollectionFetchOptions{}
if versionID != "" {
options.SchemaVersionID = immutable.Some(versionID)
}

if err != nil {
return err
if schemaRoot != "" {
options.SchemaRoot = immutable.Some(schemaRoot)
}

if schemaRoot != "" && versionID != "" && len(cols) > 0 {
if cols[0].SchemaRoot() != schemaRoot {
// If the a versionID has been provided that does not pair up with the given schema root
// 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(schemaRoot, versionID)
}
if name != "" {
options.Name = immutable.Some(name)
}
if getInactive {
options.IncludeInactive = immutable.Some(getInactive)
}

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().Value() == name {
cols = append(cols, c)
break
}
}
cols, err := store.GetCollections(cmd.Context(), options)
if err != nil {
return err
}

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

if tx, ok := cmd.Context().Value(txContextKey).(datastore.Txn); ok {
col = col.WithTxn(tx)
Expand All @@ -109,5 +84,6 @@ func MakeCollectionCommand() *cobra.Command {
cmd.PersistentFlags().StringVar(&name, "name", "", "Collection name")
cmd.PersistentFlags().StringVar(&schemaRoot, "schema", "", "Collection schema Root")
cmd.PersistentFlags().StringVar(&versionID, "version", "", "Collection version ID")
cmd.PersistentFlags().BoolVar(&getInactive, "get-inactive", false, "Get inactive collections as well as active")
return cmd
}
29 changes: 24 additions & 5 deletions cli/collection_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
package cli

import (
"github.com/sourcenetwork/immutable"
"github.com/spf13/cobra"

"github.com/sourcenetwork/defradb/client"
)

func MakeCollectionDescribeCommand() *cobra.Command {
var name string
var schemaRoot string
var versionID string
var getInactive bool
var cmd = &cobra.Command{
Use: "describe",
Expand All @@ -38,12 +42,24 @@ Example: view collection by version id
RunE: func(cmd *cobra.Command, args []string) error {
store := mustGetContextStore(cmd)

col, ok := tryGetContextCollection(cmd)
if ok {
return writeJSON(cmd, col.Definition())
options := client.CollectionFetchOptions{}
if versionID != "" {
options.SchemaVersionID = immutable.Some(versionID)
}
// if no collection specified list all collections
cols, err := store.GetAllCollections(cmd.Context(), getInactive)
if schemaRoot != "" {
options.SchemaRoot = immutable.Some(schemaRoot)
}
if name != "" {
options.Name = immutable.Some(name)
}
if getInactive {
options.IncludeInactive = immutable.Some(getInactive)
}

cols, err := store.GetCollections(
cmd.Context(),
options,
)
if err != nil {
return err
}
Expand All @@ -54,6 +70,9 @@ Example: view collection by version id
return writeJSON(cmd, colDesc)
},
}
cmd.Flags().StringVar(&name, "name", "", "Collection name")
cmd.Flags().StringVar(&schemaRoot, "schema", "", "Collection schema Root")
cmd.Flags().StringVar(&versionID, "version", "", "Collection version ID")
cmd.Flags().BoolVar(&getInactive, "get-inactive", false, "Get inactive collections as well as active")
return cmd
}
42 changes: 14 additions & 28 deletions cli/schema_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package cli

import (
"github.com/sourcenetwork/immutable"
"github.com/spf13/cobra"

"github.com/sourcenetwork/defradb/client"
Expand Down Expand Up @@ -41,35 +42,20 @@ Example: view a single schema by version id
RunE: func(cmd *cobra.Command, args []string) error {
store := mustGetContextStore(cmd)

var schemas []client.SchemaDescription
switch {
case versionID != "":
schema, err := store.GetSchemaByVersionID(cmd.Context(), versionID)
if err != nil {
return err
}
return writeJSON(cmd, schema)

case root != "":
s, err := store.GetSchemasByRoot(cmd.Context(), root)
if err != nil {
return err
}
schemas = s

case name != "":
s, err := store.GetSchemasByName(cmd.Context(), name)
if err != nil {
return err
}
schemas = s
options := client.SchemaFetchOptions{}
if versionID != "" {
options.ID = immutable.Some(versionID)
}
if root != "" {
options.Root = immutable.Some(root)
}
if name != "" {
options.Name = immutable.Some(name)
}

default:
s, err := store.GetAllSchemas(cmd.Context())
if err != nil {
return err
}
schemas = s
schemas, err := store.GetSchemas(cmd.Context(), options)
if err != nil {
return err
}

return writeJSON(cmd, schemas)
Expand Down
56 changes: 32 additions & 24 deletions client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,38 +189,19 @@ type Store interface {
// If no matching collection is found an error will be returned.
GetCollectionByName(context.Context, CollectionName) (Collection, error)

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

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

// GetAllCollections returns all collections and their descriptions that currently exist within
// this [Store].
//
// If `true` is provided, the results will include inactive collections. If `false`, only active collections
// will be returned.
GetAllCollections(context.Context, bool) ([]Collection, error)

// GetSchemasByName returns the all schema versions with the given name.
GetSchemasByName(context.Context, string) ([]SchemaDescription, error)
// GetCollections returns all collections and their descriptions matching the given options
// that currently exist within this [Store].
GetCollections(context.Context, CollectionFetchOptions) ([]Collection, error)

// GetSchemaByVersionID returns the schema description for the schema version of the
// ID provided.
//
// Will return an error if it is not found.
GetSchemaByVersionID(context.Context, string) (SchemaDescription, error)

// GetSchemasByRoot returns the all schema versions for the given root.
GetSchemasByRoot(context.Context, string) ([]SchemaDescription, error)

// GetAllSchemas returns all schema versions that currently exist within
// GetSchemas returns all schema versions that currently exist within
// this [Store].
GetAllSchemas(context.Context) ([]SchemaDescription, error)
GetSchemas(context.Context, SchemaFetchOptions) ([]SchemaDescription, error)

// GetAllIndexes returns all the indexes that currently exist within this [Store].
GetAllIndexes(context.Context) (map[CollectionName][]IndexDescription, error)
Expand Down Expand Up @@ -254,3 +235,30 @@ type RequestResult struct {
// if the request was a GQL subscription.
Pub *events.Publisher[events.Update]
}

// CollectionFetchOptions represents a set of options used for fetching collections.
type CollectionFetchOptions struct {
// If provided, only collections with this schema version id will be returned.
SchemaVersionID immutable.Option[string]

// If provided, only collections with schemas of this root will be returned.
SchemaRoot immutable.Option[string]

// If provided, only collections with this name will be returned.
Name immutable.Option[string]

// If IncludeInactive is true, then inactive collections will also be returned.
IncludeInactive immutable.Option[bool]
}

// SchemaFetchOptions represents a set of options used for fetching schemas.
type SchemaFetchOptions struct {
// If provided, only schemas of this root will be returned.
Root immutable.Option[string]

// If provided, only schemas with this name will be returned.
Name immutable.Option[string]

// If provided, only the schema with this id will be returned.
ID immutable.Option[string]
}
Loading

0 comments on commit 392bd96

Please sign in to comment.