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

PHPLIB-1022: Codec implementation for better BSON decoding/encoding #1059

Closed

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Apr 12, 2023

This pull request adds the concept of codecs to the library. A codec consists of an encoder and decoder (currently separated, but we could consolidate these into a single interface).

A decoder is responsible for decoding BSON data into PHP data, while the encoder converts PHP data into BSON data. This PR comes with two default codecs, LazyBSONDocumentCodec and LazyBSONArrayCodec. These work with new lazy classes that mimic the existing BSONArray and BSONDocument, but are backed by the new BSON Document and PackedArray classes provided in the new extension version. This allows us to defer reading and decoding BSON until data is actually accessed. Furthermore, only accessed fields are ever read, meaning that any BSON data that isn't actively used is never decoded and thus does not have a considerable memory or time impact.

For the time being, the new lazy classes do not extend the existing BSONArray and BSONDocument classes, but for backward compatibility it might be preferable to do so: this would allow us to enable this lazy behaviour by default. As is, this functionality is opt-in due to the BC break involved in returning classes that don't extend the current classes.

The next step is to change the Collection class to take a codec option, which would allow said class to decode and encode data in the various methods that would return modelled data. Apart from decoding BSON documents into specific PHP objects lazily, the codec functionality would also allow users to change the way BSON types are returned. For example, a DateTimeCodec could decode any MongoDB\BSON\UTCDateTime instances to a DateTime instance and vice-versa, with the collection class automatically converting those instances to UTCDateTime.

This is a first draft, and as such there are a number of items left to do:

  • Figure out naming (Array vs. PackedArray)
  • Support passing a codec to a Collection instance
  • (optional, desired) Make lazy document/array classes extend existing classes
  • (optional) Make Client aware of a codec library containing codecs that should always be used, e.g. to transform BSON types
  • (optional) Provide a codec map to associate a codec with a specific collection, avoiding the need to pass a codec option whenever fetching a Collection instance.

@alcaeus alcaeus self-assigned this Apr 12, 2023
psalm.xml.dist Show resolved Hide resolved
$this->attachEncoder($item);
}

// Yes, we'll silently discard everything. Please let me already have union types...
Copy link
Member Author

Choose a reason for hiding this comment

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

We may want to replace this with an InvalidArgumentException - for now this is fine.


$return = [];
foreach ($value as $offset => $offsetValue) {
$return[$offset] = $this->getLibrary()->canEncode($offsetValue) ? $this->getLibrary()->encode($offsetValue) : $offsetValue;
Copy link
Member Author

Choose a reason for hiding this comment

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

Currently codecs are designed to throw an exception if encode is called with an unsupported object. However, we may want to consider a encodeOrReturn kind of method that contains this logic to avoid repeating this all over the place.

Copy link
Member

Choose a reason for hiding this comment

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

encodeOrReturn() seems sensible.

*
* @template-extends IteratorIterator<TKey, TValue, TIterator>
*/
final class AsListIterator extends IteratorIterator
Copy link
Member Author

Choose a reason for hiding this comment

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

This iterator is used when serialising LazyBSONArray instances to ensure that we return a packed array. The psalm template annotations may need some work (or silencing of errors), but for the time being this works.

@@ -49,7 +49,7 @@ public function __construct(Traversable $traversable, Closure $callback)
#[ReturnTypeWillChange]
public function current()
{
return ($this->callback)($this->iterator->current());
return ($this->callback)($this->iterator->current(), $this->iterator->key());
Copy link
Member Author

Choose a reason for hiding this comment

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

I noticed that our CallbackIterator only passes the value to the callback, but not the key. This change brings the iterator in line with CallbackFilterIterator which also passes both. Note that the latter also passes the whole iterator object as well, which I can add for the sake of feature parity, but it isn't required for this PR to work properly.

Copy link
Member

Choose a reason for hiding this comment

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

Historically, CallbackIterator has only ever been used by ListCollectionNames (2e39482), which had no need of key processing.

Is it feasible to document the callback type using Psalm annotations?

Also, I would be in favor of relaxing its type from Closure to callable and using call_user_func() here if you think that's more consistent with what we do elsewhere. I believe this class is the only place in the library we actually use Closure directly.

$codec = new LazyBSONArrayCodec();
$codec->attachLibrary($this->getLibrary());

// @psalm-suppress InvalidReturnStatement
Copy link
Member Author

Choose a reason for hiding this comment

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

toPHP returns array|object, so we specifically have to ignore psalm errors here as we know that a PackedArray::toPHP() will always return an array in the absence of a type map.

Comment on lines +199 to +205
if ($offset === null) {
$this->readEntirePackedArray();

$offset = max(array_merge(
array_keys($this->read),
array_keys($this->set),
)) + 1;
Copy link
Member Author

Choose a reason for hiding this comment

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

This piece of logic is necessary to allow appending to the list, in which case $offset will be null. In that case, we grab the maximum offset that we have read or written and write the next offset. This is consistent with how PHP handles arrays with missing offsets. Note that for this to work we have to ensure that the entire BSON packed array has been read.

Copy link
Member

Choose a reason for hiding this comment

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

Did you test this for appending to an empty array? In that case, I'd expect $this->read and $this->set might both be empty, in which case you pass an empty array to max(), which is an error.

@alcaeus alcaeus force-pushed the phplib-1022-codec-implementation branch from 8e27a3c to d2d950d Compare April 12, 2023 11:46
@alcaeus alcaeus force-pushed the phplib-1022-codec-implementation branch from d2d950d to cf85d8c Compare April 13, 2023 07:33
Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

Took a first pass through the source files but not tests.

Comment on lines +8 to +9
* @psalm-template B
* @psalm-template T
Copy link
Member

Choose a reason for hiding this comment

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

I assume "T" refers to a PHP value and "B" is for BSON. Can we use more descriptive names (e.g. TBson) like you did in AsListIterator?

namespace MongoDB\Codec;

/**
* @api
Copy link
Member

Choose a reason for hiding this comment

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

Does this imply the class is something we want documented?

I know we use @api elsewhere, but I've forgotten the motivation behind that. When we're thinking about preserving BC, I know everything in the library is implicitly part of the API unless we've annotated it with @internal, so that might lead one to think @api is redundant.

Copy link
Member Author

Choose a reason for hiding this comment

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

It may be superfluous, in which case I'd go through and remove instances of @api across the board. 👍

Copy link
Member

Choose a reason for hiding this comment

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

/** @param Decoder|Encoder $items */
public function __construct(...$items)
{
array_map(
Copy link
Member

Choose a reason for hiding this comment

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

Any reason to use this over foreach? You're not actually modifying the array elements, so the callback doesn't seem necessary.

}
}

return $value === null;
Copy link
Member

Choose a reason for hiding this comment

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

Why does null get special treatment? Likewise for canEncode().

Copy link
Member Author

Choose a reason for hiding this comment

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

The idea was for each codec to decide how to handle a null value. For example, an ODM may decide that for a list of embedded documents it may not want to encode null as an empty array. If a single codec can't deal with a null value, it should return false in its canEncode method.

On second thought, the special handling for null seems misguided. If null deserves special treatment, other scalar values should probably enjoy the same. By that logic, it would make more sense to include a separate ScalarValueCodec that handles all scalar values that PHP offers, including null and returns them as-is. This would also do away with the encodeOrReturn topic in other places. Same goes for array, where a separate codec should support any array and traverse the array to encode all items within the array. I think that would make it more explicit.

Copy link
Member

Choose a reason for hiding this comment

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

Would ScalarValueCodec require additional changes in PHPC? If so, this may be a good opportunity to try and provide a way to differentiate 32-bit and 64-bit integers.

Comment on lines +103 to +107
if (! $decoder->canDecode($value)) {
continue;
}

return $decoder->decode($value);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (! $decoder->canDecode($value)) {
continue;
}
return $decoder->decode($value);
if ($decoder->canDecode($value)) {
return $decoder->decode($value);
}

IMO, the guard statement makes sense if we were doing something else here that would need a break afterwards, but that's not the case with return. This just seems more concise unless you expect the body of this loop to get more complicated down the line.

$value = $this->bson->get($offset);

// Decode value using the codec library if a codec exists
$this->read[$offset] = $this->decodeBSONValue($value);
Copy link
Member

Choose a reason for hiding this comment

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

Based on the logic in offsetGet(), $this->set is preferred over $this->read.

When offsetSet() is called, it clears out the corresponding offset in $this->unset and $this->notFound. Would it make sense for it to also clear out $this->read as well? As-is, you're leaving behind a decoded value that won't be accessed.


private function getLibrary(): CodecLibrary
{
return $this->library ?? $this->library = new CodecLibrary(new LazyBSONDocumentCodec(), new LazyBSONArrayCodec());
Copy link
Member

Choose a reason for hiding this comment

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

Similar to my earlier comment, I think this is more readable with a conditional to initialize $this->library followed by an unconditional return.

return;
}

$this->entirePackedArrayRead = true;
Copy link
Member

Choose a reason for hiding this comment

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

Would it be safer to defer this until after iteration? I'm considering some edge case where decoding BSON fails (e.g. during a call to offsetSet()) but the user catches and ignores the exception. That might leave LazyBSONArray in an inconsistent state.

$this->entirePackedArrayRead = true;

foreach ($this->bson as $key => $value) {
if (isset($this->read[$key])) {
Copy link
Member

Choose a reason for hiding this comment

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

Should you use array_key_exists() here? $this->read stores decoded BSON values, and null is a valid field value.

If this was an oversight, it'd be worth adding a regression test.

Although, if this is just an optimization to cut down on additional calls to decodeBSONValue() maybe it isn't worth the trouble, since most things won't decode to null. In that case, a comment explaining the logic here would still be helpful.

Comment on lines +80 to +82
if (is_object($value)) {
$this->set[$key] = clone $value;
}
Copy link
Member

Choose a reason for hiding this comment

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

Similar to my earlier comment in LazyBSONArray, I think you may want recursive_copy() here. And perhaps a regression test for both that fails with this original code.

@alcaeus
Copy link
Member Author

alcaeus commented Jul 7, 2023

Closing this prototype PR in favour of actual work: #1125

@alcaeus alcaeus closed this Jul 7, 2023
@alcaeus alcaeus deleted the phplib-1022-codec-implementation branch July 7, 2023 08:55
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.

2 participants