Skip to content

Commit

Permalink
refactor: Deprecate CollectionDescription.Schema (sourcenetwork#1939)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves sourcenetwork#1955

## Description

Deprecate CollectionDescription.Schema. Schema is not a sub-property of
collection.

Removes as many references to CollectionDescription.Schema as possible
without making any breaking changes. Breaking changes will be made in a
later PR. An exception has been made to the http API, which does have a
breaking change in this PR.
  • Loading branch information
AndrewSisley committed Oct 16, 2023
1 parent 20eb20c commit 7afd32e
Show file tree
Hide file tree
Showing 44 changed files with 761 additions and 1,615 deletions.
6 changes: 3 additions & 3 deletions cli/collection_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ Example: view collection by version id

col, ok := tryGetCollectionContext(cmd)
if ok {
return writeJSON(cmd, col.Description())
return writeJSON(cmd, col.Definition())
}
// 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([]client.CollectionDefinition, len(cols))
for i, col := range cols {
colDesc[i] = col.Description()
colDesc[i] = col.Definition()
}
return writeJSON(cmd, colDesc)
},
Expand Down
19 changes: 15 additions & 4 deletions client/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,35 @@ import (
"github.com/sourcenetwork/defradb/datastore"
)

// CollectionDefinition contains the metadata defining what a Collection is.
type CollectionDefinition struct {
// Description returns the CollectionDescription of this Collection.
Description CollectionDescription `json:"description"`
// Schema returns the SchemaDescription used to define this Collection.
Schema SchemaDescription `json:"schema"`
}

// 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 {
// Description returns the CollectionDescription of this Collection.
Description() CollectionDescription
// Name returns the name of this collection.
Name() string
// Schema returns the SchemaDescription used to define this Collection.
Schema() SchemaDescription
// ID returns the ID of this Collection.
ID() uint32
// SchemaID returns the ID of the Schema used to define this Collection.
SchemaID() string

// Definition contains the metadata defining what a Collection is.
Definition() CollectionDefinition
// Schema returns the SchemaDescription used to define this Collection.
Schema() SchemaDescription
// Description returns the CollectionDescription of this Collection.
Description() CollectionDescription

// Create a new document.
//
// Will verify the DocKey/CID to ensure that the new document is correctly formatted.
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 7afd32e

Please sign in to comment.