MCPcopy Create free account
hub / github.com/GoEdgeLab/EdgeNode / Open

Method Open

internal/caches/list_file_db_sqlite.go:63–121  ·  view source on GitHub ↗
(dbPath string)

Source from the content-addressed store, hash-verified

61}
62
63func (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

Callers 1

loadHashMapMethod · 0.95

Calls 8

shouldRecoverMethod · 0.95
OpenWriterFunction · 0.92
OpenReaderFunction · 0.92
ErrorfMethod · 0.80
SetMaxOpenConnsMethod · 0.80
StringMethod · 0.45
ExecMethod · 0.45
EnableStatMethod · 0.45

Tested by

no test coverage detected