Skip to content

Commit

Permalink
SQUASHED - sourcenetwork#1939
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Oct 10, 2023
1 parent 3445723 commit d4c61b8
Show file tree
Hide file tree
Showing 44 changed files with 764 additions and 1,615 deletions.
17 changes: 14 additions & 3 deletions cli/collection_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import (
"github.com/sourcenetwork/defradb/client"
)

type CollectionDefinition struct {
Description client.CollectionDescription `json:"description"`
Schema client.SchemaDescription `json:"schema"`
}

func MakeCollectionDescribeCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "describe",
Expand All @@ -39,16 +44,22 @@ Example: view collection by version id

col, ok := tryGetCollectionContext(cmd)
if ok {
return writeJSON(cmd, col.Description())
return writeJSON(cmd, CollectionDefinition{
Description: col.Description(),
Schema: col.Schema(),
})
}
// if no collection specified list all collections
cols, err := store.GetAllCollections(cmd.Context())
if err != nil {
return err
}
colDesc := make([]client.CollectionDescription, len(cols))
colDesc := make([]CollectionDefinition, len(cols))
for i, col := range cols {
colDesc[i] = col.Description()
colDesc[i] = CollectionDefinition{
Description: col.Description(),
Schema: col.Schema(),
}
}
return writeJSON(cmd, colDesc)
},
Expand Down
19 changes: 12 additions & 7 deletions client/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ import (
"github.com/sourcenetwork/defradb/datastore"
)

// Collection represents a defradb collection.
//
// A Collection is mostly analogous to a SQL table, however a collection is specific to its
// host, and many collections may share the same schema.
//
// Many functions on this object will interact with the underlying datastores.
type Collection interface {
// CollectionDefinition contains the metadata defining what this Collection is.
type CollectionDefinition interface {
// Description returns the CollectionDescription of this Collection.
Description() CollectionDescription
// Name returns the name of this collection.
Expand All @@ -33,6 +28,16 @@ type Collection interface {
ID() uint32
// SchemaID returns the ID of the Schema used to define this Collection.
SchemaID() string
}

// Collection represents a defradb collection.
//
// A Collection is mostly analogous to a SQL table, however a collection is specific to its
// host, and many collections may share the same schema.
//
// Many functions on this object will interact with the underlying datastores.
type Collection interface {
CollectionDefinition

// Create a new document.
//
Expand Down
49 changes: 22 additions & 27 deletions client/descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type CollectionDescription struct {
ID uint32

// Schema contains the data type information that this Collection uses.
//
// This property is deprecated and should not be used.
Schema SchemaDescription

// Indexes contains the secondary indexes that this Collection has.
Expand All @@ -41,12 +43,21 @@ func (col CollectionDescription) IDString() string {

// GetFieldByID searches for a field with the given ID. If such a field is found it
// will return it and true, if it is not found it will return false.
func (col CollectionDescription) GetFieldByID(id FieldID) (FieldDescription, bool) {
if !col.Schema.IsEmpty() {
for _, field := range col.Schema.Fields {
if field.ID == id {
return field, true
}
func (col CollectionDescription) GetFieldByID(id FieldID, schema *SchemaDescription) (FieldDescription, bool) {
for _, field := range schema.Fields {
if field.ID == id {
return field, true
}
}
return FieldDescription{}, false
}

// GetFieldByName returns the field for the given field name. If such a field is found it
// will return it and true, if it is not found it will return false.
func (col CollectionDescription) GetFieldByName(fieldName string, schema *SchemaDescription) (FieldDescription, bool) {
for _, field := range schema.Fields {
if field.Name == fieldName {
return field, true
}
}
return FieldDescription{}, false
Expand All @@ -57,8 +68,9 @@ func (col CollectionDescription) GetFieldByRelation(
relationName string,
otherCollectionName string,
otherFieldName string,
schema *SchemaDescription,
) (FieldDescription, bool) {
for _, field := range col.Schema.Fields {
for _, field := range schema.Fields {
if field.RelationName == relationName && !(col.Name == otherCollectionName && otherFieldName == field.Name) {
return field, true
}
Expand Down Expand Up @@ -93,28 +105,11 @@ type SchemaDescription struct {
Fields []FieldDescription
}

// IsEmpty returns true if the SchemaDescription is empty and uninitialized
func (sd SchemaDescription) IsEmpty() bool {
return len(sd.Fields) == 0
}

// GetFieldKey returns the field ID for the given field name.
func (sd SchemaDescription) GetFieldKey(fieldName string) uint32 {
for _, field := range sd.Fields {
if field.Name == fieldName {
return uint32(field.ID)
}
}
return uint32(0)
}

// GetField returns the field of the given name.
func (sd SchemaDescription) GetField(name string) (FieldDescription, bool) {
if !sd.IsEmpty() {
for _, field := range sd.Fields {
if field.Name == name {
return field, true
}
for _, field := range sd.Fields {
if field.Name == name {
return field, true
}
}
return FieldDescription{}, false
Expand Down
4 changes: 2 additions & 2 deletions core/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ type Parser interface {
NewFilterFromString(collectionType string, body string) (immutable.Option[request.Filter], error)

// ParseSDL parses an SDL string into a set of collection descriptions.
ParseSDL(ctx context.Context, schemaString string) ([]client.CollectionDescription, error)
ParseSDL(ctx context.Context, schemaString string) ([]client.CollectionDefinition, error)

// Adds the given schema to this parser's model.
SetSchema(ctx context.Context, txn datastore.Txn, collections []client.CollectionDescription) error
SetSchema(ctx context.Context, txn datastore.Txn, collections []client.CollectionDefinition) error
}
20 changes: 7 additions & 13 deletions db/base/collection_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func MakeDocKey(col client.CollectionDescription, docKey string) core.DataStoreK

func MakePrimaryIndexKeyForCRDT(
c client.CollectionDescription,
schema client.SchemaDescription,
ctype client.CType,
key core.DataStoreKey,
fieldName string,
Expand All @@ -42,19 +43,12 @@ func MakePrimaryIndexKeyForCRDT(
case client.COMPOSITE:
return MakeCollectionKey(c).WithInstanceInfo(key).WithFieldId(core.COMPOSITE_NAMESPACE), nil
case client.LWW_REGISTER:
fieldKey := getFieldKey(c, key, fieldName)
return MakeCollectionKey(c).WithInstanceInfo(fieldKey), nil
}
return core.DataStoreKey{}, ErrInvalidCrdtType
}
field, ok := c.GetFieldByName(fieldName, &schema)
if !ok {
return core.DataStoreKey{}, client.NewErrFieldNotExist(fieldName)
}

func getFieldKey(
c client.CollectionDescription,
key core.DataStoreKey,
fieldName string,
) core.DataStoreKey {
if !c.Schema.IsEmpty() {
return key.WithFieldId(fmt.Sprint(c.Schema.GetFieldKey(fieldName)))
return MakeCollectionKey(c).WithInstanceInfo(key).WithFieldId(fmt.Sprint(field.ID)), nil
}
return key.WithFieldId(fieldName)
return core.DataStoreKey{}, ErrInvalidCrdtType
}
Loading

0 comments on commit d4c61b8

Please sign in to comment.