Documentation
¶
Index ¶
- Constants
- func ClassifyBucket(name string) (category string, clearable bool)
- func DBDiskSize(path string) (int64, error)
- func ExportDBAsTarGz(dbPath, entryName, archiveType string) ([]byte, string, error)
- func FetchRemoteConfigArchive(host string, port int, token string, useHTTPS bool) ([]byte, string, error)
- func IsBackendInstalled(dataDir string) bool
- func IsConfigBucket(name string) bool
- func ReadBackendChoice(dataDir string) string
- func WriteBackendChoice(dataDir, backend string) error
- type ArchiveManifest
- type BucketStats
- type ConfigExport
- type ConfigImportResult
- type ConfigStore
- func (cs *ConfigStore) DeleteDevice(deviceID string) error
- func (cs *ConfigStore) ExportAllConfig() (*ConfigExport, error)
- func (cs *ConfigStore) HasConfigData() (bool, error)
- func (cs *ConfigStore) HasUsers() (bool, error)
- func (cs *ConfigStore) ImportConfig(export *ConfigExport) error
- func (cs *ConfigStore) ImportConfigReplace(export *ConfigExport, opts ImportPreserveOptions) error
- func (cs *ConfigStore) IsSystemInitialized() (bool, error)
- func (cs *ConfigStore) LoadAICopilotSettings() (*model.AICopilotSettings, error)
- func (cs *ConfigStore) LoadAllDevices() (map[string]model.Device, error)
- func (cs *ConfigStore) LoadChannels() ([]model.Channel, error)
- func (cs *ConfigStore) LoadDevice(deviceID string) (*model.Device, error)
- func (cs *ConfigStore) LoadEdgeRules() ([]model.EdgeRule, error)
- func (cs *ConfigStore) LoadNorthbound() (*model.NorthboundConfig, error)
- func (cs *ConfigStore) LoadServerConfig() (*model.ServerConfig, error)
- func (cs *ConfigStore) LoadSystem() (*model.SystemConfig, error)
- func (cs *ConfigStore) LoadUsers() ([]model.UserConfig, error)
- func (cs *ConfigStore) LoadVirtualShadows() ([]model.VirtualShadowDeviceConfig, error)
- func (cs *ConfigStore) SaveAICopilotSettings(settings model.AICopilotSettings) error
- func (cs *ConfigStore) SaveAllConfig(server model.ServerConfig, channels []model.Channel, devices []model.Device, ...) error
- func (cs *ConfigStore) SaveChannels(channels []model.Channel) error
- func (cs *ConfigStore) SaveDevice(device model.Device) error
- func (cs *ConfigStore) SaveEdgeRules(rules []model.EdgeRule) error
- func (cs *ConfigStore) SaveNorthbound(config model.NorthboundConfig) error
- func (cs *ConfigStore) SaveServerConfig(config model.ServerConfig) error
- func (cs *ConfigStore) SaveSystem(config model.SystemConfig) error
- func (cs *ConfigStore) SaveUsers(users []model.UserConfig) error
- func (cs *ConfigStore) SaveVirtualShadows(devices []model.VirtualShadowDeviceConfig) error
- type ImportArchiveOptions
- type ImportPreserveOptions
- type KVCursor
- type KVDB
- type KVTx
- type OfflineMessage
- type Storage
- func (s *Storage) Backend() string
- func (s *Storage) BackupConfigDB(backupDir string) (*model.BackupInfo, error)
- func (s *Storage) BackupRuntimeDB(backupDir string) (*model.BackupInfo, error)
- func (s *Storage) ClearAllRuntimeBuckets() ([]string, error)
- func (s *Storage) ClearBucket(bucketName string) error
- func (s *Storage) Close() error
- func (s *Storage) CompactRuntimeDB() error
- func (s *Storage) DeleteData(bucketName string, key string) error
- func (s *Storage) ExportConfigDBArchive() ([]byte, string, error)
- func (s *Storage) ExportRuntimeDBArchive() ([]byte, string, error)
- func (s *Storage) GetAllValues() (map[string]model.Value, error)
- func (s *Storage) GetBucketStats() ([]BucketStats, int64, error)
- func (s *Storage) GetConfigDB() KVDB
- func (s *Storage) GetConfigPath() string
- func (s *Storage) GetDB() KVDB
- func (s *Storage) GetData(bucketName string, key string, result interface{}) error
- func (s *Storage) GetLastValue(pointID string) (*model.Value, error)
- func (s *Storage) GetOfflineMessages(configID string, limit int) ([]OfflineMessage, error)
- func (s *Storage) GetPath() string
- func (s *Storage) GetRuntimePath() string
- func (s *Storage) ImportConfigDBArchive(archiveData []byte, opts ImportArchiveOptions) (*ConfigImportResult, error)
- func (s *Storage) ImportConfigDBData(data []byte, opts ImportArchiveOptions) (*ConfigImportResult, error)
- func (s *Storage) LoadAll(bucketName string, callback func(k, v []byte) error) error
- func (s *Storage) LoadLatest(bucketName string, limit int, callback func(k, v []byte) error) error
- func (s *Storage) LoadRange(bucketName string, minKey, maxKey string, callback func(k, v []byte) error) error
- func (s *Storage) PruneOldest(bucketName string, maxRecords int) error
- func (s *Storage) PullRemoteConfigAndImport(host string, port int, token string, useHTTPS, overwriteUsers bool) (*ConfigImportResult, error)
- func (s *Storage) RemoveOfflineMessage(key string) error
- func (s *Storage) SaveData(bucketName string, key string, data interface{}) error
- func (s *Storage) SaveOfflineMessage(configID string, data []byte, maxCount int) error
- func (s *Storage) SaveValue(val model.Value) error
- func (s *Storage) SyncConfigDB() error
Constants ¶
const ( BucketValues = "values" BucketRuleState = "RuleState" BucketDataCache = "DataCache" BucketWindow = "WindowData" BucketNorthboundCache = "NorthboundCache" )
const ( BucketConfigVersion = "ConfigVersion" BucketChannels = "Channels" BucketDevices = "Devices" BucketNorthbound = "Northbound" BucketEdgeRules = "EdgeRules" BucketSystem = "System" BucketUsers = "Users" BucketServer = "Server" BucketVirtualShadows = "VirtualShadows" BucketAICopilot = "ai_copilot" ConfigVersionKey = "version" ConfigVersionValue = "1.0" )
const ( ArchiveManifestName = "manifest.json" ConfigDBEntryName = "config.db" ConfigDBEntrySQLite = "config.sqlite" RuntimeDBEntryName = "runtime.db" RuntimeDBEntrySQLite = "runtime.sqlite" )
const ( BackendBbolt = "bbolt" BackendSQLite = "sqlite" BackendMetaFile = "storage.meta" )
存储后端常量与 meta 文件名(安装页选择、启动时恢复)。
Variables ¶
This section is empty.
Functions ¶
func ClassifyBucket ¶
func DBDiskSize ¶ added in v0.1.3
DBDiskSize 供外部(HTTP 层)统计数据库实际磁盘占用,含 sqlite 的 WAL/SHM 附件。
func ExportDBAsTarGz ¶
ExportDBAsTarGz exports a database file as a tar.gz archive. Used when the database file is not already open (e.g. tests). 按文件头魔数自动识别 bbolt / SQLite 格式,与本地后端无关。
func FetchRemoteConfigArchive ¶
func FetchRemoteConfigArchive(host string, port int, token string, useHTTPS bool) ([]byte, string, error)
FetchRemoteConfigArchive downloads a config tar.gz from a remote edgeCore gateway.
func IsBackendInstalled ¶ added in v0.1.3
IsBackendInstalled 判断 dataDir 下是否已存在某个后端的配置库 (安装模式检测:首次安装时两者都不存在)。
func IsConfigBucket ¶
func ReadBackendChoice ¶ added in v0.1.3
ReadBackendChoice 读取已保存的后端选择;无记录时返回默认 bbolt。
func WriteBackendChoice ¶ added in v0.1.3
WriteBackendChoice 把后端选择写入 dataDir/storage.meta(启动时读取恢复)。
Types ¶
type ArchiveManifest ¶
type ArchiveManifest struct {
Type string `json:"type"`
Version string `json:"version"`
ExportTime time.Time `json:"export_time"`
SourcePath string `json:"source_path,omitempty"`
}
ArchiveManifest describes export package metadata.
type BucketStats ¶
type ConfigExport ¶
type ConfigExport struct {
Server model.ServerConfig `json:"server"`
Channels []model.Channel `json:"channels"`
Devices map[string]model.Device `json:"devices"`
Northbound model.NorthboundConfig `json:"northbound"`
EdgeRules []model.EdgeRule `json:"edge_rules"`
System model.SystemConfig `json:"system"`
Users []model.UserConfig `json:"users"`
VirtualShadows []model.VirtualShadowDeviceConfig `json:"virtual_shadows"`
}
type ConfigImportResult ¶
type ConfigImportResult struct {
ForceOverwrite bool `json:"force_overwrite"`
PreservedUsers bool `json:"preserved_users"`
PreservedPort int `json:"preserved_port"`
DeviceCount int `json:"device_count"`
ChannelCount int `json:"channel_count"`
RemoteSource string `json:"remote_source,omitempty"`
}
ConfigImportResult summarizes a config database import operation.
type ConfigStore ¶
type ConfigStore struct {
// contains filtered or unexported fields
}
func NewConfigStore ¶
func NewConfigStore(db KVDB) (*ConfigStore, error)
NewConfigStore 基于 KVDB 后端构建配置仓库(bbolt / SQLite 双后端共用)。
func (*ConfigStore) DeleteDevice ¶
func (cs *ConfigStore) DeleteDevice(deviceID string) error
func (*ConfigStore) ExportAllConfig ¶
func (cs *ConfigStore) ExportAllConfig() (*ConfigExport, error)
func (*ConfigStore) HasConfigData ¶
func (cs *ConfigStore) HasConfigData() (bool, error)
func (*ConfigStore) HasUsers ¶
func (cs *ConfigStore) HasUsers() (bool, error)
func (*ConfigStore) ImportConfig ¶
func (cs *ConfigStore) ImportConfig(export *ConfigExport) error
func (*ConfigStore) ImportConfigReplace ¶
func (cs *ConfigStore) ImportConfigReplace(export *ConfigExport, opts ImportPreserveOptions) error
ImportConfigReplace replaces configuration buckets from export data. When preserve options are set, current users and/or server port are kept.
func (*ConfigStore) IsSystemInitialized ¶
func (cs *ConfigStore) IsSystemInitialized() (bool, error)
func (*ConfigStore) LoadAICopilotSettings ¶
func (cs *ConfigStore) LoadAICopilotSettings() (*model.AICopilotSettings, error)
func (*ConfigStore) LoadAllDevices ¶
func (cs *ConfigStore) LoadAllDevices() (map[string]model.Device, error)
func (*ConfigStore) LoadChannels ¶
func (cs *ConfigStore) LoadChannels() ([]model.Channel, error)
func (*ConfigStore) LoadDevice ¶
func (cs *ConfigStore) LoadDevice(deviceID string) (*model.Device, error)
func (*ConfigStore) LoadEdgeRules ¶
func (cs *ConfigStore) LoadEdgeRules() ([]model.EdgeRule, error)
func (*ConfigStore) LoadNorthbound ¶
func (cs *ConfigStore) LoadNorthbound() (*model.NorthboundConfig, error)
func (*ConfigStore) LoadServerConfig ¶
func (cs *ConfigStore) LoadServerConfig() (*model.ServerConfig, error)
func (*ConfigStore) LoadSystem ¶
func (cs *ConfigStore) LoadSystem() (*model.SystemConfig, error)
func (*ConfigStore) LoadUsers ¶
func (cs *ConfigStore) LoadUsers() ([]model.UserConfig, error)
func (*ConfigStore) LoadVirtualShadows ¶
func (cs *ConfigStore) LoadVirtualShadows() ([]model.VirtualShadowDeviceConfig, error)
func (*ConfigStore) SaveAICopilotSettings ¶
func (cs *ConfigStore) SaveAICopilotSettings(settings model.AICopilotSettings) error
func (*ConfigStore) SaveAllConfig ¶
func (cs *ConfigStore) SaveAllConfig(server model.ServerConfig, channels []model.Channel, devices []model.Device, northbound model.NorthboundConfig, edgeRules []model.EdgeRule, system model.SystemConfig, users []model.UserConfig) error
func (*ConfigStore) SaveChannels ¶
func (cs *ConfigStore) SaveChannels(channels []model.Channel) error
func (*ConfigStore) SaveDevice ¶
func (cs *ConfigStore) SaveDevice(device model.Device) error
func (*ConfigStore) SaveEdgeRules ¶
func (cs *ConfigStore) SaveEdgeRules(rules []model.EdgeRule) error
func (*ConfigStore) SaveNorthbound ¶
func (cs *ConfigStore) SaveNorthbound(config model.NorthboundConfig) error
func (*ConfigStore) SaveServerConfig ¶
func (cs *ConfigStore) SaveServerConfig(config model.ServerConfig) error
func (*ConfigStore) SaveSystem ¶
func (cs *ConfigStore) SaveSystem(config model.SystemConfig) error
func (*ConfigStore) SaveUsers ¶
func (cs *ConfigStore) SaveUsers(users []model.UserConfig) error
func (*ConfigStore) SaveVirtualShadows ¶
func (cs *ConfigStore) SaveVirtualShadows(devices []model.VirtualShadowDeviceConfig) error
type ImportArchiveOptions ¶
type ImportArchiveOptions struct {
ForceOverwrite bool
}
ImportArchiveOptions controls import behavior.
type ImportPreserveOptions ¶
ImportPreserveOptions controls fields preserved during full config import.
type KVCursor ¶ added in v0.1.3
type KVCursor interface {
First() (k, v []byte)
Last() (k, v []byte)
Next() (k, v []byte)
Prev() (k, v []byte)
// Seek 定位到第一个 >= prefix 的键。
Seek(prefix []byte) (k, v []byte)
// Delete 删除当前键(迭代安全)。
Delete() error
}
KVCursor 是 bucket 内的字节序游标。 约定:迭代方向一致使用(正向 First/Next/Seek,反向 Last/Prev), 混用方向时行为由后端定义(bbolt 原生允许,sqlite 实现会重开流)。
type KVDB ¶ added in v0.1.3
type KVDB interface {
// View 执行只读事务。
View(fn func(tx KVTx) error) error
// Update 执行可写事务(提交或整体回滚)。
Update(fn func(tx KVTx) error) error
Close() error
Path() string
// Backend 返回后端标识:"bbolt" 或 "sqlite"。
Backend() string
// Sync 强制将已提交数据落盘(bbolt: db.Sync;sqlite: WAL checkpoint FULL)。
Sync() error
// Compact 回收已删除数据占用的空间。
// bbolt:整文件重写并原子换绑(内部处理 rename 与重开);
// sqlite:VACUUM 原地重建。
Compact() error
// BackupTo 将数据库的事务一致性快照写入 dstPath(热备,不阻塞读写)。
// bbolt: tx.WriteTo;sqlite: VACUUM INTO。
BackupTo(dstPath string) error
}
KVDB 是配置/运行时存储后端的统一抽象(2026-10-13 引入)。 现有两种实现:
- bboltKV — go.etcd.io/bbolt(B+树 mmap,默认后端)
- sqliteKV — modernc.org/sqlite(纯 Go SQLite,安装页可选)
语义对齐 bbolt:
- View 只读事务可并发;Update 可写事务由后端保证全局串行;
- key 按字节序(memcmp)排序;
- Get 对缺失 key 返回 nil、对缺失 bucket 返回 nil;
- 对缺失 bucket 遍历(ForEach/Cursor)视为空集合;
- Put 前调用方需保证 bucket 存在(CreateBucketIfNotExists), 否则返回 "bucket not found" 类错误。
type KVTx ¶ added in v0.1.3
type KVTx interface {
// Get 返回 key 对应值的副本;缺失返回 nil。
Get(bucket string, key []byte) []byte
Put(bucket string, key []byte, value []byte) error
Delete(bucket string, key []byte) error
CreateBucketIfNotExists(bucket string) error
// DeleteBucket 删除 bucket 及其全部数据(bucket 不存在返回错误)。
DeleteBucket(bucket string) error
// Buckets 返回全部 bucket 名(字节序)。
Buckets() ([]string, error)
// HasBucket 判断 bucket 是否已创建(空 bucket 也算存在)。
HasBucket(bucket string) bool
// ForEach 遍历 bucket 内全部键值(字节序);bucket 缺失视为空。
ForEach(bucket string, fn func(k, v []byte) error) error
// Cursor 获取 bucket 游标(同向迭代:First/Next 或 Last/Prev/Seek)。
Cursor(bucket string) KVCursor
// CountKeys 返回 bucket 内 key 数量。
CountKeys(bucket string) (int, error)
}
KVTx 是单个事务内的 bucket 型 KV 操作集合。
type OfflineMessage ¶
type Storage ¶
type Storage struct {
// contains filtered or unexported fields
}
func NewStorage ¶
NewStorage 以默认后端(bbolt)打开存储(保持既有调用方/测试兼容)。
func NewStorageWithBackend ¶ added in v0.1.3
NewStorageWithBackend 以指定后端打开存储。backend ∈ {"bbolt","sqlite"}。 选择结果写入 data/storage.meta,供后续启动恢复同一后端。
func (*Storage) BackupConfigDB ¶
func (s *Storage) BackupConfigDB(backupDir string) (*model.BackupInfo, error)
BackupConfigDB 备份配置数据库到指定目录。 配置库优先备份,不包含运行时数据。
func (*Storage) BackupRuntimeDB ¶
func (s *Storage) BackupRuntimeDB(backupDir string) (*model.BackupInfo, error)
BackupRuntimeDB 备份运行时数据库到指定目录(可选诊断用途)。
func (*Storage) ClearAllRuntimeBuckets ¶
ClearAllRuntimeBuckets 清空 runtime.db 中的全部 bucket(含 values、缓存、历史、遗留 WAL 等)。 配置 bucket 不应出现在 runtime.db;若检测到则报错。
func (*Storage) ClearBucket ¶
func (*Storage) CompactRuntimeDB ¶
CompactRuntimeDB 回收运行时数据库空间。
- bbolt:整文件重写 + 毫秒级换绑(换绑窗口写入在 runtimeMu 排队), 两步 rename 间崩溃由 NewStorage 的 .pre-compact.bak 自愈兜底;
- sqlite:原地 VACUUM(SQLite 内建保证原子性,无换绑窗口)。
两后端 Compact 期间数据面写入均不中断服务语义(短暂排队)。
func (*Storage) ExportConfigDBArchive ¶
ExportConfigDBArchive exports the config database as tar.gz(条目名随后端)。
func (*Storage) ExportRuntimeDBArchive ¶
ExportRuntimeDBArchive exports the runtime database as tar.gz。
func (*Storage) GetBucketStats ¶
func (s *Storage) GetBucketStats() ([]BucketStats, int64, error)
func (*Storage) GetConfigPath ¶
func (*Storage) GetOfflineMessages ¶
func (s *Storage) GetOfflineMessages(configID string, limit int) ([]OfflineMessage, error)
func (*Storage) GetRuntimePath ¶
GetRuntimePath returns the on-disk path of runtime.db.
func (*Storage) ImportConfigDBArchive ¶
func (s *Storage) ImportConfigDBArchive(archiveData []byte, opts ImportArchiveOptions) (*ConfigImportResult, error)
ImportConfigDBArchive imports configuration from a tar.gz archive. By default existing user accounts/passwords and server port are preserved. 归档内容按魔数自动识别 bbolt / SQLite 格式,与本地后端无关(跨后端导入)。
func (*Storage) ImportConfigDBData ¶ added in v0.1.3
func (s *Storage) ImportConfigDBData(data []byte, opts ImportArchiveOptions) (*ConfigImportResult, error)
ImportConfigDBData 从上传数据导入配置(统一入口,"恢复备份"走这里)。 data 可以是:
- tar.gz 归档(export-config-db 产物,内含 config.db / config.sqlite 条目)
- 原始配置库备份文件(BackupConfigDB 产出的 .db / .sqlite)
按文件头魔数自动识别格式,与本地后端无关(bbolt 系统可恢复 SQLite 备份,反之亦然)。 By default existing user accounts/passwords and server port are preserved.
func (*Storage) LoadLatest ¶
func (*Storage) PruneOldest ¶
func (*Storage) PullRemoteConfigAndImport ¶
func (s *Storage) PullRemoteConfigAndImport(host string, port int, token string, useHTTPS, overwriteUsers bool) (*ConfigImportResult, error)
PullRemoteConfigAndImport 从远程网关拉取配置并导入本地。 P1-17 防护(2026-10-13 审查):此前硬编码 ForceOverwrite=true——远端的 用户账号/密码与 System(含网络)配置直接覆盖本地,无任何开关与确认。 现在默认保留本地用户与端口(与 import-config-db 一致),仅当调用方显式 overwriteUsers=true 才整体覆盖。
func (*Storage) RemoveOfflineMessage ¶
func (*Storage) SaveOfflineMessage ¶
func (*Storage) SyncConfigDB ¶
SyncConfigDB 对配置数据库执行强制同步,确保持久化到磁盘。 配置库使用强一致写入策略。