2024-12-22 20:56:52 -03:00
|
|
|
use gb_core::{Result, Error};
|
2024-12-24 13:05:54 -03:00
|
|
|
use tikv_client::{RawClient, Config, KvPair};
|
|
|
|
use tracing::{error, instrument};
|
2024-12-22 20:56:52 -03:00
|
|
|
|
|
|
|
pub struct TiKVStorage {
|
|
|
|
client: RawClient,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TiKVStorage {
|
|
|
|
pub async fn new(pd_endpoints: Vec<String>) -> Result<Self> {
|
|
|
|
let config = Config::default();
|
2024-12-24 13:05:54 -03:00
|
|
|
let client = RawClient::new(pd_endpoints)
|
2024-12-22 20:56:52 -03:00
|
|
|
.await
|
2024-12-24 13:05:54 -03:00
|
|
|
.map_err(|e| Error::internal(format!("TiKV error: {}", e)))?;
|
2024-12-22 20:56:52 -03:00
|
|
|
|
|
|
|
Ok(Self { client })
|
|
|
|
}
|
|
|
|
|
2024-12-24 13:05:54 -03:00
|
|
|
#[instrument(skip(self))]
|
|
|
|
pub async fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
|
2024-12-22 20:56:52 -03:00
|
|
|
self.client
|
2024-12-24 13:05:54 -03:00
|
|
|
.get(key.to_vec())
|
2024-12-22 20:56:52 -03:00
|
|
|
.await
|
|
|
|
.map_err(|e| {
|
2024-12-24 13:05:54 -03:00
|
|
|
error!("TiKV get error: {}", e);
|
2024-12-23 00:54:50 -03:00
|
|
|
Error::internal(format!("TiKV error: {}", e))
|
2024-12-22 20:56:52 -03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument(skip(self))]
|
2024-12-24 13:05:54 -03:00
|
|
|
pub async fn put(&self, key: &[u8], value: &[u8]) -> Result<()> {
|
2024-12-22 20:56:52 -03:00
|
|
|
self.client
|
2024-12-24 13:05:54 -03:00
|
|
|
.put(key.to_vec(), value.to_vec())
|
2024-12-22 20:56:52 -03:00
|
|
|
.await
|
|
|
|
.map_err(|e| {
|
2024-12-24 13:05:54 -03:00
|
|
|
error!("TiKV put error: {}", e);
|
2024-12-23 00:54:50 -03:00
|
|
|
Error::internal(format!("TiKV error: {}", e))
|
2024-12-22 20:56:52 -03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument(skip(self))]
|
|
|
|
pub async fn delete(&self, key: &[u8]) -> Result<()> {
|
|
|
|
self.client
|
|
|
|
.delete(key.to_vec())
|
|
|
|
.await
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("TiKV delete error: {}", e);
|
2024-12-23 00:54:50 -03:00
|
|
|
Error::internal(format!("TiKV error: {}", e))
|
2024-12-22 20:56:52 -03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument(skip(self))]
|
2024-12-24 13:05:54 -03:00
|
|
|
pub async fn batch_get(&self, keys: Vec<Vec<u8>>) -> Result<Vec<KvPair>> {
|
2024-12-22 20:56:52 -03:00
|
|
|
self.client
|
|
|
|
.batch_get(keys)
|
|
|
|
.await
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("TiKV batch get error: {}", e);
|
2024-12-23 00:54:50 -03:00
|
|
|
Error::internal(format!("TiKV error: {}", e))
|
2024-12-22 20:56:52 -03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument(skip(self))]
|
2024-12-24 13:05:54 -03:00
|
|
|
pub async fn scan(&self, start: &[u8], end: &[u8], limit: u32) -> Result<Vec<KvPair>> {
|
2024-12-22 20:56:52 -03:00
|
|
|
self.client
|
|
|
|
.scan(start.to_vec()..end.to_vec(), limit)
|
|
|
|
.await
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("TiKV scan error: {}", e);
|
2024-12-23 00:54:50 -03:00
|
|
|
Error::internal(format!("TiKV error: {}", e))
|
2024-12-22 20:56:52 -03:00
|
|
|
})
|
|
|
|
}
|
2024-12-24 13:05:54 -03:00
|
|
|
}
|