github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/catalog/patch.go (about) 1 // Copyright 2022 zGraph Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package catalog 16 17 import ( 18 "github.com/vescale/zgraph/internal/logutil" 19 "github.com/vescale/zgraph/parser/model" 20 ) 21 22 // PatchType represents the type of patch. 23 type PatchType byte 24 25 const ( 26 PatchTypeCreateGraph PatchType = iota 27 PatchTypeCreateLabel 28 PatchTypeCreateIndex 29 PatchTypeDropGraph 30 PatchTypeDropLabel 31 PatchTypeDropIndex 32 PatchTypeCreateProperties 33 ) 34 35 type ( 36 // Patch represents patch which contains a DDL change. 37 Patch struct { 38 Type PatchType 39 Data interface{} 40 } 41 42 // PatchLabel represents the payload of patching create/drop label DDL. 43 PatchLabel struct { 44 GraphID int64 45 LabelInfo *model.LabelInfo 46 } 47 48 // PatchProperties represents the payload of patching create properties 49 PatchProperties struct { 50 MaxPropID uint16 51 GraphID int64 52 Properties []*model.PropertyInfo 53 } 54 ) 55 56 // Apply applies the patch to catalog. 57 // Note: we need to ensure the DDL changes have applied to persistent storage first. 58 func (c *Catalog) Apply(patch *Patch) { 59 switch patch.Type { 60 case PatchTypeCreateGraph: 61 data := patch.Data.(*model.GraphInfo) 62 graph := NewGraph(data) 63 c.mu.Lock() 64 c.byName[data.Name.L] = graph 65 c.byID[data.ID] = graph 66 c.mu.Unlock() 67 68 case PatchTypeDropGraph: 69 data := patch.Data.(*model.GraphInfo) 70 c.mu.Lock() 71 delete(c.byName, data.Name.L) 72 delete(c.byID, data.ID) 73 c.mu.Unlock() 74 75 case PatchTypeCreateLabel: 76 data := patch.Data.(*PatchLabel) 77 graph := c.GraphByID(data.GraphID) 78 if graph == nil { 79 logutil.Errorf("Create label on not exists graph. GraphID: %d", data.GraphID) 80 return 81 } 82 graph.CreateLabel(data.LabelInfo) 83 84 case PatchTypeDropLabel: 85 data := patch.Data.(*PatchLabel) 86 graph := c.GraphByID(data.GraphID) 87 if graph == nil { 88 logutil.Errorf("Drop label on not exists graph. GraphID: %d", data.GraphID) 89 return 90 } 91 graph.DropLabel(data.LabelInfo) 92 93 case PatchTypeCreateProperties: 94 data := patch.Data.(*PatchProperties) 95 graph := c.GraphByID(data.GraphID) 96 graph.SetNextPropID(data.MaxPropID) 97 for _, p := range data.Properties { 98 graph.CreateProperty(p) 99 } 100 } 101 }