github.com/cayleygraph/cayley@v0.7.7/graph/registry.go (about) 1 // Copyright 2014 The Cayley 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 graph 16 17 import ( 18 "fmt" 19 "sort" 20 ) 21 22 var ( 23 ErrQuadStoreNotRegistred = fmt.Errorf("This QuadStore is not registered.") 24 ErrQuadStoreNotPersistent = fmt.Errorf("cannot specify address for non-persistent backend") 25 ErrOperationNotSupported = fmt.Errorf("This Operation is not supported.") 26 ) 27 28 var storeRegistry = make(map[string]QuadStoreRegistration) 29 30 type NewStoreFunc func(string, Options) (QuadStore, error) 31 type InitStoreFunc func(string, Options) error 32 type UpgradeStoreFunc func(string, Options) error 33 34 type QuadStoreRegistration struct { 35 NewFunc NewStoreFunc 36 UpgradeFunc UpgradeStoreFunc 37 InitFunc InitStoreFunc 38 IsPersistent bool 39 } 40 41 func RegisterQuadStore(name string, register QuadStoreRegistration) { 42 if register.NewFunc == nil { 43 panic("NewFunc must not be nil") 44 } 45 46 // Register QuadStore with friendly name 47 if _, found := storeRegistry[name]; found { 48 panic(fmt.Sprintf("Already registered QuadStore %q.", name)) 49 } 50 storeRegistry[name] = register 51 } 52 53 func NewQuadStore(name string, dbpath string, opts Options) (QuadStore, error) { 54 r, registered := storeRegistry[name] 55 if !registered { 56 return nil, ErrQuadStoreNotRegistred 57 } else if dbpath != "" && !r.IsPersistent { 58 return nil, ErrQuadStoreNotPersistent 59 } 60 return r.NewFunc(dbpath, opts) 61 } 62 63 func InitQuadStore(name string, dbpath string, opts Options) error { 64 r, registered := storeRegistry[name] 65 if !registered { 66 return ErrQuadStoreNotRegistred 67 } else if r.InitFunc == nil { 68 return ErrOperationNotSupported 69 } 70 return r.InitFunc(dbpath, opts) 71 } 72 73 func UpgradeQuadStore(name string, dbpath string, opts Options) error { 74 r, registered := storeRegistry[name] 75 if !registered { 76 return ErrQuadStoreNotRegistred 77 } else if r.UpgradeFunc == nil { 78 // return ErrOperationNotSupported 79 return nil 80 } 81 return r.UpgradeFunc(dbpath, opts) 82 } 83 84 func IsRegistered(name string) bool { 85 _, ok := storeRegistry[name] 86 return ok 87 } 88 89 func IsPersistent(name string) bool { 90 return storeRegistry[name].IsPersistent 91 } 92 93 func QuadStores() []string { 94 t := make([]string, 0, len(storeRegistry)) 95 for n := range storeRegistry { 96 t = append(t, n) 97 } 98 sort.Strings(t) 99 return t 100 }