-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*: support CREATE/DROP graph statements
- Loading branch information
Showing
16 changed files
with
481 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2022 zGraph Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package catalog | ||
|
||
import "github.com/vescale/zgraph/parser/model" | ||
|
||
// PatchType represents the type of patch. | ||
type PatchType byte | ||
|
||
const ( | ||
PatchTypeCreateGraph PatchType = iota | ||
PatchTypeCreateLabel | ||
PatchTypeCreateIndex | ||
PatchTypeDropGraph | ||
PatchTypeDropLabel | ||
PatchTypeDropIndex | ||
) | ||
|
||
// Patch represents patch which contains a DDL change. | ||
type Patch struct { | ||
Type PatchType | ||
Data interface{} | ||
} | ||
|
||
// Apply applies the patch to catalog. | ||
// Note: we need to ensure the DDL changes have applied to persistent storage first. | ||
func (c *Catalog) Apply(patch *Patch) { | ||
switch patch.Type { | ||
case PatchTypeCreateGraph: | ||
data := patch.Data.(*model.GraphInfo) | ||
graph := NewGraph(data) | ||
c.mu.Lock() | ||
c.byName[data.Name.L] = graph | ||
c.byID[data.ID] = graph | ||
c.mu.Unlock() | ||
|
||
case PatchTypeDropGraph: | ||
data := patch.Data.(*model.GraphInfo) | ||
c.mu.Lock() | ||
delete(c.byName, data.Name.L) | ||
delete(c.byID, data.ID) | ||
c.mu.Unlock() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2022 zGraph Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package executor | ||
|
||
import ( | ||
"github.com/pingcap/errors" | ||
"github.com/vescale/zgraph/catalog" | ||
"github.com/vescale/zgraph/planner" | ||
"github.com/vescale/zgraph/stmtctx" | ||
) | ||
|
||
// Builder is used to build from a plan into executor. | ||
type Builder struct { | ||
sc *stmtctx.Context | ||
catalog *catalog.Catalog | ||
err error | ||
} | ||
|
||
// NewBuilder returns a build instance. | ||
func NewBuilder(sc *stmtctx.Context, catalog *catalog.Catalog) *Builder { | ||
return &Builder{ | ||
sc: sc, | ||
catalog: catalog, | ||
} | ||
} | ||
|
||
// Build builds an executor from a plan. | ||
func (b *Builder) Build(plan planner.Plan) Executor { | ||
switch p := plan.(type) { | ||
case *planner.DDL: | ||
return b.buildDDL(p) | ||
case *planner.Simple: | ||
return b.buildSimple(p) | ||
default: | ||
b.err = errors.Errorf("unknown plan: %T", plan) | ||
} | ||
return nil | ||
} | ||
|
||
// Error returns the internal error encountered while building. | ||
func (b *Builder) Error() error { | ||
return b.err | ||
} | ||
|
||
func (b *Builder) buildDDL(plan *planner.DDL) Executor { | ||
exec := &DDLExec{ | ||
baseExecutor: newBaseExecutor(b.sc, plan.Schema(), plan.ID()), | ||
sc: b.sc, | ||
statement: plan.Statement, | ||
catalog: b.catalog, | ||
} | ||
return exec | ||
} | ||
|
||
func (b *Builder) buildSimple(plan *planner.Simple) Executor { | ||
exec := &SimpleExec{ | ||
baseExecutor: newBaseExecutor(b.sc, plan.Schema(), plan.ID()), | ||
statement: plan.Statement, | ||
} | ||
return exec | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright 2022 zGraph Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package executor | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pingcap/errors" | ||
"github.com/vescale/zgraph/catalog" | ||
"github.com/vescale/zgraph/internal/chunk" | ||
"github.com/vescale/zgraph/meta" | ||
"github.com/vescale/zgraph/parser/ast" | ||
"github.com/vescale/zgraph/parser/model" | ||
"github.com/vescale/zgraph/stmtctx" | ||
"github.com/vescale/zgraph/storage/kv" | ||
) | ||
|
||
// DDLExec is used to execute a DDL operation. | ||
type DDLExec struct { | ||
baseExecutor | ||
|
||
sc *stmtctx.Context | ||
done bool | ||
statement ast.DDLNode | ||
catalog *catalog.Catalog | ||
} | ||
|
||
// Next implements the Executor interface. | ||
func (e *DDLExec) Next(_ context.Context, _ *chunk.Chunk) error { | ||
if e.done { | ||
return nil | ||
} | ||
e.done = true | ||
|
||
// TODO: prevent executing DDL in transaction context. | ||
// Prevent executing DDL concurrently. | ||
e.catalog.MDLock() | ||
defer e.catalog.MDUnlock() | ||
|
||
var patch *catalog.Patch | ||
err := kv.RunNewTxn(e.sc.Store(), func(txn kv.Transaction) error { | ||
m := meta.New(txn) | ||
var err error | ||
switch stmt := e.statement.(type) { | ||
case *ast.CreateGraphStmt: | ||
patch, err = e.createGraph(m, stmt) | ||
case *ast.DropGraphStmt: | ||
patch, err = e.dropGraph(m, stmt) | ||
default: | ||
return errors.Errorf("unknown DDL(%T)", e.statement) | ||
} | ||
return err | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Apply the patch to catalog after the DDL changes have persistent in storage. | ||
if patch != nil { | ||
e.catalog.Apply(patch) | ||
} | ||
return nil | ||
} | ||
|
||
func (e *DDLExec) createGraph(m *meta.Meta, stmt *ast.CreateGraphStmt) (*catalog.Patch, error) { | ||
graph := e.catalog.Graph(stmt.Graph.L) | ||
if graph != nil { | ||
if stmt.IfNotExists { | ||
return nil, nil | ||
} | ||
return nil, meta.ErrGraphExists | ||
} | ||
|
||
// Persistent to storage. | ||
id, err := m.NextGlobalID() | ||
if err != nil { | ||
return nil, err | ||
} | ||
graphInfo := &model.GraphInfo{ | ||
ID: id, | ||
Name: stmt.Graph, | ||
} | ||
err = m.CreateGraph(graphInfo) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
patch := &catalog.Patch{ | ||
Type: catalog.PatchTypeCreateGraph, | ||
Data: graphInfo, | ||
} | ||
return patch, nil | ||
} | ||
|
||
func (e *DDLExec) dropGraph(m *meta.Meta, stmt *ast.DropGraphStmt) (*catalog.Patch, error) { | ||
graph := e.catalog.Graph(stmt.Graph.L) | ||
if graph == nil { | ||
if stmt.IfExists { | ||
return nil, nil | ||
} | ||
return nil, meta.ErrGraphNotExists | ||
} | ||
|
||
// Persistent to storage. | ||
graphID := graph.Meta().ID | ||
labels := graph.Labels() | ||
for _, label := range labels { | ||
err := m.DropLabel(graphID, label.Meta().ID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
err := m.DropGraph(graphID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
patch := &catalog.Patch{ | ||
Type: catalog.PatchTypeDropGraph, | ||
Data: graph.Meta(), | ||
} | ||
return patch, nil | ||
} |
Oops, something went wrong.