types.go 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. package DB
  2. import (
  3. "TsDB2/DB/core/page"
  4. "TsDB2/DB/core/types"
  5. "errors"
  6. "fmt"
  7. "math/big"
  8. "reflect"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. )
  13. // FieldDef ORM 字段定义
  14. type FieldDef struct {
  15. Name string // Go 字段名
  16. ColName string // 数据库列名 (来自 vrtx tag 或字段名)
  17. FieldType types.FieldType // 存储类型
  18. // Flag 列级标志 (随 schema 落盘, 直接透传给 types.FieldInfo.Flag):
  19. // - 低 5 位: 列级类型参数【按类型解释】——
  20. // FieldType_Decimal 是小数位数 scale (0..types.MaxDecimalScale),
  21. // 来自 `vrtx:"decimal:2"` / `vrtx:"scale:2"`;
  22. // 字符串家族 (16/32/64/128/256byte) 是行内宽度码, 来自
  23. // `vrtx:"maxlen:N"` (见 maxlenToColumn; N 的上界是
  24. // types.MaxStringSize, 超了在建模阶段就报错);
  25. // - 状态位: types.FlagNullable (来自 `vrtx:"nullable"`);
  26. // types.FlagUnique (来自 `vrtx:"unique"`, 唯一约束; NULL 不参与);
  27. // types.FlagDeleted 由核心层 DropField 置位, 声明侧不设。
  28. //
  29. // Decimal 的保序前提是"同一列 scale 一致", 字符串列的定宽前提是"同一列宽度码
  30. // 一致" —— 两者都必须作为列元数据存下来, 而不能只活在 Go 类型里 (见 types.FieldSize)。
  31. Flag byte
  32. GoKind reflect.Kind // Go 原始类型
  33. Index int // 该字段在 AllFields 中的位置
  34. IsPK bool // 用户声明的主键字段 (vrtx:"pk" / 显式列名 _id)
  35. }
  36. // Param 列级类型参数 (Flag 低 5 位): Decimal 读作 scale, 字符串家族读作宽度码。
  37. func (fd FieldDef) Param() byte { return fd.Flag & types.FlagScaleMask }
  38. // Width 该列在行内的实际宽度 (字节)。
  39. //
  40. // 不能写成 types.GetSize(fd.FieldType): 那只是【类型默认】宽度, 对 `maxlen:1000`
  41. // 这种用宽度码声明出来的列会算成 256 —— ORM 侧任何按这个宽度补零/截断的地方, 都会
  42. // 与引擎按 1024 存下来的字节不同源。宽度只认这一处 (转发给 types.FieldSize)。
  43. func (fd FieldDef) Width() uint16 { return types.FieldSize(fd.FieldType, fd.Param()) }
  44. // IndexDef 从结构体标签解析的二级索引定义
  45. type IndexDef struct {
  46. Name string // 索引名 (空=自动生成 idx_表名_首个字段名)
  47. FieldID byte // 首个字段在 CoreFields 中的位置 (单列索引)
  48. FieldIDs []byte // 索引分量在 CoreFields 中的位置, 按键序 (复合索引)
  49. ColName string // 首个字段列名 (用于自动生成索引名)
  50. IndexType page.IndexType // 索引类型 (默认 BTree)
  51. Unique bool // 唯一索引: 同一索引键只允许一行, NULL 分量不参与 (可多行 NULL)
  52. }
  53. // coreFieldIDs 返回 core.CreateIndex 需要的分量列表
  54. func (d IndexDef) coreFieldIDs() []byte {
  55. if len(d.FieldIDs) > 0 {
  56. return d.FieldIDs
  57. }
  58. return []byte{d.FieldID}
  59. }
  60. // 自动索引名的前缀。
  61. //
  62. // 索引元数据链本来就是「每表独有」的 —— TableInfo.FirstIndexID 指向本表自己的
  63. // IndexMeta 链, 所以索引名只需要表内唯一, 表名前缀并不是唯一性所必需的。保留它
  64. // 纯粹是为了维持既有的 pk_<表名> / idx_<表名>_<列名> 命名, 不破坏已引用这两个名字的代码。
  65. //
  66. // 代价: 这两个前缀要占用 core.IndexNameSize 的额度 —— 注意是索引名的, 不是表名的。
  67. // 因为 IndexNameSize(64) 比 TableNameSize(48) 宽出 16 字节, 前缀这笔账只从索引名里出,
  68. // 表名依旧可以用满 48 —— 这正是把索引名单独放宽的目的。
  69. // UseTable 会在建表前把这笔账算清楚 (checkNameBudget), 而不是等到建索引时才报错。
  70. const (
  71. pkIndexPrefix = "pk_" // 主键索引: pk_<表名>
  72. idxIndexPrefix = "idx_" // 自动二级索引: idx_<表名>_<列名>
  73. )
  74. // 主键列默认列名: 引擎的行主键号 (pkID) 固定 8 字节, ORM 用这一列把它镜像进结构体。
  75. // BaseModel / PrimaryModel 默认就是这一列 (TimeSeriesModel 是 _ts), 表可以用 vrtx:"pk"
  76. // 换成自己的列名; 写 `vrtx:"_id"` 等价于声明这个列名作主键 (见 parseField)。
  77. const pkColumnName = "_id"
  78. // 时序表默认的时序键列名。时序键是该表的"主键" —— 它是时序索引的分段键, 决定这一行
  79. // 落在哪个时间段, 也是 ORM 头部 Load/Reload/Delete 默认的定位键。用户写 `vrtx:"pk"`
  80. // 时同样可以换成自己的列。
  81. //
  82. // 注意它【不是】引擎行号 (pkID): 时序索引的 DataPage 里, 行首 8 字节才是 pkID, 时间键
  83. // 是紧跟着的第一个字段。两者之所以必须分开, 是因为同一个时间戳下允许存在多行
  84. // (同一时刻的多个测点 / 多次事件) —— 时间键只能定位到时间段, 行号才能定位到具体那一行。
  85. //
  86. // 取值是纳秒级 Unix 时间戳; 插入时若没给 (==0), ORM 会补上当前纳秒时间 (见 table.go)。
  87. const tsColumnName = "_ts"
  88. // ErrInvalidPrimaryKey 主键定义非法: 类型不是整数、一个结构体声明了多个主键、
  89. // 或同时内嵌了多个表基类。具体原因见错误信息。
  90. var ErrInvalidPrimaryKey = errors.New("主键定义非法")
  91. // indexName 返回索引的最终名字: 显式命名优先, 否则 idx_表名_首个字段名
  92. func (d IndexDef) indexName(tableName string) string {
  93. if d.Name != "" {
  94. return d.Name
  95. }
  96. return idxIndexPrefix + tableName + "_" + d.ColName
  97. }
  98. // ModelInfo 模型元信息
  99. type ModelInfo struct {
  100. Fields []FieldDef // 用户定义字段 (不含自动主键)
  101. AllFields []FieldDef // 全部字段 (含自动主键,按存储顺序)
  102. PKField string // 主键列名 ("_id" 或 "_ts")
  103. PKIndex int // 主键在 AllFields 中的索引
  104. IsTimeSeries bool // 是否时序表
  105. CoreFields []types.FieldInfo // 传给 core 的字段列表 (含自动主键)
  106. CorePKField byte // 主键在 CoreFields 中的索引 (用于 CreateIndex)
  107. StructType reflect.Type // 结构体类型 (用于反射)
  108. PKInStruct bool // 主键字段是否在结构体中 (嵌入基类时有)
  109. Indexes []IndexDef // 从标签解析的二级索引定义
  110. }
  111. // goKindToFieldType Go 类型到数据库字段类型的默认映射
  112. //
  113. // 有符号与无符号必须落在不同的类型值上: 1/2/4/8byte 是无符号列 (补码大端即其正确序),
  114. // 有符号整数走 Int8/Int16/Int32/Int64 (保序编码)。旧映射把二者都压在 1/2/4/8byte 上,
  115. // 于是 int64 列上的负数跨 0 的范围扫描 / ORDER BY 会静默漏行 —— 详见 types.FieldType_Int64。
  116. var goKindToFieldType = map[reflect.Kind]types.FieldType{
  117. reflect.Int8: types.FieldType_Int8,
  118. reflect.Int16: types.FieldType_Int16,
  119. reflect.Int32: types.FieldType_Int32,
  120. reflect.Int64: types.FieldType_Int64,
  121. reflect.Int: types.FieldType_Int64,
  122. reflect.Uint8: types.FieldType_1byte,
  123. reflect.Uint16: types.FieldType_2byte,
  124. reflect.Uint32: types.FieldType_4byte,
  125. reflect.Uint64: types.FieldType_8byte,
  126. // 浮点必须映射到浮点列。旧映射 (Float32→4byte, Float64→8byte) 是整数列,
  127. // 写入时按整数截断: float64 的 123.75 会安静地变成 123 (金额/比例失真)。
  128. //
  129. // Float32 单独立类型 (FieldType_Float32): 旧做法折进 Float64 让列宽翻倍,
  130. // 现在列宽 == Go 类型宽度, 且 32 位精度是用户自己声明的, 不该被悄悄放宽。
  131. reflect.Float32: types.FieldType_Float32,
  132. reflect.Float64: types.FieldType_Float64,
  133. // bool 单独立类型: 借用 1byte 时"写 2 也算真", 等值/索引/DISTINCT 会给出多套答案。
  134. reflect.Bool: types.FieldType_Bool,
  135. }
  136. // goTypeToFieldType 按【完整 Go 类型】映射到列类型, 优先于 goKindToFieldType。
  137. //
  138. // 这些类型都不是"Kind 能区分"的: types.UUID 是 [16]byte (Kind=Array), types.Decimal /
  139. // big.Int 是结构体。它们自带语义, 所以字段不用写 tag 就能落成正确的列。
  140. //
  141. // 只列【非指针】类型: 查表前统一解了一层指针 (见 parseField 的 carrierType),
  142. // `*T` 命中 T 的条目即可 —— 指针在这里的语义只是"可为 NULL", 不是另一种列型。
  143. var goTypeToFieldType = map[reflect.Type]types.FieldType{
  144. reflect.TypeOf(types.Decimal{}): types.FieldType_Decimal,
  145. reflect.TypeOf(types.UUID{}): types.FieldType_UUID,
  146. reflect.TypeOf(big.Int{}): types.FieldType_Int128,
  147. }
  148. // tagFieldTypes vrtx tag 中 `type:<name>` 可声明的列类型。
  149. //
  150. // 为什么不复用 Go Kind 推断: 同一个 Go 类型可能对应多种列语义 —— string 可以是定长串 /
  151. // UUID 文本 / Decimal 文本, int16 可以是普通整数 / 年份, []byte 可以是普通变长列 /
  152. // 文件大对象。tag 是用户表达"这一列的语义"的地方; 没写 tag 时才退回 Go 类型推断。
  153. //
  154. // 两个约定 (由 TestTagFieldTypesNamesAreCanonical 守):
  155. //
  156. // 1. 名字必须与 migrate.TypeName 的输出逐字一致 —— 那是导出 schema 时写进
  157. // __vrtx_meta 的名字, 对不上的话 "dump 出来照着再声明一遍" 就读不回去了。
  158. // 2. 一个列型只留一个名字, 不收别名 (曾经有 bool/boolean、float32/f32、decimal/numeric
  159. // 三对)。别名看着无害, 实则没有文档、没有测试、也永远不会被 TypeName 吐出来,
  160. // 只是给"同一列型两种写法"开口子。
  161. var tagFieldTypes = map[string]types.FieldType{
  162. "bool": types.FieldType_Bool,
  163. "float32": types.FieldType_Float32,
  164. "float64": types.FieldType_Float64,
  165. "decimal": types.FieldType_Decimal,
  166. "uuid": types.FieldType_UUID,
  167. "year": types.FieldType_Year,
  168. "int128": types.FieldType_Int128,
  169. "blob": types.FieldType_Blob,
  170. }
  171. // tagTypeNames 列出 `type:<name>` 可用的全部名称 (排序, 供报错信息使用)。
  172. //
  173. // 从 map 生成而不是手写一串: 手写的那份已经漏过一次 —— 新增 blob 时没同步, 于是
  174. // "可用类型"的提示里始终看不到 blob, 而报错信息正是用户找答案的地方。
  175. func tagTypeNames() string {
  176. names := make([]string, 0, len(tagFieldTypes))
  177. for n := range tagFieldTypes {
  178. names = append(names, n)
  179. }
  180. sort.Strings(names)
  181. return strings.Join(names, "/")
  182. }
  183. var (
  184. baseModelType = reflect.TypeOf(BaseModel{})
  185. timeSeriesModelType = reflect.TypeOf(TimeSeriesModel{})
  186. )
  187. // ParseModel 解析结构体,提取模型信息
  188. //
  189. // obj 必须是指向结构体的指针 (可以是零值), 且该结构体必须内嵌表基类
  190. // (DB.BaseModel / DB.PrimaryModel / DB.TimeSeriesModel) —— 否则返回 ErrNotModel。
  191. // "内嵌基类"既是"这是一张本框架的表"的标记, 也是头部增删改查的来源, 详见 requireBase。
  192. func ParseModel(obj interface{}) (*ModelInfo, error) {
  193. t := reflect.TypeOf(obj)
  194. if t.Kind() == reflect.Ptr {
  195. t = t.Elem()
  196. }
  197. if t.Kind() != reflect.Struct {
  198. return nil, fmt.Errorf("vrtx: obj must be a struct, got %s", t.Kind())
  199. }
  200. s := newModelScanner()
  201. if err := s.scan(t); err != nil {
  202. return nil, err
  203. }
  204. return s.modelInfo(t)
  205. }
  206. // baseSpecOf 判断一个嵌入字段是不是表基类, 并取出它自报的物理形态。
  207. //
  208. // 只认本包的公开基类, 不做接口通配: 用户自己写的嵌入结构体里也可能嵌着 BaseModel,
  209. // 那种情况应当继续递归展开它内部的字段, 而不是整块当成表基类 (否则它内部声明的字段
  210. // 会凭空消失)。再加一种基类时, 只需要让它实现 ModelSpec 并登记到这里。
  211. func baseSpecOf(t reflect.Type) (ModelSpec, bool) {
  212. if t != baseModelType && t != timeSeriesModelType {
  213. return nil, false
  214. }
  215. // ModelSpec 的三个方法都是值接收者, 零值即可断言。
  216. spec, ok := reflect.Zero(t).Interface().(ModelSpec)
  217. return spec, ok
  218. }
  219. // scannedField 一个已解析的用户字段 (不含基类带来的主键列)。
  220. type scannedField struct {
  221. def FieldDef
  222. idxDef *IndexDef
  223. }
  224. // modelScanner 逐层展开结构体, 收集用户字段并识别表基类 / 显式主键。
  225. //
  226. // 之所以要"先认全、再决定": 主键列既可能来自基类 (_id / _ts), 也可能被 vrtx:"pk"
  227. // 覆盖, 只有把两类候选都看完, 才知道 AllFields 里该放哪一列。
  228. type modelScanner struct {
  229. fields []scannedField
  230. spec ModelSpec // 内嵌的表基类自报的物理形态
  231. baseType reflect.Type // 基类的具体类型 (报错用)
  232. hasPK bool // 用户用 vrtx:"pk" 显式声明了主键
  233. pkField int // 显式主键在 fields 中的下标
  234. }
  235. func newModelScanner() *modelScanner {
  236. return &modelScanner{pkField: -1}
  237. }
  238. func (s *modelScanner) scan(t reflect.Type) error {
  239. for i := 0; i < t.NumField(); i++ {
  240. sf := t.Field(i)
  241. if sf.Anonymous {
  242. // 表基类: 记下它自报的物理形态, 不当普通字段展开。
  243. // 它内部的 modelCore / ID 都是运行时状态, 不该变成列。
  244. if spec, ok := baseSpecOf(sf.Type); ok {
  245. if s.spec != nil {
  246. return fmt.Errorf("%w: 一张表只能内嵌一个表基类, 已内嵌 %s, 不能再内嵌 %s "+
  247. "(主键索引与时序索引都是行存储索引, 一张表只能有一个)",
  248. ErrInvalidPrimaryKey, s.baseType.Name(), sf.Type.Name())
  249. }
  250. s.spec, s.baseType = spec, sf.Type
  251. continue
  252. }
  253. // 其他嵌入结构体: 递归展开
  254. if sf.Type.Kind() == reflect.Struct {
  255. if err := s.scan(sf.Type); err != nil {
  256. return err
  257. }
  258. }
  259. continue
  260. }
  261. fd, idxDef, err := parseField(sf, len(s.fields))
  262. if err != nil {
  263. return fmt.Errorf("field %s: %w", sf.Name, err)
  264. }
  265. s.fields = append(s.fields, scannedField{def: fd, idxDef: idxDef})
  266. if fd.IsPK {
  267. if err := s.declarePK(sf); err != nil {
  268. return err
  269. }
  270. }
  271. }
  272. return nil
  273. }
  274. // declarePK 登记用户用 `vrtx:"pk"` (或显式列名 _id / _ts) 声明的主键。
  275. //
  276. // 类型必须是 64 位整数: 引擎的主键号 (pkID) 就是 uint64, 而且这一列会被当作 pkID
  277. // 的镜像 (插入后回写、Find 反查都靠它)。窄整数装不下时 setPKValue 会静默截断,
  278. // 字符串 / 浮点 / 切片则根本无法承载 pkID —— 与其留一个查不到的行, 不如直接报错。
  279. //
  280. // 它优先于基类带来的默认主键列: 基类给的是"默认样式", 用户显式声明了就听用户的 ——
  281. // 这正是"每个表自己设计主键"的入口。
  282. func (s *modelScanner) declarePK(sf reflect.StructField) error {
  283. if s.hasPK {
  284. return fmt.Errorf("%w: 字段 %s 与 %s 都声明了主键, 一个表只能有一个主键",
  285. ErrInvalidPrimaryKey, sf.Name, s.fields[s.pkField].def.Name)
  286. }
  287. switch sf.Type.Kind() {
  288. case reflect.Int, reflect.Uint, reflect.Int64, reflect.Uint64:
  289. // 这四种在 goKindToFieldType 里都映射到 8 字节列, 与 pkID 同宽
  290. default:
  291. return fmt.Errorf("%w: 字段 %s 是 %s, 主键只能是 64 位整数 (int/uint/int64/uint64); 字符串不能当主键",
  292. ErrInvalidPrimaryKey, sf.Name, sf.Type)
  293. }
  294. s.hasPK, s.pkField = true, len(s.fields)-1
  295. return nil
  296. }
  297. // modelInfo 把扫描结果组装成 ModelInfo。主键列的落位有两条规则, 都有实际原因:
  298. //
  299. // 1. vrtx:"pk" 覆盖基类的默认列 —— 基类给的是默认样式, 用户声明了就听用户的。
  300. // 2. 主键列固定排在 AllFields / CoreFields 第一位。行内布局是
  301. // [pkID(8B)][schemaVer(1B)][字段...], 而时序索引按固定偏移 9 读第一列当时间键
  302. // (core/index.timeRowInRange、TimeDataPage.FindRowByIndexKey), 所以时序键必须落在
  303. // 第一列。与其要求用户把字段写在基类前面, 不如统一排到最前。
  304. // 行镜像是按列名写入的 (core.serializeRow 会 lookupFieldValue 找列),
  305. // 字段顺序只影响新建表时的列序, 因此这个调整对已有表也安全, 不会把数据写错位。
  306. //
  307. // 进到这里之前必须已经确认内嵌了表基类 (见 requireBase): 没有基类就没有默认键列,
  308. // 一级索引用哪种、行主键号往哪个字段回写都无从谈起。
  309. func (s *modelScanner) modelInfo(t reflect.Type) (*ModelInfo, error) {
  310. if err := s.requireBase(t); err != nil {
  311. return nil, err
  312. }
  313. info := &ModelInfo{StructType: t}
  314. pk := s.primaryKey()
  315. info.AllFields = append(info.AllFields, pk)
  316. info.PKField = pk.ColName
  317. info.PKIndex = 0
  318. info.PKInStruct = true
  319. info.IsTimeSeries = s.spec.indexType() == page.IndexTypeTime
  320. // 每个用户字段落在 AllFields 的位置
  321. pos := make([]int, len(s.fields))
  322. for i, f := range s.fields {
  323. if s.hasPK && i == s.pkField {
  324. // 显式主键: 字段本身仍然留在 Fields 里 (调用方靠它取名 / 取类型),
  325. // 但列定义已经作为主键放进 AllFields, 这里不重复追加。
  326. fd := f.def
  327. fd.Index = 0
  328. info.Fields = append(info.Fields, fd)
  329. continue
  330. }
  331. fd := f.def
  332. fd.Index = len(info.AllFields)
  333. pos[i] = fd.Index
  334. info.Fields = append(info.Fields, fd)
  335. info.AllFields = append(info.AllFields, fd)
  336. }
  337. info.Indexes = s.indexes(pos)
  338. // 列上的 FlagUnique 只反映【该列 tag 里写了 unique】(由 parseField 置位), 这里
  339. // 【不】按索引分量补齐。原因是补齐会撒谎, 而且会骗到"从 schema 反推 tag"的工具:
  340. // - 复合唯一索引 (index:NAME 多列 + 其中一列写 unique) 约束的是整组列, 给每个
  341. // 分量都打上唯一位, 读起来就成了"这一列单独唯一" —— 恰好是它的反面;
  342. // - 工具照这个位回写 vrtx tag 时, 会给 B 也加上 unique, 而 unique 在 ORM 里
  343. // 是"该列单独建一个唯一索引", 于是凭空多出一条约束。
  344. // 复合键覆盖了哪些列, 由 IndexDef / IndexMeta 的 FieldIDs 表达 (那才是权威)。
  345. // 构建 CoreFields。Flag 必须一并带过去: Decimal 的列级 scale、可空位与唯一位都存在
  346. // 这里, 写入/索引编码都要拿它对齐 (缺失会退化成"按值自带 scale", 破坏同列保序前提)。
  347. info.CoreFields = make([]types.FieldInfo, len(info.AllFields))
  348. for i, f := range info.AllFields {
  349. info.CoreFields[i] = types.FieldInfo{
  350. FieldType: f.FieldType,
  351. FieldName: []byte(f.ColName),
  352. Flag: f.Flag,
  353. }
  354. }
  355. info.CorePKField = byte(info.PKIndex)
  356. return info, nil
  357. }
  358. // requireBase 强制"表必须内嵌基类"。
  359. //
  360. // 为什么把它做成硬约束, 而不是"没有基类也行、只是没有头部方法":
  361. //
  362. // - 基类是"这是一张本框架的表"的唯一凭据。没有它, ORM 只能靠猜 —— 猜哪一列是行
  363. // 主键号的镜像、一级索引用 Primary 还是 Time; 猜错的代价是一张"插入时不回写主键、
  364. // 按写入值查不到"的表 (旧的 struct{} 兜底分支就是这个下场)。
  365. // - 头部增删改查 (Insert/Update/Delete/Load...) 由基类提供。允许没有基类的表存在,
  366. // 等于允许一半的表用 Table[T] 的 API、另一半的表两种都能用 —— 同一个项目里两套
  367. // 并列的写法, 新人和代码审查都得先判断"这张表属于哪种"。
  368. // - 校验放在建表之前, 所以不会留下半成品: 报错时库里什么都没有。
  369. //
  370. // 迁移 (migrate 包) 是按 SQL 元数据动态建表的, 那时根本没有 Go 结构体可内嵌 ——
  371. // 它走 core 层 API (db.CreateTable), 与本约束无关。
  372. func (s *modelScanner) requireBase(t reflect.Type) error {
  373. if s.spec != nil {
  374. return nil
  375. }
  376. if s.hasPK {
  377. // 单写 vrtx:"pk" 是个很自然的误解: 它看起来"已经声明了主键"。
  378. // 点明它只是"覆盖基类默认键列", 否则用户会以为是别的地方出了错。
  379. return fmt.Errorf("%w: %s 只声明了 vrtx:\"pk\", 但没内嵌表基类 —— "+
  380. "pk 标签是用来覆盖基类默认键列的 (BaseModel 的 _id / TimeSeriesModel 的 _ts), "+
  381. "不能替代基类本身", ErrNotModel, t)
  382. }
  383. return fmt.Errorf("%w: %s 必须内嵌 DB.BaseModel / DB.PrimaryModel (主键表) 或 "+
  384. "DB.TimeSeriesModel (时序表)", ErrNotModel, t)
  385. }
  386. // primaryKey 返回主键列定义: 用户用 vrtx:"pk" 显式声明的优先, 否则用基类自报的默认键列
  387. // (_id / _ts)。两者必有其一 —— 调用前 requireBase 已经保证了基类存在。
  388. func (s *modelScanner) primaryKey() FieldDef {
  389. if s.hasPK {
  390. f := s.fields[s.pkField].def
  391. f.IsPK = true
  392. f.Index = 0
  393. // 主键列必须钉在 FieldType_8byte (无符号 64 位) 上, 不能沿用字段自身的有符号类型
  394. // (FieldType_Int*)。两条原因都不是风格问题:
  395. // 1. 这一列是引擎行主键号 pkID (uint64) 的镜像, 主键 B+ 树按它的补码字节序排序;
  396. // 2. 时序表里它就是 _ts —— 时序索引 (index.IndexTimeEntry.Compare) 与时间范围
  397. // 过滤都按 int64 补码直接读这 8 个字节, 换成保序编码会让时间轴整体错位。
  398. // 字段被声明成 pk 时值仍经 toUint64 归一化后写入 (getPKValue 已拒绝负数),
  399. // 盘上字节与默认主键完全一致, 因此按 _id 查询/排序的行为不变。
  400. f.FieldType = types.FieldType_8byte
  401. f.GoKind = reflect.Uint64
  402. return f
  403. }
  404. return FieldDef{
  405. Name: s.spec.pkGoField(),
  406. ColName: s.spec.pkColumn(),
  407. FieldType: types.FieldType_8byte,
  408. GoKind: reflect.Uint64,
  409. Index: 0,
  410. }
  411. }
  412. // indexes 收集所有标记了 index 的字段的索引定义。
  413. //
  414. // 同一个具名索引 (index:NAME) 被多个字段声明时, 合并成一个复合索引:
  415. // 分量顺序 = 字段在表中的物理顺序。这样无需在 tag 里额外表达键顺序,
  416. // 也正好对上复合索引"最左前缀"的用法 (a=? 或 a=? AND b>?)。
  417. // 未命名的 index 仍各自成单列索引。同名时以第一个声明为准 (含索引类型)。
  418. func (s *modelScanner) indexes(pos []int) []IndexDef {
  419. var out []IndexDef
  420. named := make(map[string]int, len(s.fields))
  421. for i, f := range s.fields {
  422. if f.idxDef == nil {
  423. continue
  424. }
  425. if s.hasPK && i == s.pkField {
  426. continue // 主键列本身就是一级索引, 不再单独建二级索引
  427. }
  428. at := pos[i]
  429. if f.idxDef.Name != "" {
  430. if n, ok := named[f.idxDef.Name]; ok {
  431. // 同名 → 追加为新的索引分量
  432. out[n].FieldIDs = append(out[n].FieldIDs, byte(at))
  433. out[n].Unique = out[n].Unique || f.idxDef.Unique
  434. continue
  435. }
  436. }
  437. d := *f.idxDef
  438. d.FieldID = byte(at)
  439. d.FieldIDs = []byte{byte(at)}
  440. d.ColName = f.def.ColName
  441. out = append(out, d)
  442. if d.Name != "" {
  443. named[d.Name] = len(out) - 1
  444. }
  445. }
  446. return out
  447. }
  448. // parseField 解析单个结构体字段
  449. // 返回: FieldDef, *IndexDef (如果有索引声明), error
  450. func parseField(sf reflect.StructField, idx int) (FieldDef, *IndexDef, error) {
  451. // carrierType 列载体类型: 指针载体 `*T` 的列语义由元素类型 T 决定 —— nil ↔ NULL
  452. // (见 convert.go 的 nil 判定), 非 nil 时按 T 的值写入/读回。解一层指针之后,
  453. // *int64 与 int64 落在同一个列型上, 只是多了"可以整列为空"这一档。
  454. carrierType := sf.Type
  455. if carrierType.Kind() == reflect.Ptr {
  456. carrierType = carrierType.Elem()
  457. }
  458. fd := FieldDef{
  459. Name: sf.Name,
  460. GoKind: carrierType.Kind(),
  461. Index: idx,
  462. }
  463. var idxDef *IndexDef
  464. // 列名: 优先使用 vrtx tag 中的列名
  465. fd.ColName = sf.Name
  466. tag, hasTag := sf.Tag.Lookup("vrtx")
  467. if !hasTag {
  468. tag, hasTag = sf.Tag.Lookup("tsdb") // 向后兼容: 也检查 tsdb 标签
  469. }
  470. if hasTag && tag != "" {
  471. parts := parseTag(tag)
  472. // 分类解析每个 part
  473. maxlen := 0
  474. hasIndex := false
  475. idxName := ""
  476. idxType := page.IndexTypeBTree // 默认 B+树索引
  477. typeSet := false // tag 里显式写了列类型
  478. scaleSet := false // tag 里显式写了 decimal scale
  479. for _, p := range parts {
  480. p = strings.TrimSpace(p)
  481. if p == "" {
  482. continue
  483. }
  484. // 1. 检查是否是索引声明: "index" 或 "index:NAME"
  485. if p == "index" || strings.HasPrefix(p, "index:") {
  486. hasIndex = true
  487. if strings.HasPrefix(p, "index:") {
  488. idxName = p[6:] // 去掉 "index:" 前缀
  489. }
  490. continue
  491. }
  492. // 2. 检查 key:value 选项 (冒号分隔)
  493. if strings.Contains(p, ":") && !strings.HasPrefix(p, "index:") {
  494. kv := strings.SplitN(p, ":", 2)
  495. key, val := kv[0], kv[1]
  496. switch key {
  497. case "type":
  498. // type:hash / type:btree 是索引类型; 其余值是列类型
  499. // (type:decimal / type:uuid / ...), 两者共用一个 key 但命名空间不冲突。
  500. switch val {
  501. case "hash":
  502. idxType = page.IndexTypeHash
  503. case "btree":
  504. idxType = page.IndexTypeBTree
  505. default:
  506. ft, ok := tagFieldTypes[strings.ToLower(strings.TrimSpace(val))]
  507. if !ok {
  508. return fd, nil, fmt.Errorf("未知字段类型 %q (可选: %s)", val, tagTypeNames())
  509. }
  510. fd.FieldType = ft
  511. typeSet = true
  512. }
  513. case "decimal":
  514. // decimal:N 是 `type:decimal,scale:N` 的简写。
  515. n, err := strconv.Atoi(strings.TrimSpace(val))
  516. if err != nil || n < 0 || n > types.MaxDecimalScale {
  517. return fd, nil, fmt.Errorf("decimal 精度必须是 0..%d 的整数, 得到 %q",
  518. types.MaxDecimalScale, val)
  519. }
  520. fd.FieldType = types.FieldType_Decimal
  521. // 只动低 5 位的 scale: 它与 nullable 等状态位共用一个字节, 不能整体覆盖。
  522. fd.Flag = fd.Flag&^types.FlagScaleMask | byte(n)
  523. typeSet, scaleSet = true, true
  524. case "scale":
  525. n, err := strconv.Atoi(strings.TrimSpace(val))
  526. if err != nil || n < 0 || n > types.MaxDecimalScale {
  527. return fd, nil, fmt.Errorf("decimal scale 必须是 0..%d 的整数, 得到 %q",
  528. types.MaxDecimalScale, val)
  529. }
  530. fd.Flag = fd.Flag&^types.FlagScaleMask | byte(n)
  531. scaleSet = true
  532. case "maxlen":
  533. // maxlen 的单位是【字节】(与 types.FieldSize 同口径), 不是字符数:
  534. // 引擎的行布局按字节算, 拿字符数去声明会让 utf8mb4 中文列 (3 字节/字)
  535. // 实际容量只有声明的 1/3。需要按字符数声明时请乘上编码宽度。
  536. if n, err := strconv.Atoi(val); err == nil && n > 0 {
  537. maxlen = n
  538. }
  539. }
  540. continue
  541. }
  542. // 列可空声明: `vrtx:"nullable"` / `vrtx:"null"` —— 允许该列写入 NULL,
  543. // 行内用 NULL 位图记录 (见 types.NullBitmapLen)。未声明即 NOT NULL。
  544. if p == "nullable" || p == "null" {
  545. fd.Flag |= types.FlagNullable
  546. continue
  547. }
  548. // 3. 检查是否是 "unique": 唯一约束。
  549. // 同时打两个标记 —— 列上的 FlagUnique (声明) 与索引上的 Unique (执行者):
  550. // 唯一性判定要一次索引查找, 光有列标记无法执行约束; 而只有索引标记则
  551. // "哪些列被约束"要反查索引元数据才看得见。两者都写, 各司其职。
  552. if p == "unique" {
  553. if idxDef == nil {
  554. idxDef = &IndexDef{}
  555. }
  556. idxDef.Unique = true
  557. fd.Flag |= types.FlagUnique
  558. hasIndex = true
  559. continue
  560. }
  561. // 3.5 检查是否是主键声明: 该字段就是本表主键。
  562. // 时序表嵌了 TimeSeriesModel 时它同样成立 —— 声明的那一列会成为时序键
  563. // (时序索引的分段键), 而基类自带的 _ts 不再建列。
  564. // 类型校验在 declarePK 里做 —— 字符串等非整数类型直接报错。
  565. if p == "pk" {
  566. fd.IsPK = true
  567. continue
  568. }
  569. // 4. 其他: 视为列名
  570. fd.ColName = p
  571. }
  572. // 显式把列名写成 _id / _ts 等价于声明主键: 这两列正是引擎行主键号 (pkID) 的
  573. // 镜像列, 旧实现会把这种字段当普通列 —— 插入时既不填号也不回写, 静默失效。
  574. if fd.ColName == pkColumnName || fd.ColName == tsColumnName {
  575. fd.IsPK = true
  576. }
  577. // 主键列本身就是唯一的 (InsertRowWithID 用 ErrDuplicatePK 兜着), 而 indexes()
  578. // 也不会给主键列建二级索引 —— 留着 FlagUnique 只会让人以为"另有一个唯一索引
  579. // 在约束它", 实际上什么都没建。声明侧直接抹掉这个位。
  580. if fd.IsPK {
  581. fd.Flag &^= types.FlagUnique
  582. }
  583. // 处理 maxlen: 只在没有显式列类型时生效 (显式列类型优先)
  584. if maxlen > 0 && !typeSet {
  585. if sf.Type.Kind() == reflect.String {
  586. ft, code, err := maxlenToColumn(maxlen)
  587. if err != nil {
  588. // 声明了引擎兑现不了的容量: 在 ParseModel 阶段就拒, 不留"表建好了、
  589. // 某天写入才失败"的半成品 (与 validateFieldTypeParam 同一原则)。
  590. return fd, nil, fmt.Errorf("字段 %s: %w", fd.ColName, err)
  591. }
  592. fd.FieldType = ft
  593. // 宽度码写进 Flag 低 5 位: 它就是这一列的定宽依据, 随 schema 落盘,
  594. // 与 decimal 的 scale 共用同一格 (按类型解释, 见 types.FieldSize)。
  595. if code > 0 {
  596. fd.Flag = fd.Flag&^types.FlagScaleMask | code
  597. }
  598. }
  599. // 数值类型忽略 maxlen (用 Go 类型自动推断)
  600. }
  601. // 只写了 scale 就默认这是 decimal 列 (scale 是 decimal 独有的参数)。
  602. if scaleSet && fd.FieldType == 0 && !typeSet {
  603. fd.FieldType = types.FieldType_Decimal
  604. }
  605. // 处理索引声明
  606. if hasIndex {
  607. if idxDef == nil {
  608. idxDef = &IndexDef{}
  609. }
  610. idxDef.Name = idxName
  611. idxDef.IndexType = idxType
  612. }
  613. }
  614. // 字段类型: tag 没显式指定时按 Go 类型推断。
  615. // 先按完整类型 (Decimal/UUID/big.Int 这类自带语义的类型), 再按 Kind。
  616. // 两处都看 carrierType: `*int64` 与 `int64` 推出同一个列型 —— 指针只是多了
  617. // "整列为空"这一档, 不该另立一门列型。
  618. if fd.FieldType == 0 {
  619. if ft, ok := goTypeToFieldType[carrierType]; ok {
  620. fd.FieldType = ft
  621. } else if ft, ok := goKindToFieldType[carrierType.Kind()]; ok {
  622. fd.FieldType = ft
  623. } else if carrierType.Kind() == reflect.String {
  624. fd.FieldType = types.FieldType_256byte // string 默认 256byte
  625. } else if carrierType.Kind() == reflect.Slice && carrierType.Elem().Kind() == reflect.Uint8 {
  626. fd.FieldType = types.FieldType_Dynamicbyte
  627. } else {
  628. return fd, nil, fmt.Errorf("unsupported type: %s", sf.Type.String())
  629. }
  630. }
  631. // 类型参数校验: 非法组合在建表前就报错, 不留半成品。
  632. if err := validateFieldTypeParam(fd, sf); err != nil {
  633. return fd, nil, err
  634. }
  635. return fd, idxDef, nil
  636. }
  637. // validateFieldTypeParam 校验"列类型 + Go 类型 + Flag"这个组合是否成立。
  638. //
  639. // 这些检查都发生在 UseTable 建表之前: 让模型一开始就被拒, 比等到插入时才在
  640. // 某一行上报错好 —— 后者会留下"表建好了、数据写不进"的半成品状态。
  641. func validateFieldTypeParam(fd FieldDef, sf reflect.StructField) error {
  642. // 主键列不能声明 NULL: pkID 是行内固定 8 字节, 没有"缺失"这一档。
  643. if fd.IsPK && fd.Flag&types.FlagNullable != 0 {
  644. return fmt.Errorf("主键列 %s 不能声明 nullable", fd.ColName)
  645. }
  646. // 载体类型 = 解一层指针 (与 parseField 的推断口径一致): 下面校验的都是"这一列装
  647. // 什么值", 而 `*T` 装的就是 T, 指针本身只是 NULL 的表示法。
  648. carrier := sf.Type
  649. if carrier.Kind() == reflect.Ptr {
  650. carrier = carrier.Elem()
  651. }
  652. switch fd.FieldType {
  653. case types.FieldType_Decimal:
  654. if scale := fd.Flag & types.FlagScaleMask; scale > types.MaxDecimalScale {
  655. return fmt.Errorf("decimal scale=%d 超过上限 %d", scale, types.MaxDecimalScale)
  656. }
  657. // decimal 必须有可解析的载体, 否则写入时才发现就晚了。
  658. if !decimalCarrier(carrier) {
  659. return fmt.Errorf("decimal 列需要 types.Decimal / string / 数值类型, 不能是 %s", sf.Type)
  660. }
  661. case types.FieldType_UUID:
  662. if !uuidCarrier(carrier) {
  663. return fmt.Errorf("uuid 列需要 types.UUID / [16]byte / string / []byte, 不能是 %s", sf.Type)
  664. }
  665. case types.FieldType_Int128:
  666. if !int128Carrier(carrier) {
  667. return fmt.Errorf("int128 列需要 big.Int / *big.Int / types.Decimal / 整数 / string, 不能是 %s", sf.Type)
  668. }
  669. case types.FieldType_Year:
  670. // 解码载体是 int16, 所以只收整型载体 —— time.Time 能写不能读回, 收它等于埋雷。
  671. switch carrier.Kind() {
  672. case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint16:
  673. default:
  674. return fmt.Errorf("year 列需要整数载体 (int/int16/int32/int64/uint16), 不能是 %s", sf.Type)
  675. }
  676. case types.FieldType_Float32:
  677. // 单精度列只收浮点载体: 装整数没有意义, 且会把"写进去的数被悄悄放宽"这件事
  678. // 藏起来。float64 载体是允许的 —— 引擎在写入时校验它能否被 float32 无损表示。
  679. switch carrier.Kind() {
  680. case reflect.Float32, reflect.Float64:
  681. default:
  682. return fmt.Errorf("float32 列需要 float32/float64 载体, 不能是 %s", sf.Type)
  683. }
  684. case types.FieldType_Float64:
  685. // 双精度列同样只收浮点载体。整数载体是必须拒的: 读回走 setFloatField →
  686. // target.SetInt(int64(val)), 一旦列里出现小数 (例如 Update().Set 写进去的),
  687. // 就会被静默砍掉 —— 正是 Float32 那条分支要防的同一件事。
  688. //
  689. // 这条分支存在的意义主要是"让 tag 可达": float64 字段本来就会自动推断成
  690. // Float64, 所以 type:float64 是显式/对称写法 (也给 float32 载体提供加宽),
  691. // 在此之前报错提示里列着 float32 却搜不到 float64, 读着像 bug。
  692. switch carrier.Kind() {
  693. case reflect.Float32, reflect.Float64:
  694. default:
  695. return fmt.Errorf("float64 列需要 float32/float64 载体, 不能是 %s", sf.Type)
  696. }
  697. case types.FieldType_Blob:
  698. // Blob 只认 []byte —— 这里【故意不收 string】。
  699. //
  700. // 读取路径 setFieldFromValue → setBytesField 对 string 载体是按"截断到第一个
  701. // NUL 字节"写的 (那是定长串/文本列的 CHAR(n) 语义)。Blob 装的是二进制,
  702. // 内容里出现 0x00 就会被静默截断 —— 写进去 8 字节读回来 3 字节, 且不报错。
  703. // 允许 string 等于亲手留一个静默丢数据的后门, 所以直接在建模阶段拒掉。
  704. // (要存文本用 Dynamicbyte / maxlen, 那些列的 string 载体语义是明确的。)
  705. if carrier.Kind() != reflect.Slice || carrier.Elem().Kind() != reflect.Uint8 {
  706. return fmt.Errorf("blob 列需要 []byte 载体, 不能是 %s (二进制里的 0x00 会被 string 载体静默截断)", sf.Type)
  707. }
  708. }
  709. return nil
  710. }
  711. // decimalCarrier Decimal 列的 Go 载体: types.Decimal / string / 任意数值。
  712. //
  713. // 三个 carrier 判定收的都是【解过指针的载体类型】(*types.Decimal 传进来的是
  714. // types.Decimal), 所以这里不用再判指针。
  715. func decimalCarrier(t reflect.Type) bool {
  716. if _, ok := goTypeToFieldType[t]; ok {
  717. return true
  718. }
  719. switch t.Kind() {
  720. case reflect.String,
  721. reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  722. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
  723. reflect.Float32, reflect.Float64:
  724. return true
  725. }
  726. return false
  727. }
  728. // uuidCarrier UUID 列的 Go 载体: types.UUID / [16]byte / string / []byte。
  729. func uuidCarrier(t reflect.Type) bool {
  730. if t == reflect.TypeOf(types.UUID{}) {
  731. return true
  732. }
  733. if t.Kind() == reflect.String {
  734. return true
  735. }
  736. if t.Kind() == reflect.Array && t.Len() == types.UUIDSize && t.Elem().Kind() == reflect.Uint8 {
  737. return true
  738. }
  739. return t.Kind() == reflect.Slice && t.Elem().Kind() == reflect.Uint8
  740. }
  741. // int128Carrier Int128 列的 Go 载体: big.Int / *big.Int / types.Decimal / 整数 / string。
  742. func int128Carrier(t reflect.Type) bool {
  743. if _, ok := goTypeToFieldType[t]; ok {
  744. return true
  745. }
  746. switch t.Kind() {
  747. case reflect.String,
  748. reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  749. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  750. return true
  751. }
  752. return false
  753. }
  754. // maxlenToColumn 把 `vrtx:"maxlen:N"` (单位: 字节) 映射成 (列类型, 列级宽度码)。
  755. //
  756. // 返回的 code 写进 FieldInfo.Flag 低 5 位, 由引擎按它决定行内宽度 (见 types.FieldSize)。
  757. // code == 0 表示"落动态列"。
  758. //
  759. // 【语义: 向上取到最近的档位】宽度码只有 24 档 (16,24,32,...,2048), 声明 200 字节能拿到
  760. // 224 字节档, 声明 10 只能拿到 16 字节档 —— 宁可贵几字节也不能向下取, 向下取会让合法值
  761. // 写不进去。真正的闸门是【档位宽度】(写入超过档位宽度会报错并报出该列容量), 不是 N 本身。
  762. //
  763. // 超过 types.MaxInlineColumnWidth (2048) 的声明退回动态列 (行内 6 字节堆引用):
  764. // 定长档位的存在意义是"这一列可索引、可定长比较", 而动态列两者都没有 (CreateIndex 会
  765. // 明确拒绝), 所以 2048 是"要索引就别超过它"的分界线。
  766. //
  767. // 【N 还有第二道上界】落动态列不等于"声明多大都行": 动态列的内容整体落在一个堆页里,
  768. // 单值上限是 types.MaxStringSize。声明超过它的列永远兑现不了, 所以这里直接报错, 而不是
  769. // 悄悄建成一条装不下那么多字节的列。这道闸门只能在这里 —— 动态列【不保留声明长度】
  770. // (Flag 低 5 位必须为 0), 一旦过了这层, N 就不在盘上了, 事后再无据可查。
  771. func maxlenToColumn(maxlen int) (types.FieldType, byte, error) {
  772. if err := types.CheckDeclaredStringSize(maxlen); err != nil {
  773. return 0, 0, err
  774. }
  775. code := types.WidthCodeFor(maxlen)
  776. if code == 0 {
  777. return types.FieldType_Dynamicbyte, 0, nil
  778. }
  779. return baseStringTypeFor(maxlen), code, nil
  780. }
  781. // baseStringTypeFor 选一个"类型名不离谱"的定长字符串基底类型。
  782. //
  783. // 字符串家族的行内宽度【统一由宽度码决定】, 基底类型只影响回显 (sqlTypeName / 类型名),
  784. // 所以这里取"默认宽度不超过声明值"的最大档: `maxlen:200` 显示成 256byte 而不是 16byte。
  785. func baseStringTypeFor(size int) types.FieldType {
  786. switch {
  787. case size <= 16:
  788. return types.FieldType_16byte
  789. case size <= 32:
  790. return types.FieldType_32byte
  791. case size <= 64:
  792. return types.FieldType_64byte
  793. case size <= 128:
  794. return types.FieldType_128byte
  795. default:
  796. return types.FieldType_256byte
  797. }
  798. }
  799. // parseTag 解析 vrtx tag: 按逗号分隔
  800. func parseTag(tag string) []string {
  801. var parts []string
  802. current := ""
  803. for _, c := range tag {
  804. if c == ',' {
  805. parts = append(parts, current)
  806. current = ""
  807. } else {
  808. current += string(c)
  809. }
  810. }
  811. if current != "" {
  812. parts = append(parts, current)
  813. }
  814. return parts
  815. }
  816. // GetIndexType 返回该模型应创建的一级索引类型
  817. func (m *ModelInfo) GetIndexType() page.IndexType {
  818. if m.IsTimeSeries {
  819. return page.IndexTypeTime
  820. }
  821. return page.IndexTypePrimary
  822. }