github.com/polarismesh/polaris@v1.17.8/store/boltdb/config_file_template.go (about) 1 /* 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package boltdb 19 20 import ( 21 "time" 22 23 bolt "go.etcd.io/bbolt" 24 "go.uber.org/zap" 25 26 "github.com/polarismesh/polaris/common/model" 27 "github.com/polarismesh/polaris/store" 28 ) 29 30 const ( 31 tblConfigFileTemplate string = "ConfigFileTemplate" 32 tblConfigFileTemplateID string = "ConfigFileTemplateID" 33 ) 34 35 type configFileTemplateStore struct { 36 handler BoltHandler 37 } 38 39 func newConfigFileTemplateStore(handler BoltHandler) *configFileTemplateStore { 40 s := &configFileTemplateStore{handler: handler} 41 return s 42 } 43 44 // QueryAllConfigFileTemplates query all config file templates 45 func (cf *configFileTemplateStore) QueryAllConfigFileTemplates() ([]*model.ConfigFileTemplate, error) { 46 ret, err := cf.handler.LoadValuesAll(tblConfigFileTemplate, &model.ConfigFileTemplate{}) 47 if err != nil { 48 return nil, err 49 } 50 if len(ret) == 0 { 51 return nil, nil 52 } 53 var templates []*model.ConfigFileTemplate 54 for _, v := range ret { 55 templates = append(templates, v.(*model.ConfigFileTemplate)) 56 } 57 return templates, nil 58 } 59 60 // GetConfigFileTemplate get config file template 61 func (cf *configFileTemplateStore) GetConfigFileTemplate(name string) (*model.ConfigFileTemplate, error) { 62 proxy, err := cf.handler.StartTx() 63 if err != nil { 64 return nil, err 65 } 66 tx := proxy.GetDelegateTx().(*bolt.Tx) 67 68 defer func() { 69 _ = tx.Rollback() 70 }() 71 72 values := make(map[string]interface{}) 73 if err = loadValues(tx, tblConfigFileTemplate, []string{name}, &model.ConfigFileTemplate{}, values); err != nil { 74 return nil, err 75 } 76 77 err = tx.Commit() 78 if err != nil { 79 return nil, err 80 } 81 82 if len(values) == 0 { 83 return nil, nil 84 } 85 86 if len(values) > 1 { 87 return nil, ErrMultipleConfigFileFound 88 } 89 90 data := values[name].(*model.ConfigFileTemplate) 91 92 return data, nil 93 } 94 95 // CreateConfigFileTemplate create config file template 96 func (cf *configFileTemplateStore) CreateConfigFileTemplate( 97 template *model.ConfigFileTemplate) (*model.ConfigFileTemplate, error) { 98 proxy, err := cf.handler.StartTx() 99 if err != nil { 100 return nil, err 101 } 102 tx := proxy.GetDelegateTx().(*bolt.Tx) 103 104 defer func() { 105 _ = tx.Rollback() 106 }() 107 108 table, err := tx.CreateBucketIfNotExists([]byte(tblConfigFile)) 109 if err != nil { 110 return nil, store.Error(err) 111 } 112 nextId, err := table.NextSequence() 113 if err != nil { 114 return nil, store.Error(err) 115 } 116 117 template.Id = nextId 118 template.CreateTime = time.Now() 119 template.ModifyTime = time.Now() 120 121 key := template.Name 122 if err := saveValue(tx, tblConfigFileTemplate, key, template); err != nil { 123 log.Error("[ConfigFileTemplate] save error", zap.Error(err)) 124 return nil, err 125 } 126 127 if err = tx.Commit(); err != nil { 128 log.Error("[ConfigFileTemplate] commit error", zap.Error(err)) 129 return nil, err 130 } 131 132 return template, nil 133 }