(dbPath string)
| 61 | } |
| 62 | |
| 63 | func (this *SQLiteFileListDB) Open(dbPath string) error { |
| 64 | this.dbPath = dbPath |
| 65 | |
| 66 | // 动态调整Cache值 |
| 67 | var cacheSize = 512 |
| 68 | var memoryGB = memutils.SystemMemoryGB() |
| 69 | if memoryGB >= 1 { |
| 70 | cacheSize = 256 * memoryGB |
| 71 | } |
| 72 | |
| 73 | // write db |
| 74 | // 这里不能加 EXCLUSIVE 锁,不然异步事务可能会失败 |
| 75 | writeDB, err := dbs.OpenWriter("file:" + dbPath + "?cache=private&mode=rwc&_journal_mode=WAL&_sync=" + dbs.SyncMode + "&_cache_size=" + types.String(cacheSize) + "&_secure_delete=FAST") |
| 76 | if err != nil { |
| 77 | return fmt.Errorf("open write database failed: %w", err) |
| 78 | } |
| 79 | |
| 80 | writeDB.SetMaxOpenConns(1) |
| 81 | |
| 82 | this.writeDB = writeDB |
| 83 | |
| 84 | // TODO 耗时过长,暂时不整理数据库 |
| 85 | // TODO 需要根据行数来判断是否VACUUM |
| 86 | // TODO 注意VACUUM反而可能让数据库文件变大 |
| 87 | /**_, err = db.Exec("VACUUM") |
| 88 | if err != nil { |
| 89 | return err |
| 90 | }**/ |
| 91 | |
| 92 | // 检查是否损坏 |
| 93 | // TODO 暂时屏蔽,因为用时过长 |
| 94 | |
| 95 | var recoverEnv, _ = os.LookupEnv("EdgeRecover") |
| 96 | if len(recoverEnv) > 0 && this.shouldRecover() { |
| 97 | for _, indexName := range []string{"staleAt", "hash"} { |
| 98 | _, _ = this.writeDB.Exec(`REINDEX "` + indexName + `"`) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if teaconst.EnableDBStat { |
| 103 | this.writeDB.EnableStat(true) |
| 104 | } |
| 105 | |
| 106 | // read db |
| 107 | readDB, err := dbs.OpenReader("file:" + dbPath + "?cache=private&mode=ro&_journal_mode=WAL&_sync=OFF&_cache_size=" + types.String(cacheSize)) |
| 108 | if err != nil { |
| 109 | return fmt.Errorf("open read database failed: %w", err) |
| 110 | } |
| 111 | |
| 112 | readDB.SetMaxOpenConns(runtime.NumCPU()) |
| 113 | |
| 114 | this.readDB = readDB |
| 115 | |
| 116 | if teaconst.EnableDBStat { |
| 117 | this.readDB.EnableStat(true) |
| 118 | } |
| 119 | |
| 120 | return nil |
no test coverage detected