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