mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-24 03:06:38 +00:00
pre-processing
This commit is contained in:
17
management/server/types/network_map_helpers.go
Normal file
17
management/server/types/network_map_helpers.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
// SerializeNetworkMap serializes a NetworkMap to JSON
|
||||
func SerializeNetworkMap(nm *NetworkMap) ([]byte, error) {
|
||||
return json.Marshal(nm)
|
||||
}
|
||||
|
||||
// DeserializeNetworkMap deserializes JSON data into a NetworkMap
|
||||
func DeserializeNetworkMap(data []byte) (*NetworkMap, error) {
|
||||
var nm NetworkMap
|
||||
err := json.Unmarshal(data, &nm)
|
||||
return &nm, err
|
||||
}
|
||||
39
management/server/types/network_map_record.go
Normal file
39
management/server/types/network_map_record.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// NetworkMapRecord stores a precomputed network map for a peer
|
||||
// MapJSON is stored as jsonb (Postgres), json (MySQL), or text (SQLite)
|
||||
type NetworkMapRecord struct {
|
||||
PeerID string `gorm:"primaryKey"`
|
||||
AccountID string `gorm:"index"`
|
||||
MapJSON datatypes.JSON `gorm:"type:jsonb"` // GORM will use the right type for your DB
|
||||
Serial uint64
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// TableName sets the table name for GORM
|
||||
// This ensures the table is named consistently across all supported databases.
|
||||
func (NetworkMapRecord) TableName() string {
|
||||
return "network_map_records"
|
||||
}
|
||||
|
||||
// SaveNetworkMapRecord stores or updates a NetworkMapRecord in the database
|
||||
func SaveNetworkMapRecord(db *gorm.DB, record *NetworkMapRecord) error {
|
||||
return db.Save(record).Error
|
||||
}
|
||||
|
||||
// GetNetworkMapRecord retrieves a NetworkMapRecord by peer ID
|
||||
func GetNetworkMapRecord(db *gorm.DB, peerID string) (*NetworkMapRecord, error) {
|
||||
var record NetworkMapRecord
|
||||
err := db.First(&record, "peer_id = ?", peerID).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &record, nil
|
||||
}
|
||||
102
management/server/types/network_map_record_test.go
Normal file
102
management/server/types/network_map_record_test.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestNetworkMapRecordCRUD(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, db.AutoMigrate(&NetworkMapRecord{}))
|
||||
|
||||
record := &NetworkMapRecord{
|
||||
PeerID: "peer1",
|
||||
AccountID: "account1",
|
||||
MapJSON: []byte(`{"Peers":[],"Network":null}`),
|
||||
Serial: 1,
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
require.NoError(t, SaveNetworkMapRecord(db, record))
|
||||
|
||||
fetched, err := GetNetworkMapRecord(db, "peer1")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, record.PeerID, fetched.PeerID)
|
||||
require.Equal(t, record.AccountID, fetched.AccountID)
|
||||
require.Equal(t, record.Serial, fetched.Serial)
|
||||
require.Equal(t, record.MapJSON, fetched.MapJSON)
|
||||
}
|
||||
|
||||
// Simulate a normalized structure for comparison
|
||||
// In a real scenario, this would be split across multiple tables
|
||||
// Here, we just use a struct for benchmarking
|
||||
|
||||
type NormalizedPeer struct {
|
||||
ID string
|
||||
AccountID string
|
||||
Name string
|
||||
IP string
|
||||
}
|
||||
|
||||
type NormalizedNetworkMap struct {
|
||||
PeerID string
|
||||
Peers []NormalizedPeer
|
||||
Serial uint64
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func BenchmarkNetworkMapRecord_StoreAndRetrieve_JSON(b *testing.B) {
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
db.AutoMigrate(&NetworkMapRecord{})
|
||||
|
||||
record := &NetworkMapRecord{
|
||||
PeerID: "peer1",
|
||||
AccountID: "account1",
|
||||
MapJSON: []byte(`{"Peers":[{"ID":"p1","AccountID":"account1","Name":"peer1","IP":"10.0.0.1"}],"Network":null}`),
|
||||
Serial: 1,
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
record.Serial = uint64(i)
|
||||
record.UpdatedAt = time.Now()
|
||||
if err := SaveNetworkMapRecord(db, record); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
_, err := GetNetworkMapRecord(db, "peer1")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkNetworkMapRecord_StoreAndRetrieve_Normalized(b *testing.B) {
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
db.AutoMigrate(&NormalizedPeer{})
|
||||
|
||||
peers := []NormalizedPeer{{ID: "p1", AccountID: "account1", Name: "peer1", IP: "10.0.0.1"}}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, peer := range peers {
|
||||
if err := db.Save(&peer).Error; err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
var fetched []NormalizedPeer
|
||||
if err := db.Find(&fetched, "account_id = ?", "account1").Error; err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user