forked from AllocZero/EfficientDynamoDb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Memory-based binary converters (AllocZero#242)
* Add Memory-based binary converters * Update converters section in docs.
- Loading branch information
Showing
7 changed files
with
86 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/EfficientDynamoDb/Internal/Converters/Primitives/Binary/BinaryToMemoryDdbConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using EfficientDynamoDb.Converters; | ||
using EfficientDynamoDb.DocumentModel; | ||
|
||
namespace EfficientDynamoDb.Internal.Converters.Primitives.Binary | ||
{ | ||
internal sealed class BinaryToMemoryDdbConverter : DdbConverter<Memory<byte>> | ||
{ | ||
public override Memory<byte> Read(in AttributeValue attributeValue) => | ||
attributeValue.AsBinaryAttribute().Value; | ||
|
||
public override AttributeValue Write(ref Memory<byte> value) | ||
{ | ||
var array = MemoryMarshal.TryGetArray((ReadOnlyMemory<byte>)value, out var segment) | ||
? segment.Array | ||
: value.ToArray(); | ||
Debug.Assert(array != null); | ||
|
||
return new AttributeValue(new BinaryAttributeValue(array)); | ||
} | ||
|
||
public override Memory<byte> Read(ref DdbReader reader) => | ||
reader.JsonReaderValue.GetBytesFromBase64(); | ||
|
||
public override void Write(in DdbWriter writer, ref Memory<byte> value) => | ||
writer.WriteDdbBinary(value.Span); | ||
} | ||
|
||
internal sealed class BinaryToMemoryDdbConverterFactory : DdbConverterFactory | ||
{ | ||
public override bool CanConvert(Type typeToConvert) => | ||
typeToConvert == typeof(Memory<byte>) || typeToConvert == typeof(Memory<byte>?); | ||
|
||
public override DdbConverter CreateConverter(Type typeToConvert, DynamoDbContextMetadata metadata) => | ||
new BinaryToMemoryDdbConverter(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...cientDynamoDb/Internal/Converters/Primitives/Binary/BinaryToReadOnlyMemoryDdbConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using EfficientDynamoDb.Converters; | ||
using EfficientDynamoDb.DocumentModel; | ||
|
||
namespace EfficientDynamoDb.Internal.Converters.Primitives.Binary | ||
{ | ||
internal sealed class BinaryToReadOnlyMemoryDdbConverter : DdbConverter<ReadOnlyMemory<byte>> | ||
{ | ||
public override ReadOnlyMemory<byte> Read(in AttributeValue attributeValue) => | ||
attributeValue.AsBinaryAttribute().Value; | ||
|
||
public override AttributeValue Write(ref ReadOnlyMemory<byte> value) | ||
{ | ||
var array = MemoryMarshal.TryGetArray(value, out var segment) | ||
? segment.Array | ||
: value.ToArray(); | ||
Debug.Assert(array != null); | ||
|
||
return new AttributeValue(new BinaryAttributeValue(array)); | ||
} | ||
|
||
public override ReadOnlyMemory<byte> Read(ref DdbReader reader) => | ||
reader.JsonReaderValue.GetBytesFromBase64(); | ||
|
||
public override void Write(in DdbWriter writer, ref ReadOnlyMemory<byte> value) => | ||
writer.WriteDdbBinary(value.Span); | ||
} | ||
|
||
internal sealed class BinaryToReadOnlyMemoryDdbConverterFactory : DdbConverterFactory | ||
{ | ||
public override bool CanConvert(Type typeToConvert) => | ||
typeToConvert == typeof(ReadOnlyMemory<byte>) || typeToConvert == typeof(ReadOnlyMemory<byte>?); | ||
|
||
public override DdbConverter CreateConverter(Type typeToConvert, DynamoDbContextMetadata metadata) => | ||
new BinaryToReadOnlyMemoryDdbConverter(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters