2022-10-09 16:50:40 +02:00
//! Waku [store](https://rfc.vac.dev/spec/36/#waku-store) handling methods
// std
2023-02-14 18:30:08 +01:00
use std ::ffi ::CString ;
2022-10-09 16:50:40 +02:00
use std ::time ::Duration ;
// crates
// internal
2023-02-14 18:30:08 +01:00
use crate ::general ::{ PeerId , Result , StoreQuery , StoreResponse } ;
use crate ::utils ::decode_and_free_response ;
2022-10-09 16:50:40 +02:00
/// Retrieves historical messages on specific content topics. This method may be called with [`PagingOptions`](`crate::general::PagingOptions`),
/// to retrieve historical messages on a per-page basis. If the request included [`PagingOptions`](`crate::general::PagingOptions`),
/// the node must return messages on a per-page basis and include [`PagingOptions`](`crate::general::PagingOptions`) in the response.
/// These [`PagingOptions`](`crate::general::PagingOptions`) must contain a cursor pointing to the Index from which a new page can be requested
pub fn waku_store_query (
query : & StoreQuery ,
2022-10-19 15:58:09 +02:00
peer_id : & PeerId ,
timeout : Option < Duration > ,
2022-10-09 16:50:40 +02:00
) -> Result < StoreResponse > {
2023-02-14 18:30:08 +01:00
let query_ptr = CString ::new (
serde_json ::to_string ( query ) . expect ( " StoreQuery should always be able to be serialized " ) ,
)
. expect ( " CString should build properly from the serialized filter subscription " )
. into_raw ( ) ;
let peer_id_ptr = CString ::new ( peer_id . clone ( ) )
. expect ( " CString should build properly from peer id " )
. into_raw ( ) ;
let result_ptr = unsafe {
let res = waku_sys ::waku_store_query (
query_ptr ,
peer_id_ptr ,
2022-10-09 16:50:40 +02:00
timeout
2022-10-19 15:58:09 +02:00
. map ( | timeout | {
timeout
. as_millis ( )
. try_into ( )
. expect ( " Duration as milliseconds should fit in a i32 " )
} )
. unwrap_or ( 0 ) ,
2023-02-14 18:30:08 +01:00
) ;
drop ( CString ::from_raw ( query_ptr ) ) ;
drop ( CString ::from_raw ( peer_id_ptr ) ) ;
res
} ;
2022-10-09 16:50:40 +02:00
2023-02-14 18:30:08 +01:00
decode_and_free_response ( result_ptr )
2022-10-09 16:50:40 +02:00
}
2023-02-13 13:46:39 +01:00
/// Retrieves locally stored historical messages on specific content topics from the local archive system. This method may be called with [`PagingOptions`](`crate::general::PagingOptions`),
/// to retrieve historical messages on a per-page basis. If the request included [`PagingOptions`](`crate::general::PagingOptions`),
/// the node must return messages on a per-page basis and include [`PagingOptions`](`crate::general::PagingOptions`) in the response.
/// These [`PagingOptions`](`crate::general::PagingOptions`) must contain a cursor pointing to the Index from which a new page can be requested
pub fn waku_local_store_query ( query : & StoreQuery ) -> Result < StoreResponse > {
let result = unsafe {
CStr ::from_ptr ( waku_sys ::waku_store_local_query (
CString ::new (
serde_json ::to_string ( query )
. expect ( " StoreQuery should always be able to be serialized " ) ,
)
. expect ( " CString should build properly from the serialized filter subscription " )
. into_raw ( ) ,
) )
}
. to_str ( )
. expect ( " Response should always succeed to load to a &str " ) ;
let response : JsonResponse < StoreResponse > =
serde_json ::from_str ( result ) . expect ( " JsonResponse should always succeed to deserialize " ) ;
response . into ( )
}