-
Notifications
You must be signed in to change notification settings - Fork 109
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
WIP: Switch batches to new serialization traits #870
Closed
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Introduce a derive macro which serializes a struct into a UDT. Unlike the previous IntoUserType, the new macro takes care to match the struct fields to UDT fields by their names. It does not assume that the order of the fields in the Rust struct is the same as in the UDT.
Introduce a derive macro which serializes a struct into bind markers of a statement. Unlike the previous ValueList, the new macro takes care to match the struct fields to bind markers/columns by their names.
Some users might not need the additional robustness of `SerializeCql` that comes from sorting the fields before serializing, as they are used to the current behavior of `Value` and properly set the order of the fields in their Rust struct. In order to give them some performance boost, add an additional mode to `SerializeCql` called "enforce_order" which expects that the order of the fields in the struct is kept in sync with the DB definition of the UDT. It's still safe to use because, as the struct fields are serialized, their names are compared with the fields in the UDT definition order and serialization fails if the field name on some position is mismatched.
Like in the case of `SerializeRow`, some people might be used to working with the old `ValueList` and already order their Rust struct fields with accordance to the queries they are used with and don't need the overhead associated with looking up columns by name. The `enforce_order` mode is added to `SerializeRow` which works analogously as in `SerializeCql` - expects the columns to be in the correct order and verifies that this is the case when serializing, but just fails instead of reordering if that expectation is broken.
This was a missing method allowing actually creating this struct.
This commit adds new variant of QueryError and a necessary From impl.
After serialization refactor it will be impossible to perform unprepared query with values, because serializing values will require knowing column types. In order to make refactor easier and better split responsibility, this commit removes `values` arguments from `Connection` methods, so that it is callers responsibility to prepare the query if necessary.
After serialization refactor, there will be a new struct called SerializedValues. In order to make transition easier, we decided to retain old structs and traits for now (probably 1 release), so that quick temporary update (before proper migration to new traits) can be performed with simple replacements in the code.
This struct is very similar to LegacySerializedValues, but does not support named values (as those are no longer necessary after serialization refactor) and uses new serialization interface, guaranteeing better type safety.
This is one of the main steps of serialization refactor: using new trait in public API. Thanks to our implementations for this new trait migrating should not be a huge issue for users - in many cases being fully compatible. One change missing from this commit is Batch support, as this requires more changes (which will also be more breaking and require more migration effort), so it will be implemented separately.
Closing as it was succeeded by #881 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pre-review checklist
This is still a work in progress - some tests are still not adapted.
The approach I took is the one we discussed - of making
Batch
a builder that serializes query parameters as user adds them.At first I thought about gradully serializing the whole batch into one buffer, but there are many places that require iterating trough batch's contents - which would not be possible then. So now I'm serializing query parameters into
SerializedValues
, and onlyBatch
inscylla-cql
puts everything togother into one buffer.The downside is that each call to
append_statement
performs an allocation - but that was already the case with the previous interface usedBatchValuesIterator::write_next_to_request
which internally usesValueList::serialized
which performs an allocation.The downsides in current interface compared to previous one are:
append_statement
may returnSerializationError
- because it createsSerializedValues
from providedSerializeRow
immediately.Query
variant doesn't take values - we can't prepare it as there is noSession
available. I'm thinking about changing the interface a bit and fixing this, it shouldn't complicate the interface too much - but first I'd like to know if that is something we want, or are we going in the direction of disallowing values in simple queries at all (in APIs likeSession::query*
).The upside is that it is much easier to understand - there isn't a ton of weird structs and traits like in
value.rs
.I considered (and tried to implement) some alternative solutions
Interface pretty much identical to the old one.
First idea was to copy the previous interface, change
ValueList
toSerializeRow
, addPreparedMetadata
argument toBatchValuesIterator
methods, and adjust the rest of the code.The problem is that
Batch
in scylla-cql doesn't storePreparedMetadata
. I could add it to it'sBatchStatement
enum, but then we couldn't deserialize it - as the serialized for doesn't contain the metadata.There would also be a question on how to handle Queries (as they wouldn't have values) - store empty SerializedValues in the iterator? Store
None
? Skip it, which makes statements list and values list different in length? I don't see elegant answer.Similar interface, but containing not values, but statement-value pairs.
The idea is to create
BatchStatement
trait withwrite_to_request
method. RenameBatchValues
toBatchStatements
and provide similar methods to the ones currently present inBatchValues
.scylla crate would implement
BatchStatement
for it's version which would be generic and contain a reference (or Cow) to SerializeRow, and scylla-cql would implement it's own version, with SerializedValues.This version doesn't add
PreparedMetadata
argument to methods, as the iterator values contain a query or statement-values pair, so the values can be serialized using proper statement. This would also remove the possibility of statement list and values list having different lengths.The problem is that
Session::Batch
and a lot of other code needs to inspect the statements - so they can't be hidden. This is the case inprepare_batch
and in calculating the token of first statement inbatch
.I considered adding a
calculate_token
method toBatchStatement
trait, but it seems like an ugly hack, and there are other places in code code that inspect the queries.There are other solutions which I considered / tried but I don't remember them now - in many the problem is the layering (scylla vs scylla-cql, scylla-cql not having access to scylla types) - imo this layering is a good design, but it doesn't make this particular task easier :D
TODO:
./docs/source/
.Fixes:
annotations to PR description.