Rename tree_id to root_id
This commit is contained in:
parent
da2c3d86f5
commit
cb1fdb41b8
6 changed files with 69 additions and 70 deletions
|
|
@ -20,11 +20,11 @@ pub async fn export_to_file(
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
let mut exported_trees = 0;
|
let mut exported_trees = 0;
|
||||||
let mut exported_msgs = 0;
|
let mut exported_msgs = 0;
|
||||||
let mut tree_id = vault.first_tree_id().await;
|
let mut root_id = vault.first_root_id().await;
|
||||||
while let Some(some_tree_id) = tree_id {
|
while let Some(some_root_id) = root_id {
|
||||||
let tree = vault.tree(some_tree_id).await;
|
let tree = vault.tree(some_root_id).await;
|
||||||
write_tree(file, &tree, some_tree_id, 0)?;
|
write_tree(file, &tree, some_root_id, 0)?;
|
||||||
tree_id = vault.next_tree_id(some_tree_id).await;
|
root_id = vault.next_root_id(some_root_id).await;
|
||||||
|
|
||||||
exported_trees += 1;
|
exported_trees += 1;
|
||||||
exported_msgs += tree.len();
|
exported_msgs += tree.len();
|
||||||
|
|
|
||||||
|
|
@ -83,48 +83,48 @@ impl MsgStore<LogMsg> for Logger {
|
||||||
self.messages.lock().get(*id).cloned()
|
self.messages.lock().get(*id).cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn tree(&self, tree_id: &usize) -> Tree<LogMsg> {
|
async fn tree(&self, root_id: &usize) -> Tree<LogMsg> {
|
||||||
let msgs = self
|
let msgs = self
|
||||||
.messages
|
.messages
|
||||||
.lock()
|
.lock()
|
||||||
.get(*tree_id)
|
.get(*root_id)
|
||||||
.map(|msg| vec![msg.clone()])
|
.map(|msg| vec![msg.clone()])
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
Tree::new(*tree_id, msgs)
|
Tree::new(*root_id, msgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn first_tree_id(&self) -> Option<usize> {
|
async fn first_root_id(&self) -> Option<usize> {
|
||||||
let empty = self.messages.lock().is_empty();
|
let empty = self.messages.lock().is_empty();
|
||||||
Some(0).filter(|_| !empty)
|
Some(0).filter(|_| !empty)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn last_tree_id(&self) -> Option<usize> {
|
async fn last_root_id(&self) -> Option<usize> {
|
||||||
self.messages.lock().len().checked_sub(1)
|
self.messages.lock().len().checked_sub(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn prev_tree_id(&self, tree_id: &usize) -> Option<usize> {
|
async fn prev_root_id(&self, root_id: &usize) -> Option<usize> {
|
||||||
tree_id.checked_sub(1)
|
root_id.checked_sub(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn next_tree_id(&self, tree_id: &usize) -> Option<usize> {
|
async fn next_root_id(&self, root_id: &usize) -> Option<usize> {
|
||||||
let len = self.messages.lock().len();
|
let len = self.messages.lock().len();
|
||||||
tree_id.checked_add(1).filter(|t| *t < len)
|
root_id.checked_add(1).filter(|t| *t < len)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn oldest_msg_id(&self) -> Option<usize> {
|
async fn oldest_msg_id(&self) -> Option<usize> {
|
||||||
self.first_tree_id().await
|
self.first_root_id().await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn newest_msg_id(&self) -> Option<usize> {
|
async fn newest_msg_id(&self) -> Option<usize> {
|
||||||
self.last_tree_id().await
|
self.last_root_id().await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn older_msg_id(&self, id: &usize) -> Option<usize> {
|
async fn older_msg_id(&self, id: &usize) -> Option<usize> {
|
||||||
self.prev_tree_id(id).await
|
self.prev_root_id(id).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn newer_msg_id(&self, id: &usize) -> Option<usize> {
|
async fn newer_msg_id(&self, id: &usize) -> Option<usize> {
|
||||||
self.next_tree_id(id).await
|
self.next_root_id(id).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn oldest_unseen_msg_id(&self) -> Option<usize> {
|
async fn oldest_unseen_msg_id(&self) -> Option<usize> {
|
||||||
|
|
|
||||||
10
src/store.rs
10
src/store.rs
|
|
@ -134,11 +134,11 @@ 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 msg(&self, id: &M::Id) -> Option<M>;
|
async fn msg(&self, id: &M::Id) -> Option<M>;
|
||||||
async fn tree(&self, tree_id: &M::Id) -> Tree<M>;
|
async fn tree(&self, root_id: &M::Id) -> Tree<M>;
|
||||||
async fn first_tree_id(&self) -> Option<M::Id>;
|
async fn first_root_id(&self) -> Option<M::Id>;
|
||||||
async fn last_tree_id(&self) -> Option<M::Id>;
|
async fn last_root_id(&self) -> Option<M::Id>;
|
||||||
async fn prev_tree_id(&self, tree_id: &M::Id) -> Option<M::Id>;
|
async fn prev_root_id(&self, root_id: &M::Id) -> Option<M::Id>;
|
||||||
async fn next_tree_id(&self, tree_id: &M::Id) -> Option<M::Id>;
|
async fn next_root_id(&self, root_id: &M::Id) -> Option<M::Id>;
|
||||||
async fn oldest_msg_id(&self) -> Option<M::Id>;
|
async fn oldest_msg_id(&self) -> Option<M::Id>;
|
||||||
async fn newest_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 older_msg_id(&self, id: &M::Id) -> Option<M::Id>;
|
||||||
|
|
|
||||||
|
|
@ -101,9 +101,9 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
|
||||||
} else if tree.parent(id).is_none() {
|
} else if tree.parent(id).is_none() {
|
||||||
// We're at the root of our tree, so we need to move to the root of
|
// We're at the root of our tree, so we need to move to the root of
|
||||||
// the previous tree.
|
// the previous tree.
|
||||||
if let Some(prev_tree_id) = store.prev_tree_id(tree.root()).await {
|
if let Some(prev_root_id) = store.prev_root_id(tree.root()).await {
|
||||||
*tree = store.tree(&prev_tree_id).await;
|
*tree = store.tree(&prev_root_id).await;
|
||||||
*id = prev_tree_id;
|
*id = prev_root_id;
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
|
|
@ -123,9 +123,9 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
|
||||||
} else if tree.parent(id).is_none() {
|
} else if tree.parent(id).is_none() {
|
||||||
// We're at the root of our tree, so we need to move to the root of
|
// We're at the root of our tree, so we need to move to the root of
|
||||||
// the next tree.
|
// the next tree.
|
||||||
if let Some(next_tree_id) = store.next_tree_id(tree.root()).await {
|
if let Some(next_root_id) = store.next_root_id(tree.root()).await {
|
||||||
*tree = store.tree(&next_tree_id).await;
|
*tree = store.tree(&next_root_id).await;
|
||||||
*id = next_tree_id;
|
*id = next_root_id;
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
|
|
@ -183,9 +183,9 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
|
||||||
pub async fn move_cursor_up(&mut self) {
|
pub async fn move_cursor_up(&mut self) {
|
||||||
match &mut self.cursor {
|
match &mut self.cursor {
|
||||||
Cursor::Bottom | Cursor::Pseudo { parent: None, .. } => {
|
Cursor::Bottom | Cursor::Pseudo { parent: None, .. } => {
|
||||||
if let Some(last_tree_id) = self.store.last_tree_id().await {
|
if let Some(last_root_id) = self.store.last_root_id().await {
|
||||||
let tree = self.store.tree(&last_tree_id).await;
|
let tree = self.store.tree(&last_root_id).await;
|
||||||
let mut id = last_tree_id;
|
let mut id = last_root_id;
|
||||||
while Self::find_last_child(&self.folded, &tree, &mut id) {}
|
while Self::find_last_child(&self.folded, &tree, &mut id) {}
|
||||||
self.cursor = Cursor::Msg(id);
|
self.cursor = Cursor::Msg(id);
|
||||||
}
|
}
|
||||||
|
|
@ -243,8 +243,8 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
|
||||||
pub async fn move_cursor_up_sibling(&mut self) {
|
pub async fn move_cursor_up_sibling(&mut self) {
|
||||||
match &mut self.cursor {
|
match &mut self.cursor {
|
||||||
Cursor::Bottom | Cursor::Pseudo { parent: None, .. } => {
|
Cursor::Bottom | Cursor::Pseudo { parent: None, .. } => {
|
||||||
if let Some(last_tree_id) = self.store.last_tree_id().await {
|
if let Some(last_root_id) = self.store.last_root_id().await {
|
||||||
self.cursor = Cursor::Msg(last_tree_id);
|
self.cursor = Cursor::Msg(last_root_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Cursor::Msg(msg) => {
|
Cursor::Msg(msg) => {
|
||||||
|
|
@ -392,8 +392,8 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn move_cursor_to_top(&mut self) {
|
pub async fn move_cursor_to_top(&mut self) {
|
||||||
if let Some(first_tree_id) = self.store.first_tree_id().await {
|
if let Some(first_root_id) = self.store.first_root_id().await {
|
||||||
self.cursor = Cursor::Msg(first_tree_id);
|
self.cursor = Cursor::Msg(first_root_id);
|
||||||
self.correction = Some(Correction::MakeCursorVisible);
|
self.correction = Some(Correction::MakeCursorVisible);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -181,15 +181,15 @@ impl<M: Msg + ChatMsg, S: MsgStore<M>> InnerTreeViewState<M, S> {
|
||||||
|
|
||||||
while blocks.blocks().top_line > top_line {
|
while blocks.blocks().top_line > top_line {
|
||||||
let top_root = blocks.top_root();
|
let top_root = blocks.top_root();
|
||||||
let prev_tree_id = match top_root {
|
let prev_root_id = match top_root {
|
||||||
Root::Bottom => self.store.last_tree_id().await,
|
Root::Bottom => self.store.last_root_id().await,
|
||||||
Root::Tree(tree_id) => self.store.prev_tree_id(tree_id).await,
|
Root::Tree(root_id) => self.store.prev_root_id(root_id).await,
|
||||||
};
|
};
|
||||||
let prev_tree_id = match prev_tree_id {
|
let prev_root_id = match prev_root_id {
|
||||||
Some(tree_id) => tree_id,
|
Some(id) => id,
|
||||||
None => break,
|
None => break,
|
||||||
};
|
};
|
||||||
let prev_tree = self.store.tree(&prev_tree_id).await;
|
let prev_tree = self.store.tree(&prev_root_id).await;
|
||||||
blocks.prepend(self.layout_tree(nick, frame, prev_tree));
|
blocks.prepend(self.layout_tree(nick, frame, prev_tree));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -204,12 +204,12 @@ impl<M: Msg + ChatMsg, S: MsgStore<M>> InnerTreeViewState<M, S> {
|
||||||
|
|
||||||
while blocks.blocks().bottom_line < bottom_line {
|
while blocks.blocks().bottom_line < bottom_line {
|
||||||
let bottom_root = blocks.bottom_root();
|
let bottom_root = blocks.bottom_root();
|
||||||
let next_tree_id = match bottom_root {
|
let next_root_id = match bottom_root {
|
||||||
Root::Bottom => break,
|
Root::Bottom => break,
|
||||||
Root::Tree(tree_id) => self.store.next_tree_id(tree_id).await,
|
Root::Tree(root_id) => self.store.next_root_id(root_id).await,
|
||||||
};
|
};
|
||||||
if let Some(next_tree_id) = next_tree_id {
|
if let Some(next_root_id) = next_root_id {
|
||||||
let next_tree = self.store.tree(&next_tree_id).await;
|
let next_tree = self.store.tree(&next_root_id).await;
|
||||||
blocks.append(self.layout_tree(nick, frame, next_tree));
|
blocks.append(self.layout_tree(nick, frame, next_tree));
|
||||||
} else {
|
} else {
|
||||||
blocks.append(self.layout_bottom(nick, frame));
|
blocks.append(self.layout_bottom(nick, frame));
|
||||||
|
|
|
||||||
|
|
@ -100,24 +100,24 @@ impl MsgStore<SmallMessage> for EuphRoomVault {
|
||||||
self.msg(*id).await
|
self.msg(*id).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn tree(&self, tree_id: &Snowflake) -> Tree<SmallMessage> {
|
async fn tree(&self, root_id: &Snowflake) -> Tree<SmallMessage> {
|
||||||
self.tree(*tree_id).await
|
self.tree(*root_id).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn first_tree_id(&self) -> Option<Snowflake> {
|
async fn first_root_id(&self) -> Option<Snowflake> {
|
||||||
self.first_tree_id().await
|
self.first_root_id().await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn last_tree_id(&self) -> Option<Snowflake> {
|
async fn last_root_id(&self) -> Option<Snowflake> {
|
||||||
self.last_tree_id().await
|
self.last_root_id().await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn prev_tree_id(&self, tree_id: &Snowflake) -> Option<Snowflake> {
|
async fn prev_root_id(&self, root_id: &Snowflake) -> Option<Snowflake> {
|
||||||
self.prev_tree_id(*tree_id).await
|
self.prev_root_id(*root_id).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn next_tree_id(&self, tree_id: &Snowflake) -> Option<Snowflake> {
|
async fn next_root_id(&self, root_id: &Snowflake) -> Option<Snowflake> {
|
||||||
self.next_tree_id(*tree_id).await
|
self.next_root_id(*root_id).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn oldest_msg_id(&self) -> Option<Snowflake> {
|
async fn oldest_msg_id(&self) -> Option<Snowflake> {
|
||||||
|
|
@ -241,7 +241,6 @@ macro_rules! requests {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Rename `root` to `root_id` or `tree_id`
|
|
||||||
requests! {
|
requests! {
|
||||||
// Cookies
|
// Cookies
|
||||||
GetCookies : cookies() -> CookieJar;
|
GetCookies : cookies() -> CookieJar;
|
||||||
|
|
@ -259,11 +258,11 @@ requests! {
|
||||||
GetPath : path(room: String, id: Snowflake) -> Path<Snowflake>;
|
GetPath : path(room: String, id: Snowflake) -> Path<Snowflake>;
|
||||||
GetMsg : msg(room: String, id: Snowflake) -> Option<SmallMessage>;
|
GetMsg : msg(room: String, id: Snowflake) -> Option<SmallMessage>;
|
||||||
GetFullMsg : full_msg(room: String, id: Snowflake) -> Option<Message>;
|
GetFullMsg : full_msg(room: String, id: Snowflake) -> Option<Message>;
|
||||||
GetTree : tree(room: String, root: Snowflake) -> Tree<SmallMessage>;
|
GetTree : tree(room: String, root_id: Snowflake) -> Tree<SmallMessage>;
|
||||||
GetFirstTreeId : first_tree_id(room: String) -> Option<Snowflake>;
|
GetFirstRootId : first_root_id(room: String) -> Option<Snowflake>;
|
||||||
GetLastTreeId : last_tree_id(room: String) -> Option<Snowflake>;
|
GetLastRootId : last_root_id(room: String) -> Option<Snowflake>;
|
||||||
GetPrevTreeId : prev_tree_id(room: String, root: Snowflake) -> Option<Snowflake>;
|
GetPrevRootId : prev_root_id(room: String, root_id: Snowflake) -> Option<Snowflake>;
|
||||||
GetNextTreeId : next_tree_id(room: String, root: Snowflake) -> Option<Snowflake>;
|
GetNextRootId : next_root_id(room: String, root_id: Snowflake) -> Option<Snowflake>;
|
||||||
GetOldestMsgId : oldest_msg_id(room: String) -> Option<Snowflake>;
|
GetOldestMsgId : oldest_msg_id(room: String) -> Option<Snowflake>;
|
||||||
GetNewestMsgId : newest_msg_id(room: String) -> Option<Snowflake>;
|
GetNewestMsgId : newest_msg_id(room: String) -> Option<Snowflake>;
|
||||||
GetOlderMsgId : older_msg_id(room: String, id: Snowflake) -> Option<Snowflake>;
|
GetOlderMsgId : older_msg_id(room: String, id: Snowflake) -> Option<Snowflake>;
|
||||||
|
|
@ -707,7 +706,7 @@ impl Request for GetTree {
|
||||||
ORDER BY id ASC
|
ORDER BY id ASC
|
||||||
",
|
",
|
||||||
)?
|
)?
|
||||||
.query_map(params![self.room, WSnowflake(self.root)], |row| {
|
.query_map(params![self.room, WSnowflake(self.root_id)], |row| {
|
||||||
Ok(SmallMessage {
|
Ok(SmallMessage {
|
||||||
id: row.get::<_, WSnowflake>(0)?.0,
|
id: row.get::<_, WSnowflake>(0)?.0,
|
||||||
parent: row.get::<_, Option<WSnowflake>>(1)?.map(|s| s.0),
|
parent: row.get::<_, Option<WSnowflake>>(1)?.map(|s| s.0),
|
||||||
|
|
@ -718,13 +717,13 @@ impl Request for GetTree {
|
||||||
})
|
})
|
||||||
})?
|
})?
|
||||||
.collect::<rusqlite::Result<_>>()?;
|
.collect::<rusqlite::Result<_>>()?;
|
||||||
let tree = Tree::new(self.root, msgs);
|
let tree = Tree::new(self.root_id, msgs);
|
||||||
let _ = self.result.send(tree);
|
let _ = self.result.send(tree);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Request for GetFirstTreeId {
|
impl Request for GetFirstRootId {
|
||||||
fn perform(self, conn: &mut Connection) -> rusqlite::Result<()> {
|
fn perform(self, conn: &mut Connection) -> rusqlite::Result<()> {
|
||||||
let tree = conn
|
let tree = conn
|
||||||
.prepare(
|
.prepare(
|
||||||
|
|
@ -743,7 +742,7 @@ impl Request for GetFirstTreeId {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Request for GetLastTreeId {
|
impl Request for GetLastRootId {
|
||||||
fn perform(self, conn: &mut Connection) -> rusqlite::Result<()> {
|
fn perform(self, conn: &mut Connection) -> rusqlite::Result<()> {
|
||||||
let tree = conn
|
let tree = conn
|
||||||
.prepare(
|
.prepare(
|
||||||
|
|
@ -762,7 +761,7 @@ impl Request for GetLastTreeId {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Request for GetPrevTreeId {
|
impl Request for GetPrevRootId {
|
||||||
fn perform(self, conn: &mut Connection) -> rusqlite::Result<()> {
|
fn perform(self, conn: &mut Connection) -> rusqlite::Result<()> {
|
||||||
let tree = conn
|
let tree = conn
|
||||||
.prepare(
|
.prepare(
|
||||||
|
|
@ -775,7 +774,7 @@ impl Request for GetPrevTreeId {
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
",
|
",
|
||||||
)?
|
)?
|
||||||
.query_row(params![self.room, WSnowflake(self.root)], |row| {
|
.query_row(params![self.room, WSnowflake(self.root_id)], |row| {
|
||||||
row.get::<_, WSnowflake>(0).map(|s| s.0)
|
row.get::<_, WSnowflake>(0).map(|s| s.0)
|
||||||
})
|
})
|
||||||
.optional()?;
|
.optional()?;
|
||||||
|
|
@ -784,7 +783,7 @@ impl Request for GetPrevTreeId {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Request for GetNextTreeId {
|
impl Request for GetNextRootId {
|
||||||
fn perform(self, conn: &mut Connection) -> rusqlite::Result<()> {
|
fn perform(self, conn: &mut Connection) -> rusqlite::Result<()> {
|
||||||
let tree = conn
|
let tree = conn
|
||||||
.prepare(
|
.prepare(
|
||||||
|
|
@ -797,7 +796,7 @@ impl Request for GetNextTreeId {
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
",
|
",
|
||||||
)?
|
)?
|
||||||
.query_row(params![self.room, WSnowflake(self.root)], |row| {
|
.query_row(params![self.room, WSnowflake(self.root_id)], |row| {
|
||||||
row.get::<_, WSnowflake>(0).map(|s| s.0)
|
row.get::<_, WSnowflake>(0).map(|s| s.0)
|
||||||
})
|
})
|
||||||
.optional()?;
|
.optional()?;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue