Skip to content

Commit

Permalink
Remove RelationType property
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Feb 7, 2024
1 parent f8c2e66 commit 5813c1b
Show file tree
Hide file tree
Showing 54 changed files with 431 additions and 548 deletions.
16 changes: 1 addition & 15 deletions client/descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,6 @@ var FieldKindStringToEnumMapping = map[string]FieldKind{
// RelationType describes the type of relation between two types.
type RelationType uint8

// Note: These values are serialized and persisted in the database, avoid modifying existing values
const (
Relation_Type_ONE RelationType = 1 // 0b0000 0001
Relation_Type_MANY RelationType = 2 // 0b0000 0010
Relation_Type_Primary RelationType = 128 // 0b1000 0000 Primary reference entity on relation
)

// FieldID is a unique identifier for a field in a schema.
type FieldID uint32

Expand Down Expand Up @@ -299,9 +292,7 @@ type FieldDescription struct {
// It is currently immutable.
Typ CType

// RelationType contains the relationship type if this field is a relation field. Otherwise this
// will be empty.
RelationType RelationType
IsPrimaryRelation bool
}

// IsObject returns true if this field is an object type.
Expand All @@ -315,11 +306,6 @@ func (f FieldDescription) IsObjectArray() bool {
return (f.Kind == FieldKind_FOREIGN_OBJECT_ARRAY)
}

// IsPrimaryRelation returns true if this field is a relation, and is the primary side.
func (f FieldDescription) IsPrimaryRelation() bool {
return f.RelationType > 0 && f.RelationType&Relation_Type_Primary != 0
}

// IsRelation returns true if this field is a relation.
func (f FieldDescription) IsRelation() bool {
return f.RelationName != ""
Expand Down
34 changes: 4 additions & 30 deletions db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (db *db) updateSchema(
}

for _, field := range schema.Fields {
if field.RelationType.IsSet(client.Relation_Type_ONE) {
if field.Kind == client.FieldKind_FOREIGN_OBJECT {
idFieldName := field.Name + "_id"
if _, ok := schema.GetField(idFieldName); !ok {
schema.Fields = append(schema.Fields, client.FieldDescription{
Expand Down Expand Up @@ -315,35 +315,11 @@ func validateUpdateSchemaFields(
return false, NewErrSchemaNotFound(proposedField.Name, proposedField.Schema)
}

if proposedField.Kind == client.FieldKind_FOREIGN_OBJECT {
if !proposedField.RelationType.IsSet(client.Relation_Type_ONE) {
return false, NewErrRelationalFieldInvalidRelationType(
proposedField.Name,
fmt.Sprintf(
"%v or %v",
client.Relation_Type_ONE,
client.Relation_Type_Primary,
),
proposedField.RelationType,
)
}
}

if proposedField.Kind == client.FieldKind_FOREIGN_OBJECT_ARRAY {
if !proposedField.RelationType.IsSet(client.Relation_Type_MANY) {
return false, NewErrRelationalFieldInvalidRelationType(
proposedField.Name,
client.Relation_Type_MANY,
proposedField.RelationType,
)
}
}

if proposedField.RelationName == "" {
return false, NewErrRelationalFieldMissingRelationName(proposedField.Name)
}

if proposedField.RelationType.IsSet(client.Relation_Type_Primary) {
if proposedField.IsPrimaryRelation {
if proposedField.Kind == client.FieldKind_FOREIGN_OBJECT_ARRAY {
return false, NewErrPrimarySideOnMany(proposedField.Name)
}
Expand Down Expand Up @@ -379,13 +355,11 @@ func validateUpdateSchemaFields(
return false, client.NewErrRelationOneSided(proposedField.Name, proposedField.Schema)
}

if !(proposedField.RelationType.IsSet(client.Relation_Type_Primary) ||
relatedField.RelationType.IsSet(client.Relation_Type_Primary)) {
if !(proposedField.IsPrimaryRelation || relatedField.IsPrimaryRelation) {
return false, NewErrPrimarySideNotDefined(proposedField.RelationName)
}

if proposedField.RelationType.IsSet(client.Relation_Type_Primary) &&
relatedField.RelationType.IsSet(client.Relation_Type_Primary) {
if proposedField.IsPrimaryRelation && relatedField.IsPrimaryRelation {
return false, NewErrBothSidesPrimary(proposedField.RelationName)
}
}
Expand Down
2 changes: 1 addition & 1 deletion db/collection_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (c *collection) isSecondaryIDField(fieldDesc client.FieldDescription) (clie
relationFieldDescription, valid := c.Schema().GetField(
strings.TrimSuffix(fieldDesc.Name, request.RelatedObjectID),
)
return relationFieldDescription, valid && !relationFieldDescription.IsPrimaryRelation()
return relationFieldDescription, valid && !relationFieldDescription.IsPrimaryRelation
}

// patchPrimaryDoc patches the (primary) document linked to from the document of the given DocID via the
Expand Down
22 changes: 0 additions & 22 deletions db/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,6 @@ func NewErrRelationalFieldMissingSchema(name string, kind client.FieldKind) erro
)
}

func NewErrRelationalFieldInvalidRelationType(name string, expected any, actual client.RelationType) error {
return errors.New(
errRelationalFieldInvalidRelationType,
errors.NewKV("Field", name),
errors.NewKV("Expected", expected),
errors.NewKV("Actual", actual),
)
}

func NewErrRelationalFieldMissingIDField(name string, expectedName string) error {
return errors.New(
errRelationalFieldMissingIDField,
Expand Down Expand Up @@ -307,19 +298,6 @@ func NewErrRelatedFieldKindMismatch(relationName string, expected client.FieldKi
)
}

func NewErrRelatedFieldRelationTypeMismatch(
relationName string,
expected client.RelationType,
actual client.RelationType,
) error {
return errors.New(
errRelatedFieldRelationTypeMismatch,
errors.NewKV("RelationName", relationName),
errors.NewKV("Expected", expected),
errors.NewKV("Actual", actual),
)
}

func NewErrRelationalFieldIDInvalidType(name string, expected, actual client.FieldKind) error {
return errors.New(
errRelationalFieldIDInvalidType,
Expand Down
14 changes: 4 additions & 10 deletions planner/type_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/sourcenetwork/defradb/db/base"
"github.com/sourcenetwork/defradb/planner/filter"
"github.com/sourcenetwork/defradb/planner/mapper"
"github.com/sourcenetwork/defradb/request/graphql/schema"
)

/*
Expand Down Expand Up @@ -86,10 +85,9 @@ func (p *Planner) makeTypeIndexJoin(
return nil, client.NewErrFieldNotExist(subType.Name)
}

meta := typeFieldDesc.RelationType
if schema.IsOne(meta) { // One-to-One, or One side of One-to-Many
if typeFieldDesc.Kind == client.FieldKind_FOREIGN_OBJECT { // One-to-One, or One side of One-to-Many
joinPlan, err = p.makeTypeJoinOne(parent, source, subType)
} else if schema.IsMany(meta) { // Many side of One-to-Many
} else if typeFieldDesc.Kind == client.FieldKind_FOREIGN_OBJECT_ARRAY { // Many side of One-to-Many
joinPlan, err = p.makeTypeJoinMany(parent, source, subType)
} else { // more to come, Many-to-Many, Embedded?
return nil, ErrUnknownRelationType
Expand Down Expand Up @@ -249,10 +247,6 @@ func (p *Planner) makeTypeJoinOne(
return nil, client.NewErrFieldNotExist(subType.Name)
}

// determine relation direction (primary or secondary?)
// check if the field we're querying is the primary side of the relation
isPrimary := subTypeFieldDesc.RelationType.IsSet(client.Relation_Type_Primary)

subTypeCol, err := p.db.GetCollectionByName(p.ctx, subType.CollectionName)
if err != nil {
return nil, err
Expand All @@ -270,7 +264,7 @@ func (p *Planner) makeTypeJoinOne(
}

var secondaryFieldIndex immutable.Option[int]
if !isPrimary {
if !subTypeFieldDesc.IsPrimaryRelation {
idFieldName := subTypeFieldDesc.Name + request.RelatedObjectID
secondaryFieldIndex = immutable.Some(
parent.documentMapping.FirstIndexOfName(idFieldName),
Expand All @@ -292,7 +286,7 @@ func (p *Planner) makeTypeJoinOne(
subSelect: subType,
rootName: subTypeField.Name,
subTypeName: subType.Name,
isSecondary: !isPrimary,
isSecondary: !subTypeFieldDesc.IsPrimaryRelation,
secondaryFieldIndex: secondaryFieldIndex,
secondaryFetchLimit: 1,
dir: dir,
Expand Down
15 changes: 7 additions & 8 deletions request/graphql/schema/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,20 +323,20 @@ func fieldsFromAST(field *ast.FieldDefinition,

schema := ""
relationName := ""
relationType := client.RelationType(0)
relationType := relationType(0)

fieldDescriptions := []client.FieldDescription{}

if kind == client.FieldKind_FOREIGN_OBJECT || kind == client.FieldKind_FOREIGN_OBJECT_ARRAY {
if kind == client.FieldKind_FOREIGN_OBJECT {
schema = field.Type.(*ast.Named).Name.Value
relationType = client.Relation_Type_ONE
relationType = relation_Type_ONE
if _, exists := findDirective(field, "primary"); exists {
relationType |= client.Relation_Type_Primary
relationType |= relation_Type_Primary
}
} else if kind == client.FieldKind_FOREIGN_OBJECT_ARRAY {
schema = field.Type.(*ast.List).Type.(*ast.Named).Name.Value
relationType = client.Relation_Type_MANY
relationType = relation_Type_MANY
}

relationName, err = getRelationshipName(field, hostObjectName, schema)
Expand Down Expand Up @@ -378,7 +378,6 @@ func fieldsFromAST(field *ast.FieldDefinition,
Typ: cType,
Schema: schema,
RelationName: relationName,
RelationType: relationType,
}

fieldDescriptions = append(fieldDescriptions, fieldDescription)
Expand Down Expand Up @@ -527,7 +526,7 @@ func finalizeRelations(relationManager *RelationManager, definitions []client.Co

for _, definition := range definitions {
for i, field := range definition.Schema.Fields {
if field.RelationType == 0 || (field.RelationName != "" && field.Kind == client.FieldKind_DocID) {
if field.RelationName == "" || field.Kind == client.FieldKind_DocID {
continue
}

Expand All @@ -536,7 +535,7 @@ func finalizeRelations(relationManager *RelationManager, definitions []client.Co
return err
}

_, fieldRelationType, ok := rel.GetField(field.Schema, field.Name)
_, fieldRelationType, ok := rel.getField(field.Schema, field.Name)
if !ok {
return NewErrRelationMissingField(field.Schema, field.Name)
}
Expand All @@ -552,7 +551,7 @@ func finalizeRelations(relationManager *RelationManager, definitions []client.Co
return client.NewErrRelationOneSided(field.Name, field.Schema)
}

field.RelationType = rel.Kind() | fieldRelationType
field.IsPrimaryRelation = fieldRelationType.isSet(relation_Type_Primary)
definition.Schema.Fields[i] = field
}
}
Expand Down
52 changes: 24 additions & 28 deletions request/graphql/schema/descriptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ func TestSingleSimpleType(t *testing.T) {
Kind: client.FieldKind_FOREIGN_OBJECT,
Typ: client.NONE_CRDT,
Schema: "Author",
RelationType: client.Relation_Type_ONE,
},
{
Name: "author_id",
Expand Down Expand Up @@ -224,12 +223,12 @@ func TestSingleSimpleType(t *testing.T) {
Typ: client.LWW_REGISTER,
},
{
Name: "published",
RelationName: "author_book",
Kind: client.FieldKind_FOREIGN_OBJECT,
Typ: client.NONE_CRDT,
Schema: "Book",
RelationType: client.Relation_Type_ONE | client.Relation_Type_Primary,
Name: "published",
RelationName: "author_book",
Kind: client.FieldKind_FOREIGN_OBJECT,
Typ: client.NONE_CRDT,
Schema: "Book",
IsPrimaryRelation: true,
},
{
Name: "published_id",
Expand Down Expand Up @@ -356,7 +355,6 @@ func TestSingleSimpleType(t *testing.T) {
Kind: client.FieldKind_FOREIGN_OBJECT,
Typ: client.NONE_CRDT,
Schema: "Author",
RelationType: client.Relation_Type_ONE,
},
{
Name: "author_id",
Expand Down Expand Up @@ -400,12 +398,12 @@ func TestSingleSimpleType(t *testing.T) {
Typ: client.LWW_REGISTER,
},
{
Name: "published",
RelationName: "book_authors",
Kind: client.FieldKind_FOREIGN_OBJECT,
Typ: client.NONE_CRDT,
Schema: "Book",
RelationType: client.Relation_Type_ONE | client.Relation_Type_Primary,
Name: "published",
RelationName: "book_authors",
Kind: client.FieldKind_FOREIGN_OBJECT,
Typ: client.NONE_CRDT,
Schema: "Book",
IsPrimaryRelation: true,
},
{
Name: "published_id",
Expand Down Expand Up @@ -447,12 +445,12 @@ func TestSingleSimpleType(t *testing.T) {
Typ: client.NONE_CRDT,
},
{
Name: "author",
RelationName: "author_book",
Kind: client.FieldKind_FOREIGN_OBJECT,
Typ: client.NONE_CRDT,
Schema: "Author",
RelationType: client.Relation_Type_ONE | client.Relation_Type_Primary,
Name: "author",
RelationName: "author_book",
Kind: client.FieldKind_FOREIGN_OBJECT,
Typ: client.NONE_CRDT,
Schema: "Author",
IsPrimaryRelation: true,
},
{
Name: "author_id",
Expand Down Expand Up @@ -501,7 +499,6 @@ func TestSingleSimpleType(t *testing.T) {
Kind: client.FieldKind_FOREIGN_OBJECT,
Typ: client.NONE_CRDT,
Schema: "Book",
RelationType: client.Relation_Type_ONE,
},
{
Name: "published_id",
Expand Down Expand Up @@ -543,12 +540,12 @@ func TestSingleSimpleType(t *testing.T) {
Typ: client.NONE_CRDT,
},
{
Name: "author",
RelationName: "author_book",
Kind: client.FieldKind_FOREIGN_OBJECT,
Typ: client.NONE_CRDT,
Schema: "Author",
RelationType: client.Relation_Type_ONE | client.Relation_Type_Primary,
Name: "author",
RelationName: "author_book",
Kind: client.FieldKind_FOREIGN_OBJECT,
Typ: client.NONE_CRDT,
Schema: "Author",
IsPrimaryRelation: true,
},
{
Name: "author_id",
Expand Down Expand Up @@ -597,7 +594,6 @@ func TestSingleSimpleType(t *testing.T) {
Kind: client.FieldKind_FOREIGN_OBJECT_ARRAY,
Typ: client.NONE_CRDT,
Schema: "Book",
RelationType: client.Relation_Type_MANY,
},
},
},
Expand Down
Loading

0 comments on commit 5813c1b

Please sign in to comment.