Skip to content

Commit

Permalink
fix additional internal paths
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Aug 27, 2024
1 parent 46fef77 commit 430e999
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 30 deletions.
12 changes: 10 additions & 2 deletions src/MongoDB.Bson/Serialization/IBsonSerializerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,18 @@ public static object Deserialize(this IBsonSerializer serializer, BsonDeserializ
/// <typeparam name="TValue">The type that this serializer knows how to serialize.</typeparam>
/// <param name="serializer">The serializer.</param>
/// <param name="context">The deserialization context.</param>
/// <param name="forceStaticSerializerRegistry">Force to use static serializer registry</param>
/// <returns>A deserialized value.</returns>
public static TValue Deserialize<TValue>(this IBsonSerializer<TValue> serializer, BsonDeserializationContext context)
public static TValue Deserialize<TValue>(
this IBsonSerializer<TValue> serializer,
BsonDeserializationContext context,
bool forceStaticSerializerRegistry = false)
{
var args = new BsonDeserializationArgs { NominalType = serializer.ValueType };
var args = new BsonDeserializationArgs
{
ForceStaticSerializerRegistry = forceStaticSerializerRegistry,
NominalType = serializer.ValueType
};
return serializer.Deserialize(context, args);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ public CommandMessageBinaryEncoder(Stream stream, MessageEncoderSettings encoder
/// <summary>
/// Reads the message.
/// </summary>
/// <param name="forceStaticSerializerRegistry">Force to use static serializer registry</param>
/// <returns>A message.</returns>
public CommandMessage ReadMessage()
public CommandMessage ReadMessage(
bool forceStaticSerializerRegistry = false)
{
var reader = CreateBinaryReader();
var stream = reader.BsonStream;
Expand All @@ -67,7 +69,7 @@ public CommandMessage ReadMessage()
EnsureFlagsAreValid(flags);
var moreToCome = (flags & OpMsgFlags.MoreToCome) != 0;
var exhaustAllowed = (flags & OpMsgFlags.ExhaustAllowed) != 0;
var sections = ReadSections(reader, messageEndPosition);
var sections = ReadSections(reader, messageEndPosition, forceStaticSerializerRegistry);
EnsureExactlyOneType0SectionIsPresent(sections);
EnsureMessageEndedAtEndPosition(stream, messageEndPosition);

Expand All @@ -81,7 +83,10 @@ public CommandMessage ReadMessage()
/// Writes the message.
/// </summary>
/// <param name="message">The message.</param>
public void WriteMessage(CommandMessage message)
/// <param name="forceStaticSerializerRegistry">Force to use static serializer registry</param>
public void WriteMessage(
CommandMessage message,
bool forceStaticSerializerRegistry = false)
{
Ensure.IsNotNull(message, nameof(message));

Expand All @@ -94,7 +99,7 @@ public void WriteMessage(CommandMessage message)
stream.WriteInt32(message.ResponseTo);
stream.WriteInt32((int)Opcode.OpMsg);
stream.WriteInt32((int)CreateFlags(message));
WriteSections(writer, message.Sections, messageStartPosition);
WriteSections(writer, message.Sections, messageStartPosition, forceStaticSerializerRegistry);
stream.BackpatchSize(messageStartPosition);

message.PostWriteAction?.Invoke(new PostProcessor(message, stream, messageStartPosition));
Expand Down Expand Up @@ -198,7 +203,9 @@ private void EnsureType1PayloadLengthIsValid(int sectionLength)
}
}

private CommandMessageSection ReadSection(BsonBinaryReader reader)
private CommandMessageSection ReadSection(
BsonBinaryReader reader,
bool forceStaticSerializerRegistry)
{
var payloadType = reader.BsonStream.ReadByte();
if (payloadType == -1)
Expand All @@ -209,36 +216,43 @@ private CommandMessageSection ReadSection(BsonBinaryReader reader)
switch (payloadType)
{
case 0:
return ReadType0Section(reader);
return ReadType0Section(reader, forceStaticSerializerRegistry);

case 1:
return ReadType1Section(reader);
return ReadType1Section(reader, forceStaticSerializerRegistry);

default:
throw new FormatException($"Command message invalid payload type: {payloadType}.");
}
}

private List<CommandMessageSection> ReadSections(BsonBinaryReader reader, long messageEndPosition)
private List<CommandMessageSection> ReadSections(
BsonBinaryReader reader,
long messageEndPosition,
bool forceStaticSerializerRegistry)
{
var sections = new List<CommandMessageSection>();
while (reader.BsonStream.Position < messageEndPosition)
{
var section = ReadSection(reader);
var section = ReadSection(reader, forceStaticSerializerRegistry);
sections.Add(section);
}
return sections;
}

private Type0CommandMessageSection<RawBsonDocument> ReadType0Section(IBsonReader reader)
private Type0CommandMessageSection<RawBsonDocument> ReadType0Section(
IBsonReader reader,
bool forceStaticSerializerRegistry)
{
var serializer = RawBsonDocumentSerializer.Instance;
var context = BsonDeserializationContext.CreateRoot(reader);
var document = serializer.Deserialize(context);
var document = serializer.Deserialize(context, forceStaticSerializerRegistry);
return new Type0CommandMessageSection<RawBsonDocument>(document, serializer);
}

private Type1CommandMessageSection<RawBsonDocument> ReadType1Section(BsonBinaryReader reader)
private Type1CommandMessageSection<RawBsonDocument> ReadType1Section(
BsonBinaryReader reader,
bool forceStaticSerializerRegistry)
{
var stream = reader.BsonStream;

Expand All @@ -252,7 +266,7 @@ private Type1CommandMessageSection<RawBsonDocument> ReadType1Section(BsonBinaryR
var documents = new List<RawBsonDocument>();
while (stream.Position < payloadEndPosition)
{
var document = serializer.Deserialize(context);
var document = serializer.Deserialize(context, forceStaticSerializerRegistry);
documents.Add(document);
}
EnsurePayloadEndedAtEndPosition(stream, payloadEndPosition);
Expand All @@ -261,41 +275,56 @@ private Type1CommandMessageSection<RawBsonDocument> ReadType1Section(BsonBinaryR
return new Type1CommandMessageSection<RawBsonDocument>(identifier, batch, serializer, NoOpElementNameValidator.Instance, null, null);
}

private void WriteSection(BsonBinaryWriter writer, CommandMessageSection section, long messageStartPosition)
private void WriteSection(
BsonBinaryWriter writer,
CommandMessageSection section,
long messageStartPosition,
bool forceStaticSerializerRegistry)
{
writer.BsonStream.WriteByte((byte)section.PayloadType);

switch (section.PayloadType)
{
case PayloadType.Type0:
WriteType0Section(writer, (Type0CommandMessageSection)section);
WriteType0Section(writer, (Type0CommandMessageSection)section, forceStaticSerializerRegistry);
break;

case PayloadType.Type1:
WriteType1Section(writer, (Type1CommandMessageSection)section, messageStartPosition);
WriteType1Section(writer, (Type1CommandMessageSection)section, messageStartPosition, forceStaticSerializerRegistry);
break;

default:
throw new ArgumentException("Invalid payload type.", nameof(section));
}
}

private void WriteSections(BsonBinaryWriter writer, IEnumerable<CommandMessageSection> sections, long messageStartPosition)
private void WriteSections(
BsonBinaryWriter writer,
IEnumerable<CommandMessageSection> sections,
long messageStartPosition,
bool forceStaticSerializerRegistry)
{
foreach (var section in sections)
{
WriteSection(writer, section, messageStartPosition);
WriteSection(writer, section, messageStartPosition, forceStaticSerializerRegistry);
}
}

private void WriteType0Section(BsonBinaryWriter writer, Type0CommandMessageSection section)
private void WriteType0Section(
BsonBinaryWriter writer,
Type0CommandMessageSection section,
bool forceStaticSerializerRegistry)
{
var serializer = section.DocumentSerializer;
var context = BsonSerializationContext.CreateRoot(writer);
serializer.Serialize(context, section.Document);
serializer.Serialize(context, section.Document, forceStaticSerializerRegistry);
}

private void WriteType1Section(BsonBinaryWriter writer, Type1CommandMessageSection section, long messageStartPosition)
private void WriteType1Section(
BsonBinaryWriter writer,
Type1CommandMessageSection section,
long messageStartPosition,
bool forceStaticSerializerRegistry)
{
var stream = writer.BsonStream;
var serializer = section.DocumentSerializer;
Expand Down Expand Up @@ -329,7 +358,7 @@ private void WriteType1Section(BsonBinaryWriter writer, Type1CommandMessageSecti
{
var documentStartPosition = stream.Position;
var document = batch.Items[batch.Offset + i];
serializer.Serialize(context, document);
serializer.Serialize(context, document, forceStaticSerializerRegistry);

var messageSize = stream.Position - messageStartPosition;
if (messageSize > maxMessageSize && batch.CanBeSplit && i > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,40 @@ public CommandRequestMessageBinaryEncoder(CommandMessageBinaryEncoder wrappedEnc
/// <summary>
/// Reads the message.
/// </summary>
/// <param name="forceStaticSerializerRegistry">Force to use static serializer registry</param>
/// <returns>A message.</returns>
public CommandRequestMessage ReadMessage()
public CommandRequestMessage ReadMessage(
bool forceStaticSerializerRegistry = false)
{
var wrappedMessage = (CommandMessage)_wrappedEncoder.ReadMessage();
var wrappedMessage = (CommandMessage)_wrappedEncoder.ReadMessage(forceStaticSerializerRegistry);
return new CommandRequestMessage(wrappedMessage, null);
}

/// <summary>
/// Writes the message.
/// </summary>
/// <param name="message">The message.</param>
public void WriteMessage(CommandRequestMessage message)
/// <param name="forceStaticSerializerRegistry">Force to use static serializer registry</param>
public void WriteMessage(
CommandRequestMessage message,
bool forceStaticSerializerRegistry = false)
{
var wrappedMessage = message.WrappedMessage;
_wrappedEncoder.WriteMessage(wrappedMessage);
_wrappedEncoder.WriteMessage(wrappedMessage, forceStaticSerializerRegistry);
}

// explicit interface implementations
MongoDBMessage IMessageEncoder.ReadMessage(
bool forceStaticSerializerRegistry)
{
return ReadMessage();
return ReadMessage(forceStaticSerializerRegistry);
}

void IMessageEncoder.WriteMessage(
MongoDBMessage message,
bool forceStaticSerializerRegistry)
{
WriteMessage((CommandRequestMessage)message);
WriteMessage((CommandRequestMessage)message, forceStaticSerializerRegistry);
}
}
}

0 comments on commit 430e999

Please sign in to comment.