falcot/src/common/protocol.rs

155 lines
3.9 KiB
Rust
Raw Normal View History

2025-09-12 00:40:20 +03:00
// src/common/protocol.rs
//! Протокол обмена данными для Falcot
//!
//! Определяет структуры команд и ответов для взаимодействия между
//! компонентами системы с использованием wait-free сериализации.
#![allow(dead_code)]
use serde::{Deserialize, Serialize};
use crate::server::database::Index;
/// Команды для выполнения в базе данных
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Command {
Create {
collection: String,
document: Vec<u8>,
},
Read {
collection: String,
id: String,
},
Update {
collection: String,
id: String,
document: Vec<u8>,
},
Delete {
collection: String,
id: String,
},
Query {
collection: String,
filter: Vec<u8>,
},
CreateProcedure {
name: String,
code: Vec<u8>,
},
CallProcedure {
name: String,
},
BeginTransaction {
transaction_id: String,
},
CommitTransaction {
transaction_id: String,
},
RollbackTransaction {
transaction_id: String,
},
CreateIndex {
collection: String,
index: Index,
},
QueryByIndex {
collection: String,
index_name: String,
value: Vec<u8>,
},
// Новые команды для шардинга
AddShardNode {
node_id: String,
address: String,
capacity: u64,
},
RemoveShardNode {
node_id: String,
},
MigrateShard {
collection: String,
from_node: String,
to_node: String,
shard_key: String,
},
RebalanceCluster,
GetClusterStatus,
// Команды для constraints
AddConstraint {
collection: String,
constraint_name: String,
constraint_type: String,
field: String,
value: Vec<u8>,
},
RemoveConstraint {
collection: String,
constraint_name: String,
},
// Команды для компрессии
EnableCompression {
collection: String,
algorithm: String,
},
DisableCompression {
collection: String,
},
// Команды для глобальных индексов
CreateGlobalIndex {
name: String,
field: String,
unique: bool,
},
QueryGlobalIndex {
index_name: String,
value: Vec<u8>,
},
}
/// Ответы от базы данных
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Response {
Success(Vec<u8>),
Error(String),
}
/// Сообщение для репликации
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReplicationMessage {
pub sequence: u64,
pub command: Command,
pub timestamp: i64,
}
/// Структура для информации о шарде
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShardInfo {
pub node_id: String,
pub address: String,
pub capacity: u64,
pub used: u64,
pub collections: Vec<String>,
}
/// Структура для статуса кластера
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClusterStatus {
pub nodes: Vec<ShardInfo>,
pub total_capacity: u64,
pub total_used: u64,
pub rebalance_needed: bool,
}
/// Wait-Free сериализация сообщений
pub fn serialize<T: serde::Serialize>(value: &T) -> crate::common::error::Result<Vec<u8>> {
rmp_serde::to_vec(value)
.map_err(|e| crate::common::error::FalcotError::SerializationError(e.to_string()))
}
/// Wait-Free десериализация сообщений
pub fn deserialize<'a, T: serde::Deserialize<'a>>(bytes: &'a [u8]) -> crate::common::error::Result<T> {
rmp_serde::from_slice(bytes)
.map_err(|e| crate::common::error::FalcotError::SerializationError(e.to_string()))
}