types.go 38 KB

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