sqlstore

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2026 License: MIT Imports: 4 Imported by: 0

README

gitee.com/idcu-go/sqlstore

当前版本:v0.1.0

1. 定位 + 当前版本

模块类型:通用公共模块(极简「时序样本」存储抽象 + 默认本地 SQLite 落盘)。

提供「按目标 + 时间窗写入 / 查询结构化样本」的存储抽象 Store 与默认的本地 SQLiteStore 实现,把监控领域反复出现的「历史样本落盘 / 回溯」抽成通用能力, 同时让后端可替换:默认用现代 doc(modernc.org/sqlite,无 cgo) 落本地文件;需要 对接远端时序库(ClickHouse / Loki / InfluxDB 等)时,实现 Store 接口并替换注入点即可, 上层(写入 / 查询) 不感知。本包不依赖任何 idcu-go 领域/应用类型,只依赖数据库驱动。

2. 安装

go get gitee.com/idcu-go/sqlstore@v0.1.0

依赖:

  • 内部(gitee.com/idcu-go/*):无
  • 外部(第三方):modernc.org/sqlite(无 cgo 的 SQLite 驱动)

3. API 签名

// 时序样本(存储无关)
type Sample struct {
	Name   string  `json:"name"`   // 序列/目标名
	Host   string  `json:"host"`
	Ts     int64   `json:"ts"`     // Unix 毫秒
	OK     bool    `json:"ok"`
	RTTMs  float64 `json:"rtt_ms"` // 毫秒
	Loss   float64 `json:"loss"`   // [0,1]
	Status string  `json:"status"` // up/down
}

// 存储抽象:实现者只需满足本接口
type Store interface {
	Insert(s Sample) error
	Query(name string, since, until int64, limit int) ([]Sample, error)
	Close() error
}

// 默认 SQLite 实现
func NewSQLiteStore(path string) (*SQLiteStore, error)

4. 关键类型/结构体

类型 字段 说明
Sample Name/Host/Ts/OK/RTTMs/Loss/Status 存储无关的时序样本最小集
Store — 存储抽象接口(Insert/Query/Close)
SQLiteStore db *sql.DB 默认本地 SQLite 实现(单写者,SetMaxOpenConns(1))
DefaultLimit — Query 在 limit<=0 时的默认返回条数(1000)

5. 最小用法示例

package main

import (
	"fmt"

	"gitee.com/idcu-go/sqlstore"
)

func main() {
	st, err := sqlstore.NewSQLiteStore("./hist.db")
	if err != nil {
		panic(err)
	}
	defer st.Close()

	_ = st.Insert(sqlstore.Sample{Name: "svc-a", Host: "h1",
		Ts: 1700000000000, OK: true, RTTMs: 3.2, Status: "up"})

	rows, _ := st.Query("svc-a", 0, 0, 100)
	for _, s := range rows {
		fmt.Printf("%s %d ok=%v rtt=%.1fms\n", s.Name, s.Ts, s.OK, s.RTTMs)
	}
}

6. 工作机制/边界语义

  • 存储无关:Sample / Store 不绑定任何领域类型,上层负责把「领域样本」转换为 Sample(如 realtime-ping 的 monitor.Sample → sqlstore.Sample)。
  • 默认落盘:SQLiteStore 基于现代 doc(无 cgo) 打开本地文件并自动建表(samples); Insert 在调用方 goroutine 同步执行,SetMaxOpenConns(1) 保证单写者无并发写锁。
  • 查询语义:Query 按 name 过滤(name=="" 查全部),[since, until] 时间窗含边界 (0 表示该侧不设界),limit<=0 用 DefaultLimit,结果按 id 倒序(最新在前)。
  • 可替换:接远端时序库时实现 Store 并替换注入点,调用方零改动。

7. 适用场景

  • 监控历史样本落盘与回溯查询(探测结果的「可回溯」分析)。
  • 需要本地持久化时序样本、又不想引入重型时序库的应用。
  • 作为后续对接远端时序库(ClickHouse / Loki / InfluxDB)的统一接缝层。

8. 变更记录

见仓库 tag v0.1.0(gitee.com/idcu-go/sqlstore 对应提交)。后续版本变更以 git tag / CHANGELOG 为准。

Documentation

Overview

sqlite.go 提供 Store 的默认本地 SQLite 实现。 基于现代 doc(modernc.org/sqlite,无 cgo) 打开本地文件,表结构极简,仅承载 上述最小字段的时序样本。设计为「单写者」:Insert 在调用方 goroutine 同步执行, 上层每个目标对应独立目标,单库单写天然无并发写冲突(见 NewSQLiteStore 设置 SetMaxOpenConns(1))。

Package sqlstore 提供极简的「时序样本」存储抽象与默认的 SQLite 落盘实现。

定位:把「按目标 + 时间窗写入 / 查询结构化样本」这件在监控领域反复出现的事 抽象成一个通用公共模块,供多个应用 / 模块复用,同时让实现可替换—— 默认用现代 doc(ModernC.org/sqlite,无 cgo) 落本地文件;需要对接远端时序库 (ClickHouse / Loki / InfluxDB 等) 时,只要提供满足 Store 接口的实现并替换注入点 即可,调用方(写入 / 查询)不感知。

与其他模块的边界:本包**不依赖任何 idcu-go 领域/应用类型**,只依赖数据库驱动; 上层负责把「领域样本」(如 monitor.Sample) 转换为存储无关的 Sample。

Index

Constants

View Source
const DefaultLimit = 1000

DefaultLimit 是 Store.Query 在 limit<=0 时的默认返回条数。

Variables

This section is empty.

Functions

This section is empty.

Types

type SQLiteStore

type SQLiteStore struct {
	// contains filtered or unexported fields
}

SQLiteStore 是 Store 的默认 SQLite 实现。

func NewSQLiteStore

func NewSQLiteStore(path string) (*SQLiteStore, error)

NewSQLiteStore 打开(或创建) SQLite 数据库并建表。 path 为本地数据库文件路径(需调用方保证所在目录已存在)。

func (*SQLiteStore) Close

func (s *SQLiteStore) Close() error

Close 关闭底层连接。

func (*SQLiteStore) Insert

func (s *SQLiteStore) Insert(samp Sample) error

Insert 写入一条样本。

func (*SQLiteStore) Query

func (s *SQLiteStore) Query(name string, since, until int64, limit int) ([]Sample, error)

Query 返回某目标(按 name)在 [since, until] 时间窗内的样本;name 为空时返回 所有目标;since/until 为 0 表示不设边界;limit<=0 时默认 DefaultLimit。

type Sample

type Sample struct {
	Name   string  `json:"name"`   // 序列/目标名,Query 按此维度过滤
	Host   string  `json:"host"`   // 目标主机
	Ts     int64   `json:"ts"`     // 采样时间,Unix 毫秒
	OK     bool    `json:"ok"`     // 是否正常
	RTTMs  float64 `json:"rtt_ms"` // 往返时延,毫秒
	Loss   float64 `json:"loss"`   // 丢包率,[0,1]
	Status string  `json:"status"` // 状态文本,如 up/down
}

Sample 是「存储无关」的一条时序样本。 各存储实现(本地 SQLite / 远端时序库)都基于它读写,保证对外 Query 契约稳定。 Ts 为 Unix 毫秒;字段为监控时序的常见最小集,便于多应用按 name 维度聚合。

type Store

type Store interface {
	// Insert 写入一条样本。
	Insert(s Sample) error
	// Query 返回某目标(按 name)在 [since, until] 时间窗(Unix 毫秒,含边界)内的
	// 样本,最新在前;name 为空返回全部目标;since/until 为 0 表示该侧不设边界;
	// limit<=0 时使用实现默认(如 1000)。
	Query(name string, since, until int64, limit int) ([]Sample, error)
	// Close 释放底层资源。
	Close() error
}

Store 是时序样本存储的抽象。 DefaultSQLiteStore 为默认实现;实现远端时序库后端时,仅需满足本接口并替换 构造点,上层(写入 / 查询) 不感知。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL