open
pub fn open(endpoint string) Client用途
打开资源或建立网络连接。
参数
endpointstring- endpoint(string)
返回值
Client — 该类型的值
#github.com/xulang-org/packages/qdrant扩展包 `qdrant #github.com/xulang-org/packages/qdrant`,由模块 github.com/xulang-org/packages 提供。
github.com/xulang-org/packages · 1.17.2 · 安装与使用
以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。
38_standalone_qdrant/main.xu@main
qdrant #github.com/xulang-org/packages/qdrant
#errors
fn verify_qdrant_basic() {
println("--- 1. Testing Qdrant Client Basic Operations ---")
$client := qdrant.open("http://127.0.0.1:6333")
// Ping check
pong := client.ping()
println(" Qdrant healthz ping: " + str(pong))
if !pong {
println(" [FAIL] Qdrant ping failed.")
<<
}
col := "xu_ai_embeddings"
if client.collection_exists(col + "") {
client.delete_collection(col + "")
}
// 1. Create collection with 4-dim Cosine
println(" Creating collection '" + col + "' (dim=4, dist=Cosine)...")
ok1 := client.create_collection(col + "", 4, "Cosine")
if ok1 {
println(" [PASS] Collection created.")
} else {
println(" [FAIL] Failed to create collection.")
<<
}
// 2. Upsert vector points
println(" Upserting 3 vector points...")
client.upsert_point(col + "", 1, ["0.1", "0.2", "0.9", "0.8"], "{\"category\": \"AI\"}")
client.upsert_point(col + "", 2, ["0.9", "0.8", "0.1", "0.2"], "{\"category\": \"Food\"}")
client.upsert_point(col + "", 3, ["0.15", "0.25", "0.85", "0.75"], "{\"category\": \"Robotics\"}")
println(" [PASS] 3 vector points upserted.")
// 3. Search nearest neighbors to [0.12, 0.22, 0.88, 0.78]
println(" Searching vector nearest neighbors...")
results := client.search(col + "", ["0.12", "0.22", "0.88", "0.78"], 2) >>> (err) {
println(" [FAIL] Search failed: " + err.msg())
<<
}
println(" Search results count: " + str(results.len))
$i := 0
while i < results.len {
println(" Hit[" + str(i) + "]: id=" + str(results[i].id) + ", score=" + results[i].score)
i = i + 1
}
if results.len >= 1 && (results[0].id == 1 || results[0].id == 3) {
println(" [PASS] Semantic similarity correctly identified relevant vectors!")
} else {
println(" [WARN] Unexpected top match: id=" + str(results[0].id))
}
// 4. Delete collection
del_ok := client.delete_collection(col + "")
if del_ok {
println(" [PASS] Collection deleted successfully.")
} else {
println(" [FAIL] Failed to delete collection.")
}
}
fn verify_qdrant_pool() {
println("\n--- 2. Testing Qdrant Connection Pool ---")
$p := qdrant.open_pool("http://127.0.0.1:6333", "", 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...")
pool_col := "xu_pool_vectors"
if p.collection_exists(pool_col + "") {
p.delete_collection(pool_col + "")
}
p.create_collection(pool_col + "", 4, "Cosine")
println(" [PASS] Pool created collection: " + pool_col)
p.upsert_point(pool_col + "", 101, ["0.5", "0.5", "0.5", "0.5"], "{\"label\": \"balanced\"}")
p.upsert_point(pool_col + "", 102, ["0.0", "1.0", "0.0", "0.0"], "{\"label\": \"y_axis\"}")
println(" [PASS] Pool upserted vector points.")
pool_hits := p.search(pool_col + "", ["0.0", "0.99", "0.0", "0.0"], 1) >>> (err) {
println(" [FAIL] Pool search failed: " + err.msg())
<<
}
println(" Pool search top hit: id=" + str(pool_hits[0].id) + ", score=" + pool_hits[0].score)
if pool_hits.len == 1 && pool_hits[0].id == 102 {
println(" [PASS] Pool search perfectly matched top vector id=102!")
} else {
println(" [FAIL] Pool search match mismatch.")
}
p.delete_collection(pool_col + "")
println(" [PASS] Pool deleted collection.")
p.close()
println(" [PASS] Qdrant Pool closed cleanly.")
}
fn main() {
println("=== Xu Qdrant Vector Database Native Driver Verification ===\n")
verify_qdrant_basic()
verify_qdrant_pool()
println("\n[QDRANT VERIFICATION 100% COMPLETED]")
}openpub fn open(endpoint string) Client打开资源或建立网络连接。
endpoint stringClient — 该类型的值
open_authpub fn open_auth(endpoint string, api_key string) Client执行 open auth 操作。
endpoint stringapi_key stringClient — 该类型的值
struct Responsepub struct Response {
pub status_code int
pub body string
}Qdrant 服务端响应结果,包含状态码、响应头与正文数据。
status_code intbody stringstruct Clientpub struct Client {
pub host string
pub port int
pub secure bool
pub api_key string
}Qdrant 客户端会话实例,维护与服务端的连接并提供操作接口。
host stringport intsecure boolapi_key stringnew_clientpub fn new_client(endpoint string, api_key string) Client构造 client。
endpoint stringapi_key stringClient — 该类型的值
($&Client) requestpub fn (c $&Client) request(method string, path string, body string) Response$&Client 上的操作:请求。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。method stringpath stringbody stringResponse — 该类型的值
($&Client) pingpub fn (c $&Client) ping() bool发送心跳保活探测请求,检测连接活性与服务可用性。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。bool — 是否成立
($&Client) create_collectionpub fn (c $&Client) create_collection(name string, vector_size int, distance string) bool$&Client 上的操作:执行 create collection 操作。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。name stringvector_size intdistance stringbool — 是否成立
($&Client) collection_existspub fn (c $&Client) collection_exists(name string) bool$&Client 上的操作:执行 collection exists 操作。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。name stringbool — 是否成立
($&Client) delete_collectionpub fn (c $&Client) delete_collection(name string) bool$&Client 上的操作:执行 delete collection 操作。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。name stringbool — 是否成立
struct Pointpub struct Point {
pub id int
pub vector []string
pub payload_json string
}承载 id、vector、payload_json 等属性的结构体。
id intvector []stringpayload_json stringstruct ScoredPointpub struct ScoredPoint {
pub id int
pub score string
pub payload_json string
}承载 id、score、payload_json 等属性的结构体。
id intscore stringpayload_json string($&Client) upsert_pointpub fn (c $&Client) upsert_point(collection string, id int, vector []string, payload_json string) bool$&Client 上的操作:执行 upsert point 操作。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。collection stringid intvector []stringpayload_json stringbool — 是否成立
($&Client) searchpub fn (c $&Client) search(collection string, vector []string, limit int) []ScoredPoint$&Client 上的操作:二分查找。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。collection stringvector []stringlimit int[]ScoredPoint — 切片结果
struct Poolpub struct Pool {
pub host string
pub port int
pub secure bool
pub api_key string
pub state chan pool.State
pub max_idle int
}Qdrant 向量数据库连接池管理器。
host stringport intsecure boolapi_key stringstate chan pool.Statemax_idle intnew_poolpub fn new_pool(endpoint string, api_key string, max_idle int) Pool构造 pool。
endpoint stringapi_key stringmax_idle intPool — 该类型的值
open_poolpub fn open_pool(endpoint string, api_key string, max_idle int) Pool执行 open pool 操作。
endpoint stringapi_key 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) create_collectionpub fn (p &Pool) create_collection(name string, vector_size int, distance string) bool&Pool 上的操作:执行 create collection 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。name stringvector_size intdistance stringbool — 是否成立
(&Pool) collection_existspub fn (p &Pool) collection_exists(name string) bool&Pool 上的操作:执行 collection exists 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。name stringbool — 是否成立
(&Pool) delete_collectionpub fn (p &Pool) delete_collection(name string) bool&Pool 上的操作:执行 delete collection 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。name stringbool — 是否成立
(&Pool) upsert_pointpub fn (p &Pool) upsert_point(collection string, id int, vector []string, payload_json string) bool&Pool 上的操作:执行 upsert point 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。collection stringid intvector []stringpayload_json stringbool — 是否成立
(&Pool) searchpub fn (p &Pool) search(collection string, vector []string, limit int) []ScoredPoint&Pool 上的操作:二分查找。失败时抛出错误,调用方需处理或继续传播。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。collection stringvector []stringlimit int[]ScoredPoint — 切片结果
(&Pool) pingpub fn (p &Pool) ping() bool发送心跳保活探测请求,检测连接活性与服务可用性。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。bool — 是否成立
(&Pool) closepub fn (p &Pool) close()&Pool 上的操作:关闭资源。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。不返回值。