open
pub fn open(endpoint string, token string, org string, bucket string) Client用途
打开资源或建立网络连接。
参数
endpointstring- endpoint(string)
tokenstring- token(string)
orgstring- org(string)
bucketstring- bucket(string)
返回值
Client — 该类型的值
#github.com/xulang-org/packages/influxdb扩展包 `influxdb #github.com/xulang-org/packages/influxdb`,由模块 github.com/xulang-org/packages 提供。
github.com/xulang-org/packages · 1.17.2 · 安装与使用
以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。
32_standalone_influxdb/main.xu@main
influxdb #github.com/xulang-org/packages/influxdb
#errors
fn verify_influxdb_health() {
println("--- 1. Testing InfluxDB Health & Ping ---")
$client := influxdb.open(
"http://127.0.0.1:8086",
"my-super-secret-token",
"xu-org",
"xu-bucket"
)
is_ping := client.ping() >>> (err) {
println(" [FAIL] Ping failed: " + err.msg())
<<
}
if is_ping {
println(" [PASS] Ping succeeded.")
}
is_health := client.health() >>> (err) {
println(" [FAIL] Health check failed: " + err.msg())
<<
}
if is_health {
println(" [PASS] Health check passed.")
}
}
fn verify_influxdb_write_query() {
println("\n--- 2. Testing InfluxDB Line Protocol & Flux Query ---")
$client := influxdb.open(
"http://127.0.0.1:8086",
"my-super-secret-token",
"xu-org",
"xu-bucket"
)
// 1. Write single point using Point builder
println(" Constructing point 'cpu_load' with tags & fields...")
$pt := influxdb.new_point("cpu_load")
pt.add_tag("host", "worker-01")
pt.add_tag("env", "prod")
pt.add_field_float("usage", "45.8")
pt.add_field_int("cores", 16)
pt.add_field_bool("alert", false)
client.write_point(pt) >>> (err) {
println(" [FAIL] write_point failed: " + err.msg())
<<
}
println(" [PASS] Point written successfully.")
// 2. Write raw batch lines
println(" Writing batch metrics via raw line protocol...")
$raw_lines := "mem_metrics,host=worker-01 total=32768i,used=16384i\n"
raw_lines = raw_lines + "mem_metrics,host=worker-02 total=65536i,used=20480i\n"
client.write_raw(raw_lines) >>> (err) {
println(" [FAIL] write_raw failed: " + err.msg())
<<
}
println(" [PASS] Batch lines written successfully.")
// 3. Query using Flux
println(" Executing Flux query...")
flux := "from(bucket: \"xu-bucket\") |> range(start: -1h) |> filter(fn: (r) => r._measurement == \"cpu_load\") |> limit(n: 5)"
csv := client.query_flux(flux) >>> (err) {
println(" [FAIL] Flux query failed: " + err.msg())
<<
}
println(" Flux result preview:")
println(csv)
println(" [PASS] Flux query executed successfully.")
}
fn verify_influxdb_pool() {
println("\n--- 3. Testing InfluxDB Connection Pool ---")
$p := influxdb.open_pool(
"http://127.0.0.1:8086",
"my-super-secret-token",
"xu-org",
"xu-bucket",
5
)
// 1. Connection Reuse Test
fd1 := p.get_conn()
println(" Borrow 1 fd: " + str(fd1))
p.put_conn(fd1)
fd2 := p.get_conn()
println(" Borrow 2 (after return) fd: " + str(fd2))
if fd1 == fd2 {
println(" [PASS] Connection reused successfully (fd " + str(fd1) + " == " + str(fd2) + ")")
} else {
println(" [WARN] Different fd allocated: " + str(fd1) + " vs " + str(fd2))
}
// 2. Multi-connection concurrent borrow
fd3 := p.get_conn()
println(" Borrow 3 (concurrently) fd: " + str(fd3))
if fd2 != fd3 {
println(" [PASS] Concurrently allocated distinct fds (" + str(fd2) + " != " + str(fd3) + ")")
} else {
println(" [FAIL] Expected distinct fds for concurrent connections!")
}
p.put_conn(fd2)
p.put_conn(fd3)
// 3. Delegated operations via pool
println(" Testing delegated operations via pool...")
$pt := influxdb.new_point("pool_sensor")
pt.add_tag("room", "lab-01")
pt.add_field_float("humidity", "55.2")
p.write_point(pt) >>> (err) {
println(" [FAIL] Pool write_point error: " + err.msg())
<<
}
println(" [PASS] Pool write_point succeeded.")
p.write_raw("pool_sensor,room=lab-02 humidity=60.1\n") >>> (err) {
println(" [FAIL] Pool write_raw error: " + err.msg())
<<
}
println(" [PASS] Pool write_raw succeeded.")
flux := "from(bucket: \"xu-bucket\") |> range(start: -5m) |> filter(fn: (r) => r._measurement == \"pool_sensor\") |> limit(n: 5)"
csv := p.query_flux(flux) >>> (err) {
println(" [FAIL] Pool query_flux error: " + err.msg())
<<
}
println(" Retrieved flux data preview length: " + str(csv.len) + " bytes")
if csv.len > 0 {
println(" [PASS] Pool delegated query_flux succeeded.")
} else {
println(" [FAIL] Pool query returned empty.")
}
p.close()
println(" [PASS] InfluxDB Pool closed cleanly.")
}
fn main() {
println("=== Xu InfluxDB v2 Time-Series Database Driver Verification ===\n")
verify_influxdb_health()
verify_influxdb_write_query()
verify_influxdb_pool()
println("\n[INFLUXDB VERIFICATION 100% COMPLETED]")
}openpub fn open(endpoint string, token string, org string, bucket string) Client打开资源或建立网络连接。
endpoint stringtoken stringorg stringbucket stringClient — 该类型的值
open_pool_clientpub fn open_pool_client(endpoint string, token string, org string, bucket string, max_idle int) Pool执行 open pool client 操作。
endpoint stringtoken stringorg stringbucket stringmax_idle intPool — 该类型的值
struct Responsepub struct Response {
pub status_code int
pub body string
}Influxdb 服务端响应结果,包含状态码、响应头与正文数据。
status_code intbody stringstruct Clientpub struct Client {
pub host string
pub port int
pub secure bool
pub token string
pub org string
pub bucket string
}Influxdb 客户端会话实例,维护与服务端的连接并提供操作接口。
host stringport intsecure booltoken stringorg stringbucket stringnew_clientpub fn new_client(endpoint string, token string, org string, bucket string) Client构造 client。
endpoint stringtoken stringorg stringbucket stringClient — 该类型的值
($&Client) requestpub fn (c $&Client) request(method string, path string, body string, content_type string, accept string) Response$&Client 上的操作:请求。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。method stringpath stringbody stringcontent_type stringaccept stringResponse — 该类型的值
struct Tagpub struct Tag {
pub key string
pub val string
}承载 key、val 等属性的结构体。
key stringval stringstruct Fieldpub struct Field {
pub key string
pub val string
}承载 key、val 等属性的结构体。
key stringval stringstruct Pointpub struct Point {
pub measurement string
pub tags []Tag
pub fields []Field
pub timestamp int
}承载 measurement、tags、fields、timestamp 等属性的结构体。
measurement stringtags []Tagfields []Fieldtimestamp intnew_pointpub fn new_point(measurement string) Point构造 point。
measurement stringPoint — 该类型的值
($&Point) add_tagpub fn (p $&Point) add_tag(key string, val string)$&Point 上的操作:增加标签。
p $&PointPoint 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringval string不返回值。
($&Point) add_field_stringpub fn (p $&Point) add_field_string(key string, val string)$&Point 上的操作:执行 add field string 操作。
p $&PointPoint 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringval string不返回值。
($&Point) add_field_intpub fn (p $&Point) add_field_int(key string, val int)$&Point 上的操作:执行 add field int 操作。
p $&PointPoint 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringval int不返回值。
($&Point) add_field_floatpub fn (p $&Point) add_field_float(key string, val string)$&Point 上的操作:执行 add field float 操作。
p $&PointPoint 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringval string不返回值。
($&Point) add_field_boolpub fn (p $&Point) add_field_bool(key string, val bool)$&Point 上的操作:执行 add field bool 操作。
p $&PointPoint 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringval bool不返回值。
($&Point) set_timestamppub fn (p $&Point) set_timestamp(ts int)$&Point 上的操作:设置 timestamp。
p $&PointPoint 实例;以独占借用传入,方法可修改它,调用方仍持有实例。ts int不返回值。
(&Point) to_linepub fn (p &Point) to_line() string&Point 上的操作:转换为 line。
p &PointPoint 实例;以只读借用传入,调用方仍持有实例。string — 结果字符串
($&Client) write_pointpub fn (c $&Client) write_point(point Point) bool$&Client 上的操作:执行 write point 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。point Pointbool — 是否成立
($&Client) write_linepub fn (c $&Client) write_line(line string) bool$&Client 上的操作:执行 write line 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。line stringbool — 是否成立
($&Client) write_rawpub fn (c $&Client) write_raw(lines string) bool$&Client 上的操作:执行 write raw 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。lines stringbool — 是否成立
struct Poolpub struct Pool {
pub host string
pub port int
pub secure bool
pub token string
pub org string
pub bucket string
pub state chan pool.State
pub max_idle int
}InfluxDB 连接池管理器,维护复用套接字连接。
host stringport intsecure booltoken stringorg stringbucket stringstate chan pool.Statemax_idle intnew_poolpub fn new_pool(endpoint string, token string, org string, bucket string, max_idle int) Pool构造 pool。
endpoint stringtoken stringorg stringbucket stringmax_idle intPool — 该类型的值
open_poolpub fn open_pool(endpoint string, token string, org string, bucket string, max_idle int) Pool执行 open pool 操作。
endpoint stringtoken stringorg stringbucket stringmax_idle intPool — 该类型的值
(&Pool) getpub fn (p &Pool) get() Client在 &Pool 上执行 get 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。Client — 该类型的值
(&Pool) duppub fn (p &Pool) dup() Pool创建并返回持有独立状态或底层的副本对象。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。Pool — 该类型的值
(&Pool) get_connpub fn (p &Pool) get_conn() int&Pool 上的操作:读取 conn。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。int — 该类型的值
(&Pool) put_connpub fn (p &Pool) put_conn(fd int)&Pool 上的操作:执行 put conn 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。fd int不返回值。
(&Pool) discard_connpub fn (p &Pool) discard_conn(fd int)&Pool 上的操作:丢弃连接。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。fd int不返回值。
(&Pool) write_pointpub fn (p &Pool) write_point(point Point) bool&Pool 上的操作:执行 write point 操作。失败时抛出错误,调用方需处理或继续传播。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。point Pointbool — 是否成立
(&Pool) write_linepub fn (p &Pool) write_line(line string) bool&Pool 上的操作:执行 write line 操作。失败时抛出错误,调用方需处理或继续传播。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。line stringbool — 是否成立
(&Pool) write_rawpub fn (p &Pool) write_raw(lines string) bool&Pool 上的操作:执行 write raw 操作。失败时抛出错误,调用方需处理或继续传播。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。lines stringbool — 是否成立
(&Pool) query_fluxpub fn (p &Pool) query_flux(flux string) string&Pool 上的操作:执行 query flux 操作。失败时抛出错误,调用方需处理或继续传播。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。flux stringstring — 结果字符串
(&Pool) pingpub fn (p &Pool) ping() bool发送心跳保活探测请求,检测连接活性与服务可用性。失败时抛出错误,调用方需处理或继续传播。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。bool — 是否成立
(&Pool) closepub fn (p &Pool) close()&Pool 上的操作:关闭资源。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。不返回值。
($&Client) healthpub fn (c $&Client) health() bool在 $&Client 上执行 health 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。bool — 是否成立
($&Client) pingpub fn (c $&Client) ping() bool发送心跳保活探测请求,检测连接活性与服务可用性。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。bool — 是否成立
($&Client) query_fluxpub fn (c $&Client) query_flux(flux string) string$&Client 上的操作:执行 query flux 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。flux stringstring — 结果字符串