use std::sync::Arc;
use surrealdb_core::dbs::Session;
use surrealdb_core::kvs::Datastore;
use surrealdb_core::rpc::{DbResult, Method, RpcProtocol, method_not_found};
use surrealdb_types::{Array, Error as TypesError, HashMap, Value};
use tokio::sync::RwLock;
use uuid::Uuid;
use crate::cnf::{PKG_NAME, PKG_VERSION};
pub struct Http {
pub kvs: Arc<Datastore>,
pub sessions: HashMap<Option<Uuid>, Arc<RwLock<Session>>>,
}
impl Http {
pub fn new(kvs: Arc<Datastore>, session: Session) -> Self {
let http = Self {
kvs,
sessions: HashMap::new(),
};
http.sessions.insert(None, Arc::new(RwLock::new(session)));
http
}
}
impl RpcProtocol for Http {
fn kvs(&self) -> &Datastore {
&self.kvs
}
fn version_data(&self) -> DbResult {
let value = Value::String(format!("{PKG_NAME}-{}", *PKG_VERSION));
DbResult::Other(value)
}
fn session_map(&self) -> &HashMap<Option<Uuid>, Arc<RwLock<Session>>> {
&self.sessions
}
const LQ_SUPPORT: bool = false;
async fn cleanup_lqs(&self, _session_id: Option<&Uuid>) {
}
async fn cleanup_all_lqs(&self) {
}
async fn begin(
&self,
_txn: Option<Uuid>,
_session_id: Option<Uuid>,
) -> Result<DbResult, TypesError> {
Err(method_not_found(Method::Begin.to_string()))
}
async fn commit(
&self,
_txn: Option<Uuid>,
_session_id: Option<Uuid>,
_params: Array,
) -> Result<DbResult, TypesError> {
Err(method_not_found(Method::Commit.to_string()))
}
async fn cancel(
&self,
_txn: Option<Uuid>,
_session_id: Option<Uuid>,
_params: Array,
) -> Result<DbResult, TypesError> {
Err(method_not_found(Method::Cancel.to_string()))
}
}