struct Client
pub struct Client {
pub host string
pub port int
pub keyspace string
}用途
Cassandra 客户端会话实例,维护与服务端的连接并提供操作接口。
公开字段
hoststring- 主机
portint- 端口
keyspacestring- 字段(string)
#github.com/xulang-org/packages/cassandra扩展包 `cassandra #github.com/xulang-org/packages/cassandra`,由模块 github.com/xulang-org/packages 提供。
github.com/xulang-org/packages · 1.17.2 · 安装与使用
以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。
42_standalone_cassandra/main.xu@main
cassandra #github.com/xulang-org/packages/cassandra
#errors
#strings
fn verify_cassandra_pool() {
println("--- 1. Testing Cassandra / ScyllaDB Connection Pool ---")
$p := cassandra.open_pool("127.0.0.1:9042", "", 5)
// 1. Connection Reuse
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)
p.close()
println(" [PASS] Cassandra Pool closed cleanly.")
}
fn verify_cassandra_cql_crud() {
println("\n--- 2. Testing Cassandra / ScyllaDB CQL DDL & DML ---")
$p := cassandra.open_pool("127.0.0.1:9042", "", 5)
// 1. Create Keyspace
cql_ks := "CREATE KEYSPACE IF NOT EXISTS xu_test WITH replication = {'class': 'NetworkTopologyStrategy', 'replication_factor': 1} AND tablets = {'enabled': false};"
r1 := p.execute(cql_ks) >>> (err) {
println(" [FAIL] Create keyspace failed: " + err.msg())
p.close()
<<
}
println(" [PASS] Created keyspace xu_test (result: " + r1 + ")")
// 2. Create Table
cql_tbl := "CREATE TABLE IF NOT EXISTS xu_test.devices (id int PRIMARY KEY, model text, status text);"
r2 := p.execute(cql_tbl) >>> (err) {
println(" [FAIL] Create table failed: " + err.msg())
p.close()
<<
}
println(" [PASS] Created table xu_test.devices (result: " + r2 + ")")
// 3. Insert Data
cql_ins := "INSERT INTO xu_test.devices (id, model, status) VALUES (101, 'Sensor-X', 'ONLINE');"
r3 := p.execute(cql_ins) >>> (err) {
println(" [FAIL] Insert data failed: " + err.msg())
p.close()
<<
}
println(" [PASS] Inserted record id=101 (result: " + r3 + ")")
// 4. Select Data
cql_sel := "SELECT * FROM xu_test.devices WHERE id = 101;"
r4 := p.execute(cql_sel) >>> (err) {
println(" [FAIL] Select query failed: " + err.msg())
p.close()
<<
}
println(" [PASS] Selected record id=101 (result: " + r4 + ")")
// 5. Cleanup
cql_drop := "DROP KEYSPACE IF EXISTS xu_test;"
r5 := p.execute(cql_drop) >>> (err) {
println(" [FAIL] Drop keyspace failed: " + err.msg())
p.close()
<<
}
println(" [PASS] Cleaned up keyspace xu_test (result: " + r5 + ")")
p.close()
}
fn main() {
println("=== Xu Cassandra & ScyllaDB Native Driver Verification ===\n")
verify_cassandra_pool()
verify_cassandra_cql_crud()
println("\n[CASSANDRA VERIFICATION 100% COMPLETED]")
}struct Clientpub struct Client {
pub host string
pub port int
pub keyspace string
}Cassandra 客户端会话实例,维护与服务端的连接并提供操作接口。
host stringport intkeyspace stringnew_clientpub fn new_client(endpoint string, keyspace string) Client构造 client。
endpoint stringkeyspace stringClient — 该类型的值
(Client) read_framepub fn (c Client) read_frame(fd int) FrameClient 上的操作:执行 read frame 操作。失败时抛出错误,调用方需处理或继续传播。
c ClientClient 实例;按值接收。fd intFrame — 该类型的值
(Client) connectpub fn (c Client) connect() int建立与目标服务端点的网络通信连接。失败时抛出错误,调用方需处理或继续传播。
c ClientClient 实例;按值接收。int — 该类型的值
(Client) execute_on_fdpub fn (c Client) execute_on_fd(fd int, cql string) stringClient 上的操作:执行 execute on fd 操作。失败时抛出错误,调用方需处理或继续传播。
c ClientClient 实例;按值接收。fd intcql stringstring — 结果字符串
(Client) executepub fn (c Client) execute(cql string) stringClient 上的操作:将数据上下文应用到模板并执行渲染。失败时抛出错误,调用方需处理或继续传播。
c ClientClient 实例;按值接收。cql stringstring — 结果字符串
struct Poolpub struct Pool {
pub client Client
pub idle chan int
pub max_idle int
}Cassandra 连接池管理器,并发安全地维护和调度复用套接字连接。
client Clientidle chan intmax_idle intnew_poolpub fn new_pool(endpoint string, keyspace string, max_idle int) Pool构造 pool。
endpoint stringkeyspace stringmax_idle intPool — 该类型的值
open_poolpub fn open_pool(endpoint string, keyspace string, max_idle int) Pool执行 open pool 操作。
endpoint stringkeyspace stringmax_idle intPool — 该类型的值
(&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) executepub fn (p &Pool) execute(cql string) string&Pool 上的操作:将数据上下文应用到模板并执行渲染。失败时抛出错误,调用方需处理或继续传播。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。cql stringstring — 结果字符串
(&Pool) closepub fn (p &Pool) close()&Pool 上的操作:关闭资源。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。不返回值。
const OPCODE_ERRORpub const OPCODE_ERROR = 0常量 OPCODE_ERROR,定义了相关的协议或算法配置参数。
const OPCODE_STARTUPpub const OPCODE_STARTUP = 1常量 OPCODE_STARTUP,定义了相关的协议或算法配置参数。
const OPCODE_READYpub const OPCODE_READY = 2常量 OPCODE_READY,定义了相关的协议或算法配置参数。
const OPCODE_AUTHENTICATEpub const OPCODE_AUTHENTICATE = 3常量 OPCODE_AUTHENTICATE,定义了相关的协议或算法配置参数。
const OPCODE_QUERYpub const OPCODE_QUERY = 7常量 OPCODE_QUERY,定义了相关的协议或算法配置参数。
const OPCODE_RESULTpub const OPCODE_RESULT = 8常量 OPCODE_RESULT,定义了相关的协议或算法配置参数。
const RESULT_KIND_VOIDpub const RESULT_KIND_VOID = 1常量 RESULT_KIND_VOID,定义了相关的协议或算法配置参数。
const RESULT_KIND_ROWSpub const RESULT_KIND_ROWS = 2常量 RESULT_KIND_ROWS,定义了相关的协议或算法配置参数。
const RESULT_KIND_SET_KEYSPACEpub const RESULT_KIND_SET_KEYSPACE = 3常量 RESULT_KIND_SET_KEYSPACE,定义了相关的协议或算法配置参数。
const RESULT_KIND_PREPAREDpub const RESULT_KIND_PREPARED = 4常量 RESULT_KIND_PREPARED,定义了相关的协议或算法配置参数。
const RESULT_KIND_SCHEMA_CHANGEpub const RESULT_KIND_SCHEMA_CHANGE = 5常量 RESULT_KIND_SCHEMA_CHANGE,定义了相关的协议或算法配置参数。
struct Framepub struct Frame {
pub version int
pub flags int
pub stream int
pub opcode int
pub body string
}Cassandra 协议数据帧结构封装。
version intflags intstream intopcode intbody stringencode_u16pub fn encode_u16(val int) string编码 u16 数据。
val intstring — 结果字符串
encode_u32pub fn encode_u32(val int) string编码 u32 数据。
val intstring — 结果字符串
decode_u16pub fn decode_u16(b1 int, b2 int) int解码 u16 数据。
b1 intb2 intint — 该类型的值
decode_u32pub fn decode_u32(b1 int, b2 int, b3 int, b4 int) int解码 u32 数据。
b1 intb2 intb3 intb4 intint — 该类型的值
build_framepub fn build_frame(opcode int, stream int, body string) string执行 build frame 操作。
opcode intstream intbody stringstring — 结果字符串
build_startup_framepub fn build_startup_frame() string执行 build startup frame 操作。
无需传入参数。
string — 结果字符串
build_query_framepub fn build_query_frame(cql string, stream int) string执行 build query frame 操作。
cql stringstream intstring — 结果字符串
struct TlsClientpub struct TlsClient {
pub conn transport.Conn
pub host string
pub port int
}Cassandra 客户端会话实例,维护与服务端的连接并提供操作接口。
conn transport.Connhost stringport inttls_connectpub fn tls_connect(endpoint string, default_port int) TlsClient执行 tls connect 操作。失败时抛出错误,调用方需处理或继续传播。
endpoint stringdefault_port intTlsClient — 该类型的值
tls_connect_configpub fn tls_connect_config(endpoint string, default_port int, cfg tls.Config) TlsClient执行 tls connect config 操作。失败时抛出错误,调用方需处理或继续传播。
endpoint stringdefault_port intcfg tls.ConfigTlsClient — 该类型的值
($&TlsClient) write_allpub fn (c $&TlsClient) write_all(data string) int$&TlsClient 上的操作:执行 write all 操作。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。data stringint — 该类型的值
($&TlsClient) readpub fn (c $&TlsClient) read(max int) string$&TlsClient 上的操作:读出最多 max 字节。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。max intstring — 结果字符串
($&TlsClient) read_exactpub fn (c $&TlsClient) read_exact(need int) string$&TlsClient 上的操作:执行 read exact 操作。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。need intstring — 结果字符串
($&TlsClient) closepub fn (c $&TlsClient) close() int$&TlsClient 上的操作:关闭资源。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。int — 该类型的值
($&TlsClient) set_deadlinepub fn (c $&TlsClient) set_deadline(t_ns int) int$&TlsClient 上的操作:设置 deadline。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值
($&TlsClient) set_read_deadlinepub fn (c $&TlsClient) set_read_deadline(t_ns int) int$&TlsClient 上的操作:设置 read deadline。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值
($&TlsClient) set_write_deadlinepub fn (c $&TlsClient) set_write_deadline(t_ns int) int$&TlsClient 上的操作:设置 write deadline。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值