Files
ChuSQL/chusql-storage/src/protocol.rs
T

127 lines
2.6 KiB
Rust
Raw Normal View History

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
2026-09-24 21:28:49 +08:00
// 协议:Haskell 与 Rust 之间的请求 / 响应类型。
pub type Row = HashMap<String, serde_json::Value>;
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum ColumnType {
Int,
Str,
Bool,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SchemaColumn {
pub name: String,
pub ty: ColumnType,
}
2026-09-25 14:21:55 +08:00
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ColumnStatWire {
pub name: String,
pub distinct: u64,
#[serde(default)]
pub capped: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct IndexWire {
pub column: String,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "method", rename_all = "snake_case")]
pub enum Request {
Ping,
2026-09-25 14:21:55 +08:00
Scan {
table: String,
},
Insert {
table: String,
row: Row,
},
2026-09-25 14:21:55 +08:00
InsertBatch {
table: String,
rows: Vec<Row>,
},
DeleteKeys {
table: String,
keys: Vec<i64>,
},
LookupByIndex {
table: String,
#[serde(default = "default_id_column")]
column: String,
key: i64,
},
ReplaceAll {
table: String,
rows: Vec<Row>,
},
ListTables,
2026-09-25 14:21:55 +08:00
DescribeTable {
table: String,
},
2026-09-24 21:28:49 +08:00
CreateTable {
table: String,
columns: Vec<SchemaColumn>,
},
2026-09-25 14:21:55 +08:00
DropTable {
table: String,
},
ListCatalog,
CreateIndex {
table: String,
column: String,
},
DropIndex {
table: String,
column: String,
},
2026-09-25 17:41:51 +08:00
DropColumn {
table: String,
column: String,
},
2026-09-25 14:21:55 +08:00
}
/// 没写 column 时按 `id` 算
fn default_id_column() -> String {
"id".to_string()
}
#[derive(Debug, Serialize, Deserialize)]
2026-09-25 14:21:55 +08:00
#[serde(tag = "status", rename_all = "snake_case")]
pub enum Response {
Pong,
Rows { rows: Vec<Row> },
Tables { tables: Vec<String> },
2026-09-24 21:28:49 +08:00
Schema {
2026-09-25 14:21:55 +08:00
table: String,
2026-09-24 21:28:49 +08:00
columns: Vec<SchemaColumn>,
#[serde(default)]
row_count: u64,
2026-09-25 14:21:55 +08:00
#[serde(default)]
indexes: Vec<IndexWire>,
#[serde(default)]
stats: Vec<ColumnStatWire>,
2026-09-24 21:28:49 +08:00
},
2026-09-25 14:21:55 +08:00
NoIndex,
Ok,
Error { message: String },
2026-09-24 23:56:07 +08:00
Catalog { schemas: Vec<TableSchemaWire> },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TableSchemaWire {
pub table: String,
pub columns: Vec<SchemaColumn>,
#[serde(default)]
pub row_count: u64,
2026-09-25 14:21:55 +08:00
#[serde(default)]
pub indexes: Vec<IndexWire>,
#[serde(default)]
pub stats: Vec<ColumnStatWire>,
}