Query store chronologically

This commit is contained in:
Joscha 2022-08-03 01:45:43 +02:00
parent 01ee4b4ce8
commit 1f19b4cdf5
3 changed files with 205 additions and 21 deletions

View file

@ -85,6 +85,15 @@ impl MsgStore<LogMsg> for Logger {
Tree::new(*tree_id, msgs) Tree::new(*tree_id, msgs)
} }
async fn first_tree_id(&self) -> Option<usize> {
let empty = self.messages.lock().is_empty();
Some(0).filter(|_| !empty)
}
async fn last_tree_id(&self) -> Option<usize> {
self.messages.lock().len().checked_sub(1)
}
async fn prev_tree_id(&self, tree_id: &usize) -> Option<usize> { async fn prev_tree_id(&self, tree_id: &usize) -> Option<usize> {
tree_id.checked_sub(1) tree_id.checked_sub(1)
} }
@ -94,13 +103,20 @@ impl MsgStore<LogMsg> for Logger {
tree_id.checked_add(1).filter(|t| *t < len) tree_id.checked_add(1).filter(|t| *t < len)
} }
async fn first_tree_id(&self) -> Option<usize> { async fn oldest_msg_id(&self) -> Option<usize> {
let empty = self.messages.lock().is_empty(); self.first_tree_id().await
Some(0).filter(|_| !empty)
} }
async fn last_tree_id(&self) -> Option<usize> { async fn newest_msg_id(&self) -> Option<usize> {
self.messages.lock().len().checked_sub(1) self.last_tree_id().await
}
async fn older_msg_id(&self, id: &usize) -> Option<usize> {
self.prev_tree_id(id).await
}
async fn newer_msg_id(&self, id: &usize) -> Option<usize> {
self.next_tree_id(id).await
} }
} }

View file

@ -120,8 +120,12 @@ impl<M: Msg> Tree<M> {
pub trait MsgStore<M: Msg> { pub trait MsgStore<M: Msg> {
async fn path(&self, id: &M::Id) -> Path<M::Id>; async fn path(&self, id: &M::Id) -> Path<M::Id>;
async fn tree(&self, tree_id: &M::Id) -> Tree<M>; async fn tree(&self, tree_id: &M::Id) -> Tree<M>;
async fn prev_tree_id(&self, tree_id: &M::Id) -> Option<M::Id>;
async fn next_tree_id(&self, tree_id: &M::Id) -> Option<M::Id>;
async fn first_tree_id(&self) -> Option<M::Id>; async fn first_tree_id(&self) -> Option<M::Id>;
async fn last_tree_id(&self) -> Option<M::Id>; async fn last_tree_id(&self) -> Option<M::Id>;
async fn prev_tree_id(&self, tree_id: &M::Id) -> Option<M::Id>;
async fn next_tree_id(&self, tree_id: &M::Id) -> Option<M::Id>;
async fn oldest_msg_id(&self) -> Option<M::Id>;
async fn newest_msg_id(&self) -> Option<M::Id>;
async fn older_msg_id(&self, id: &M::Id) -> Option<M::Id>;
async fn newer_msg_id(&self, id: &M::Id) -> Option<M::Id>;
} }

View file

@ -155,6 +155,28 @@ impl MsgStore<SmallMessage> for EuphVault {
rx.await.unwrap() rx.await.unwrap()
} }
async fn first_tree_id(&self) -> Option<Snowflake> {
// TODO vault::Error
let (tx, rx) = oneshot::channel();
let request = EuphRequest::GetFirstTreeId {
room: self.room.clone(),
result: tx,
};
let _ = self.vault.tx.send(request.into());
rx.await.unwrap()
}
async fn last_tree_id(&self) -> Option<Snowflake> {
// TODO vault::Error
let (tx, rx) = oneshot::channel();
let request = EuphRequest::GetLastTreeId {
room: self.room.clone(),
result: tx,
};
let _ = self.vault.tx.send(request.into());
rx.await.unwrap()
}
async fn prev_tree_id(&self, tree_id: &Snowflake) -> Option<Snowflake> { async fn prev_tree_id(&self, tree_id: &Snowflake) -> Option<Snowflake> {
// TODO vault::Error // TODO vault::Error
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
@ -179,10 +201,10 @@ impl MsgStore<SmallMessage> for EuphVault {
rx.await.unwrap() rx.await.unwrap()
} }
async fn first_tree_id(&self) -> Option<Snowflake> { async fn oldest_msg_id(&self) -> Option<Snowflake> {
// TODO vault::Error // TODO vault::Error
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
let request = EuphRequest::GetFirstTreeId { let request = EuphRequest::GetOldestMsgId {
room: self.room.clone(), room: self.room.clone(),
result: tx, result: tx,
}; };
@ -190,16 +212,40 @@ impl MsgStore<SmallMessage> for EuphVault {
rx.await.unwrap() rx.await.unwrap()
} }
async fn last_tree_id(&self) -> Option<Snowflake> { async fn newest_msg_id(&self) -> Option<Snowflake> {
// TODO vault::Error // TODO vault::Error
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
let request = EuphRequest::GetLastTreeId { let request = EuphRequest::GetNewestMsgId {
room: self.room.clone(), room: self.room.clone(),
result: tx, result: tx,
}; };
let _ = self.vault.tx.send(request.into()); let _ = self.vault.tx.send(request.into());
rx.await.unwrap() rx.await.unwrap()
} }
async fn older_msg_id(&self, id: &Snowflake) -> Option<Snowflake> {
// TODO vault::Error
let (tx, rx) = oneshot::channel();
let request = EuphRequest::GetOlderMsgId {
room: self.room.clone(),
id: *id,
result: tx,
};
let _ = self.vault.tx.send(request.into());
rx.await.unwrap()
}
async fn newer_msg_id(&self, id: &Snowflake) -> Option<Snowflake> {
// TODO vault::Error
let (tx, rx) = oneshot::channel();
let request = EuphRequest::GetNewerMsgId {
room: self.room.clone(),
id: *id,
result: tx,
};
let _ = self.vault.tx.send(request.into());
rx.await.unwrap()
}
} }
pub(super) enum EuphRequest { pub(super) enum EuphRequest {
@ -254,6 +300,14 @@ pub(super) enum EuphRequest {
root: Snowflake, root: Snowflake,
result: oneshot::Sender<Tree<SmallMessage>>, result: oneshot::Sender<Tree<SmallMessage>>,
}, },
GetFirstTreeId {
room: String,
result: oneshot::Sender<Option<Snowflake>>,
},
GetLastTreeId {
room: String,
result: oneshot::Sender<Option<Snowflake>>,
},
GetPrevTreeId { GetPrevTreeId {
room: String, room: String,
root: Snowflake, root: Snowflake,
@ -264,14 +318,24 @@ pub(super) enum EuphRequest {
root: Snowflake, root: Snowflake,
result: oneshot::Sender<Option<Snowflake>>, result: oneshot::Sender<Option<Snowflake>>,
}, },
GetFirstTreeId { GetOldestMsgId {
room: String, room: String,
result: oneshot::Sender<Option<Snowflake>>, result: oneshot::Sender<Option<Snowflake>>,
}, },
GetLastTreeId { GetNewestMsgId {
room: String, room: String,
result: oneshot::Sender<Option<Snowflake>>, result: oneshot::Sender<Option<Snowflake>>,
}, },
GetOlderMsgId {
room: String,
id: Snowflake,
result: oneshot::Sender<Option<Snowflake>>,
},
GetNewerMsgId {
room: String,
id: Snowflake,
result: oneshot::Sender<Option<Snowflake>>,
},
} }
impl EuphRequest { impl EuphRequest {
@ -295,17 +359,29 @@ impl EuphRequest {
EuphRequest::GetLastSpan { room, result } => Self::get_last_span(conn, room, result), EuphRequest::GetLastSpan { room, result } => Self::get_last_span(conn, room, result),
EuphRequest::GetPath { room, id, result } => Self::get_path(conn, room, id, result), EuphRequest::GetPath { room, id, result } => Self::get_path(conn, room, id, result),
EuphRequest::GetTree { room, root, result } => Self::get_tree(conn, room, root, result), EuphRequest::GetTree { room, root, result } => Self::get_tree(conn, room, root, result),
EuphRequest::GetFirstTreeId { room, result } => {
Self::get_first_tree_id(conn, room, result)
}
EuphRequest::GetLastTreeId { room, result } => {
Self::get_last_tree_id(conn, room, result)
}
EuphRequest::GetPrevTreeId { room, root, result } => { EuphRequest::GetPrevTreeId { room, root, result } => {
Self::get_prev_tree_id(conn, room, root, result) Self::get_prev_tree_id(conn, room, root, result)
} }
EuphRequest::GetNextTreeId { room, root, result } => { EuphRequest::GetNextTreeId { room, root, result } => {
Self::get_next_tree_id(conn, room, root, result) Self::get_next_tree_id(conn, room, root, result)
} }
EuphRequest::GetFirstTreeId { room, result } => { EuphRequest::GetOldestMsgId { room, result } => {
Self::get_first_tree_id(conn, room, result) Self::get_oldest_msg_id(conn, room, result)
} }
EuphRequest::GetLastTreeId { room, result } => { EuphRequest::GetNewestMsgId { room, result } => {
Self::get_last_tree_id(conn, room, result) Self::get_newest_msg_id(conn, room, result)
}
EuphRequest::GetOlderMsgId { room, id, result } => {
Self::get_older_msg_id(conn, room, id, result)
}
EuphRequest::GetNewerMsgId { room, id, result } => {
Self::get_newer_msg_id(conn, room, id, result)
} }
}; };
if let Err(e) = result { if let Err(e) = result {
@ -679,6 +755,48 @@ impl EuphRequest {
Ok(()) Ok(())
} }
fn get_first_tree_id(
conn: &Connection,
room: String,
result: oneshot::Sender<Option<Snowflake>>,
) -> rusqlite::Result<()> {
let tree = conn
.prepare(
"
SELECT id
FROM euph_trees
WHERE room = ?
ORDER BY id ASC
LIMIT 1
",
)?
.query_row([room], |row| row.get(0))
.optional()?;
let _ = result.send(tree);
Ok(())
}
fn get_last_tree_id(
conn: &Connection,
room: String,
result: oneshot::Sender<Option<Snowflake>>,
) -> rusqlite::Result<()> {
let tree = conn
.prepare(
"
SELECT id
FROM euph_trees
WHERE room = ?
ORDER BY id DESC
LIMIT 1
",
)?
.query_row([room], |row| row.get(0))
.optional()?;
let _ = result.send(tree);
Ok(())
}
fn get_prev_tree_id( fn get_prev_tree_id(
conn: &Connection, conn: &Connection,
room: String, room: String,
@ -725,7 +843,7 @@ impl EuphRequest {
Ok(()) Ok(())
} }
fn get_first_tree_id( fn get_oldest_msg_id(
conn: &Connection, conn: &Connection,
room: String, room: String,
result: oneshot::Sender<Option<Snowflake>>, result: oneshot::Sender<Option<Snowflake>>,
@ -734,7 +852,7 @@ impl EuphRequest {
.prepare( .prepare(
" "
SELECT id SELECT id
FROM euph_trees FROM euph_msgs
WHERE room = ? WHERE room = ?
ORDER BY id ASC ORDER BY id ASC
LIMIT 1 LIMIT 1
@ -746,7 +864,7 @@ impl EuphRequest {
Ok(()) Ok(())
} }
fn get_last_tree_id( fn get_newest_msg_id(
conn: &Connection, conn: &Connection,
room: String, room: String,
result: oneshot::Sender<Option<Snowflake>>, result: oneshot::Sender<Option<Snowflake>>,
@ -755,7 +873,7 @@ impl EuphRequest {
.prepare( .prepare(
" "
SELECT id SELECT id
FROM euph_trees FROM euph_msgs
WHERE room = ? WHERE room = ?
ORDER BY id DESC ORDER BY id DESC
LIMIT 1 LIMIT 1
@ -766,4 +884,50 @@ impl EuphRequest {
let _ = result.send(tree); let _ = result.send(tree);
Ok(()) Ok(())
} }
fn get_older_msg_id(
conn: &Connection,
room: String,
id: Snowflake,
result: oneshot::Sender<Option<Snowflake>>,
) -> rusqlite::Result<()> {
let tree = conn
.prepare(
"
SELECT id
FROM euph_msgs
WHERE room = ?
AND id < ?
ORDER BY id DESC
LIMIT 1
",
)?
.query_row(params![room, id], |row| row.get(0))
.optional()?;
let _ = result.send(tree);
Ok(())
}
fn get_newer_msg_id(
conn: &Connection,
room: String,
id: Snowflake,
result: oneshot::Sender<Option<Snowflake>>,
) -> rusqlite::Result<()> {
let tree = conn
.prepare(
"
SELECT id
FROM euph_msgs
WHERE room = ?
AND id > ?
ORDER BY id ASC
LIMIT 1
",
)?
.query_row(params![room, id], |row| row.get(0))
.optional()?;
let _ = result.send(tree);
Ok(())
}
} }