Skip to content

Commit dbf4f92

Browse files
committed
DOC-9636: Document new RawBinaryTranscoder features in .NET SDK 3.2.6
Motivation ---------- As of .NET SDK 3.2.6 the RawBinaryTranscoder supports additional types besides byte[] that offer greater performance when dealing with large binary objects. Modifications ------------- Add documentation and an example that cover encoding of `Memory<byte>` and `ReadOnlyMemory<byte>` and decoding of `IMemoryOwner<byte>`. Results ------- This "hidden" performance feature is now documented.
1 parent e841453 commit dbf4f92

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

modules/howtos/examples/Transcoding.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Buffers;
23
using System.IO;
34
using System.Threading.Tasks;
45
using System.Text.Json;
@@ -80,6 +81,26 @@ public async Task Binary()
8081
Console.WriteLine(returned);
8182
}
8283

84+
public async Task BinaryMemory()
85+
{
86+
87+
// #tag::binary-memory[]
88+
var docId = "doc";
89+
90+
using var buffer = MemoryPool<byte>.Shared.Rent(16);
91+
var byteCount = System.Text.Encoding.UTF8.GetBytes("hello world", buffer.Memory.Span);
92+
Memory<byte> bytes = buffer.Memory.Slice(0, byteCount);
93+
94+
await _collection.UpsertAsync(docId, bytes, options => options.Transcoder(new RawBinaryTranscoder()));
95+
96+
var result = await _collection.GetAsync(docId, options => options.Transcoder(new RawBinaryTranscoder()));
97+
98+
// Be sure to dispose of the IMemoryOwner<byte> when done, typically via a using statement
99+
using var returned = result.ContentAs<IMemoryOwner<byte>>();
100+
// #end::binary-memory[]
101+
Console.WriteLine(System.Text.Encoding.UTF8.GetString(returned.Memory.Span));
102+
}
103+
83104
public async Task CustomEncode()
84105
{
85106
// #tag::custom-encode[]

modules/howtos/pages/transcoders-nonjson.adoc

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,41 @@ include::example$Transcoding.cs[tag=string,indent=0]
145145
The RawBinaryTranscoder provides the ability for the user to explicitly store and retrieve raw byte data to Couchbase.
146146
The transcoder does not perform any form of real transcoding, and does not take a serializer, but rather passes the data through and assigns the appropriate binary Common Flag.
147147

148-
[cols="3", options="header"]
148+
[cols="4", options="header"]
149149
|===
150150
|Item
151-
|Result
151+
|Encoding Result
152+
|Decoding Result
152153
|Common Flag
153154

154155
|String
155156
|InvalidArgumentException
157+
|InvalidArgumentException
156158
|-
157159

158160
|byte[]
159161
|Passthrough
162+
|Passthrough
163+
|Binary
164+
165+
|Memory<byte>
166+
|Passthrough (≥ 3.2.6)
167+
|InvalidArgumentException
168+
|Binary
169+
170+
|ReadOnlyMemory<byte>
171+
|Passthrough (≥ 3.2.6)
172+
|InvalidArgumentException
173+
|Binary
174+
175+
|IMemoryOwner<byte>
176+
|InvalidArgumentException
177+
|Passthrough (≥ 3.2.6)
160178
|Binary
161179

162180
|Other `Object`
163181
|InvalidArgumentException
182+
|InvalidArgumentException
164183
|-
165184
|===
166185

@@ -171,6 +190,15 @@ Here’s an example of using the `RawBinaryTranscoder`:
171190
include::example$Transcoding.cs[tag=binary,indent=0]
172191
----
173192

193+
From version 3.2.6, the `RawBinaryTranscoder` will accept `Memory<byte>` and `ReadOnlyMemory<byte>` inputs
194+
and can return `IMemoryOwner<byte>` outputs. Using these types may improve performace by not allocating
195+
large, temporary `byte[]` arrays on the heap.
196+
197+
[source,csharp]
198+
----
199+
include::example$Transcoding.cs[tag=binary-memory,indent=0]
200+
----
201+
174202
== Custom Transcoders and Serializers
175203
More advanced transcoding needs can be accomplished if the application implements their own transcoders and serializers.
176204

0 commit comments

Comments
 (0)