#github.com/xulang-org/packages/cassandra
Xu 0.16.0 API
import: cassandra #github.com/xulang-org/packages/cassandra
counts: 12 functions, 15 methods, 4 types, 11 constants, 0 method sets
Apache Cassandra & ScyllaDB Native CQL Binary Protocol Driver for Xu Implements CQL Binary Protocol v4 frame serialization, connection pooling, and DDL/DML execution.
github.com/xulang-org/packages · 1.17.2 · Install and use
Examples · API declarations
Examples
These sources come from the official examples repository and can be copied or downloaded. Run them with a local Xu SDK in the complete project; network, database, and desktop examples require the corresponding services or assets.
42_standalone_cassandra/main.xu
Required SDK: Xu 0.8.3 · Complete project · Download source
@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]")
}
API declarations
struct Client
pub struct Client {
pub host string
pub port int
pub keyspace string
}
Purpose
Cassandra Client session maintaining connection and operations.
Public fields
host string- host
port int- port
keyspace string- field (string)
new_client
pub fn new_client(endpoint string, keyspace string) Client
Purpose
Construct client.
Parameters
endpoint string- endpoint (string)
keyspace string- keyspace (string)
Returns
Client — a value of this type
(Client) read_frame
pub fn (c Client) read_frame(fd int) Frame
Purpose
Operation on Client: read frame. Throws on failure; handle the error or propagate it to the caller.
Parameters
c Client- The
Client instance used by this call (value receiver).
fd int- file descriptor (int)
Returns
Frame — a value of this type
(Client) connect
pub fn (c Client) connect() int
Purpose
Establish network connection to the target server endpoint. Throws on failure; handle the error or propagate it to the caller.
Parameters
c Client- The
Client instance used by this call (value receiver).
Returns
int — a value of this type
(Client) execute_on_fd
pub fn (c Client) execute_on_fd(fd int, cql string) string
Purpose
Operation on Client: execute on fd. Throws on failure; handle the error or propagate it to the caller.
Parameters
c Client- The
Client instance used by this call (value receiver).
fd int- file descriptor (int)
cql string- cql (string)
Returns
string — result string
(Client) execute
pub fn (c Client) execute(cql string) string
Purpose
Operation on Client: apply data context to template and execute rendering. Throws on failure; handle the error or propagate it to the caller.
Parameters
c Client- The
Client instance used by this call (value receiver).
cql string- cql (string)
Returns
string — result string
struct Pool
pub struct Pool {
pub client Client
pub idle chan int
pub max_idle int
}
Purpose
Cassandra Connection pool manager scheduling reusable socket connections.
Public fields
client Client- field (Client)
idle chan int- field (channel; type chan int)
max_idle int- field (int)
new_pool
pub fn new_pool(endpoint string, keyspace string, max_idle int) Pool
Purpose
Construct pool.
Parameters
endpoint string- endpoint (string)
keyspace string- keyspace (string)
max_idle int- max_idle (int)
Returns
Pool — a value of this type
open_pool
pub fn open_pool(endpoint string, keyspace string, max_idle int) Pool
Purpose
Open pool.
Parameters
endpoint string- endpoint (string)
keyspace string- keyspace (string)
max_idle int- max_idle (int)
Returns
Pool — a value of this type
See usage example
(&Pool) get_conn
pub fn (p &Pool) get_conn() int
Purpose
Operation on &Pool: read conn.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
Returns
int — a value of this type
(&Pool) put_conn
pub fn (p &Pool) put_conn(fd int)
Purpose
Operation on &Pool: put connection.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
fd int- file descriptor (int)
Returns
This call does not return a value.
(&Pool) execute
pub fn (p &Pool) execute(cql string) string
Purpose
Operation on &Pool: apply data context to template and execute rendering. Throws on failure; handle the error or propagate it to the caller.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
cql string- cql (string)
Returns
string — result string
(&Pool) close
Purpose
Operation on &Pool: close the resource.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
Returns
This call does not return a value.
const OPCODE_ERROR
pub const OPCODE_ERROR = 0
Purpose
Constant OPCODE_ERROR, defining protocol or algorithm parameters.
const OPCODE_STARTUP
pub const OPCODE_STARTUP = 1
Purpose
Constant OPCODE_STARTUP, defining protocol or algorithm parameters.
const OPCODE_READY
pub const OPCODE_READY = 2
Purpose
Constant OPCODE_READY, defining protocol or algorithm parameters.
const OPCODE_AUTHENTICATE
pub const OPCODE_AUTHENTICATE = 3
Purpose
Constant OPCODE_AUTHENTICATE, defining protocol or algorithm parameters.
const OPCODE_QUERY
pub const OPCODE_QUERY = 7
Purpose
Constant OPCODE_QUERY, defining protocol or algorithm parameters.
const OPCODE_RESULT
pub const OPCODE_RESULT = 8
Purpose
Constant OPCODE_RESULT, defining protocol or algorithm parameters.
const RESULT_KIND_VOID
pub const RESULT_KIND_VOID = 1
Purpose
Constant RESULT_KIND_VOID, defining protocol or algorithm parameters.
const RESULT_KIND_ROWS
pub const RESULT_KIND_ROWS = 2
Purpose
Constant RESULT_KIND_ROWS, defining protocol or algorithm parameters.
const RESULT_KIND_SET_KEYSPACE
pub const RESULT_KIND_SET_KEYSPACE = 3
Purpose
Constant RESULT_KIND_SET_KEYSPACE, defining protocol or algorithm parameters.
const RESULT_KIND_PREPARED
pub const RESULT_KIND_PREPARED = 4
Purpose
Constant RESULT_KIND_PREPARED, defining protocol or algorithm parameters.
const RESULT_KIND_SCHEMA_CHANGE
pub const RESULT_KIND_SCHEMA_CHANGE = 5
Purpose
Constant RESULT_KIND_SCHEMA_CHANGE, defining protocol or algorithm parameters.
struct Frame
pub struct Frame {
pub version int
pub flags int
pub stream int
pub opcode int
pub body string
}
Purpose
Cassandra Protocol data frame encapsulation.
Public fields
version int- field (int)
flags int- field (int)
stream int- field (int)
opcode int- field (int)
body string- body
encode_u16
pub fn encode_u16(val int) string
Purpose
Encode u16 data.
Parameters
val int- value (int)
Returns
string — result string
encode_u32
pub fn encode_u32(val int) string
Purpose
Encode u32 data.
Parameters
val int- value (int)
Returns
string — result string
decode_u16
pub fn decode_u16(b1 int, b2 int) int
Purpose
Decode u16 data.
Parameters
b1 int- b1 (int)
b2 int- b2 (int)
Returns
int — a value of this type
decode_u32
pub fn decode_u32(b1 int, b2 int, b3 int, b4 int) int
Purpose
Decode u32 data.
Parameters
b1 int- b1 (int)
b2 int- b2 (int)
b3 int- b3 (int)
b4 int- b4 (int)
Returns
int — a value of this type
build_frame
pub fn build_frame(opcode int, stream int, body string) string
Purpose
Build frame.
Parameters
opcode int- opcode (int)
stream int- stream (int)
body string- body (string)
Returns
string — result string
build_startup_frame
pub fn build_startup_frame() string
Purpose
Build startup frame.
Parameters
No arguments are required.
Returns
string — result string
build_query_frame
pub fn build_query_frame(cql string, stream int) string
Purpose
Build query frame.
Parameters
cql string- cql (string)
stream int- stream (int)
Returns
string — result string
struct TlsClient
pub struct TlsClient {
pub conn transport.Conn
pub host string
pub port int
}
Purpose
Cassandra Client session maintaining connection and operations.
Public fields
conn transport.Conn- field (transport.Conn)
host string- host
port int- port
tls_connect
pub fn tls_connect(endpoint string, default_port int) TlsClient
Purpose
Tls connect. Throws on failure; handle the error or propagate it to the caller.
Parameters
endpoint string- endpoint (string)
default_port int- default_port (int)
Returns
TlsClient — a value of this type
tls_connect_config
pub fn tls_connect_config(endpoint string, default_port int, cfg tls.Config) TlsClient
Purpose
Tls connect config. Throws on failure; handle the error or propagate it to the caller.
Parameters
endpoint string- endpoint (string)
default_port int- default_port (int)
cfg tls.Config- cfg (tls.Config)
Returns
TlsClient — a value of this type
($&TlsClient) write_all
pub fn (c $&TlsClient) write_all(data string) int
Purpose
Operation on $&TlsClient: write all. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&TlsClient- The
TlsClient instance used by this call (exclusive borrow).
data string- data (string)
Returns
int — a value of this type
($&TlsClient) read
pub fn (c $&TlsClient) read(max int) string
Purpose
Operation on $&TlsClient: read up to max bytes. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&TlsClient- The
TlsClient instance used by this call (exclusive borrow).
max int- maximum (int)
Returns
string — result string
($&TlsClient) read_exact
pub fn (c $&TlsClient) read_exact(need int) string
Purpose
Operation on $&TlsClient: read exact. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&TlsClient- The
TlsClient instance used by this call (exclusive borrow).
need int- need (int)
Returns
string — result string
($&TlsClient) close
pub fn (c $&TlsClient) close() int
Purpose
Operation on $&TlsClient: close the resource. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&TlsClient- The
TlsClient instance used by this call (exclusive borrow).
Returns
int — a value of this type
($&TlsClient) set_deadline
pub fn (c $&TlsClient) set_deadline(t_ns int) int
Purpose
Operation on $&TlsClient: set deadline. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&TlsClient- The
TlsClient instance used by this call (exclusive borrow).
t_ns int- t_ns (int)
Returns
int — a value of this type
($&TlsClient) set_read_deadline
pub fn (c $&TlsClient) set_read_deadline(t_ns int) int
Purpose
Operation on $&TlsClient: set read deadline. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&TlsClient- The
TlsClient instance used by this call (exclusive borrow).
t_ns int- t_ns (int)
Returns
int — a value of this type
($&TlsClient) set_write_deadline
pub fn (c $&TlsClient) set_write_deadline(t_ns int) int
Purpose
Operation on $&TlsClient: set write deadline. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&TlsClient- The
TlsClient instance used by this call (exclusive borrow).
t_ns int- t_ns (int)
Returns
int — a value of this type