Skip to content

Commit

Permalink
Write stream instead of flush (for v7.x) (#3101)
Browse files Browse the repository at this point in the history
* Write to stream instead of flushing  when Utf8JsonWriter buffer full

* Temporarily set Utf8JsonWriter buffer size to 1MB

* Rename method

* Add tests

* Fix error in comments
  • Loading branch information
habbes authored Oct 30, 2024
1 parent e9578f2 commit d6c41a8
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 83 deletions.
8 changes: 4 additions & 4 deletions src/Microsoft.OData.Core/Json/ODataUtf8JsonWriter.Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public async Task<Stream> StartStreamValueScopeAsync()
this.WriteItemWithSeparatorIfNeeded();
this.bufferWriter.Write(this.DoubleQuote.Slice(0, 1).Span);

await this.FlushIfBufferThresholdReachedAsync().ConfigureAwait(false);
await this.DrainBufferIfThresholdReachedAsync().ConfigureAwait(false);

this.binaryValueStream = new ODataUtf8JsonWriteStream(this);

Expand All @@ -83,7 +83,7 @@ public async Task EndStreamValueScopeAsync()
this.binaryValueStream = null;
}

await this.FlushIfBufferThresholdReachedAsync().ConfigureAwait(false);
await this.DrainBufferIfThresholdReachedAsync().ConfigureAwait(false);

this.bufferWriter.Write(this.DoubleQuote.Slice(0, 1).Span);

Expand Down Expand Up @@ -282,7 +282,7 @@ private void WriteByteValueInChunks(ReadOnlySpan<byte> value)
}

// Flush the writer if the buffer threshold is reached
this.jsonWriter.FlushIfBufferThresholdReached();
this.jsonWriter.DrainBufferIfThresholdReached();
}
}

Expand Down Expand Up @@ -328,7 +328,7 @@ private async ValueTask WriteByteValueInChunksAsync(ReadOnlyMemory<byte> value)
}

// Flush the writer if the buffer threshold is reached
await this.jsonWriter.FlushIfBufferThresholdReachedAsync().ConfigureAwait(false);
await this.jsonWriter.DrainBufferIfThresholdReachedAsync().ConfigureAwait(false);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/Microsoft.OData.Core/Json/ODataUtf8JsonWriter.TextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public async Task EndTextWriterValueScopeAsync()
await this.textWriter.DisposeAsync();
}

await this.FlushIfBufferThresholdReachedAsync().ConfigureAwait(false);
await this.DrainBufferIfThresholdReachedAsync().ConfigureAwait(false);

CheckIfSeparatorNeeded();
CheckIfManualValueAtArrayStart();
Expand Down Expand Up @@ -347,7 +347,7 @@ private void WriteCharsInChunks(ReadOnlySpan<char> value)
}

// Flush the writer if the buffer threshold is reached
this.jsonWriter.FlushIfBufferThresholdReached();
this.jsonWriter.DrainBufferIfThresholdReached();
}
}

Expand Down Expand Up @@ -397,7 +397,7 @@ private async ValueTask WriteCharsInChunksAsync(ReadOnlyMemory<char> value)
}

// Flush the writer if the buffer threshold is reached
await this.jsonWriter.FlushIfBufferThresholdReachedAsync().ConfigureAwait(false);
await this.jsonWriter.DrainBufferIfThresholdReachedAsync().ConfigureAwait(false);
}
}

Expand Down Expand Up @@ -441,7 +441,7 @@ private void WriteCharsInChunksWithoutEscaping(ReadOnlySpan<char> value)
}

// Flush the writer if the buffer threshold is reached
this.jsonWriter.FlushIfBufferThresholdReached();
this.jsonWriter.DrainBufferIfThresholdReached();
}
}

Expand Down Expand Up @@ -485,7 +485,7 @@ private async ValueTask WriteCharsInChunksWithoutEscapingAsync(ReadOnlyMemory<ch
}

// Flush the writer if the buffer threshold is reached
await this.jsonWriter.FlushIfBufferThresholdReachedAsync().ConfigureAwait(false);
await this.jsonWriter.DrainBufferIfThresholdReachedAsync().ConfigureAwait(false);
}
}

Expand Down
Loading

0 comments on commit d6c41a8

Please sign in to comment.