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

Avoid converting BTreeMap to Vec in TantivyDocument::add_object. #2369

Closed
wants to merge 1 commit into from

Conversation

adamreichold
Copy link
Contributor

For the next breaking release when the immediate issues with 0.22.x are resolved.

@fulmicoton
Copy link
Collaborator

@adamreichold it won't compile.

@adamreichold
Copy link
Contributor Author

@adamreichold it won't compile.

Yes, because a sequence of key-value pairs cannot directly be deserialized from a map. I added a thin wrapper to make that adjustment which avoids the overhead of the associative data structures, but keeps the convenience during deserialization.

@@ -150,7 +151,7 @@ impl TantivyDocument {
}

/// Add a dynamic object field
pub fn add_object(&mut self, field: Field, object: BTreeMap<String, OwnedValue>) {
pub fn add_object(&mut self, field: Field, FlatObject(object): FlatObject) {
Copy link
Contributor

Choose a reason for hiding this comment

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

that seems like a worse API

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please expand on worse in what way or rather what you would prefer?

It definitely adds a bit of ceremony for the wrapper type, but it does avoid the unnecessary conversion for example in the unit tests here. Would you prefer some kind of implicit conversion via generics and trait bounds?

Copy link
Contributor

Choose a reason for hiding this comment

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

BTreeMap is a well know data structure for objects, while FlatObject would require some digging on how to convert into it

Copy link
Collaborator

Choose a reason for hiding this comment

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

what about any impl IntoIterator<Item=(String, OwnedValue>)>

@fulmicoton
Copy link
Collaborator

fulmicoton commented Apr 26, 2024

I don't quite understand the motivation for this PR. @adamreichold ?

@adamreichold
Copy link
Contributor Author

I don't quite understand the motivation for this PR. @adamreichold ?

TantivyDocument stores objects as flat vectors of key-value pairs but the API here requires going through BTreeMap only to convert it internally. To me, this seems wasteful and while I do not want to specifically avoid BTreeMap, I would like the API to enable at least one way of using it without the unnecessary intermediate collection.

Copy link
Contributor

@PSeitz PSeitz left a comment

Choose a reason for hiding this comment

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

add_object is a convenience function, you can now add anything that implements Value

@adamreichold
Copy link
Contributor Author

add_object is a convenience function, you can now add anything that implements Value

Agreed. I was somewhat mechanically rebasing my branches and did not review all of the API changes implied by CompactDoc. Closing this one.

@adamreichold adamreichold deleted the add-object-conversion branch May 24, 2024 15:39
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.

None yet

3 participants