Skip to content

Commit c4a6377

Browse files
authored
Merge pull request #239 from brantburnett/raw-binary-transcoder-memory
DOC-9636: Document new RawBinaryTranscoder features in .NET SDK 3.2.6
2 parents ce3b732 + f5dc45a commit c4a6377

2 files changed

Lines changed: 50 additions & 6 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: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,25 +143,40 @@ include::example$Transcoding.cs[tag=string,indent=0]
143143

144144
=== RawBinaryTranscoder
145145
The RawBinaryTranscoder provides the ability for the user to explicitly store and retrieve raw byte data to Couchbase.
146-
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.
146+
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 (except in the case of an exception).
147147

148148
[cols="3", options="header"]
149149
|===
150150
|Item
151-
|Result
152-
|Common Flag
151+
|Encoding Result
152+
|Decoding Result
153153

154154
|String
155155
|InvalidArgumentException
156-
|-
156+
|InvalidArgumentException
157157

158158
|byte[]
159159
|Passthrough
160-
|Binary
160+
|Passthrough
161+
162+
|Memory<byte>
163+
|Passthrough +
164+
(from 3.2.6)
165+
|InvalidArgumentException
166+
167+
|ReadOnlyMemory<byte>
168+
|Passthrough +
169+
(from 3.2.6)
170+
|InvalidArgumentException
171+
172+
|IMemoryOwner<byte>
173+
|InvalidArgumentException
174+
|Passthrough +
175+
(from 3.2.6)
161176

162177
|Other `Object`
163178
|InvalidArgumentException
164-
|-
179+
|InvalidArgumentException
165180
|===
166181

167182
Here’s an example of using the `RawBinaryTranscoder`:
@@ -171,6 +186,14 @@ Here’s an example of using the `RawBinaryTranscoder`:
171186
include::example$Transcoding.cs[tag=binary,indent=0]
172187
----
173188

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

0 commit comments

Comments
 (0)