Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[graph] Allow search to return result #584 #587

Merged
merged 1 commit into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/agdb/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,13 @@ impl Db {
let search = GraphSearch::from(&self.graph);

let indexes = match (limit, offset) {
(0, 0) => search.breadth_first_search(GraphIndex(from.0), DefaultHandler {}),
(_, 0) => search.breadth_first_search(GraphIndex(from.0), LimitHandler::new(limit)),
(0, _) => search.breadth_first_search(GraphIndex(from.0), OffsetHandler::new(offset)),
(0, 0) => search.breadth_first_search(GraphIndex(from.0), DefaultHandler {})?,
(_, 0) => search.breadth_first_search(GraphIndex(from.0), LimitHandler::new(limit))?,
(0, _) => {
search.breadth_first_search(GraphIndex(from.0), OffsetHandler::new(offset))?
}
(_, _) => search
.breadth_first_search(GraphIndex(from.0), LimitOffsetHandler::new(limit, offset)),
.breadth_first_search(GraphIndex(from.0), LimitOffsetHandler::new(limit, offset))?,
};

Ok(indexes.iter().map(|index| DbId(index.0)).collect())
Expand All @@ -336,25 +338,25 @@ impl Db {
let search = GraphSearch::from(&self.graph);

let indexes = match (limit, offset) {
(0, 0) => search.breadth_first_search_reverse(GraphIndex(to.0), DefaultHandler {}),
(0, 0) => search.breadth_first_search_reverse(GraphIndex(to.0), DefaultHandler {})?,
(_, 0) => {
search.breadth_first_search_reverse(GraphIndex(to.0), LimitHandler::new(limit))
search.breadth_first_search_reverse(GraphIndex(to.0), LimitHandler::new(limit))?
}
(0, _) => {
search.breadth_first_search_reverse(GraphIndex(to.0), OffsetHandler::new(offset))
search.breadth_first_search_reverse(GraphIndex(to.0), OffsetHandler::new(offset))?
}
(_, _) => search.breadth_first_search_reverse(
GraphIndex(to.0),
LimitOffsetHandler::new(limit, offset),
),
)?,
};

Ok(indexes.iter().map(|index| DbId(index.0)).collect())
}

pub(crate) fn search_from_to(&self, from: DbId, to: DbId) -> Result<Vec<DbId>, QueryError> {
Ok(GraphSearch::from(&self.graph)
.path(GraphIndex(from.0), GraphIndex(to.0), PathHandler {})
.path(GraphIndex(from.0), GraphIndex(to.0), PathHandler {})?
.iter()
.map(|index| DbId(index.0))
.collect())
Expand Down
25 changes: 13 additions & 12 deletions src/agdb/db/db_search_handlers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::db_error::DbError;
use crate::graph::GraphIndex;
use crate::graph_search::PathSearchHandler;
use crate::graph_search::SearchControl;
Expand All @@ -24,8 +25,8 @@ pub(crate) struct LimitOffsetHandler {
pub(crate) struct PathHandler {}

impl SearchHandler for DefaultHandler {
fn process(&mut self, _index: GraphIndex, _distance: u64) -> SearchControl {
SearchControl::Continue(true)
fn process(&mut self, _index: GraphIndex, _distance: u64) -> Result<SearchControl, DbError> {
Ok(SearchControl::Continue(true))
}
}

Expand All @@ -52,38 +53,38 @@ impl LimitOffsetHandler {
}

impl SearchHandler for LimitHandler {
fn process(&mut self, _index: GraphIndex, _distance: u64) -> SearchControl {
fn process(&mut self, _index: GraphIndex, _distance: u64) -> Result<SearchControl, DbError> {
self.counter += 1;

if self.counter == self.limit {
SearchControl::Finish(true)
Ok(SearchControl::Finish(true))
} else {
SearchControl::Continue(true)
Ok(SearchControl::Continue(true))
}
}
}

impl SearchHandler for OffsetHandler {
fn process(&mut self, _index: GraphIndex, _distance: u64) -> SearchControl {
fn process(&mut self, _index: GraphIndex, _distance: u64) -> Result<SearchControl, DbError> {
self.counter += 1;
SearchControl::Continue(self.offset < self.counter)
Ok(SearchControl::Continue(self.offset < self.counter))
}
}

impl SearchHandler for LimitOffsetHandler {
fn process(&mut self, _index: GraphIndex, _distance: u64) -> SearchControl {
fn process(&mut self, _index: GraphIndex, _distance: u64) -> Result<SearchControl, DbError> {
self.counter += 1;

if self.counter == self.limit {
SearchControl::Finish(self.offset < self.counter)
Ok(SearchControl::Finish(self.offset < self.counter))
} else {
SearchControl::Continue(self.offset < self.counter)
Ok(SearchControl::Continue(self.offset < self.counter))
}
}
}

impl PathSearchHandler for PathHandler {
fn process(&self, _index: GraphIndex, _distance: u64) -> u64 {
1
fn process(&self, _index: GraphIndex, _distance: u64) -> Result<u64, DbError> {
Ok(1)
}
}
23 changes: 12 additions & 11 deletions src/agdb/graph_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use self::depth_first_search::DepthFirstSearch;
use self::depth_first_search_reverse::DepthFirstSearchReverse;
use self::path_search::PathSearch;
use self::search_impl::SearchImpl;
use crate::db::db_error::DbError;
use crate::graph::GraphData;
use crate::graph::GraphImpl;
use crate::graph::GraphIndex;
Expand All @@ -26,7 +27,7 @@ pub enum SearchControl {
}

pub trait SearchHandler {
fn process(&mut self, index: GraphIndex, distance: u64) -> SearchControl;
fn process(&mut self, index: GraphIndex, distance: u64) -> Result<SearchControl, DbError>;
}

pub struct GraphSearch<'a, Data>
Expand All @@ -45,48 +46,48 @@ where
&self,
index: GraphIndex,
handler: Handler,
) -> Vec<GraphIndex> {
) -> Result<Vec<GraphIndex>, DbError> {
if self.is_valid_index(index) {
SearchImpl::<'a, Data, BreadthFirstSearch>::new(self.graph, index).search(handler)
} else {
vec![]
Ok(vec![])
}
}

pub fn breadth_first_search_reverse<Handler: SearchHandler>(
&self,
index: GraphIndex,
handler: Handler,
) -> Vec<GraphIndex> {
) -> Result<Vec<GraphIndex>, DbError> {
if self.is_valid_index(index) {
SearchImpl::<'a, Data, BreadthFirstSearchReverse>::new(self.graph, index)
.search(handler)
} else {
vec![]
Ok(vec![])
}
}

pub fn depth_first_search<Handler: SearchHandler>(
&self,
index: GraphIndex,
handler: Handler,
) -> Vec<GraphIndex> {
) -> Result<Vec<GraphIndex>, DbError> {
if self.is_valid_index(index) {
SearchImpl::<'a, Data, DepthFirstSearch>::new(self.graph, index).search(handler)
} else {
vec![]
Ok(vec![])
}
}

pub fn depth_first_search_reverse<Handler: SearchHandler>(
&self,
index: GraphIndex,
handler: Handler,
) -> Vec<GraphIndex> {
) -> Result<Vec<GraphIndex>, DbError> {
if self.is_valid_index(index) {
SearchImpl::<'a, Data, DepthFirstSearchReverse>::new(self.graph, index).search(handler)
} else {
vec![]
Ok(vec![])
}
}

Expand All @@ -95,11 +96,11 @@ where
from: GraphIndex,
to: GraphIndex,
handler: Handler,
) -> Vec<GraphIndex> {
) -> Result<Vec<GraphIndex>, DbError> {
if from != to && self.is_valid_node(from) && self.is_valid_node(to) {
PathSearch::<Data, Handler>::new(self.graph, from, to, handler).search()
} else {
vec![]
Ok(vec![])
}
}

Expand Down
21 changes: 12 additions & 9 deletions src/agdb/graph_search/breadth_first_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ mod tests {
use super::super::SearchControl;
use super::super::SearchHandler;
use super::*;
use crate::db::db_error::DbError;
use crate::graph::DbGraph;
use crate::graph_search::GraphSearch;
use crate::storage::file_storage::FileStorage;
Expand All @@ -69,8 +70,8 @@ mod tests {
}

impl SearchHandler for Handler {
fn process(&mut self, index: GraphIndex, distance: u64) -> SearchControl {
(self.processor)(index, distance)
fn process(&mut self, index: GraphIndex, distance: u64) -> Result<SearchControl, DbError> {
Ok((self.processor)(index, distance))
}
}

Expand All @@ -85,7 +86,7 @@ mod tests {
let result = GraphSearch::from(&graph)
.breadth_first_search(GraphIndex::default(), Handler::default());

assert_eq!(result, vec![]);
assert_eq!(result, Ok(vec![]));
}

#[test]
Expand All @@ -111,7 +112,9 @@ mod tests {

assert_eq!(
result,
vec![node1, edge2, edge1, node2, edge4, edge3, node3, edge6, edge5]
Ok(vec![
node1, edge2, edge1, node2, edge4, edge3, node3, edge6, edge5
])
);
}

Expand All @@ -136,7 +139,7 @@ mod tests {

assert_eq!(
result,
vec![node1, edge3, edge2, edge1, node4, node3, node2]
Ok(vec![node1, edge3, edge2, edge1, node4, node3, node2])
);
}

Expand Down Expand Up @@ -166,7 +169,7 @@ mod tests {
},
);

assert_eq!(result, vec![node1, node4, node3, node2]);
assert_eq!(result, Ok(vec![node1, node4, node3, node2]));
}

#[test]
Expand Down Expand Up @@ -201,7 +204,7 @@ mod tests {
},
);

assert_eq!(result, vec![node2]);
assert_eq!(result, Ok(vec![node2]));
}

#[test]
Expand All @@ -222,7 +225,7 @@ mod tests {
let edge3 = graph.insert_edge(node1, node4).unwrap();

let mut result = GraphSearch::from(&graph).breadth_first_search(node1, Handler::default());
let expected = vec![node1, edge3, edge2, edge1, node4, node3, node2];
let expected = Ok(vec![node1, edge3, edge2, edge1, node4, node3, node2]);

assert_eq!(result, expected);

Expand Down Expand Up @@ -261,6 +264,6 @@ mod tests {
},
);

assert_eq!(result, vec![node1, edge2, edge1, node2]);
assert_eq!(result, Ok(vec![node1, edge2, edge1, node2]));
}
}
9 changes: 5 additions & 4 deletions src/agdb/graph_search/breadth_first_search_reverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ mod tests {
use super::super::SearchControl;
use super::super::SearchHandler;
use super::*;
use crate::db::db_error::DbError;
use crate::graph::DbGraph;
use crate::graph_search::GraphSearch;
use crate::storage::file_storage::FileStorage;
Expand All @@ -69,8 +70,8 @@ mod tests {
}

impl SearchHandler for Handler {
fn process(&mut self, index: GraphIndex, distance: u64) -> SearchControl {
(self.processor)(index, distance)
fn process(&mut self, index: GraphIndex, distance: u64) -> Result<SearchControl, DbError> {
Ok((self.processor)(index, distance))
}
}

Expand All @@ -85,7 +86,7 @@ mod tests {
let result = GraphSearch::from(&graph)
.breadth_first_search_reverse(GraphIndex::default(), Handler::default());

assert_eq!(result, vec![]);
assert_eq!(result, Ok(vec![]));
}

#[test]
Expand All @@ -107,7 +108,7 @@ mod tests {

let result =
GraphSearch::from(&graph).breadth_first_search_reverse(node4, Handler::default());
let expected = vec![node4, edge3, node3, edge2, node2, edge1, node1];
let expected = Ok(vec![node4, edge3, node3, edge2, node2, edge1, node1]);

assert_eq!(result, expected);
}
Expand Down
Loading