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

feat: implement rht_pq_map #16

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft

feat: implement rht_pq_map #16

wants to merge 22 commits into from

Conversation

blurfx
Copy link
Member

@blurfx blurfx commented Feb 23, 2022

What this PR does / why we need it?

What are the relevant tickets?

Fixes #9

Any background context you want to provide?

Checklist

  • Added relevant tests or not required
  • Didn't break anything

@CLAassistant
Copy link

CLAassistant commented Feb 23, 2022

CLA assistant check
All committers have signed the CLA.

@blurfx blurfx marked this pull request as ready for review February 24, 2022 12:50
@blurfx blurfx requested a review from a team February 24, 2022 12:50
Copy link
Member

@dc7303 dc7303 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the great work! I left a few comments, so please check them out.

src/document/json/rht_pq_map.rs Outdated Show resolved Hide resolved
src/document/json/rht_pq_map.rs Outdated Show resolved Hide resolved
src/document/json/rht_pq_map.rs Outdated Show resolved Hide resolved
src/document/json/rht_pq_map.rs Outdated Show resolved Hide resolved
src/document/json/rht_pq_map.rs Outdated Show resolved Hide resolved
src/document/json/rht_pq_map.rs Outdated Show resolved Hide resolved
Co-authored-by: Dongcheol Choe <40932237+dc7303@users.noreply.github.com>
Copy link
Member

@hackerwins hackerwins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution.

  1. I'm a beginner in Rust now and there is a pattern I'm not familiar with. Please explain the part I commented on. I think @dc7303 and I can learn something new.
  2. How about implementing this with test codes?

src/document/json/element.rs Show resolved Hide resolved
src/document/json/rht_pq_map.rs Outdated Show resolved Hide resolved
src/document/json/rht_pq_map.rs Outdated Show resolved Hide resolved
src/document/json/rht_pq_map.rs Show resolved Hide resolved
Copy link
Member

@hackerwins hackerwins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution.
(Sorry for the late)

Copy link
Member

@dc7303 dc7303 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late review. 🙏

About Test Code

Using smart pointers in Rust can cause errors at runtime. Also, since this data structure deals with an abstract element called Element, it is easy to make mistakes in concrete implementation.
So I think we need some test code for the step now.
(Most of the changes I suggest are issues I discovered while writing the test code.)

So, I wrote a test. And in the case of the actual set() method, it was confirmed that the intended operation was not performed.

failures:

---- document::json::rht_pq_map::tests::data_handle stdout ----
thread 'document::json::rht_pq_map::tests::data_handle' panicked at 'assertion failed: false', src/document/json/rht_pq_map.rs:387:18
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

How about improving the codes along with the tests based on this test code?

  • The MockElement in the test code shared below may seem overdone. If an element such as Primitive is added, MockElement will be replaced with Primitive.

my test code:

#[cfg(test)]
mod tests {
    use super::*;
    use crate::document::time::ticket::Ticket;
    use crate::document::json::element::Element;
    use crate::document::time::actor_id::ActorID;

    struct MockElement {
        value: u32,
        created_at: Ticket,
        moved_at: Option<Ticket>,
        removed_at: Option<Ticket>,
    }

    impl MockElement {
        fn new(value: u32, created_at: Ticket) -> MockElement {
            return MockElement {
                value,
                created_at,
                moved_at: None,
                removed_at: None,
            }
        }
    }

    impl Element for MockElement {
        fn to_string(&self) -> String {
            self.value.to_string()
        }

        fn deepcopy(&self) -> Box<dyn Element> {
            let moved_at = match &self.moved_at {
                Some(moved_at) => Some(moved_at.clone()),
                _ => None,
            };

            let removed_at = match &self.removed_at {
                Some(removed_at) => Some(removed_at.clone()),
                _ => None,
            };

            let element = MockElement {
                value: self.value,
                created_at: self.created_at.clone(),
                moved_at: moved_at,
                removed_at: removed_at,
            };

            Box::new(element)
        }

        fn created_at(&self) -> Ticket {
            self.created_at.clone()
        }

        fn moved_at(&self) -> Option<Ticket> {
            if let Some(moved_at) = &self.moved_at {
                return Some(moved_at.clone());
            }

            None
        }

        fn set_moved_at(&mut self, ticket: Ticket) {
            self.moved_at = Some(ticket);
        }

        fn removed_at(&self) -> Option<Ticket> {
            if let Some(removed_at) = &self.removed_at {
                return Some(removed_at.clone());
            }

            None
        }

        fn remove(&mut self, ticket: Ticket) -> bool {
            if ticket.after(&self.created_at) {
                match &self.removed_at {
                    Some(removed_at) => {
                        if ticket.after(removed_at) {
                            self.removed_at = Some(ticket);
                            return true;
                        }
                    },
                    _ => {
                        self.removed_at = Some(ticket);
                        return true;
                    }
                }
            }

            false
        }
    }

    #[test]
    fn data_handle() {
        let mut map = RHTPriorityQueueMap::new();
        let hex_str = "0123456789abcdef01234567";
        let actor_id = ActorID::from_hex(hex_str).unwrap();
        let created_at = Ticket::new(0, 0, actor_id);

        // set return None
        let result = map.set("data".to_string(), Box::new(MockElement::new(1, created_at)));
        if let Some(_) = result {
            assert!(false);
        };

        // set return removed element
        let created_at = Ticket::new(0, 1, actor_id.clone());
        let result = map.set("data".to_string(), Box::new(MockElement::new(2, created_at)));
        match result {
            Some(element) => assert_eq!(element.to_string(), "2"),
            _ => assert!(false),
        }
    }
}

About generic

The current code is using the Box smart pointer. If you use generics, I think you can make your code look better by not using Box. What do you think?

src/document/json/element.rs Outdated Show resolved Hide resolved
src/document/json/element.rs Outdated Show resolved Hide resolved
src/document/json/rht_pq_map.rs Outdated Show resolved Hide resolved
src/document/json/rht_pq_map.rs Outdated Show resolved Hide resolved
src/document/json/rht_pq_map.rs Outdated Show resolved Hide resolved
src/document/json/rht_pq_map.rs Outdated Show resolved Hide resolved
src/document/json/rht_pq_map.rs Outdated Show resolved Hide resolved
src/document/json/rht_pq_map.rs Outdated Show resolved Hide resolved
blurfx and others added 11 commits April 20, 2022 21:35
Co-authored-by: Dongcheol Choe <40932237+dc7303@users.noreply.github.com>
Co-authored-by: Dongcheol Choe <40932237+dc7303@users.noreply.github.com>
Co-authored-by: Dongcheol Choe <40932237+dc7303@users.noreply.github.com>
Co-authored-by: Dongcheol Choe <40932237+dc7303@users.noreply.github.com>
Co-authored-by: Dongcheol Choe <40932237+dc7303@users.noreply.github.com>
Co-authored-by: Dongcheol Choe <40932237+dc7303@users.noreply.github.com>
Co-authored-by: Dongcheol Choe <40932237+dc7303@users.noreply.github.com>
Co-authored-by: Dongcheol Choe <40932237+dc7303@users.noreply.github.com>
Copy link
Member

@dc7303 dc7303 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for taking my comments into account 🙇

Now that we are ready to test, we can extend our tests. How about extending your tests to test other public methods?

It's also good to add a test case. We need to check that the currently implemented public methods work well.

For example, you can test the has() and delete() methods.
I tested the has() and delete() methods and found that it didn't work as we intended. Below I attach the test code that I have extended.

    #[test]
    fn data_handle() {
        .......
        // set return removed element
        let created_at = Ticket::new(0, 1, actor_id.clone());
        let result = map.set("data".to_string(), MockElement::new(2, created_at));
        match result {
            Some(element) => assert_eq!(element.to_string(), "1"),
            _ => assert!(false),
        }
                
        // has data
        assert!(map.has("data"));

        // delete
        let deleted_at = Ticket::new(0, 2, actor_id.clone());
        match map.delete("data".to_string(), deleted_at) {
            Some(element) => assert_eq!(element.to_string(), "2"),
            _ => assert!(false),
        }
    }

has() Fail log

failures:

---- document::json::rht_pq_map::rht_pq_map_tests::data_handle stdout ----
thread 'document::json::rht_pq_map::rht_pq_map_tests::data_handle' panicked at 'assertion failed: map.has(\"data\")', src/document/json/rht_pq_map.rs:386:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

delete() Fail log

failures:

---- document::json::rht_pq_map::rht_pq_map_tests::data_handle stdout ----
thread 'document::json::rht_pq_map::rht_pq_map_tests::data_handle' panicked at 'assertion failed: `(left == right)`
  left: `"1"`,
 right: `"2"`', src/document/json/rht_pq_map.rs:391:30

@blurfx blurfx marked this pull request as draft July 9, 2022 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add json.rht_pq_map
4 participants