Skip to content

Commit 4b0a5a9

Browse files
committed
kv: add raw bytes helper types
1 parent 64e3e1f commit 4b0a5a9

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

src/kv.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{get_blob, Message, PackageId, Request};
2+
use alloy::rpc::types::error;
23
use serde::{de::DeserializeOwned, Deserialize, Serialize};
34
use std::marker::PhantomData;
45
use thiserror::Error;
@@ -364,6 +365,107 @@ where
364365
}
365366
}
366367

368+
impl Kv<Vec<u8>, Vec<u8>> {
369+
/// Get raw bytes directly
370+
pub fn get_raw(&self, key: &[u8]) -> anyhow::Result<Vec<u8>> {
371+
let res = Request::new()
372+
.target(("our", "kv", "distro", "sys"))
373+
.body(serde_json::to_vec(&KvRequest {
374+
package_id: self.package_id.clone(),
375+
db: self.db.clone(),
376+
action: KvAction::Get { key: key.to_vec() },
377+
})?)
378+
.send_and_await_response(self.timeout)?;
379+
380+
match res {
381+
Ok(Message::Response { body, .. }) => {
382+
let response = serde_json::from_slice::<KvResponse>(&body)?;
383+
384+
match response {
385+
KvResponse::Get { .. } => {
386+
let bytes = match get_blob() {
387+
Some(bytes) => bytes.bytes,
388+
None => return Err(anyhow::anyhow!("kv: no blob")),
389+
};
390+
Ok(bytes)
391+
}
392+
KvResponse::Err {
393+
0: error
394+
} => Err(error.into()),
395+
_ => Err(anyhow::anyhow!("kv: unexpected response {:?}", response)),
396+
}
397+
}
398+
_ => Err(anyhow::anyhow!("kv: unexpected message: {:?}", res)),
399+
}
400+
}
401+
402+
/// Set raw bytes directly
403+
pub fn set_raw(&self, key: &[u8], value: &[u8], tx_id: Option<u64>) -> anyhow::Result<()> {
404+
let res = Request::new()
405+
.target(("our", "kv", "distro", "sys"))
406+
.body(serde_json::to_vec(&KvRequest {
407+
package_id: self.package_id.clone(),
408+
db: self.db.clone(),
409+
action: KvAction::Set {
410+
key: key.to_vec(),
411+
tx_id,
412+
},
413+
})?)
414+
.blob_bytes(value.to_vec())
415+
.send_and_await_response(self.timeout)?;
416+
417+
match res {
418+
Ok(Message::Response { body, .. }) => {
419+
let response = serde_json::from_slice::<KvResponse>(&body)?;
420+
421+
match response {
422+
KvResponse::Ok => Ok(()),
423+
KvResponse::Err { 0: error } => Err(error.into()),
424+
_ => Err(anyhow::anyhow!("kv: unexpected response {:?}", response)),
425+
}
426+
}
427+
_ => Err(anyhow::anyhow!("kv: unexpected message: {:?}", res)),
428+
}
429+
}
430+
431+
/// Delete raw bytes directly
432+
pub fn delete_raw(&self, key: &[u8], tx_id: Option<u64>) -> anyhow::Result<()> {
433+
let res = Request::new()
434+
.target(("our", "kv", "distro", "sys"))
435+
.body(serde_json::to_vec(&KvRequest {
436+
package_id: self.package_id.clone(),
437+
db: self.db.clone(),
438+
action: KvAction::Delete {
439+
key: key.to_vec(),
440+
tx_id,
441+
},
442+
})?)
443+
.send_and_await_response(self.timeout)?;
444+
445+
match res {
446+
Ok(Message::Response { body, .. }) => {
447+
let response = serde_json::from_slice::<KvResponse>(&body)?;
448+
449+
match response {
450+
KvResponse::Ok => Ok(()),
451+
KvResponse::Err { 0: error } => Err(error.into()),
452+
_ => Err(anyhow::anyhow!("kv: unexpected response {:?}", response)),
453+
}
454+
}
455+
_ => Err(anyhow::anyhow!("kv: unexpected message: {:?}", res)),
456+
}
457+
}
458+
}
459+
460+
/// Helper function to open a raw bytes key-value store
461+
pub fn open_raw(
462+
package_id: PackageId,
463+
db: &str,
464+
timeout: Option<u64>,
465+
) -> anyhow::Result<Kv<Vec<u8>, Vec<u8>>> {
466+
open(package_id, db, timeout)
467+
}
468+
367469
/// Opens or creates a kv db.
368470
pub fn open<K, V>(package_id: PackageId, db: &str, timeout: Option<u64>) -> anyhow::Result<Kv<K, V>>
369471
where

0 commit comments

Comments
 (0)