-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Query history from history server
- Loading branch information
Showing
35 changed files
with
529 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod pg_history_service; | ||
pub use pg_history_service::PgHistoryService; | ||
mod server; | ||
pub use server::*; | ||
|
||
|
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,85 @@ | ||
use std::borrow::BorrowMut; | ||
use std::collections::HashMap; | ||
|
||
use diesel::dsl::sql; | ||
use diesel::prelude::*; | ||
use diesel_async::{AsyncConnection, AsyncPgConnection}; | ||
use futures::stream::StreamExt; | ||
use tokio::sync::Mutex; | ||
use uuid::Uuid; | ||
|
||
use sable_network::prelude::*; | ||
|
||
use crate::schema::channels; | ||
|
||
/// Implementation of [`HistoryService`] backed PostgreSQL | ||
pub struct PgHistoryService<'a> { | ||
database_connection: &'a Mutex<AsyncPgConnection>, | ||
} | ||
|
||
impl<'a> PgHistoryService<'a> { | ||
pub fn new(database_connection: &'a Mutex<AsyncPgConnection>) -> Self { | ||
Self { | ||
database_connection, | ||
} | ||
} | ||
} | ||
|
||
impl<'a> HistoryService for PgHistoryService<'a> { | ||
async fn list_targets( | ||
Check failure on line 29 in sable_history/src/pg_history_service.rs GitHub Actions / Test (nightly)
|
||
&self, | ||
user: UserId, | ||
after_ts: Option<i64>, | ||
before_ts: Option<i64>, | ||
limit: Option<usize>, | ||
) -> HashMap<TargetId, i64> { | ||
// TODO: access control | ||
match diesel_async::RunQueryDsl::load_stream( | ||
channels::dsl::channels.select(( | ||
channels::dsl::id, | ||
sql::<diesel::pg::sql_types::Uuid>( | ||
"SELECT MAX(id) FROM messages WHERE target_channel=channels.id", | ||
), | ||
)), | ||
&mut *self.database_connection.lock().await, | ||
) | ||
.await | ||
{ | ||
Err(e) => { | ||
tracing::error!("Could not get history channels: {e}"); | ||
return HashMap::new(); | ||
} | ||
Ok(rows) => { | ||
rows.map(|row| row.expect("Could not deserialize row")) | ||
.map( | ||
|(channel_id, max_message_id): (i64, Uuid)| -> (TargetId, i64) { | ||
let (seconds, _) = max_message_id | ||
.get_timestamp() | ||
.expect("messages.id is not a UUID7") | ||
.to_unix(); | ||
( | ||
TargetId::Channel(ChannelId::from(Snowflake::from( | ||
u64::try_from(channel_id).expect("channel id is negative"), | ||
))), | ||
seconds | ||
.try_into() | ||
.expect("message's UNIX timestamp is negative"), | ||
) | ||
}, | ||
) | ||
.collect() | ||
.await | ||
} | ||
} | ||
} | ||
|
||
async fn get_entries( | ||
&self, | ||
user: UserId, | ||
target: TargetId, | ||
request: HistoryRequest, | ||
) -> Result<impl IntoIterator<Item = HistoryLogEntry>, HistoryError> { | ||
todo!(); | ||
Ok(vec![]) | ||
} | ||
} |
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,12 @@ | ||
use sable_network::network::wrapper::ObjectWrapper; | ||
|
||
use super::*; | ||
use crate::server::event::IntroduceHistoryServer; | ||
|
||
impl HistoryServer { | ||
pub(super) async fn burst_to_network(&self) { | ||
// Set ourselves as the active history node | ||
self.node | ||
.submit_event(self.node.id(), IntroduceHistoryServer {}); | ||
} | ||
} |
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
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
Oops, something went wrong.