-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Client encryption]: Switch to non-allocating MDE 2.0 api (#4753)
# Pull Request Template ## Description Switch both MDE encryption and encryption paths to use MDE2.0 api ## Type of change Please delete options that are not relevant. - [] New feature (non-breaking change which adds functionality) ## Closing issues Contributes to #4678 --------- Co-authored-by: Juraj Blazek <jublazek@microsoft.com> Co-authored-by: juraj-blazek <53177060+juraj-blazek@users.noreply.github.com> Co-authored-by: Santosh Kulkarni <66682828+kr-santosh@users.noreply.github.com>
- Loading branch information
1 parent
27843aa
commit 43c14a3
Showing
8 changed files
with
366 additions
and
94 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
Microsoft.Azure.Cosmos.Encryption.Custom/src/ArrayPoolManager.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,54 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.Encryption.Custom | ||
{ | ||
using System; | ||
using System.Buffers; | ||
using System.Collections.Generic; | ||
|
||
#pragma warning disable SA1402 // File may only contain a single type | ||
internal class ArrayPoolManager<T> : IDisposable | ||
#pragma warning restore SA1402 // File may only contain a single type | ||
{ | ||
private List<T[]> rentedBuffers = new (); | ||
private bool disposedValue; | ||
|
||
public T[] Rent(int minimumLength) | ||
{ | ||
T[] buffer = ArrayPool<T>.Shared.Rent(minimumLength); | ||
this.rentedBuffers.Add(buffer); | ||
return buffer; | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!this.disposedValue) | ||
{ | ||
if (disposing) | ||
{ | ||
foreach (T[] buffer in this.rentedBuffers) | ||
{ | ||
ArrayPool<T>.Shared.Return(buffer, clearArray: true); | ||
} | ||
|
||
this.rentedBuffers = null; | ||
} | ||
|
||
this.disposedValue = true; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method | ||
this.Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
|
||
internal class ArrayPoolManager : ArrayPoolManager<byte> | ||
{ | ||
} | ||
} |
195 changes: 124 additions & 71 deletions
195
Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionProcessor.cs
Large diffs are not rendered by default.
Oops, something went wrong.
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
161 changes: 161 additions & 0 deletions
161
Microsoft.Azure.Cosmos.Encryption.Custom/src/MemoryTextReader.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,161 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.Encryption.Custom | ||
{ | ||
using System; | ||
using System.Diagnostics.Contracts; | ||
using System.IO; | ||
|
||
/// <summary> | ||
/// Adjusted implementation of .Net StringReader reading from a Memory<char> instead of a string. | ||
/// </summary> | ||
internal class MemoryTextReader : TextReader | ||
{ | ||
private Memory<char> chars; | ||
private int length; | ||
private int pos; | ||
private bool closed; | ||
|
||
public MemoryTextReader(Memory<char> chars) | ||
{ | ||
this.chars = chars; | ||
this.length = chars.Length; | ||
} | ||
|
||
public override void Close() | ||
{ | ||
this.Dispose(true); | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
this.chars = null; | ||
this.pos = 0; | ||
this.length = 0; | ||
this.closed = true; | ||
base.Dispose(disposing); | ||
} | ||
|
||
[Pure] | ||
public override int Peek() | ||
{ | ||
if (this.closed) | ||
{ | ||
throw new InvalidOperationException("Reader is closed"); | ||
} | ||
|
||
if (this.pos == this.length) | ||
{ | ||
return -1; | ||
} | ||
|
||
return this.chars.Span[this.pos]; | ||
} | ||
|
||
public override int Read() | ||
{ | ||
if (this.closed) | ||
{ | ||
throw new InvalidOperationException("Reader is closed"); | ||
} | ||
|
||
if (this.pos == this.length) | ||
{ | ||
return -1; | ||
} | ||
|
||
return this.chars.Span[this.pos++]; | ||
} | ||
|
||
public override int Read(char[] buffer, int index, int count) | ||
{ | ||
if (buffer == null) | ||
{ | ||
throw new ArgumentNullException(nameof(buffer)); | ||
} | ||
|
||
if (index < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(index)); | ||
} | ||
|
||
if (count < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(count)); | ||
} | ||
|
||
if (buffer.Length - index < count) | ||
{ | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
|
||
if (this.closed) | ||
{ | ||
throw new InvalidOperationException("Reader is closed"); | ||
} | ||
|
||
int n = this.length - this.pos; | ||
if (n > 0) | ||
{ | ||
if (n > count) | ||
{ | ||
n = count; | ||
} | ||
|
||
this.chars.Span.Slice(this.pos, n).CopyTo(buffer.AsSpan(index, n)); | ||
this.pos += n; | ||
} | ||
|
||
return n; | ||
} | ||
|
||
public override string ReadToEnd() | ||
{ | ||
if (this.closed) | ||
{ | ||
throw new InvalidOperationException("Reader is closed"); | ||
} | ||
|
||
this.pos = this.length; | ||
return new string(this.chars.Slice(this.pos, this.length - this.pos).ToArray()); | ||
} | ||
|
||
public override string ReadLine() | ||
{ | ||
if (this.closed) | ||
{ | ||
throw new InvalidOperationException("Reader is closed"); | ||
} | ||
|
||
int i = this.pos; | ||
while (i < this.length) | ||
{ | ||
char ch = this.chars.Span[i]; | ||
if (ch == '\r' || ch == '\n') | ||
{ | ||
string result = new (this.chars.Slice(this.pos, i - this.pos).ToArray()); | ||
this.pos = i + 1; | ||
if (ch == '\r' && this.pos < this.length && this.chars.Span[this.pos] == '\n') | ||
{ | ||
this.pos++; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
i++; | ||
} | ||
|
||
if (i > this.pos) | ||
{ | ||
string result = new (this.chars.Slice(this.pos, i - this.pos).ToArray()); | ||
this.pos = i; | ||
return result; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
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
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