-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement kvs postgres backend
- Loading branch information
Showing
10 changed files
with
212 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
use std::any::Any; | ||
use std::sync::Arc; | ||
|
||
use snafu::ResultExt; | ||
use tokio_postgres::{Client, NoTls}; | ||
|
||
use super::{KvBackend, TxnService}; | ||
use crate::error::{Error, PostgresFailedSnafu, Result}; | ||
use crate::kv_backend::txn::{Txn as KvTxn, TxnResponse as KvTxnResponse}; | ||
use crate::kv_backend::KvBackendRef; | ||
use crate::rpc::store::{ | ||
BatchDeleteRequest, BatchDeleteResponse, BatchGetRequest, BatchGetResponse, BatchPutRequest, | ||
BatchPutResponse, DeleteRangeRequest, DeleteRangeResponse, PutRequest, PutResponse, | ||
RangeRequest, RangeResponse, | ||
}; | ||
use crate::rpc::KeyValue; | ||
|
||
pub struct PgStore { | ||
client: Client, | ||
} | ||
|
||
const METADKV_CREATION: &str = | ||
"CREATE TABLE IF NOT EXISTS metakv(k varchar PRIMARY KEY, v varchar);"; | ||
|
||
impl PgStore { | ||
pub async fn with_url(url: &str) -> Result<KvBackendRef> { | ||
let (client, _) = tokio_postgres::connect(url, NoTls).await.unwrap(); | ||
Self::with_pg_client(client).await | ||
} | ||
|
||
pub async fn with_pg_client(client: Client) -> Result<KvBackendRef> { | ||
// This step ensure the postgres metadata backend is ready to use by | ||
// checking if metadata kv table exists, and create a new table | ||
// if it does not exist. | ||
client | ||
.execute(METADKV_CREATION, &[]) | ||
.await | ||
.context(PostgresFailedSnafu)?; | ||
Ok(Arc::new(Self { client })) | ||
} | ||
|
||
/* | ||
SELECT K, V FROM metakv | ||
WHERE K >= start_key and k <= end_key | ||
*/ | ||
async fn range_scan( | ||
client: Client, | ||
start_key: String, | ||
end_key: Option<String>, | ||
) -> Result<Vec<KeyValue>> { | ||
todo!() | ||
} | ||
|
||
/* | ||
SELECT K, V FROM metakv | ||
WHERE K IN (keys) | ||
*/ | ||
async fn batch_scan(client: Client, keys: Vec<String>) -> Result<Vec<KeyValue>> { | ||
todo!() | ||
} | ||
|
||
/* | ||
BEGIN; | ||
SELECT K, V FROM metakv FOR UPDATE; --return k, v as previous kvs. | ||
INSERT INTO metakv VALUES (kvs) ON CONFLICT DO UPDATE; | ||
COMMIT; | ||
*/ | ||
async fn batch_upsert(client: Client, kvs: Vec<KeyValue>) -> Result<Vec<KeyValue>> { | ||
todo!() | ||
} | ||
|
||
/* | ||
DELETE FROM metakv WHERE k >= start_key and k <= end_key RETURNING K, V; | ||
*/ | ||
async fn range_delete( | ||
client: Client, | ||
start_key: String, | ||
end_key: Option<String>, | ||
) -> Result<Vec<KeyValue>> { | ||
todo!() | ||
} | ||
|
||
/* | ||
DELETE FROM metakv WHERE k IN (kvs) RETURNING K, V; | ||
*/ | ||
async fn BATCH_delete(client: Client, kvs: Vec<KeyValue>) -> Result<Vec<KeyValue>> { | ||
todo!() | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl KvBackend for PgStore { | ||
fn name(&self) -> &str { | ||
"Postgres" | ||
} | ||
|
||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
async fn range(&self, req: RangeRequest) -> Result<RangeResponse> { | ||
todo!() | ||
} | ||
|
||
async fn put(&self, req: PutRequest) -> Result<PutResponse> { | ||
todo!() | ||
} | ||
|
||
async fn batch_put(&self, req: BatchPutRequest) -> Result<BatchPutResponse> { | ||
todo!() | ||
} | ||
|
||
async fn batch_get(&self, req: BatchGetRequest) -> Result<BatchGetResponse> { | ||
todo!() | ||
} | ||
|
||
async fn delete_range(&self, req: DeleteRangeRequest) -> Result<DeleteRangeResponse> { | ||
todo!() | ||
} | ||
|
||
async fn batch_delete(&self, req: BatchDeleteRequest) -> Result<BatchDeleteResponse> { | ||
todo!() | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl TxnService for PgStore { | ||
type Error = Error; | ||
|
||
async fn txn(&self, txn: KvTxn) -> Result<KvTxnResponse> { | ||
todo!() | ||
} | ||
|
||
fn max_txn_ops(&self) -> usize { | ||
todo!() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters