github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/catalog/catalog.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 "strings" 19 "sync" 20 21 "github.com/vescale/zgraph/meta" 22 "github.com/vescale/zgraph/storage/kv" 23 ) 24 25 // Catalog maintains the catalog of graphs and label information. 26 type Catalog struct { 27 // mdl prevent executing DDL concurrently. 28 mdl sync.Mutex 29 30 // mu protect the catalog fields. 31 mu sync.RWMutex 32 byName map[string]*Graph 33 byID map[int64]*Graph 34 } 35 36 // Load loads the catalog from a kv snapshot. 37 func Load(snapshot kv.Snapshot) (*Catalog, error) { 38 c := &Catalog{ 39 byName: map[string]*Graph{}, 40 byID: map[int64]*Graph{}, 41 } 42 43 meta := meta.NewSnapshot(snapshot) 44 graphs, err := meta.ListGraphs() 45 if err != nil { 46 return nil, err 47 } 48 for _, g := range graphs { 49 // Load labels 50 labels, err := meta.ListLabels(g.ID) 51 if err != nil { 52 return nil, err 53 } 54 g.Labels = labels 55 56 // Load properties 57 properties, err := meta.ListProperties(g.ID) 58 if err != nil { 59 return nil, err 60 } 61 g.Properties = properties 62 63 // Build graph instance. 64 graph := NewGraph(g) 65 c.byName[g.Name.L] = graph 66 c.byID[g.ID] = graph 67 } 68 69 return c, nil 70 } 71 72 // Graph returns the graph of specified name. 73 func (c *Catalog) Graph(name string) *Graph { 74 c.mu.RLock() 75 defer c.mu.RUnlock() 76 77 return c.byName[strings.ToLower(name)] 78 } 79 80 // GraphByID returns the graph of specified ID. 81 func (c *Catalog) GraphByID(id int64) *Graph { 82 c.mu.RLock() 83 defer c.mu.RUnlock() 84 85 return c.byID[id] 86 } 87 88 // Graphs returns all graphs. 89 func (c *Catalog) Graphs() []*Graph { 90 c.mu.RLock() 91 defer c.mu.RUnlock() 92 93 graphs := make([]*Graph, 0, len(c.byName)) 94 for _, g := range c.byName { 95 graphs = append(graphs, g) 96 } 97 98 return graphs 99 } 100 101 // Label returns the label of specified graph. 102 func (c *Catalog) Label(graphName, labelName string) *Label { 103 g := c.Graph(graphName) 104 if g == nil { 105 return nil 106 } 107 108 return g.Label(labelName) 109 } 110 111 // LabelByID returns the label of specified graph. 112 func (c *Catalog) LabelByID(graphID, labelID int64) *Label { 113 g := c.GraphByID(graphID) 114 if g == nil { 115 return nil 116 } 117 118 return g.LabelByID(labelID) 119 } 120 121 // Labels returns all labels of specified graph. 122 func (c *Catalog) Labels(graphName string) []*Label { 123 g := c.Graph(graphName) 124 if g == nil { 125 return nil 126 } 127 128 return g.Labels() 129 } 130 131 // MDLock locks the catalog to prevent executing DDL concurrently. 132 func (c *Catalog) MDLock() { 133 c.mdl.Lock() 134 } 135 136 // MDUnlock unlocks the catalog. 137 func (c *Catalog) MDUnlock() { 138 c.mdl.Unlock() 139 }