github.com/polarismesh/polaris@v1.17.8/store/boltdb/transaction.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  	"github.com/polarismesh/polaris/common/model"
    22  )
    23  
    24  type transaction struct {
    25  	handler BoltHandler
    26  }
    27  
    28  // Commit 提交事务
    29  func (t *transaction) Commit() error {
    30  	return nil
    31  }
    32  
    33  // LockBootstrap 启动锁,限制Server启动的并发数
    34  func (t *transaction) LockBootstrap(key string, server string) error {
    35  	return nil
    36  }
    37  
    38  // LockNamespace 排它锁namespace
    39  func (t *transaction) LockNamespace(name string) (*model.Namespace, error) {
    40  	return t.loadNamespace(name)
    41  }
    42  
    43  func (t *transaction) loadNamespace(name string) (*model.Namespace, error) {
    44  	values, err := t.handler.LoadValues(tblNameNamespace, []string{name}, &model.Namespace{})
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	value, ok := values[name]
    49  	if !ok {
    50  		return nil, nil
    51  	}
    52  	return value.(*model.Namespace), nil
    53  }
    54  
    55  // RLockNamespace 共享锁namespace
    56  func (t *transaction) RLockNamespace(name string) (*model.Namespace, error) {
    57  	return t.loadNamespace(name)
    58  }
    59  
    60  // DeleteNamespace 删除namespace
    61  func (t *transaction) DeleteNamespace(name string) error {
    62  	return t.handler.DeleteValues(tblNameNamespace, []string{name})
    63  }
    64  
    65  const (
    66  	svcFieldName      string = "Name"
    67  	svcFieldNamespace string = "Namespace"
    68  	svcFieldValid     string = "Valid"
    69  )
    70  
    71  func (t *transaction) loadService(name string, namespace string) (*model.Service, error) {
    72  	filter := func(m map[string]interface{}) bool {
    73  		validVal, ok := m[svcFieldValid]
    74  		if ok && !validVal.(bool) {
    75  			return false
    76  		}
    77  		nameValue, ok := m[svcFieldName]
    78  		if !ok {
    79  			return false
    80  		}
    81  		namespaceValue, ok := m[svcFieldNamespace]
    82  		if !ok {
    83  			return false
    84  		}
    85  		return nameValue.(string) == name && namespaceValue.(string) == namespace
    86  	}
    87  	values, err := t.handler.LoadValuesByFilter(
    88  		tblNameService, []string{svcFieldName, svcFieldNamespace, svcFieldValid}, &model.Service{}, filter)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  	var svc *model.Service
    93  	for _, svcValue := range values {
    94  		svc = svcValue.(*model.Service)
    95  		break
    96  	}
    97  	return svc, nil
    98  }
    99  
   100  // LockService 排它锁service
   101  func (t *transaction) LockService(name string, namespace string) (*model.Service, error) {
   102  	return t.loadService(name, namespace)
   103  }
   104  
   105  // RLockService 共享锁service
   106  func (t *transaction) RLockService(name string, namespace string) (*model.Service, error) {
   107  	return t.loadService(name, namespace)
   108  }