Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve submitter client ext #39

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
37 changes: 37 additions & 0 deletions Api/csharp/ArmoniK.Api.Client/Submitter/IPayload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// This file is part of the ArmoniK project
//
// Copyright (C) ANEO, 2021-2022. All rights reserved.
// W. Kirschenmann <[email protected]>
// J. Gurhem <[email protected]>
// D. Dubuc <[email protected]>
// L. Ziane Khodja <[email protected]>
// F. Lemaitre <[email protected]>
// S. Djebbar <[email protected]>
// J. Fonseca <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System.Collections.Generic;
using System.Threading;

using Google.Protobuf;

namespace ArmoniK.Api.Client.Submitter
{
public interface IPayload
{
IAsyncEnumerable<ByteString> ToChunkedByteStringAsync(int maxChunkSize,
CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// This file is part of the ArmoniK project
//
// Copyright (C) ANEO, 2021-2022. All rights reserved.
// W. Kirschenmann <[email protected]>
// J. Gurhem <[email protected]>
// D. Dubuc <[email protected]>
// L. Ziane Khodja <[email protected]>
// F. Lemaitre <[email protected]>
// S. Djebbar <[email protected]>
// J. Fonseca <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;

using Google.Protobuf;

namespace ArmoniK.Api.Client.Submitter
{
public class ReadOnlyByteArrayPayload : IPayload
{
private readonly byte[] bytes_;

public ReadOnlyByteArrayPayload(byte[] bytes)
=> bytes_ = bytes;

#pragma warning disable CS1998
public async IAsyncEnumerable<ByteString> ToChunkedByteStringAsync(int maxChunkSize,
#pragma warning restore CS1998
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var start = 0;

while (start < bytes_.Length)
{
cancellationToken.ThrowIfCancellationRequested();
var chunkSize = Math.Min(maxChunkSize,
bytes_.Length - start);
yield return UnsafeByteOperations.UnsafeWrap(bytes_.AsMemory(start,
chunkSize));
start += chunkSize;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// This file is part of the ArmoniK project
//
// Copyright (C) ANEO, 2021-2022. All rights reserved.
// W. Kirschenmann <[email protected]>
// J. Gurhem <[email protected]>
// D. Dubuc <[email protected]>
// L. Ziane Khodja <[email protected]>
// F. Lemaitre <[email protected]>
// S. Djebbar <[email protected]>
// J. Fonseca <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;

using Google.Protobuf;

namespace ArmoniK.Api.Client.Submitter
{
public class ReadOnlyByteMemoryPayload : IPayload
{
private readonly ReadOnlyMemory<byte> readOnlyMemory_;

public ReadOnlyByteMemoryPayload(ReadOnlyMemory<byte> readOnlyMemory)
=> readOnlyMemory_ = readOnlyMemory;

#pragma warning disable CS1998
public async IAsyncEnumerable<ByteString> ToChunkedByteStringAsync(int maxChunkSize,
#pragma warning restore CS1998
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var start = 0;

while (start < readOnlyMemory_.Length)
{
cancellationToken.ThrowIfCancellationRequested();
var chunkSize = Math.Min(maxChunkSize,
readOnlyMemory_.Length - start);
yield return UnsafeByteOperations.UnsafeWrap(readOnlyMemory_.Slice(start,
chunkSize));
start += chunkSize;
}
}
}
}
62 changes: 62 additions & 0 deletions Api/csharp/ArmoniK.Api.Client/Submitter/StreamPayload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// This file is part of the ArmoniK project
//
// Copyright (C) ANEO, 2021-2022. All rights reserved.
// W. Kirschenmann <[email protected]>
// J. Gurhem <[email protected]>
// D. Dubuc <[email protected]>
// L. Ziane Khodja <[email protected]>
// F. Lemaitre <[email protected]>
// S. Djebbar <[email protected]>
// J. Fonseca <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;

using Google.Protobuf;

namespace ArmoniK.Api.Client.Submitter
{
public class StreamPayload : IPayload
{
private readonly Stream data_;

public StreamPayload(Stream data)
=> data_ = data;

public async IAsyncEnumerable<ByteString> ToChunkedByteStringAsync(int maxChunkSize,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
while (true)
{
var buffer = new byte[maxChunkSize];
var size = await data_.ReadAsync(buffer,
0,
maxChunkSize,
cancellationToken);
if (size == 0)
{
yield break;
}

yield return UnsafeByteOperations.UnsafeWrap(buffer.AsMemory(0,
size));
}
}
}
}
Loading