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

Method init

internal/iplibrary/ip_list_sqlite.go:51–179  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

49}
50
51func (this *SQLiteIPList) init() error {
52 // 检查目录是否存在
53 _, err := os.Stat(this.dir)
54 if err != nil {
55 err = os.MkdirAll(this.dir, 0777)
56 if err != nil {
57 return err
58 }
59 remotelogs.Println("IP_LIST_DB", "create data dir '"+this.dir+"'")
60 }
61
62 var path = this.dir + "/ip_list.db"
63
64 db, err := dbs.OpenWriter("file:" + path + "?cache=shared&mode=rwc&_journal_mode=WAL&_sync=" + dbs.SyncMode + "&_locking_mode=EXCLUSIVE")
65 if err != nil {
66 return err
67 }
68 db.SetMaxOpenConns(1)
69
70 //_, err = db.Exec("VACUUM")
71 //if err != nil {
72 // return err
73 //}
74
75 this.db = db
76
77 // 恢复数据库
78 var recoverEnv, _ = os.LookupEnv("EdgeRecover")
79 if len(recoverEnv) > 0 {
80 for _, indexName := range []string{"ip_list_itemId", "ip_list_expiredAt"} {
81 _, _ = db.Exec(`REINDEX "` + indexName + `"`)
82 }
83 }
84
85 // 初始化数据库
86 _, err = db.Exec(`CREATE TABLE IF NOT EXISTS "` + this.itemTableName + `" (
87 "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
88 "listId" integer DEFAULT 0,
89 "listType" varchar(32),
90 "isGlobal" integer(1) DEFAULT 0,
91 "type" varchar(16),
92 "itemId" integer DEFAULT 0,
93 "ipFrom" varchar(64) DEFAULT 0,
94 "ipTo" varchar(64) DEFAULT 0,
95 "expiredAt" integer DEFAULT 0,
96 "eventLevel" varchar(32),
97 "isDeleted" integer(1) DEFAULT 0,
98 "version" integer DEFAULT 0,
99 "nodeId" integer DEFAULT 0,
100 "serverId" integer DEFAULT 0
101);
102
103CREATE INDEX IF NOT EXISTS "ip_list_itemId"
104ON "` + this.itemTableName + `" (
105 "itemId" ASC
106);
107
108CREATE INDEX IF NOT EXISTS "ip_list_expiredAt"

Callers 1

NewSQLiteIPListFunction · 0.45

Calls 14

CloseMethod · 0.95
DeleteExpiredItemsMethod · 0.95
PrintlnFunction · 0.92
OpenWriterFunction · 0.92
NewFunction · 0.92
OnCloseFunction · 0.92
RunTickerFunction · 0.92
ErrorFunction · 0.92
SetMaxOpenConnsMethod · 0.80
StatMethod · 0.65
StopMethod · 0.65
ExecMethod · 0.45

Tested by

no test coverage detected