You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

datasource_resource.go 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package sql
  18. import (
  19. "fmt"
  20. "time"
  21. "github.com/bluele/gcache"
  22. "github.com/seata/seata-go/pkg/datasource/sql/types"
  23. "github.com/seata/seata-go/pkg/protocol/branch"
  24. )
  25. type Holdable interface {
  26. SetHeld(held bool)
  27. IsHeld() bool
  28. ShouldBeHeld() bool
  29. }
  30. type BaseDataSourceResource struct {
  31. db *DBResource
  32. shouldBeHeld bool
  33. keeper map[string]interface{}
  34. Cache map[string]branch.BranchStatus
  35. }
  36. var BranchStatusCache = gcache.New(1024).LRU().Expiration(time.Minute * 10).Build()
  37. func (b *BaseDataSourceResource) init() error {
  38. return nil
  39. }
  40. func (b *BaseDataSourceResource) GetDB() *DBResource {
  41. return b.db
  42. }
  43. func (b *BaseDataSourceResource) SetDB(db *DBResource) {
  44. b.db = db
  45. }
  46. func (b *BaseDataSourceResource) IsShouldBeHeld() bool {
  47. return b.shouldBeHeld
  48. }
  49. func (b *BaseDataSourceResource) SetShouldBeHeld(shouldBeHeld bool) {
  50. b.shouldBeHeld = shouldBeHeld
  51. }
  52. func (b *BaseDataSourceResource) GetKeeper() map[string]interface{} {
  53. return b.keeper
  54. }
  55. func (b *BaseDataSourceResource) SetKeeper(keeper map[string]interface{}) {
  56. b.keeper = keeper
  57. }
  58. func (b *BaseDataSourceResource) GetCache() map[string]branch.BranchStatus {
  59. return b.Cache
  60. }
  61. func (b *BaseDataSourceResource) SetCache(cache map[string]branch.BranchStatus) {
  62. b.Cache = cache
  63. }
  64. func (b *BaseDataSourceResource) GetResourceId() string {
  65. return b.db.GetResourceId()
  66. }
  67. func (b *BaseDataSourceResource) Hold(key string, value Holdable) (interface{}, error) {
  68. if value.IsHeld() {
  69. var x = b.keeper[key]
  70. if x != value {
  71. return nil, fmt.Errorf("something wrong with keeper, keeping[%v] but[%v] is also kept with the same key[%v]", x, value, key)
  72. }
  73. return value, nil
  74. }
  75. var x = b.keeper[key]
  76. b.keeper[key] = value
  77. value.SetHeld(true)
  78. return x, nil
  79. }
  80. func (b *BaseDataSourceResource) Release(key string, value Holdable) (interface{}, error) {
  81. if value.IsHeld() {
  82. var x = b.keeper[key]
  83. if x != value {
  84. return nil, fmt.Errorf("something wrong with keeper, keeping[%v] but[%v] is also kept with the same key[%v]", x, value, key)
  85. }
  86. return value, nil
  87. }
  88. var x = b.keeper[key]
  89. b.keeper[key] = value
  90. value.SetHeld(true)
  91. return x, nil
  92. }
  93. func (b *BaseDataSourceResource) GetBranchStatus(xaBranchXid string) (interface{}, error) {
  94. branchStatus, err := BranchStatusCache.GetIFPresent(xaBranchXid)
  95. return branchStatus, err
  96. }
  97. func (b *BaseDataSourceResource) GetDbType() string {
  98. return b.db.dbType.String()
  99. }
  100. func (b *BaseDataSourceResource) SetDbType(dbType types.DBType) {
  101. b.db.dbType = dbType
  102. }