XU XUv0.16.0

#github.com/xulang-org/packages/qdrant

导入方式: qdrant #github.com/xulang-org/packages/qdrant

公开 API: 5 函数, 19 方法, 5 类型, 0 常量, 0 方法集

扩展包 `qdrant #github.com/xulang-org/packages/qdrant`,由模块 github.com/xulang-org/packages 提供。

github.com/xulang-org/packages · 1.17.2 · 安装与使用

示例代码 · API 声明

示例代码

以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。

38_standalone_qdrant/main.xu

所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码

Xu本地 SDK 示例
@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]")
}

API 声明

open

XuAPI
pub fn open(endpoint string) Client

用途

打开资源或建立网络连接。

参数

endpoint string
endpoint(string)

返回值

Client — 该类型的值

查看用法示例

open_auth

XuAPI
pub fn open_auth(endpoint string, api_key string) Client

用途

执行 open auth 操作。

参数

endpoint string
endpoint(string)
api_key string
api_key(string)

返回值

Client — 该类型的值

struct Response

XuAPI
pub struct Response {
    pub status_code int
    pub body string
}

用途

Qdrant 服务端响应结果,包含状态码、响应头与正文数据。

公开字段

status_code int
字段(int)
body string
正文

struct Client

XuAPI
pub struct Client {
    pub host string
    pub port int
    pub secure bool
    pub api_key string
}

用途

Qdrant 客户端会话实例,维护与服务端的连接并提供操作接口。

公开字段

host string
主机
port int
端口
secure bool
字段(bool)
api_key string
字段(string)

new_client

XuAPI
pub fn new_client(endpoint string, api_key string) Client

用途

构造 client。

参数

endpoint string
endpoint(string)
api_key string
api_key(string)

返回值

Client — 该类型的值

($&Client) request

XuAPI
pub fn (c $&Client) request(method string, path string, body string) Response

用途

$&Client 上的操作:请求。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
method string
method(string)
path string
文件系统路径(string)
body string
正文(string)

返回值

Response — 该类型的值

($&Client) ping

XuAPI
pub fn (c $&Client) ping() bool

用途

发送心跳保活探测请求,检测连接活性与服务可用性。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

bool — 是否成立

($&Client) create_collection

XuAPI
pub fn (c $&Client) create_collection(name string, vector_size int, distance string) bool

用途

$&Client 上的操作:执行 create collection 操作。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
name string
名字(string)
vector_size int
vector_size(int)
distance string
distance(string)

返回值

bool — 是否成立

($&Client) collection_exists

XuAPI
pub fn (c $&Client) collection_exists(name string) bool

用途

$&Client 上的操作:执行 collection exists 操作。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
name string
名字(string)

返回值

bool — 是否成立

($&Client) delete_collection

XuAPI
pub fn (c $&Client) delete_collection(name string) bool

用途

$&Client 上的操作:执行 delete collection 操作。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
name string
名字(string)

返回值

bool — 是否成立

struct Point

XuAPI
pub struct Point {
    pub id int
    pub vector []string
    pub payload_json string
}

用途

承载 id、vector、payload_json 等属性的结构体。

公开字段

id int
字段(int)
vector []string
字段([]string)
payload_json string
字段(string)

struct ScoredPoint

XuAPI
pub struct ScoredPoint {
    pub id int
    pub score string
    pub payload_json string
}

用途

承载 id、score、payload_json 等属性的结构体。

公开字段

id int
字段(int)
score string
字段(string)
payload_json string
字段(string)

($&Client) upsert_point

XuAPI
pub fn (c $&Client) upsert_point(collection string, id int, vector []string, payload_json string) bool

用途

$&Client 上的操作:执行 upsert point 操作。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
collection string
collection(string)
id int
id(int)
vector []string
vector([]string)
payload_json string
payload_json(string)

返回值

bool — 是否成立

struct Pool

XuAPI
pub 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 string
主机
port int
端口
secure bool
字段(bool)
api_key string
字段(string)
state chan pool.State
字段(通道;类型 chan pool.State)
max_idle int
字段(int)

new_pool

XuAPI
pub fn new_pool(endpoint string, api_key string, max_idle int) Pool

用途

构造 pool。

参数

endpoint string
endpoint(string)
api_key string
api_key(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

open_pool

XuAPI
pub fn open_pool(endpoint string, api_key string, max_idle int) Pool

用途

执行 open pool 操作。

参数

endpoint string
endpoint(string)
api_key string
api_key(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

查看用法示例

(&Pool) get

XuAPI
pub fn (p &Pool) get() Client

用途

在 &Pool 上执行 get 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

Client — 该类型的值

(&Pool) dup

XuAPI
pub fn (p &Pool) dup() Pool

用途

创建并返回持有独立状态或底层的副本对象。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

Pool — 该类型的值

(&Pool) get_conn

XuAPI
pub fn (p &Pool) get_conn() int

用途

&Pool 上的操作:读取 conn。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

int — 该类型的值

(&Pool) put_conn

XuAPI
pub fn (p &Pool) put_conn(fd int)

用途

&Pool 上的操作:执行 put conn 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

(&Pool) discard_conn

XuAPI
pub fn (p &Pool) discard_conn(fd int)

用途

&Pool 上的操作:丢弃连接。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

(&Pool) create_collection

XuAPI
pub fn (p &Pool) create_collection(name string, vector_size int, distance string) bool

用途

&Pool 上的操作:执行 create collection 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
name string
名字(string)
vector_size int
vector_size(int)
distance string
distance(string)

返回值

bool — 是否成立

(&Pool) collection_exists

XuAPI
pub fn (p &Pool) collection_exists(name string) bool

用途

&Pool 上的操作:执行 collection exists 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
name string
名字(string)

返回值

bool — 是否成立

(&Pool) delete_collection

XuAPI
pub fn (p &Pool) delete_collection(name string) bool

用途

&Pool 上的操作:执行 delete collection 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
name string
名字(string)

返回值

bool — 是否成立

(&Pool) upsert_point

XuAPI
pub fn (p &Pool) upsert_point(collection string, id int, vector []string, payload_json string) bool

用途

&Pool 上的操作:执行 upsert point 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
collection string
collection(string)
id int
id(int)
vector []string
vector([]string)
payload_json string
payload_json(string)

返回值

bool — 是否成立

(&Pool) ping

XuAPI
pub fn (p &Pool) ping() bool

用途

发送心跳保活探测请求,检测连接活性与服务可用性。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

bool — 是否成立

(&Pool) close

XuAPI
pub fn (p &Pool) close()

用途

&Pool 上的操作:关闭资源。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

不返回值。