gitee.com/quant1x/engine@v1.8.4/cache/data_adapter.go (about)

     1  package cache
     2  
     3  import (
     4  	"errors"
     5  	"slices"
     6  	"sync"
     7  )
     8  
     9  type Kind = uint64
    10  
    11  const (
    12  	PluginMaskBaseData Kind = 0x1000000000000000 // 基础数据
    13  	PluginMaskFeature  Kind = 0x2000000000000000 // 特征数据
    14  	PluginMaskStrategy Kind = 0x3000000000000000 // 策略
    15  )
    16  
    17  const (
    18  	// DefaultDataProvider 默认数据提供者
    19  	DefaultDataProvider = "engine"
    20  )
    21  
    22  // DataAdapter 数据插件
    23  type DataAdapter interface {
    24  	// Schema 继承基础特性接口
    25  	Schema
    26  	// Print 控制台输出指定日期的数据
    27  	Print(code string, date ...string)
    28  }
    29  
    30  // Handover 缓存切换接口
    31  type Handover interface {
    32  	// ChangingOverDate 缓存数据转换日期
    33  	//	数据集等基础数据不需要切换日期
    34  	ChangingOverDate(date string)
    35  }
    36  
    37  type Depend interface {
    38  	DependOn() []Kind
    39  }
    40  
    41  var (
    42  	ErrAlreadyExists = errors.New("the plugin already exists")
    43  )
    44  
    45  var (
    46  	pluginMutex    sync.Mutex
    47  	mapDataPlugins = map[Kind]DataAdapter{}
    48  	//setupStatus map[string]bool
    49  )
    50  
    51  // Register 注册插件
    52  func Register(plugin DataAdapter) error {
    53  	pluginMutex.Lock()
    54  	defer pluginMutex.Unlock()
    55  	_, ok := mapDataPlugins[plugin.Kind()]
    56  	if ok {
    57  		return ErrAlreadyExists
    58  	}
    59  	mapDataPlugins[plugin.Kind()] = plugin
    60  	return nil
    61  }
    62  
    63  // GetDataAdapter 获取数据适配器
    64  func GetDataAdapter(kind Kind) DataAdapter {
    65  	pluginMutex.Lock()
    66  	defer pluginMutex.Unlock()
    67  	adapter, ok := mapDataPlugins[kind]
    68  	if ok {
    69  		return adapter
    70  	}
    71  	return nil
    72  }
    73  
    74  // Plugins 按照类型标志位捡出数据插件
    75  func Plugins(mask ...Kind) (list []DataAdapter) {
    76  	pluginMutex.Lock()
    77  	defer pluginMutex.Unlock()
    78  	pluginType := Kind(0)
    79  	if len(mask) > 0 {
    80  		if mask[0] == PluginMaskBaseData || mask[0] == PluginMaskFeature {
    81  			pluginType = mask[0]
    82  		}
    83  	}
    84  	// TODO: 这个地方的内存申请方面需要优化
    85  	var kinds []Kind
    86  	for kind, _ := range mapDataPlugins {
    87  		if pluginType == 0 || kind&pluginType == pluginType {
    88  			kinds = append(kinds, kind)
    89  		}
    90  	}
    91  	slices.Sort(kinds)
    92  	for _, kind := range kinds {
    93  		plugin, ok := mapDataPlugins[kind]
    94  		if ok {
    95  			list = append(list, plugin)
    96  		}
    97  	}
    98  	return
    99  }
   100  
   101  func PluginsWithName(pluginType Kind, keywords ...string) (list []DataAdapter) {
   102  	pluginMutex.Lock()
   103  	defer pluginMutex.Unlock()
   104  	if len(keywords) == 0 {
   105  		return
   106  	}
   107  	var kinds []Kind
   108  	for kind, plugin := range mapDataPlugins {
   109  		if kind&pluginType == pluginType && slices.Contains(keywords, plugin.Key()) {
   110  			kinds = append(kinds, kind)
   111  		}
   112  	}
   113  	if len(kinds) == 0 {
   114  		return
   115  	}
   116  	slices.Sort(kinds)
   117  	for _, kind := range kinds {
   118  		plugin, ok := mapDataPlugins[kind]
   119  		if ok {
   120  			list = append(list, plugin)
   121  		}
   122  	}
   123  	return
   124  }