mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-06 18:10:26 +08:00
优化代码
This commit is contained in:
@@ -15,7 +15,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IPListKV struct {
|
type KVIPList struct {
|
||||||
ipTable *kvstore.Table[*pb.IPItem]
|
ipTable *kvstore.Table[*pb.IPItem]
|
||||||
versionsTable *kvstore.Table[int64]
|
versionsTable *kvstore.Table[int64]
|
||||||
|
|
||||||
@@ -28,8 +28,8 @@ type IPListKV struct {
|
|||||||
offsetItemKey string
|
offsetItemKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIPListKV() (*IPListKV, error) {
|
func NewKVIPList() (*KVIPList, error) {
|
||||||
var db = &IPListKV{
|
var db = &KVIPList{
|
||||||
cleanTicker: time.NewTicker(24 * time.Hour),
|
cleanTicker: time.NewTicker(24 * time.Hour),
|
||||||
encoder: &IPItemEncoder[*pb.IPItem]{},
|
encoder: &IPItemEncoder[*pb.IPItem]{},
|
||||||
}
|
}
|
||||||
@@ -37,7 +37,7 @@ func NewIPListKV() (*IPListKV, error) {
|
|||||||
return db, err
|
return db, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IPListKV) init() error {
|
func (this *KVIPList) init() error {
|
||||||
store, storeErr := kvstore.DefaultStore()
|
store, storeErr := kvstore.DefaultStore()
|
||||||
if storeErr != nil {
|
if storeErr != nil {
|
||||||
return storeErr
|
return storeErr
|
||||||
@@ -89,12 +89,12 @@ func (this *IPListKV) init() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Name 数据库名称代号
|
// Name 数据库名称代号
|
||||||
func (this *IPListKV) Name() string {
|
func (this *KVIPList) Name() string {
|
||||||
return "kvstore"
|
return "kvstore"
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteExpiredItems 删除过期的条目
|
// DeleteExpiredItems 删除过期的条目
|
||||||
func (this *IPListKV) DeleteExpiredItems() error {
|
func (this *KVIPList) DeleteExpiredItems() error {
|
||||||
if this.isClosed {
|
if this.isClosed {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -134,7 +134,7 @@ func (this *IPListKV) DeleteExpiredItems() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IPListKV) AddItem(item *pb.IPItem) error {
|
func (this *KVIPList) AddItem(item *pb.IPItem) error {
|
||||||
if this.isClosed {
|
if this.isClosed {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -159,7 +159,7 @@ func (this *IPListKV) AddItem(item *pb.IPItem) error {
|
|||||||
return this.UpdateMaxVersion(item.Version)
|
return this.UpdateMaxVersion(item.Version)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IPListKV) ReadItems(offset int64, size int64) (items []*pb.IPItem, goNextLoop bool, err error) {
|
func (this *KVIPList) ReadItems(offset int64, size int64) (items []*pb.IPItem, goNextLoop bool, err error) {
|
||||||
if this.isClosed {
|
if this.isClosed {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -181,7 +181,7 @@ func (this *IPListKV) ReadItems(offset int64, size int64) (items []*pb.IPItem, g
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReadMaxVersion 读取当前最大版本号
|
// ReadMaxVersion 读取当前最大版本号
|
||||||
func (this *IPListKV) ReadMaxVersion() (int64, error) {
|
func (this *KVIPList) ReadMaxVersion() (int64, error) {
|
||||||
if this.isClosed {
|
if this.isClosed {
|
||||||
return 0, errors.New("database has been closed")
|
return 0, errors.New("database has been closed")
|
||||||
}
|
}
|
||||||
@@ -197,15 +197,15 @@ func (this *IPListKV) ReadMaxVersion() (int64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateMaxVersion 修改版本号
|
// UpdateMaxVersion 修改版本号
|
||||||
func (this *IPListKV) UpdateMaxVersion(version int64) error {
|
func (this *KVIPList) UpdateMaxVersion(version int64) error {
|
||||||
if this.isClosed {
|
if this.isClosed {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.versionsTable.SetSync("version", version)
|
return this.versionsTable.Set("version", version)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IPListKV) TestInspect(t *testing.T) error {
|
func (this *KVIPList) TestInspect(t *testing.T) error {
|
||||||
return this.ipTable.
|
return this.ipTable.
|
||||||
Query().
|
Query().
|
||||||
FindAll(func(tx *kvstore.Tx[*pb.IPItem], item kvstore.Item[*pb.IPItem]) (goNext bool, err error) {
|
FindAll(func(tx *kvstore.Tx[*pb.IPItem], item kvstore.Item[*pb.IPItem]) (goNext bool, err error) {
|
||||||
@@ -219,11 +219,11 @@ func (this *IPListKV) TestInspect(t *testing.T) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Flush to disk
|
// Flush to disk
|
||||||
func (this *IPListKV) Flush() error {
|
func (this *KVIPList) Flush() error {
|
||||||
return this.ipTable.DB().Store().Flush()
|
return this.ipTable.DB().Store().Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IPListKV) Close() error {
|
func (this *KVIPList) Close() error {
|
||||||
this.isClosed = true
|
this.isClosed = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIPListKV_AddItem(t *testing.T) {
|
func TestKVIPList_AddItem(t *testing.T) {
|
||||||
kv, err := iplibrary.NewIPListKV()
|
kv, err := iplibrary.NewKVIPList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -71,8 +71,8 @@ func TestIPListKV_AddItem(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIPListKV_AddItems_Many(t *testing.T) {
|
func TestKVIPList_AddItems_Many(t *testing.T) {
|
||||||
kv, err := iplibrary.NewIPListKV()
|
kv, err := iplibrary.NewKVIPList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -109,8 +109,8 @@ func TestIPListKV_AddItems_Many(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIPListKV_DeleteExpiredItems(t *testing.T) {
|
func TestKVIPList_DeleteExpiredItems(t *testing.T) {
|
||||||
kv, err := iplibrary.NewIPListKV()
|
kv, err := iplibrary.NewKVIPList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -125,8 +125,8 @@ func TestIPListKV_DeleteExpiredItems(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIPListKV_UpdateMaxVersion(t *testing.T) {
|
func TestKVIPList_UpdateMaxVersion(t *testing.T) {
|
||||||
kv, err := iplibrary.NewIPListKV()
|
kv, err := iplibrary.NewKVIPList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -148,8 +148,8 @@ func TestIPListKV_UpdateMaxVersion(t *testing.T) {
|
|||||||
t.Log("version:", maxVersion)
|
t.Log("version:", maxVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIPListKV_ReadMaxVersion(t *testing.T) {
|
func TestKVIPList_ReadMaxVersion(t *testing.T) {
|
||||||
kv, err := iplibrary.NewIPListKV()
|
kv, err := iplibrary.NewKVIPList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -162,8 +162,8 @@ func TestIPListKV_ReadMaxVersion(t *testing.T) {
|
|||||||
t.Log("version:", maxVersion)
|
t.Log("version:", maxVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIPListKV_ReadItems(t *testing.T) {
|
func TestKVIPList_ReadItems(t *testing.T) {
|
||||||
kv, err := iplibrary.NewIPListKV()
|
kv, err := iplibrary.NewKVIPList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -184,8 +184,8 @@ func TestIPListKV_ReadItems(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIPListKV_CountItems(t *testing.T) {
|
func TestKVIPList_CountItems(t *testing.T) {
|
||||||
kv, err := iplibrary.NewIPListKV()
|
kv, err := iplibrary.NewKVIPList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -209,8 +209,8 @@ func TestIPListKV_CountItems(t *testing.T) {
|
|||||||
t.Log("count:", count, "len:", len(m))
|
t.Log("count:", count, "len:", len(m))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIPListKV_Inspect(t *testing.T) {
|
func TestKVIPList_Inspect(t *testing.T) {
|
||||||
kv, err := iplibrary.NewIPListKV()
|
kv, err := iplibrary.NewKVIPList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IPListSQLite struct {
|
type SQLiteIPList struct {
|
||||||
db *dbs.DB
|
db *dbs.DB
|
||||||
|
|
||||||
itemTableName string
|
itemTableName string
|
||||||
@@ -36,8 +36,8 @@ type IPListSQLite struct {
|
|||||||
isClosed bool
|
isClosed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIPListSqlite() (*IPListSQLite, error) {
|
func NewSQLiteIPList() (*SQLiteIPList, error) {
|
||||||
var db = &IPListSQLite{
|
var db = &SQLiteIPList{
|
||||||
itemTableName: "ipItems",
|
itemTableName: "ipItems",
|
||||||
versionTableName: "versions",
|
versionTableName: "versions",
|
||||||
dir: filepath.Clean(Tea.Root + "/data"),
|
dir: filepath.Clean(Tea.Root + "/data"),
|
||||||
@@ -47,7 +47,7 @@ func NewIPListSqlite() (*IPListSQLite, error) {
|
|||||||
return db, err
|
return db, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IPListSQLite) init() error {
|
func (this *SQLiteIPList) init() error {
|
||||||
// 检查目录是否存在
|
// 检查目录是否存在
|
||||||
_, err := os.Stat(this.dir)
|
_, err := os.Stat(this.dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -178,12 +178,12 @@ ON "` + this.itemTableName + `" (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Name 数据库名称代号
|
// Name 数据库名称代号
|
||||||
func (this *IPListSQLite) Name() string {
|
func (this *SQLiteIPList) Name() string {
|
||||||
return "sqlite"
|
return "sqlite"
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteExpiredItems 删除过期的条目
|
// DeleteExpiredItems 删除过期的条目
|
||||||
func (this *IPListSQLite) DeleteExpiredItems() error {
|
func (this *SQLiteIPList) DeleteExpiredItems() error {
|
||||||
if this.isClosed {
|
if this.isClosed {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -192,7 +192,7 @@ func (this *IPListSQLite) DeleteExpiredItems() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IPListSQLite) AddItem(item *pb.IPItem) error {
|
func (this *SQLiteIPList) AddItem(item *pb.IPItem) error {
|
||||||
if this.isClosed {
|
if this.isClosed {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -215,7 +215,7 @@ func (this *IPListSQLite) AddItem(item *pb.IPItem) error {
|
|||||||
return this.UpdateMaxVersion(item.Version)
|
return this.UpdateMaxVersion(item.Version)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IPListSQLite) ReadItems(offset int64, size int64) (items []*pb.IPItem, goNext bool, err error) {
|
func (this *SQLiteIPList) ReadItems(offset int64, size int64) (items []*pb.IPItem, goNext bool, err error) {
|
||||||
if this.isClosed {
|
if this.isClosed {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -243,7 +243,7 @@ func (this *IPListSQLite) ReadItems(offset int64, size int64) (items []*pb.IPIte
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReadMaxVersion 读取当前最大版本号
|
// ReadMaxVersion 读取当前最大版本号
|
||||||
func (this *IPListSQLite) ReadMaxVersion() (int64, error) {
|
func (this *SQLiteIPList) ReadMaxVersion() (int64, error) {
|
||||||
if this.isClosed {
|
if this.isClosed {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
@@ -278,7 +278,7 @@ func (this *IPListSQLite) ReadMaxVersion() (int64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateMaxVersion 修改版本号
|
// UpdateMaxVersion 修改版本号
|
||||||
func (this *IPListSQLite) UpdateMaxVersion(version int64) error {
|
func (this *SQLiteIPList) UpdateMaxVersion(version int64) error {
|
||||||
if this.isClosed {
|
if this.isClosed {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -287,7 +287,7 @@ func (this *IPListSQLite) UpdateMaxVersion(version int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IPListSQLite) Close() error {
|
func (this *SQLiteIPList) Close() error {
|
||||||
this.isClosed = true
|
this.isClosed = true
|
||||||
|
|
||||||
if this.db != nil {
|
if this.db != nil {
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIPListDB_AddItem(t *testing.T) {
|
func TestSQLiteIPList_AddItem(t *testing.T) {
|
||||||
db, err := iplibrary.NewIPListSqlite()
|
db, err := iplibrary.NewSQLiteIPList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -58,8 +58,8 @@ func TestIPListDB_AddItem(t *testing.T) {
|
|||||||
t.Log("ok")
|
t.Log("ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIPListDB_ReadItems(t *testing.T) {
|
func TestSQLiteIPList_ReadItems(t *testing.T) {
|
||||||
db, err := iplibrary.NewIPListSqlite()
|
db, err := iplibrary.NewSQLiteIPList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -79,8 +79,8 @@ func TestIPListDB_ReadItems(t *testing.T) {
|
|||||||
logs.PrintAsJSON(items, t)
|
logs.PrintAsJSON(items, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIPListDB_ReadMaxVersion(t *testing.T) {
|
func TestSQLiteIPList_ReadMaxVersion(t *testing.T) {
|
||||||
db, err := iplibrary.NewIPListSqlite()
|
db, err := iplibrary.NewSQLiteIPList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -90,8 +90,8 @@ func TestIPListDB_ReadMaxVersion(t *testing.T) {
|
|||||||
t.Log(db.ReadMaxVersion())
|
t.Log(db.ReadMaxVersion())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIPListDB_UpdateMaxVersion(t *testing.T) {
|
func TestSQLiteIPList_UpdateMaxVersion(t *testing.T) {
|
||||||
db, err := iplibrary.NewIPListSqlite()
|
db, err := iplibrary.NewSQLiteIPList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,9 +120,9 @@ func (this *IPListManager) init() {
|
|||||||
var db IPListDB
|
var db IPListDB
|
||||||
var err error
|
var err error
|
||||||
if sqliteErr == nil {
|
if sqliteErr == nil {
|
||||||
db, err = NewIPListSqlite()
|
db, err = NewSQLiteIPList()
|
||||||
} else {
|
} else {
|
||||||
db, err = NewIPListKV()
|
db, err = NewKVIPList()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user