From a325be57e3e614c52c811787e8e4615064a3b1c0 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Fri, 28 Oct 2022 15:20:56 -0400 Subject: [PATCH 1/2] Conceptually split up query-language from schema A later commit will remove this function from the Parser type, so please dont comment on that :) Performance is not an issue here either, so the wastage is not really an issue in the short/medium-term --- core/parser.go | 12 +++--------- db/schema.go | 18 +++++++++++------- query/graphql/parser.go | 25 ++++++++++++++++--------- tests/bench/query/planner/utils.go | 2 +- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/core/parser.go b/core/parser.go index a704f3c09b..b428bb53e6 100644 --- a/core/parser.go +++ b/core/parser.go @@ -27,14 +27,6 @@ type SchemaDefinition struct { Body []byte } -type Schema struct { - // The individual declarations of types defined by this schema. - Definitions []SchemaDefinition - - // The collection descriptions created from/defined by this schema. - Descriptions []client.CollectionDescription -} - // Parser represents the object responsible for handling stuff specific to // a query language. This includes schema and query parsing, and introspection. type Parser interface { @@ -48,5 +40,7 @@ type Parser interface { Parse(request string) (*request.Request, []error) // Adds the given schema to this parser's model. - AddSchema(ctx context.Context, schema string) (*Schema, error) + AddSchema(ctx context.Context, schema string) error + + CreateDescriptions(ctx context.Context, schema string) ([]client.CollectionDescription, []SchemaDefinition, error) } diff --git a/db/schema.go b/db/schema.go index 84326bac8f..adcf1b10f2 100644 --- a/db/schema.go +++ b/db/schema.go @@ -21,18 +21,23 @@ import ( // LoadSchema takes the provided schema in SDL format, and applies it to the database, // and creates the necessary collections, query types, etc. func (db *db) AddSchema(ctx context.Context, schemaString string) error { - schema, err := db.parser.AddSchema(ctx, schemaString) + err := db.parser.AddSchema(ctx, schemaString) if err != nil { return err } - for _, desc := range schema.Descriptions { + collectionDescriptions, schemaDefinitions, err := db.parser.CreateDescriptions(ctx, schemaString) + if err != nil { + return err + } + + for _, desc := range collectionDescriptions { if _, err := db.CreateCollection(ctx, desc); err != nil { return err } } - return db.saveSchema(ctx, schema) + return db.saveSchema(ctx, schemaDefinitions) } func (db *db) loadSchema(ctx context.Context) error { @@ -50,13 +55,12 @@ func (db *db) loadSchema(ctx context.Context) error { sdl += "\n" + string(buf) } - _, err = db.parser.AddSchema(ctx, sdl) - return err + return db.parser.AddSchema(ctx, sdl) } -func (db *db) saveSchema(ctx context.Context, schema *core.Schema) error { +func (db *db) saveSchema(ctx context.Context, schemaDefinitions []core.SchemaDefinition) error { // save each type individually - for _, def := range schema.Definitions { + for _, def := range schemaDefinitions { key := core.NewSchemaKey(def.Name) if err := db.systemstore().Put(ctx, key.ToDS(), def.Body); err != nil { return err diff --git a/query/graphql/parser.go b/query/graphql/parser.go index 7f2f6893e2..e29022c887 100644 --- a/query/graphql/parser.go +++ b/query/graphql/parser.go @@ -96,15 +96,25 @@ func (p *parser) Parse(request string) (*request.Request, []error) { return query, nil } -func (p *parser) AddSchema(ctx context.Context, schema string) (*core.Schema, error) { - types, astdoc, err := p.schemaManager.Generator.FromSDL(ctx, schema) +func (p *parser) AddSchema(ctx context.Context, schema string) error { + _, _, err := p.schemaManager.Generator.FromSDL(ctx, schema) + return err +} + +func (p *parser) CreateDescriptions(ctx context.Context, schemaString string) ([]client.CollectionDescription, []core.SchemaDefinition, error) { + schemaManager, err := schema.NewSchemaManager() if err != nil { - return nil, err + return nil, nil, err } - colDesc, err := p.schemaManager.Generator.CreateDescriptions(types) + types, astdoc, err := schemaManager.Generator.FromSDL(ctx, schemaString) if err != nil { - return nil, err + return nil, nil, err + } + + colDesc, err := schemaManager.Generator.CreateDescriptions(types) + if err != nil { + return nil, nil, err } definitions := make([]core.SchemaDefinition, len(astdoc.Definitions)) @@ -120,8 +130,5 @@ func (p *parser) AddSchema(ctx context.Context, schema string) (*core.Schema, er } } - return &core.Schema{ - Definitions: definitions, - Descriptions: colDesc, - }, nil + return colDesc, definitions, nil } diff --git a/tests/bench/query/planner/utils.go b/tests/bench/query/planner/utils.go index 5a3de03321..78bf80e4d0 100644 --- a/tests/bench/query/planner/utils.go +++ b/tests/bench/query/planner/utils.go @@ -97,7 +97,7 @@ func buildParser( if err != nil { return nil, err } - _, err = parser.AddSchema(ctx, schema) + err = parser.AddSchema(ctx, schema) if err != nil { return nil, err } From 3cfc861e6f2effd741632c6fd86801b619b3c742 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Fri, 28 Oct 2022 15:23:46 -0400 Subject: [PATCH 2/2] Remove schema definition from parser --- core/parser.go | 2 -- db/schema.go | 42 ++++++++++++++++++++++++++++++++++++++++- query/graphql/parser.go | 33 -------------------------------- 3 files changed, 41 insertions(+), 36 deletions(-) diff --git a/core/parser.go b/core/parser.go index b428bb53e6..e1c6635970 100644 --- a/core/parser.go +++ b/core/parser.go @@ -41,6 +41,4 @@ type Parser interface { // Adds the given schema to this parser's model. AddSchema(ctx context.Context, schema string) error - - CreateDescriptions(ctx context.Context, schema string) ([]client.CollectionDescription, []SchemaDefinition, error) } diff --git a/db/schema.go b/db/schema.go index adcf1b10f2..9622752361 100644 --- a/db/schema.go +++ b/db/schema.go @@ -13,9 +13,12 @@ package db import ( "context" + "github.com/graphql-go/graphql/language/ast" dsq "github.com/ipfs/go-datastore/query" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/query/graphql/schema" ) // LoadSchema takes the provided schema in SDL format, and applies it to the database, @@ -26,7 +29,7 @@ func (db *db) AddSchema(ctx context.Context, schemaString string) error { return err } - collectionDescriptions, schemaDefinitions, err := db.parser.CreateDescriptions(ctx, schemaString) + collectionDescriptions, schemaDefinitions, err := createDescriptions(ctx, schemaString) if err != nil { return err } @@ -68,3 +71,40 @@ func (db *db) saveSchema(ctx context.Context, schemaDefinitions []core.SchemaDef } return nil } + +func createDescriptions( + ctx context.Context, + schemaString string, +) ([]client.CollectionDescription, []core.SchemaDefinition, error) { + // We should not be using this package at all here, and should not be doing most of what this package does + // Rework: https://github.com/sourcenetwork/defradb/issues/923 + schemaManager, err := schema.NewSchemaManager() + if err != nil { + return nil, nil, err + } + + types, astdoc, err := schemaManager.Generator.FromSDL(ctx, schemaString) + if err != nil { + return nil, nil, err + } + + colDesc, err := schemaManager.Generator.CreateDescriptions(types) + if err != nil { + return nil, nil, err + } + + definitions := make([]core.SchemaDefinition, len(astdoc.Definitions)) + for i, astDefinition := range astdoc.Definitions { + objDef, isObjDef := astDefinition.(*ast.ObjectDefinition) + if !isObjDef { + continue + } + + definitions[i] = core.SchemaDefinition{ + Name: objDef.Name.Value, + Body: objDef.Loc.Source.Body[objDef.Loc.Start:objDef.Loc.End], + } + } + + return colDesc, definitions, nil +} diff --git a/query/graphql/parser.go b/query/graphql/parser.go index e29022c887..6385661cf8 100644 --- a/query/graphql/parser.go +++ b/query/graphql/parser.go @@ -21,7 +21,6 @@ import ( "github.com/sourcenetwork/defradb/query/graphql/schema" gql "github.com/graphql-go/graphql" - "github.com/graphql-go/graphql/language/ast" gqlp "github.com/graphql-go/graphql/language/parser" "github.com/graphql-go/graphql/language/source" ) @@ -100,35 +99,3 @@ func (p *parser) AddSchema(ctx context.Context, schema string) error { _, _, err := p.schemaManager.Generator.FromSDL(ctx, schema) return err } - -func (p *parser) CreateDescriptions(ctx context.Context, schemaString string) ([]client.CollectionDescription, []core.SchemaDefinition, error) { - schemaManager, err := schema.NewSchemaManager() - if err != nil { - return nil, nil, err - } - - types, astdoc, err := schemaManager.Generator.FromSDL(ctx, schemaString) - if err != nil { - return nil, nil, err - } - - colDesc, err := schemaManager.Generator.CreateDescriptions(types) - if err != nil { - return nil, nil, err - } - - definitions := make([]core.SchemaDefinition, len(astdoc.Definitions)) - for i, astDefinition := range astdoc.Definitions { - objDef, isObjDef := astDefinition.(*ast.ObjectDefinition) - if !isObjDef { - continue - } - - definitions[i] = core.SchemaDefinition{ - Name: objDef.Name.Value, - Body: objDef.Loc.Source.Body[objDef.Loc.Start:objDef.Loc.End], - } - } - - return colDesc, definitions, nil -}