初始化
(times int)
| 491 | |
| 492 | // 初始化 |
| 493 | func (this *SQLiteFileListDB) initTables(times int) error { |
| 494 | { |
| 495 | // expiredAt - 过期时间,用来判断有无过期 |
| 496 | // staleAt - 过时缓存最大时间,用来清理缓存 |
| 497 | // 不对 hash 增加 unique 参数,是尽可能避免产生 malformed 错误 |
| 498 | _, err := this.writeDB.Exec(`CREATE TABLE IF NOT EXISTS "` + this.itemsTableName + `" ( |
| 499 | "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, |
| 500 | "hash" varchar(32), |
| 501 | "key" varchar(1024), |
| 502 | "tag" varchar(64), |
| 503 | "headerSize" integer DEFAULT 0, |
| 504 | "bodySize" integer DEFAULT 0, |
| 505 | "metaSize" integer DEFAULT 0, |
| 506 | "expiredAt" integer DEFAULT 0, |
| 507 | "staleAt" integer DEFAULT 0, |
| 508 | "createdAt" integer DEFAULT 0, |
| 509 | "host" varchar(128), |
| 510 | "serverId" integer |
| 511 | ); |
| 512 | |
| 513 | DROP INDEX IF EXISTS "createdAt"; |
| 514 | DROP INDEX IF EXISTS "expiredAt"; |
| 515 | DROP INDEX IF EXISTS "serverId"; |
| 516 | |
| 517 | CREATE INDEX IF NOT EXISTS "staleAt" |
| 518 | ON "` + this.itemsTableName + `" ( |
| 519 | "staleAt" ASC |
| 520 | ); |
| 521 | |
| 522 | CREATE INDEX IF NOT EXISTS "hash" |
| 523 | ON "` + this.itemsTableName + `" ( |
| 524 | "hash" ASC |
| 525 | ); |
| 526 | `) |
| 527 | |
| 528 | if err != nil { |
| 529 | // 忽略可以预期的错误 |
| 530 | if strings.Contains(err.Error(), "duplicate column name") { |
| 531 | err = nil |
| 532 | } |
| 533 | |
| 534 | // 尝试删除重建 |
| 535 | if err != nil { |
| 536 | if times < 3 { |
| 537 | _, dropErr := this.writeDB.Exec(`DROP TABLE "` + this.itemsTableName + `"`) |
| 538 | if dropErr == nil { |
| 539 | return this.initTables(times + 1) |
| 540 | } |
| 541 | return this.WrapError(err) |
| 542 | } |
| 543 | |
| 544 | return this.WrapError(err) |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | // 删除hits表 |
| 550 | { |