types.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package DB
  2. import (
  3. "TsDB2/DB/core/page"
  4. "TsDB2/DB/core/types"
  5. "fmt"
  6. "reflect"
  7. )
  8. // FieldDef ORM 字段定义
  9. type FieldDef struct {
  10. Name string // Go 字段名
  11. ColName string // 数据库列名 (来自 tsdb tag 或字段名)
  12. FieldType types.FieldType // 存储类型
  13. GoKind reflect.Kind // Go 原始类型
  14. Index int // 在结构体中的字段索引
  15. }
  16. // ModelInfo 模型元信息
  17. type ModelInfo struct {
  18. Fields []FieldDef // 用户定义字段 (不含自动主键)
  19. AllFields []FieldDef // 全部字段 (含自动主键,按存储顺序)
  20. PKField string // 主键列名 ("_id" 或 "_ts")
  21. PKIndex int // 主键在 AllFields 中的索引
  22. IsTimeSeries bool // 是否时序表
  23. CoreFields []types.FieldInfo // 传给 core 的字段列表 (含自动主键)
  24. CorePKField byte // 主键在 CoreFields 中的索引 (用于 CreateIndex)
  25. StructType reflect.Type // 结构体类型 (用于反射)
  26. PKInStruct bool // 主键字段是否在结构体中 (嵌入基类时有)
  27. }
  28. // goKindToFieldType Go 类型到数据库字段类型的默认映射
  29. var goKindToFieldType = map[reflect.Kind]types.FieldType{
  30. reflect.Int8: types.FieldType_1byte,
  31. reflect.Int16: types.FieldType_2byte,
  32. reflect.Int32: types.FieldType_4byte,
  33. reflect.Int64: types.FieldType_8byte,
  34. reflect.Int: types.FieldType_8byte,
  35. reflect.Uint8: types.FieldType_1byte,
  36. reflect.Uint16: types.FieldType_2byte,
  37. reflect.Uint32: types.FieldType_4byte,
  38. reflect.Uint64: types.FieldType_8byte,
  39. reflect.Float32: types.FieldType_4byte,
  40. reflect.Float64: types.FieldType_8byte,
  41. reflect.Bool: types.FieldType_1byte,
  42. }
  43. // tagToFieldType 结构体标签到数据库字段类型的映射
  44. var tagToFieldType = map[string]types.FieldType{
  45. "1byte": types.FieldType_1byte,
  46. "2byte": types.FieldType_2byte,
  47. "4byte": types.FieldType_4byte,
  48. "8byte": types.FieldType_8byte,
  49. "16byte": types.FieldType_16byte,
  50. "32byte": types.FieldType_32byte,
  51. "64byte": types.FieldType_64byte,
  52. "128byte": types.FieldType_128byte,
  53. "256byte": types.FieldType_256byte,
  54. "dynamic": types.FieldType_Dynamicbyte,
  55. }
  56. var (
  57. baseModelType = reflect.TypeOf(BaseModel{})
  58. timeSeriesModelType = reflect.TypeOf(TimeSeriesModel{})
  59. )
  60. // ParseModel 解析结构体,提取模型信息
  61. // obj 必须是指向结构体的指针 (可以是零值)
  62. func ParseModel(obj interface{}) (*ModelInfo, error) {
  63. t := reflect.TypeOf(obj)
  64. if t.Kind() == reflect.Ptr {
  65. t = t.Elem()
  66. }
  67. if t.Kind() != reflect.Struct {
  68. return nil, fmt.Errorf("tsdb: obj must be a struct, got %s", t.Kind())
  69. }
  70. info := &ModelInfo{
  71. StructType: t,
  72. PKField: "_id",
  73. }
  74. // 扫描字段,检测嵌入的基类
  75. fieldIdx := 0
  76. for i := 0; i < t.NumField(); i++ {
  77. sf := t.Field(i)
  78. // 处理嵌入结构体
  79. if sf.Anonymous {
  80. if sf.Type == baseModelType {
  81. info.IsTimeSeries = false
  82. info.PKField = "_id"
  83. info.PKInStruct = true
  84. // 添加 _id 主键列
  85. info.AllFields = append(info.AllFields, FieldDef{
  86. Name: "ID",
  87. ColName: "_id",
  88. FieldType: types.FieldType_8byte,
  89. GoKind: reflect.Uint64,
  90. Index: len(info.AllFields),
  91. })
  92. info.PKIndex = len(info.AllFields) - 1
  93. continue
  94. }
  95. if sf.Type == timeSeriesModelType {
  96. info.IsTimeSeries = true
  97. info.PKField = "_ts"
  98. info.PKInStruct = true
  99. // 添加 _ts 主键列
  100. info.AllFields = append(info.AllFields, FieldDef{
  101. Name: "TS",
  102. ColName: "_ts",
  103. FieldType: types.FieldType_8byte,
  104. GoKind: reflect.Uint64,
  105. Index: len(info.AllFields),
  106. })
  107. info.PKIndex = len(info.AllFields) - 1
  108. continue
  109. }
  110. // 其他嵌入结构体: 递归展开
  111. if sf.Type.Kind() == reflect.Struct {
  112. if err := parseEmbeddedFields(sf.Type, info, &fieldIdx); err != nil {
  113. return nil, err
  114. }
  115. continue
  116. }
  117. continue
  118. }
  119. // 普通字段
  120. fd, err := parseField(sf, fieldIdx)
  121. if err != nil {
  122. return nil, fmt.Errorf("field %s: %w", sf.Name, err)
  123. }
  124. info.Fields = append(info.Fields, fd)
  125. info.AllFields = append(info.AllFields, fd)
  126. fieldIdx++
  127. }
  128. // 如果没有嵌入任何基类,默认添加 _id 主键
  129. if len(info.AllFields) == 0 || (info.PKField != "_id" && info.PKField != "_ts") {
  130. info.PKField = "_id"
  131. info.IsTimeSeries = false
  132. info.PKInStruct = false
  133. pk := FieldDef{
  134. Name: "ID",
  135. ColName: "_id",
  136. FieldType: types.FieldType_8byte,
  137. GoKind: reflect.Uint64,
  138. Index: 0,
  139. }
  140. // 将 PK 插入到 AllFields 开头
  141. newAll := make([]FieldDef, 0, len(info.AllFields)+1)
  142. newAll = append(newAll, pk)
  143. for j, f := range info.AllFields {
  144. f.Index = j + 1
  145. newAll = append(newAll, f)
  146. }
  147. for j := range info.Fields {
  148. info.Fields[j].Index++
  149. }
  150. info.AllFields = newAll
  151. info.PKIndex = 0
  152. }
  153. // 构建 CoreFields
  154. info.CoreFields = make([]types.FieldInfo, len(info.AllFields))
  155. for i, f := range info.AllFields {
  156. info.CoreFields[i] = types.FieldInfo{
  157. FieldType: f.FieldType,
  158. FieldName: []byte(f.ColName),
  159. }
  160. }
  161. info.CorePKField = byte(info.PKIndex)
  162. return info, nil
  163. }
  164. // parseEmbeddedFields 递归解析嵌入结构体的字段
  165. func parseEmbeddedFields(t reflect.Type, info *ModelInfo, fieldIdx *int) error {
  166. for i := 0; i < t.NumField(); i++ {
  167. sf := t.Field(i)
  168. if sf.Anonymous && sf.Type.Kind() == reflect.Struct {
  169. if err := parseEmbeddedFields(sf.Type, info, fieldIdx); err != nil {
  170. return err
  171. }
  172. continue
  173. }
  174. fd, err := parseField(sf, *fieldIdx)
  175. if err != nil {
  176. return err
  177. }
  178. info.Fields = append(info.Fields, fd)
  179. info.AllFields = append(info.AllFields, fd)
  180. *fieldIdx++
  181. }
  182. return nil
  183. }
  184. // parseField 解析单个结构体字段
  185. func parseField(sf reflect.StructField, idx int) (FieldDef, error) {
  186. fd := FieldDef{
  187. Name: sf.Name,
  188. GoKind: sf.Type.Kind(),
  189. Index: idx,
  190. }
  191. // 列名: 优先使用 tsdb tag 中的列名
  192. fd.ColName = sf.Name
  193. if tag, ok := sf.Tag.Lookup("tsdb"); ok && tag != "" {
  194. // tag 格式: "列名" 或 "列名,类型" 或 "类型"
  195. parts := parseTag(tag)
  196. if len(parts) >= 1 && parts[0] != "" {
  197. // 如果第一部分不是已知类型名,则视为列名
  198. if _, isType := tagToFieldType[parts[0]]; !isType {
  199. fd.ColName = parts[0]
  200. }
  201. }
  202. }
  203. // 字段类型: 优先使用 tsdb tag
  204. if tag, ok := sf.Tag.Lookup("tsdb"); ok && tag != "" {
  205. parts := parseTag(tag)
  206. for _, p := range parts {
  207. if ft, isType := tagToFieldType[p]; isType {
  208. fd.FieldType = ft
  209. return fd, nil
  210. }
  211. }
  212. }
  213. // 自动推断
  214. if ft, ok := goKindToFieldType[sf.Type.Kind()]; ok {
  215. fd.FieldType = ft
  216. return fd, nil
  217. }
  218. // string → 256byte
  219. if sf.Type.Kind() == reflect.String {
  220. fd.FieldType = types.FieldType_256byte
  221. return fd, nil
  222. }
  223. // []byte → dynamic
  224. if sf.Type.Kind() == reflect.Slice && sf.Type.Elem().Kind() == reflect.Uint8 {
  225. fd.FieldType = types.FieldType_Dynamicbyte
  226. return fd, nil
  227. }
  228. return fd, fmt.Errorf("unsupported type: %s", sf.Type.String())
  229. }
  230. // parseTag 解析 tsdb tag: "col_name,type" 或 "type" 或 "col_name"
  231. func parseTag(tag string) []string {
  232. var parts []string
  233. current := ""
  234. for _, c := range tag {
  235. if c == ',' {
  236. parts = append(parts, current)
  237. current = ""
  238. } else {
  239. current += string(c)
  240. }
  241. }
  242. if current != "" {
  243. parts = append(parts, current)
  244. }
  245. return parts
  246. }
  247. // GetIndexType 返回该模型应创建的一级索引类型
  248. func (m *ModelInfo) GetIndexType() page.IndexType {
  249. if m.IsTimeSeries {
  250. return page.IndexTypeTime
  251. }
  252. return page.IndexTypePrimary
  253. }